FileIO.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef FILEIO_H_
  2. #define FILEIO_H_
  3. #include <iostream>
  4. #include <list>
  5. #include <vector>
  6. #include "Base.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * Writes an XML element to the specified file stream.
  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>", elementName);
  25. 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>", elementName);
  36. 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. * @param value The value to be written
  47. * @param file The binary file stream.
  48. */
  49. void write(unsigned char value, FILE* file);
  50. void write(char value, FILE* file);
  51. void write(const char* str, FILE* file);
  52. void write(unsigned int value, FILE* file);
  53. void write(unsigned long value, FILE* file);
  54. void write(unsigned short value, FILE* file);
  55. void write(bool value, FILE* file);
  56. void write(float value, FILE* file);
  57. void write(const float* values, int length, FILE* file);
  58. /**
  59. * Writes the length of the string and the string bytes to the binary file stream.
  60. */
  61. void write(const std::string& str, FILE* file);
  62. void writeZero(FILE* file);
  63. /**
  64. * Writes the length of the list and writes each element value to the binary file stream.
  65. * @param list The list to write.
  66. * @param file The binary file stream.
  67. */
  68. template <class T>
  69. void write(std::list<T> list, FILE* file)
  70. {
  71. // First write the size of the list
  72. write(list.size(), file);
  73. // Then write each element
  74. std::list<T>::const_iterator i;
  75. for (i = list.begin(); i != list.end(); i++)
  76. {
  77. write(*i, file);
  78. }
  79. }
  80. /**
  81. * Writes the length of the vector and writes each element value to the binary file stream.
  82. * @param vector The vector to write.
  83. * @param file The binary file stream.
  84. */
  85. template <class T>
  86. void write(std::vector<T> vector, FILE* file)
  87. {
  88. // First write the size of the vector
  89. write(vector.size(), file);
  90. // Then write each element
  91. std::vector<T>::const_iterator i;
  92. for (i = vector.begin(); i != vector.end(); i++)
  93. {
  94. write(*i, file);
  95. }
  96. }
  97. /**
  98. * Skips over the string at the current file stream offset by moving the file position.
  99. * Assumes the current position points to the unsigned int length of the string.
  100. * The string is assumed to be a char array.
  101. * @param The file stream.
  102. */
  103. void skipString(FILE* file);
  104. void skipUint(FILE* file);
  105. }
  106. #endif