FileIO.h 4.3 KB

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