daeStringTable.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
  5. * file except in compliance with the License. You may obtain a copy of the License at:
  6. * http://research.scea.com/scea_shared_source_license.html
  7. *
  8. * Unless required by applicable law or agreed to in writing, software distributed under the License
  9. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing permissions and limitations under the
  11. * License.
  12. */
  13. #include <dae/daeStringTable.h>
  14. daeStringTable::daeStringTable(int stringBufferSize):_stringBufferSize(stringBufferSize), _empty( "" )
  15. {
  16. _stringBufferIndex = _stringBufferSize;
  17. //allocate initial buffer
  18. //allocateBuffer();
  19. }
  20. daeString daeStringTable::allocateBuffer()
  21. {
  22. daeString buf = new daeChar[_stringBufferSize];
  23. _stringBuffersList.append(buf);
  24. _stringBufferIndex = 0;
  25. return buf;
  26. }
  27. daeString daeStringTable::allocString(daeString string)
  28. {
  29. if ( string == NULL ) return _empty;
  30. size_t stringSize = strlen(string) + 1;
  31. size_t sizeLeft = _stringBufferSize - _stringBufferIndex;
  32. daeString buf;
  33. if (sizeLeft < stringSize)
  34. {
  35. if (stringSize > _stringBufferSize)
  36. _stringBufferSize = ((stringSize / _stringBufferSize) + 1) * _stringBufferSize ;
  37. buf = allocateBuffer();
  38. }
  39. else
  40. {
  41. buf = _stringBuffersList.get((daeInt)_stringBuffersList.getCount()-1);
  42. }
  43. daeChar *str = (char*)buf + _stringBufferIndex;
  44. memcpy(str,string,stringSize);
  45. _stringBufferIndex += stringSize;
  46. int align = sizeof(void*);
  47. _stringBufferIndex = (_stringBufferIndex+(align-1)) & (~(align-1));
  48. return str;
  49. }
  50. void daeStringTable::clear()
  51. {
  52. unsigned int i;
  53. for (i=0;i<_stringBuffersList.getCount();i++)
  54. #if defined(_MSC_VER) && (_MSC_VER <= 1200)
  55. delete [] (char *) _stringBuffersList[i];
  56. #else
  57. delete [] _stringBuffersList[i];
  58. #endif
  59. _stringBuffersList.clear();
  60. _stringBufferIndex = _stringBufferSize;
  61. }