simFieldDictionary.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sim/simFieldDictionary.h"
  23. #include "memory/dataChunker.h"
  24. #include "console/consoleInternal.h"
  25. #include "memory/frameAllocator.h"
  26. //-----------------------------------------------------------------------------
  27. SimFieldDictionary::Entry *SimFieldDictionary::mFreeList = NULL;
  28. static Chunker<SimFieldDictionary::Entry> fieldChunker;
  29. SimFieldDictionary::Entry *SimFieldDictionary::allocEntry()
  30. {
  31. if(mFreeList)
  32. {
  33. Entry *ret = mFreeList;
  34. mFreeList = ret->next;
  35. return ret;
  36. }
  37. else
  38. return fieldChunker.alloc();
  39. }
  40. void SimFieldDictionary::freeEntry(SimFieldDictionary::Entry *ent)
  41. {
  42. ent->next = mFreeList;
  43. mFreeList = ent;
  44. }
  45. SimFieldDictionary::SimFieldDictionary()
  46. {
  47. for(U32 i = 0; i < HashTableSize; i++)
  48. mHashTable[i] = 0;
  49. mVersion = 0;
  50. }
  51. SimFieldDictionary::~SimFieldDictionary()
  52. {
  53. for(U32 i = 0; i < HashTableSize; i++)
  54. {
  55. for(Entry *walk = mHashTable[i]; walk;)
  56. {
  57. Entry *temp = walk;
  58. walk = temp->next;
  59. dFree(temp->value);
  60. freeEntry(temp);
  61. }
  62. }
  63. }
  64. void SimFieldDictionary::setFieldValue(StringTableEntry slotName, const char *value)
  65. {
  66. U32 bucket = HashPointer(slotName) % HashTableSize;
  67. Entry **walk = &mHashTable[bucket];
  68. while(*walk && (*walk)->slotName != slotName)
  69. walk = &((*walk)->next);
  70. Entry *field = *walk;
  71. if(!*value)
  72. {
  73. if(field)
  74. {
  75. mVersion++;
  76. dFree(field->value);
  77. *walk = field->next;
  78. freeEntry(field);
  79. }
  80. }
  81. else
  82. {
  83. if(field)
  84. {
  85. dFree(field->value);
  86. field->value = dStrdup(value);
  87. }
  88. else
  89. {
  90. mVersion++;
  91. field = allocEntry();
  92. field->value = dStrdup(value);
  93. field->slotName = slotName;
  94. field->next = NULL;
  95. *walk = field;
  96. }
  97. }
  98. }
  99. const char *SimFieldDictionary::getFieldValue(StringTableEntry slotName)
  100. {
  101. U32 bucket = HashPointer(slotName) % HashTableSize;
  102. for(Entry *walk = mHashTable[bucket];walk;walk = walk->next)
  103. if(walk->slotName == slotName)
  104. return walk->value;
  105. return NULL;
  106. }
  107. void SimFieldDictionary::assignFrom(SimFieldDictionary *dict)
  108. {
  109. mVersion++;
  110. for(U32 i = 0; i < HashTableSize; i++)
  111. for(Entry *walk = dict->mHashTable[i];walk; walk = walk->next)
  112. setFieldValue(walk->slotName, walk->value);
  113. }
  114. static S32 QSORT_CALLBACK compareEntries(const void* a,const void* b)
  115. {
  116. SimFieldDictionary::Entry *fa = *((SimFieldDictionary::Entry **)a);
  117. SimFieldDictionary::Entry *fb = *((SimFieldDictionary::Entry **)b);
  118. return dStricmp(fa->slotName, fb->slotName);
  119. }
  120. void SimFieldDictionary::writeFields(SimObject *obj, Stream &stream, U32 tabStop)
  121. {
  122. const AbstractClassRep::FieldList &list = obj->getFieldList();
  123. Vector<Entry *> flist(__FILE__, __LINE__);
  124. for(U32 i = 0; i < HashTableSize; i++)
  125. {
  126. for(Entry *walk = mHashTable[i];walk; walk = walk->next)
  127. {
  128. // make sure we haven't written this out yet:
  129. U32 i;
  130. for(i = 0; i < (U32)list.size(); i++)
  131. if(list[i].pFieldname == walk->slotName)
  132. break;
  133. if(i != list.size())
  134. continue;
  135. if (!obj->writeField(walk->slotName, walk->value))
  136. continue;
  137. flist.push_back(walk);
  138. }
  139. }
  140. // Sort Entries to prevent version control conflicts
  141. dQsort(flist.address(),flist.size(),sizeof(Entry *),compareEntries);
  142. // Save them out
  143. for(Vector<Entry *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  144. {
  145. U32 nBufferSize = (dStrlen( (*itr)->value ) * 2) + dStrlen( (*itr)->slotName ) + 16;
  146. FrameTemp<char> expandedBuffer( nBufferSize );
  147. stream.writeTabs(tabStop+1);
  148. dSprintf(expandedBuffer, nBufferSize, "%s = \"", (*itr)->slotName);
  149. expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), (*itr)->value);
  150. dStrcat(expandedBuffer, "\";\r\n");
  151. stream.write(dStrlen(expandedBuffer),expandedBuffer);
  152. }
  153. }
  154. void SimFieldDictionary::printFields(SimObject *obj)
  155. {
  156. const AbstractClassRep::FieldList &list = obj->getFieldList();
  157. char expandedBuffer[4096];
  158. Vector<Entry *> flist(__FILE__, __LINE__);
  159. for(U32 i = 0; i < HashTableSize; i++)
  160. {
  161. for(Entry *walk = mHashTable[i];walk; walk = walk->next)
  162. {
  163. // make sure we haven't written this out yet:
  164. U32 i;
  165. for(i = 0; i < (U32)list.size(); i++)
  166. if(list[i].pFieldname == walk->slotName)
  167. break;
  168. if(i != list.size())
  169. continue;
  170. flist.push_back(walk);
  171. }
  172. }
  173. dQsort(flist.address(),flist.size(),sizeof(Entry *),compareEntries);
  174. for(Vector<Entry *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  175. {
  176. dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s = \"", (*itr)->slotName);
  177. expandEscape(expandedBuffer + dStrlen(expandedBuffer), (*itr)->value);
  178. Con::printf("%s\"", expandedBuffer);
  179. }
  180. }
  181. //------------------------------------------------------------------------------
  182. SimFieldDictionaryIterator::SimFieldDictionaryIterator(SimFieldDictionary * dictionary)
  183. {
  184. mDictionary = dictionary;
  185. mHashIndex = -1;
  186. mEntry = 0;
  187. operator++();
  188. }
  189. SimFieldDictionary::Entry* SimFieldDictionaryIterator::operator++()
  190. {
  191. if(!mDictionary)
  192. return(mEntry);
  193. if(mEntry)
  194. mEntry = mEntry->next;
  195. while(!mEntry && (mHashIndex < (SimFieldDictionary::HashTableSize-1)))
  196. mEntry = mDictionary->mHashTable[++mHashIndex];
  197. return(mEntry);
  198. }
  199. SimFieldDictionary::Entry* SimFieldDictionaryIterator::operator*()
  200. {
  201. return(mEntry);
  202. }