FileIO.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef FILEIO_H_
  2. #define FILEIO_H_
  3. #include <cstdio>
  4. #include <list>
  5. #include <vector>
  6. #include <cstring>
  7. #include <string>
  8. #include "Vector2.h"
  9. #include "Vector3.h"
  10. #include "Vector4.h"
  11. namespace gameplay
  12. {
  13. /**
  14. * Writes an XML element to the specified file stream.
  15. *
  16. * @param file Pointer to a FILE object that identifies the stream.
  17. * @param elementName Name of the XML element to write.
  18. * @param value Value to write.
  19. */
  20. void fprintfElement(FILE* file, const char* elementName, float value);
  21. void fprintfElement(FILE* file, const char* elementName, unsigned int value);
  22. void fprintfElement(FILE* file, const char* elementName, unsigned char value);
  23. void fprintfElement(FILE* file, const char* elementName, const char* value);
  24. void fprintfElement(FILE* file, const char* elementName, const std::string& value);
  25. void fprintfElement(FILE* file, const char* elementName, const float values[], int length);
  26. template <class T>
  27. void fprintfElement(FILE* file, const char* format, const char* elementName, std::vector<T> list)
  28. {
  29. fprintf(file, "<%s count=\"%lu\">", elementName, list.size());
  30. typename std::vector<T>::const_iterator i;
  31. for (i = list.begin(); i != list.end(); ++i)
  32. {
  33. fprintf(file, format, *i);
  34. }
  35. fprintf(file, "</%s>\n", elementName);
  36. }
  37. template <class T>
  38. void fprintfElement(FILE* file, const char* format, const char* elementName, std::list<T> list)
  39. {
  40. fprintf(file, "<%s count=\"%lu\">", elementName, list.size());
  41. typename std::list<T>::const_iterator i;
  42. for (i = list.begin(); i != list.end(); ++i)
  43. {
  44. fprintf(file, format, *i);
  45. }
  46. fprintf(file, "</%s>\n", elementName);
  47. }
  48. void fprintfMatrix4f(FILE* file, const float* m);
  49. /**
  50. * Writes binary data to the given file stream.
  51. *
  52. * @param value The value to be written
  53. * @param file The binary file stream.
  54. */
  55. void write(unsigned char value, FILE* file);
  56. void write(char value, FILE* file);
  57. void write(const char* str, FILE* file);
  58. void write(unsigned int value, FILE* file);
  59. void write(unsigned long value, FILE* file);
  60. void write(unsigned short value, FILE* file);
  61. void write(bool value, FILE* file);
  62. void write(float value, FILE* file);
  63. void write(const float* values, int length, FILE* file);
  64. /**
  65. * Writes the length of the string and the string bytes to the binary file stream.
  66. */
  67. void write(const std::string& str, FILE* file);
  68. void writeZero(FILE* file);
  69. /**
  70. * Writes the length of the list and writes each element value to the binary file stream.
  71. *
  72. * @param list The list to write.
  73. * @param file The binary file stream.
  74. */
  75. template <class T>
  76. void write(std::list<T> list, FILE* file)
  77. {
  78. // First write the size of the list
  79. write(list.size(), file);
  80. // Then write each element
  81. typename std::list<T>::const_iterator i;
  82. for (i = list.begin(); i != list.end(); ++i)
  83. {
  84. write(*i, file);
  85. }
  86. }
  87. /**
  88. * Writes the length of the vector and writes each element value to the binary file stream.
  89. *
  90. * @param vector The vector to write.
  91. * @param file The binary file stream.
  92. */
  93. template <class T>
  94. void write(std::vector<T> vector, FILE* file)
  95. {
  96. // First write the size of the vector
  97. write(vector.size(), file);
  98. // Then write each element
  99. typename std::vector<T>::const_iterator i;
  100. for (i = vector.begin(); i != vector.end(); ++i)
  101. {
  102. write(*i, file);
  103. }
  104. }
  105. /**
  106. * Skips over the string at the current file stream offset by moving the file position.
  107. * Assumes the current position points to the unsigned int length of the string.
  108. * The string is assumed to be a char array.
  109. *
  110. * @param file The file stream.
  111. */
  112. void skipString(FILE* file);
  113. void skipUint(FILE* file);
  114. void writeVectorBinary(const Vector2& v, FILE* file);
  115. void writeVectorText(const Vector2& v, FILE* file);
  116. void writeVectorBinary(const Vector3& v, FILE* file);
  117. void writeVectorText(const Vector3& v, FILE* file);
  118. void writeVectorBinary(const Vector4& v, FILE* file);
  119. void writeVectorText(const Vector4& v, FILE* file);
  120. /**
  121. * Prompts the user if they want to group animations automatically.
  122. * If the user enters an invalid response, the question is asked again.
  123. *
  124. * @return True if the user wants to group animations, false otherwise.
  125. */
  126. bool promptUserGroupAnimations();
  127. }
  128. #endif