FileIO.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if (str.size() > 0)
  59. {
  60. // Write the array of characters of the string
  61. write(str.c_str(), file);
  62. }
  63. }
  64. void writeZero(FILE* file)
  65. {
  66. write((unsigned int)0, file);
  67. }
  68. // Writing to a text file //
  69. void fprintfElement(FILE* file, const char* elementName, const float values[], int length)
  70. {
  71. fprintf(file, "<%s count=\"%d\">", elementName, length);
  72. for (int i = 0; i < length; ++i)
  73. {
  74. fprintf(file, "%f ", values[i]);
  75. }
  76. fprintf(file, "</%s>\n", elementName);
  77. }
  78. void fprintfElement(FILE* file, const char* elementName, const char* value)
  79. {
  80. fprintf(file, "<%s>%s</%s>\n", elementName, value, elementName);
  81. }
  82. void fprintfElement(FILE* file, const char* elementName, const std::string& value)
  83. {
  84. fprintf(file, "<%s>%s</%s>\n", elementName, value.c_str(), elementName);
  85. }
  86. void fprintfElement(FILE* file, const char* elementName, float value)
  87. {
  88. fprintf(file, "<%s>%f</%s>\n", elementName, value, elementName);
  89. }
  90. void fprintfElement(FILE* file, const char* elementName, unsigned int value)
  91. {
  92. fprintf(file, "<%s>%u</%s>\n", elementName, value, elementName);
  93. }
  94. void fprintfElement(FILE* file, const char* elementName, unsigned char value)
  95. {
  96. fprintf(file, "<%s>%u</%s>\n", elementName, value, elementName);
  97. }
  98. void fprintfMatrix4f(FILE* file, const float* m)
  99. {
  100. for (size_t i = 0; i < 16; i ++)
  101. {
  102. float v = m[i];
  103. if (v == 1.0f)
  104. {
  105. fprintf(file, "1.0 ");
  106. }
  107. else if (v == 0.0)
  108. {
  109. fprintf(file, "0.0 ");
  110. }
  111. else
  112. {
  113. fprintf(file, "%f ",v);
  114. }
  115. }
  116. }
  117. void skipString(FILE* file)
  118. {
  119. // get the size of the char array
  120. unsigned int length = 0;
  121. fread(&length, sizeof(unsigned int), 1, file);
  122. // skip over the unsigned int length
  123. fseek(file, sizeof(unsigned int), SEEK_CUR);
  124. if (length > 0)
  125. {
  126. // Skip over the array of chars
  127. long seek = (long)(length * sizeof(char));
  128. fseek(file, seek, SEEK_CUR);
  129. }
  130. }
  131. void skipUint(FILE* file)
  132. {
  133. fseek(file, sizeof(unsigned int), SEEK_CUR);
  134. }
  135. void writeVectorBinary(const Vector2& v, FILE* file)
  136. {
  137. write(v.x, file);
  138. write(v.y, file);
  139. }
  140. void writeVectorText(const Vector2& v, FILE* file)
  141. {
  142. fprintf(file, "%f %f\n", v.x, v.y);
  143. }
  144. void writeVectorBinary(const Vector3& v, FILE* file)
  145. {
  146. write(v.x, file);
  147. write(v.y, file);
  148. write(v.z, file);
  149. }
  150. void writeVectorText(const Vector3& v, FILE* file)
  151. {
  152. fprintf(file, "%f %f %f\n", v.x, v.y, v.z);
  153. }
  154. void writeVectorBinary(const Vector4& v, FILE* file)
  155. {
  156. write(v.x, file);
  157. write(v.y, file);
  158. write(v.z, file);
  159. write(v.w, file);
  160. }
  161. void writeVectorText(const Vector4& v, FILE* file)
  162. {
  163. fprintf(file, "%f %f %f %f\n", v.x, v.y, v.z, v.w);
  164. }
  165. bool promptUserGroupAnimations()
  166. {
  167. char buffer[80];
  168. for (;;)
  169. {
  170. printf("Do you want to group animations? (y/n)\n");
  171. std::cin.getline(buffer, 80);
  172. if (buffer[0] == 'y' || buffer[0] == 'Y' || buffer[0] == '\0')
  173. {
  174. return true;
  175. }
  176. else if (buffer[0] == 'n' || buffer[0] == 'N')
  177. {
  178. return false;
  179. }
  180. }
  181. }
  182. }