FileIO.cpp 4.7 KB

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