namedsel.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/Tools/max2w3d/namedsel.cpp 4 10/28/97 6:08p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando Tools - WWSkin *
  24. * *
  25. * $Archive:: /Commando/Code/Tools/max2w3d/namedsel.cpp $*
  26. * *
  27. * $Author:: Greg_h $*
  28. * *
  29. * $Modtime:: 10/26/97 1:29p $*
  30. * *
  31. * $Revision:: 4 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "namedsel.h"
  37. NamedSelSetList::~NamedSelSetList()
  38. {
  39. for (int i=0; i<Sets.Count(); i++) {
  40. delete Sets[i];
  41. Sets[i] = NULL;
  42. delete Names[i];
  43. Names[i] = NULL;
  44. }
  45. }
  46. void NamedSelSetList::Append_Set(BitArray & nset,TSTR & setname)
  47. {
  48. BitArray *n = new BitArray(nset);
  49. Sets.Append(1,&n);
  50. TSTR * name = new TSTR(setname);
  51. Names.Append(1,&name);
  52. assert(Names.Count() == Sets.Count());
  53. }
  54. void NamedSelSetList::Delete_Set(int i)
  55. {
  56. delete Sets[i];
  57. Sets.Delete(i,1);
  58. delete Names[i];
  59. Names.Delete(i,1);
  60. assert(Names.Count() == Sets.Count());
  61. }
  62. void NamedSelSetList::Delete_Set(TSTR & setname)
  63. {
  64. int i = Find_Set(setname);
  65. if (i >= 0) Delete_Set(i);
  66. }
  67. void NamedSelSetList::Reset(void)
  68. {
  69. while (Sets.Count() > 0) {
  70. Delete_Set(0);
  71. }
  72. }
  73. void NamedSelSetList::Set_Size(int size)
  74. {
  75. for (int i=0; i<Sets.Count(); i++) {
  76. Sets[i]->SetSize(size,TRUE);
  77. }
  78. }
  79. NamedSelSetList& NamedSelSetList::operator=(NamedSelSetList& from)
  80. {
  81. for (int i=0; i<Sets.Count(); i++) {
  82. Delete_Set(i);
  83. }
  84. Sets.SetCount(0);
  85. Names.SetCount(0);
  86. for (i=0; i<from.Count(); i++) {
  87. Append_Set(from[i],*(from.Names[i]));
  88. }
  89. return *this;
  90. }
  91. int NamedSelSetList::Find_Set(TSTR &setname)
  92. {
  93. for (int i=0; i<Names.Count(); i++) {
  94. if (setname == *Names[i]) {
  95. return i;
  96. }
  97. }
  98. return -1;
  99. }
  100. IOResult NamedSelSetList::Save(ISave *isave)
  101. {
  102. assert(Sets.Count() == Names.Count());
  103. for (int i=0; i<Sets.Count(); i++) {
  104. isave->BeginChunk(NAMED_SEL_SET_CHUNK);
  105. isave->BeginChunk(NAMED_SEL_NAME_CHUNK);
  106. isave->WriteWString(*Names[i]);
  107. isave->EndChunk();
  108. isave->BeginChunk(NAMED_SEL_BITS_CHUNK);
  109. Sets[i]->Save(isave);
  110. isave->EndChunk();
  111. isave->EndChunk();
  112. }
  113. return IO_OK;
  114. }
  115. IOResult NamedSelSetList::Load(ILoad *iload)
  116. {
  117. IOResult res;
  118. while (IO_OK==(res=iload->OpenChunk())) {
  119. switch (iload->CurChunkID()) {
  120. case NAMED_SEL_SET_CHUNK:
  121. res = Load_Set(iload);
  122. break;
  123. default:
  124. assert(0);
  125. break;
  126. }
  127. iload->CloseChunk();
  128. if (res!=IO_OK) return res;
  129. }
  130. return IO_OK;
  131. }
  132. IOResult NamedSelSetList::Load_Set(ILoad * iload)
  133. {
  134. IOResult res;
  135. BitArray set;
  136. TCHAR * name;
  137. BOOL gotset = FALSE;
  138. BOOL gotname = FALSE;
  139. res = iload->OpenChunk();
  140. while (IO_OK==(res=iload->OpenChunk())) {
  141. switch (iload->CurChunkID()) {
  142. case NAMED_SEL_BITS_CHUNK:
  143. {
  144. res = set.Load(iload);
  145. gotset = TRUE;
  146. break;
  147. }
  148. case NAMED_SEL_NAME_CHUNK:
  149. {
  150. res = iload->ReadWStringChunk(&name);
  151. gotname = TRUE;
  152. break;
  153. }
  154. }
  155. iload->CloseChunk();
  156. if (res != IO_OK) return res;
  157. }
  158. assert(gotset && gotname);
  159. Append_Set(set,TSTR(name));
  160. return IO_OK;
  161. }