FileIO.cpp 4.2 KB

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