FileIO.h 4.0 KB

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