Home | History | Annotate | Download | only in dae
      1 /*
      2 * Copyright 2006 Sony Computer Entertainment Inc.
      3 *
      4 * Licensed under the MIT Open Source License, for details please see license.txt or the website
      5 * http://www.opensource.org/licenses/mit-license.php
      6 *
      7 */
      8 
      9 #include <dae/daeStringTable.h>
     10 
     11 daeStringTable::daeStringTable(int stringBufferSize):_stringBufferSize(stringBufferSize), _empty( "" )
     12 {
     13 	_stringBufferIndex = _stringBufferSize;
     14 	//allocate initial buffer
     15 	//allocateBuffer();
     16 }
     17 
     18 daeString daeStringTable::allocateBuffer()
     19 {
     20 	daeString buf = new daeChar[_stringBufferSize];
     21 	_stringBuffersList.append(buf);
     22 	_stringBufferIndex = 0;
     23 	return buf;
     24 }
     25 
     26 daeString daeStringTable::allocString(daeString string)
     27 {
     28 	if ( string == NULL ) return _empty;
     29 	size_t stringSize = strlen(string) + 1;
     30 	size_t sizeLeft = _stringBufferSize - _stringBufferIndex;
     31 	daeString buf;
     32 	if (sizeLeft < stringSize)
     33 	{
     34 		if (stringSize > _stringBufferSize)
     35 			_stringBufferSize = ((stringSize / _stringBufferSize) + 1) * _stringBufferSize ;
     36 		buf = allocateBuffer();
     37 	}
     38 	else
     39 	{
     40 		buf = _stringBuffersList.get((daeInt)_stringBuffersList.getCount()-1);
     41 	}
     42 	daeChar *str = (char*)buf + _stringBufferIndex;
     43 	memcpy(str,string,stringSize);
     44 	_stringBufferIndex += stringSize;
     45 
     46 	int align = sizeof(void*);
     47 	_stringBufferIndex = (_stringBufferIndex+(align-1)) & (~(align-1));
     48 
     49 	return str;
     50 }
     51 
     52 void daeStringTable::clear()
     53 {
     54 	unsigned int i;
     55 	for (i=0;i<_stringBuffersList.getCount();i++)
     56 #if _MSC_VER <= 1200
     57 		delete [] (char *) _stringBuffersList[i];
     58 #else
     59 		delete [] _stringBuffersList[i];
     60 #endif
     61 
     62 	_stringBuffersList.clear();
     63 	_stringBufferIndex = _stringBufferSize;
     64 }
     65