Reference.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "Base.h"
  2. #include "Reference.h"
  3. namespace gameplay
  4. {
  5. Reference::Reference(void) :
  6. _type(0),
  7. _offset(0),
  8. _ref(NULL)
  9. {
  10. }
  11. Reference::Reference(std::string _xref, Object* _ref) :
  12. _xref(_xref),
  13. _type(_ref->getTypeId()),
  14. _offset(0),
  15. _ref(_ref)
  16. {
  17. }
  18. Reference::~Reference(void)
  19. {
  20. }
  21. const char* Reference::getElementName(void) const
  22. {
  23. return "Reference";
  24. }
  25. void Reference::writeBinary(FILE* file)
  26. {
  27. Object::writeBinary(file);
  28. write(_xref, file);
  29. write(_type, file);
  30. write(_offset, file);
  31. }
  32. void Reference::writeText(FILE* file)
  33. {
  34. fprintElementStart(file);
  35. fprintfElement(file, "xref", _xref);
  36. fprintfElement(file, "type", _type);
  37. fprintfElement(file, "offset", _offset);
  38. fprintElementEnd(file);
  39. }
  40. bool Reference::updateOffset(FILE* file)
  41. {
  42. long newOffset = _ref->getFilePosition();
  43. return updateOffset(file, newOffset);
  44. }
  45. bool Reference::updateOffset(FILE* file, long newOffset)
  46. {
  47. if (getFilePosition() > 0)
  48. {
  49. // save the current offset
  50. long savedOffset = ftell(file);
  51. // update the offset data for this
  52. _offset = newOffset;
  53. // seek this Reference object in the file
  54. fseek(file, getFilePosition(), SEEK_SET);
  55. // skip over the object type
  56. //fseek(file, sizeof(unsigned int), SEEK_CUR);
  57. // skip over the id string
  58. skipString(file);
  59. // skip over the type
  60. //skipUint(file);
  61. // write over the old offset
  62. write(_offset, file);
  63. // restore the offset
  64. fseek(file, savedOffset, SEEK_SET);
  65. return true;
  66. }
  67. return false;
  68. }
  69. Object* Reference::getObj()
  70. {
  71. return _ref;
  72. }
  73. }