00001
00021 #ifndef __VEC_STR_H__
00022 #define __VEC_STR_H__
00023
00027
00028 #include <stdio.h>
00029
00033
00037
00038 typedef struct Vec_Str_t_ Vec_Str_t;
00039 struct Vec_Str_t_
00040 {
00041 int nCap;
00042 int nSize;
00043 char * pArray;
00044 };
00045
00049
00050 #define Vec_StrForEachEntry( vVec, Entry, i ) \
00051 for ( i = 0; (i < Vec_StrSize(vVec)) && (((Entry) = Vec_StrEntry(vVec, i)), 1); i++ )
00052
00056
00068 static inline Vec_Str_t * Vec_StrAlloc( int nCap )
00069 {
00070 Vec_Str_t * p;
00071 p = ALLOC( Vec_Str_t, 1 );
00072 if ( nCap > 0 && nCap < 16 )
00073 nCap = 16;
00074 p->nSize = 0;
00075 p->nCap = nCap;
00076 p->pArray = p->nCap? ALLOC( char, p->nCap ) : NULL;
00077 return p;
00078 }
00079
00091 static inline Vec_Str_t * Vec_StrStart( int nSize )
00092 {
00093 Vec_Str_t * p;
00094 p = Vec_StrAlloc( nSize );
00095 p->nSize = nSize;
00096 memset( p->pArray, 0, sizeof(char) * nSize );
00097 return p;
00098 }
00099
00111 static inline Vec_Str_t * Vec_StrAllocArray( char * pArray, int nSize )
00112 {
00113 Vec_Str_t * p;
00114 p = ALLOC( Vec_Str_t, 1 );
00115 p->nSize = nSize;
00116 p->nCap = nSize;
00117 p->pArray = pArray;
00118 return p;
00119 }
00120
00132 static inline Vec_Str_t * Vec_StrAllocArrayCopy( char * pArray, int nSize )
00133 {
00134 Vec_Str_t * p;
00135 p = ALLOC( Vec_Str_t, 1 );
00136 p->nSize = nSize;
00137 p->nCap = nSize;
00138 p->pArray = ALLOC( char, nSize );
00139 memcpy( p->pArray, pArray, sizeof(char) * nSize );
00140 return p;
00141 }
00142
00154 static inline Vec_Str_t * Vec_StrDup( Vec_Str_t * pVec )
00155 {
00156 Vec_Str_t * p;
00157 p = ALLOC( Vec_Str_t, 1 );
00158 p->nSize = pVec->nSize;
00159 p->nCap = pVec->nCap;
00160 p->pArray = p->nCap? ALLOC( char, p->nCap ) : NULL;
00161 memcpy( p->pArray, pVec->pArray, sizeof(char) * pVec->nSize );
00162 return p;
00163 }
00164
00176 static inline Vec_Str_t * Vec_StrDupArray( Vec_Str_t * pVec )
00177 {
00178 Vec_Str_t * p;
00179 p = ALLOC( Vec_Str_t, 1 );
00180 p->nSize = pVec->nSize;
00181 p->nCap = pVec->nCap;
00182 p->pArray = pVec->pArray;
00183 pVec->nSize = 0;
00184 pVec->nCap = 0;
00185 pVec->pArray = NULL;
00186 return p;
00187 }
00188
00200 static inline void Vec_StrFree( Vec_Str_t * p )
00201 {
00202 FREE( p->pArray );
00203 FREE( p );
00204 }
00205
00217 static inline char * Vec_StrReleaseArray( Vec_Str_t * p )
00218 {
00219 char * pArray = p->pArray;
00220 p->nCap = 0;
00221 p->nSize = 0;
00222 p->pArray = NULL;
00223 return pArray;
00224 }
00225
00237 static inline char * Vec_StrArray( Vec_Str_t * p )
00238 {
00239 return p->pArray;
00240 }
00241
00253 static inline int Vec_StrSize( Vec_Str_t * p )
00254 {
00255 return p->nSize;
00256 }
00257
00269 static inline char Vec_StrEntry( Vec_Str_t * p, int i )
00270 {
00271 assert( i >= 0 && i < p->nSize );
00272 return p->pArray[i];
00273 }
00274
00286 static inline void Vec_StrWriteEntry( Vec_Str_t * p, int i, char Entry )
00287 {
00288 assert( i >= 0 && i < p->nSize );
00289 p->pArray[i] = Entry;
00290 }
00291
00303 static inline char Vec_StrEntryLast( Vec_Str_t * p )
00304 {
00305 assert( p->nSize > 0 );
00306 return p->pArray[p->nSize-1];
00307 }
00308
00320 static inline void Vec_StrGrow( Vec_Str_t * p, int nCapMin )
00321 {
00322 if ( p->nCap >= nCapMin )
00323 return;
00324 p->pArray = REALLOC( char, p->pArray, 2 * nCapMin );
00325 p->nCap = 2 * nCapMin;
00326 }
00327
00339 static inline void Vec_StrFill( Vec_Str_t * p, int nSize, char Entry )
00340 {
00341 int i;
00342 Vec_StrGrow( p, nSize );
00343 p->nSize = nSize;
00344 for ( i = 0; i < p->nSize; i++ )
00345 p->pArray[i] = Entry;
00346 }
00347
00359 static inline void Vec_StrShrink( Vec_Str_t * p, int nSizeNew )
00360 {
00361 assert( p->nSize >= nSizeNew );
00362 p->nSize = nSizeNew;
00363 }
00364
00376 static inline void Vec_StrClear( Vec_Str_t * p )
00377 {
00378 p->nSize = 0;
00379 }
00380
00392 static inline void Vec_StrPush( Vec_Str_t * p, char Entry )
00393 {
00394 if ( p->nSize == p->nCap )
00395 {
00396 if ( p->nCap < 16 )
00397 Vec_StrGrow( p, 16 );
00398 else
00399 Vec_StrGrow( p, 2 * p->nCap );
00400 }
00401 p->pArray[p->nSize++] = Entry;
00402 }
00403
00415 static inline int Vec_StrBase10Log( unsigned Num )
00416 {
00417 int Res;
00418 assert( Num >= 0 );
00419 if ( Num == 0 ) return 0;
00420 if ( Num == 1 ) return 1;
00421 for ( Res = 0, Num--; Num; Num /= 10, Res++ );
00422 return Res;
00423 }
00424
00436 static inline void Vec_StrPrintNum( Vec_Str_t * p, int Num )
00437 {
00438 int i, nDigits;
00439 if ( Num < 0 )
00440 {
00441 Vec_StrPush( p, '-' );
00442 Num = -Num;
00443 }
00444 if ( Num < 10 )
00445 {
00446 Vec_StrPush( p, (char)('0' + Num) );
00447 return;
00448 }
00449 nDigits = Vec_StrBase10Log( Num );
00450 Vec_StrGrow( p, p->nSize + nDigits );
00451 for ( i = nDigits - 1; i >= 0; i-- )
00452 {
00453 Vec_StrWriteEntry( p, p->nSize + i, (char)('0' + Num % 10) );
00454 Num /= 10;
00455 }
00456 assert( Num == 0 );
00457 p->nSize += nDigits;
00458 }
00459
00471 static inline void Vec_StrPrintStr( Vec_Str_t * p, char * pStr )
00472 {
00473 int i, Length = strlen(pStr);
00474 for ( i = 0; i < Length; i++ )
00475 Vec_StrPush( p, pStr[i] );
00476 }
00477
00489 static inline void Vec_StrAppend( Vec_Str_t * p, char * pString )
00490 {
00491 int i, nLength = strlen(pString);
00492 Vec_StrGrow( p, p->nSize + nLength );
00493 for ( i = 0; i < nLength; i++ )
00494 p->pArray[p->nSize + i] = pString[i];
00495 p->nSize += nLength;
00496 }
00497
00509 static inline char Vec_StrPop( Vec_Str_t * p )
00510 {
00511 assert( p->nSize > 0 );
00512 return p->pArray[--p->nSize];
00513 }
00514
00526 static inline int Vec_StrSortCompare1( char * pp1, char * pp2 )
00527 {
00528
00529 if ( *pp1 < *pp2 )
00530 return -1;
00531 if ( *pp1 > *pp2 )
00532 return 1;
00533 return 0;
00534 }
00535
00547 static inline int Vec_StrSortCompare2( char * pp1, char * pp2 )
00548 {
00549
00550 if ( *pp1 > *pp2 )
00551 return -1;
00552 if ( *pp1 < *pp2 )
00553 return 1;
00554 return 0;
00555 }
00556
00568 static inline void Vec_StrSort( Vec_Str_t * p, int fReverse )
00569 {
00570 if ( fReverse )
00571 qsort( (void *)p->pArray, p->nSize, sizeof(char),
00572 (int (*)(const void *, const void *)) Vec_StrSortCompare2 );
00573 else
00574 qsort( (void *)p->pArray, p->nSize, sizeof(char),
00575 (int (*)(const void *, const void *)) Vec_StrSortCompare1 );
00576 }
00577
00578 #endif
00579
00583