FileIO.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "FileIO.h"
  2. namespace gameplay
  3. {
  4. // Writing out a binary file //
  5. void write(unsigned char value, FILE* file)
  6. {
  7. size_t r = fwrite(&value, sizeof(unsigned char), 1, file);
  8. assert(r == 1);
  9. }
  10. void write(char value, FILE* file)
  11. {
  12. size_t r = fwrite(&value, sizeof(char), 1, file);
  13. assert(r == 1);
  14. }
  15. void write(const char* str, FILE* file)
  16. {
  17. size_t length = strlen(str);
  18. size_t r = fwrite(str, 1, length, file);
  19. assert(r == length);
  20. }
  21. void write(unsigned int value, FILE* file)
  22. {
  23. size_t r = fwrite(&value, sizeof(unsigned int), 1, file);
  24. assert(r == 1);
  25. }
  26. void write(unsigned long value, FILE* file)
  27. {
  28. size_t r = fwrite(&value, sizeof(unsigned long), 1, file);
  29. assert(r == 1);
  30. }
  31. void write(unsigned short value, FILE* file)
  32. {
  33. size_t r = fwrite(&value, sizeof(unsigned short), 1, file);
  34. assert(r == 1);
  35. }
  36. void write(bool value, FILE* file)
  37. {
  38. // write booleans as a unsigned char
  39. unsigned char b = value;
  40. write(b, file);
  41. }
  42. void write(float value, FILE* file)
  43. {
  44. fwrite(&value, sizeof(float), 1, file);
  45. }
  46. void write(const float* values, int length, FILE* file)
  47. {
  48. for (int i = 0; i < length; i++)
  49. {
  50. write(values[i], file);
  51. }
  52. }
  53. void write(const std::string& str, FILE* file)
  54. {
  55. // Write the length of the string
  56. write(str.size(), file);
  57. // Write the array of characters of the string
  58. write(str.c_str(), file);
  59. }
  60. void writeZero(FILE* file)
  61. {
  62. write((unsigned int)0, file);
  63. }
  64. // Writing to a text file //
  65. void fprintfElement(FILE* file, const char* elementName, const float values[], int length)
  66. {
  67. fprintf(file, "<%s>", elementName);
  68. for (int i = 0; i < length; i++)
  69. {
  70. fprintf(file, "%f ", values[i]);
  71. }
  72. fprintf(file, "</%s>\n", elementName);
  73. }
  74. void fprintfElement(FILE* file, const char* elementName, const char* value)
  75. {
  76. fprintf(file, "<%s>%s</%s>\n", elementName, value, elementName);
  77. }
  78. void fprintfElement(FILE* file, const char* elementName, const std::string& value)
  79. {
  80. fprintf(file, "<%s>%s</%s>\n", elementName, value.c_str(), elementName);
  81. }
  82. void fprintfElement(FILE* file, const char* elementName, float value)
  83. {
  84. fprintf(file, "<%s>%f</%s>\n", elementName, value, elementName);
  85. }
  86. void fprintfElement(FILE* file, const char* elementName, unsigned int value)
  87. {
  88. fprintf(file, "<%s>%u</%s>\n", elementName, value, elementName);
  89. }
  90. void fprintfElement(FILE* file, const char* elementName, unsigned char value)
  91. {
  92. fprintf(file, "<%s>%u</%s>\n", elementName, value, elementName);
  93. }
  94. void fprintfMatrix4f(FILE* file, const float* m)
  95. {
  96. for (size_t i = 0; i < 16; i ++)
  97. {
  98. float v = m[i];
  99. if (v == 1.0f)
  100. {
  101. fprintf(file, "1.0 ");
  102. }
  103. else if (v == 0.0)
  104. {
  105. fprintf(file, "0.0 ");
  106. }
  107. else
  108. {
  109. fprintf(file, "%f ",v);
  110. }
  111. }
  112. }
  113. void skipString(FILE* file)
  114. {
  115. // get the size of the char array
  116. unsigned int length = 0;
  117. fread(&length, sizeof(unsigned int), 1, file);
  118. // skip over the unsigned int length
  119. fseek(file, sizeof(unsigned int), SEEK_CUR);
  120. if (length > 0)
  121. {
  122. // Skip over the array of chars
  123. long seek = (long)(length * sizeof(char));
  124. fseek(file, seek, SEEK_CUR);
  125. }
  126. }
  127. void skipUint(FILE* file)
  128. {
  129. fseek(file, sizeof(unsigned int), SEEK_CUR);
  130. }
  131. }