GPBDecoder.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "Base.h"
  2. #include "GPBDecoder.h"
  3. namespace gameplay
  4. {
  5. GPBDecoder::GPBDecoder(void) : _file(NULL), _outFile(NULL)
  6. {
  7. }
  8. GPBDecoder::~GPBDecoder(void)
  9. {
  10. }
  11. void GPBDecoder::readBinary(const std::string& filepath)
  12. {
  13. // open files
  14. _file = fopen(filepath.c_str(), "rb");
  15. std::string outfilePath = filepath;
  16. outfilePath += ".xml";
  17. _outFile = fopen(outfilePath.c_str(), "w");
  18. // read and write files
  19. assert(validateHeading());
  20. fprintf(_outFile, "<root>\n");
  21. readRefs();
  22. fprintf(_outFile, "</root>\n");
  23. // close files
  24. fclose(_outFile);
  25. _outFile = NULL;
  26. fclose(_file);
  27. _file = NULL;
  28. }
  29. bool GPBDecoder::validateHeading()
  30. {
  31. const size_t HEADING_SIZE = 9;
  32. const char identifier[] = { '«', 'G', 'P', 'B', '»', '\r', '\n', '\x1A', '\n' };
  33. char heading[HEADING_SIZE];
  34. for (size_t i = 0; i < HEADING_SIZE; ++i)
  35. {
  36. if (heading[i] != identifier[i])
  37. {
  38. return false;
  39. }
  40. }
  41. // read version
  42. unsigned char version[2];
  43. fread(version, sizeof(unsigned char), 2, _file);
  44. // don't care about version
  45. return true;
  46. }
  47. void GPBDecoder::readRefs()
  48. {
  49. fprintf(_outFile, "<RefTable>\n");
  50. // read number of refs
  51. unsigned int refCount = 0;
  52. assert(read(&refCount));
  53. for (size_t i = 0; i < refCount; ++i)
  54. {
  55. readRef();
  56. }
  57. fprintf(_outFile, "</RefTable>\n");
  58. }
  59. void GPBDecoder::readRef()
  60. {
  61. std::string xref = readString(_file);
  62. unsigned int type = 0, offset = 0;
  63. assert(read(&type));
  64. assert(read(&offset));
  65. fprintf(_outFile, "<Reference>\n");
  66. fprintfElement(_outFile, "xref", xref);
  67. fprintfElement(_outFile, "type", type);
  68. fprintfElement(_outFile, "offset", offset);
  69. fprintf(_outFile, "</Reference>\n");
  70. }
  71. bool GPBDecoder::read(unsigned int* ptr)
  72. {
  73. return fread(ptr, sizeof(unsigned int), 1, _file) == 1;
  74. }
  75. std::string GPBDecoder::readString(FILE* fp)
  76. {
  77. unsigned int length;
  78. if (fread(&length, 4, 1, fp) != 1)
  79. {
  80. return std::string();
  81. }
  82. char* str = new char[length + 1];
  83. if (length > 0)
  84. {
  85. if (fread(str, 1, length, fp) != length)
  86. {
  87. delete[] str;
  88. return std::string();
  89. }
  90. }
  91. str[length] = '\0';
  92. std::string result(str);
  93. delete[] str;
  94. return result;
  95. }
  96. }