Serial.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "stdh.h"
  13. #include <Engine/Base/Serial.h>
  14. #include <Engine/Base/Stream.h>
  15. #include <Engine/Base/CRCTable.h>
  16. /*
  17. * Default constructor.
  18. */
  19. CSerial::CSerial( void) : ser_ctUsed(0) // not used initially
  20. {
  21. }
  22. /*
  23. * Destructor.
  24. */
  25. CSerial::~CSerial( void)
  26. {
  27. // must not be used at all
  28. ASSERT(ser_ctUsed == 0); // look at _strLastCleared for possible name
  29. }
  30. /*
  31. * Clear the object.
  32. */
  33. void CSerial::Clear(void)
  34. {
  35. // mark that you have changed
  36. MarkChanged();
  37. // clear the filename
  38. ser_FileName.Clear();
  39. }
  40. /* Get the description of this object. */
  41. CTString CSerial::GetDescription(void)
  42. {
  43. return "<no description>";
  44. }
  45. /*
  46. * Load from file.
  47. */
  48. void CSerial::Load_t(const CTFileName fnFileName) // throw char *
  49. {
  50. ASSERT(!IsUsed());
  51. // mark that you have changed
  52. MarkChanged();
  53. // open a stream
  54. CTFileStream istrFile;
  55. istrFile.Open_t(fnFileName);
  56. // read object from stream
  57. Read_t(&istrFile);
  58. // if still here (no exceptions raised)
  59. // remember filename
  60. ser_FileName = fnFileName;
  61. }
  62. /*
  63. * Reload from file.
  64. */
  65. void CSerial::Reload(void)
  66. {
  67. // mark that you have changed
  68. MarkChanged();
  69. CTFileName fnmOldName = ser_FileName;
  70. Clear();
  71. // try to
  72. //try {
  73. // open a stream
  74. CTFileStream istrFile;
  75. istrFile.Open_t(fnmOldName);
  76. // read object from stream
  77. Read_t(&istrFile);
  78. // if there is some error while reloading
  79. //} catch (char *strError) {
  80. // quit the application with error explanation
  81. //FatalError(TRANS("Cannot reload file '%s':\n%s"), (CTString&)fnmOldName, strError);
  82. //}
  83. // if still here (no exceptions raised)
  84. // remember filename
  85. ser_FileName = fnmOldName;
  86. }
  87. /*
  88. * Save to file.
  89. */
  90. void CSerial::Save_t(const CTFileName fnFileName) // throw char *
  91. {
  92. // open a stream
  93. CTFileStream ostrFile;
  94. ostrFile.Create_t(fnFileName);
  95. // write object to stream
  96. Write_t(&ostrFile);
  97. // if still here (no exceptions raised)
  98. // remember new filename
  99. ser_FileName = fnFileName;
  100. }
  101. /*
  102. * Mark that object is used once more.
  103. */
  104. void CSerial::MarkUsed(void)
  105. {
  106. // use count must not have dropped below zero
  107. ASSERT(ser_ctUsed>=0);
  108. // increment use count
  109. ser_ctUsed++;
  110. }
  111. /*
  112. * Mark that object is used once less.
  113. */
  114. void CSerial::MarkUnused(void)
  115. {
  116. // decrement use count
  117. ser_ctUsed--;
  118. // use count must not have dropped below zero
  119. ASSERT(ser_ctUsed>=0);
  120. }
  121. /*
  122. * Check if object is used at least once.
  123. */
  124. BOOL CSerial::IsUsed(void)
  125. {
  126. // use count must not have dropped below zero
  127. ASSERT(ser_ctUsed>=0);
  128. return ser_ctUsed>0;
  129. }
  130. INDEX CSerial::GetUsedCount(void)
  131. {
  132. return ser_ctUsed;
  133. }
  134. // gather the CRC of the file
  135. void CSerial::AddToCRCTable(void)
  136. {
  137. // add the file to CRC table
  138. CRCT_AddFile_t(ser_FileName);
  139. }