123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091 |
- /*
- ===========================================================================
- Doom 3 BFG Edition GPL Source Code
- Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
- This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
- Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
- In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
- If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
- ===========================================================================
- */
- #include "precompiled.h"
- #pragma hdrstop
- #ifdef USE_STRING_DATA_ALLOCATOR
- static idDynamicBlockAlloc<char, 1<<18, 128, TAG_STRING> stringDataAllocator;
- #endif
- idVec4 g_color_table[16] =
- {
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- idVec4(1.0f, 0.0f, 0.0f, 1.0f), // S_COLOR_RED
- idVec4(0.0f, 1.0f, 0.0f, 1.0f), // S_COLOR_GREEN
- idVec4(1.0f, 1.0f, 0.0f, 1.0f), // S_COLOR_YELLOW
- idVec4(0.0f, 0.0f, 1.0f, 1.0f), // S_COLOR_BLUE
- idVec4(0.0f, 1.0f, 1.0f, 1.0f), // S_COLOR_CYAN
- idVec4(1.0f, 0.5f, 0.0f, 1.0f), // S_COLOR_ORANGE
- idVec4(1.0f, 1.0f, 1.0f, 1.0f), // S_COLOR_WHITE
- idVec4(0.5f, 0.5f, 0.5f, 1.0f), // S_COLOR_GRAY
- idVec4(0.0f, 0.0f, 0.0f, 1.0f), // S_COLOR_BLACK
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- idVec4(0.0f, 0.0f, 0.0f, 1.0f),
- };
- const char *units[2][4] =
- {
- { "B", "KB", "MB", "GB" },
- { "B/s", "KB/s", "MB/s", "GB/s" }
- };
- /*
- ============
- idStr::ColorForIndex
- ============
- */
- idVec4 & idStr::ColorForIndex( int i ) {
- return g_color_table[ i & 15 ];
- }
- /*
- ============
- idStr::ReAllocate
- ============
- */
- void idStr::ReAllocate( int amount, bool keepold ) {
- char *newbuffer;
- int newsize;
- int mod;
- //assert( data );
- assert( amount > 0 );
- mod = amount % STR_ALLOC_GRAN;
- if ( !mod ) {
- newsize = amount;
- }
- else {
- newsize = amount + STR_ALLOC_GRAN - mod;
- }
- SetAlloced( newsize );
- #ifdef USE_STRING_DATA_ALLOCATOR
- newbuffer = stringDataAllocator.Alloc( GetAlloced() );
- #else
- newbuffer = new (TAG_STRING) char[ GetAlloced() ];
- #endif
- if ( keepold && data ) {
- data[ len ] = '\0';
- strcpy( newbuffer, data );
- }
- if ( data && data != baseBuffer ) {
- #ifdef USE_STRING_DATA_ALLOCATOR
- stringDataAllocator.Free( data );
- #else
- delete [] data;
- #endif
- }
- data = newbuffer;
- }
- /*
- ============
- idStr::FreeData
- ============
- */
- void idStr::FreeData() {
- if ( IsStatic() ) {
- return;
- }
- if ( data && data != baseBuffer ) {
- #ifdef USE_STRING_DATA_ALLOCATOR
- stringDataAllocator.Free( data );
- #else
- delete[] data;
- #endif
- data = baseBuffer;
- }
- }
- /*
- ============
- idStr::operator=
- ============
- */
- void idStr::operator=( const char *text ) {
- int l;
- int diff;
- int i;
- if ( !text ) {
- // safe behavior if NULL
- EnsureAlloced( 1, false );
- data[ 0 ] = '\0';
- len = 0;
- return;
- }
- if ( text == data ) {
- return; // copying same thing
- }
- // check if we're aliasing
- if ( text >= data && text <= data + len ) {
- diff = text - data;
- assert( strlen( text ) < (unsigned)len );
- for ( i = 0; text[ i ]; i++ ) {
- data[ i ] = text[ i ];
- }
- data[ i ] = '\0';
- len -= diff;
- return;
- }
- l = strlen( text );
- EnsureAlloced( l + 1, false );
- strcpy( data, text );
- len = l;
- }
- /*
- ============
- idStr::FindChar
- returns -1 if not found otherwise the index of the char
- ============
- */
- int idStr::FindChar( const char *str, const char c, int start, int end ) {
- int i;
- if ( end == -1 ) {
- end = strlen( str ) - 1;
- }
- for ( i = start; i <= end; i++ ) {
- if ( str[i] == c ) {
- return i;
- }
- }
- return -1;
- }
- /*
- ============
- idStr::FindText
- returns -1 if not found otherwise the index of the text
- ============
- */
- int idStr::FindText( const char *str, const char *text, bool casesensitive, int start, int end ) {
- int l, i, j;
- if ( end == -1 ) {
- end = strlen( str );
- }
- l = end - strlen( text );
- for ( i = start; i <= l; i++ ) {
- if ( casesensitive ) {
- for ( j = 0; text[j]; j++ ) {
- if ( str[i+j] != text[j] ) {
- break;
- }
- }
- } else {
- for ( j = 0; text[j]; j++ ) {
- if ( ::toupper( str[i+j] ) != ::toupper( text[j] ) ) {
- break;
- }
- }
- }
- if ( !text[j] ) {
- return i;
- }
- }
- return -1;
- }
- /*
- ============
- idStr::Filter
- Returns true if the string conforms the given filter.
- Several metacharacter may be used in the filter.
- * match any string of zero or more characters
- ? match any single character
- [abc...] match any of the enclosed characters; a hyphen can
- be used to specify a range (e.g. a-z, A-Z, 0-9)
- ============
- */
- bool idStr::Filter( const char *filter, const char *name, bool casesensitive ) {
- idStr buf;
- int i, found, index;
- while(*filter) {
- if (*filter == '*') {
- filter++;
- buf.Empty();
- for (i = 0; *filter; i++) {
- if ( *filter == '*' || *filter == '?' || (*filter == '[' && *(filter+1) != '[') ) {
- break;
- }
- buf += *filter;
- if ( *filter == '[' ) {
- filter++;
- }
- filter++;
- }
- if ( buf.Length() ) {
- index = idStr(name).Find( buf.c_str(), casesensitive );
- if ( index == -1 ) {
- return false;
- }
- name += index + strlen(buf);
- }
- }
- else if (*filter == '?') {
- filter++;
- name++;
- }
- else if (*filter == '[') {
- if ( *(filter+1) == '[' ) {
- if ( *name != '[' ) {
- return false;
- }
- filter += 2;
- name++;
- }
- else {
- filter++;
- found = false;
- while(*filter && !found) {
- if (*filter == ']' && *(filter+1) != ']') {
- break;
- }
- if (*(filter+1) == '-' && *(filter+2) && (*(filter+2) != ']' || *(filter+3) == ']')) {
- if (casesensitive) {
- if (*name >= *filter && *name <= *(filter+2)) {
- found = true;
- }
- }
- else {
- if ( ::toupper(*name) >= ::toupper(*filter) && ::toupper(*name) <= ::toupper(*(filter+2)) ) {
- found = true;
- }
- }
- filter += 3;
- }
- else {
- if (casesensitive) {
- if (*filter == *name) {
- found = true;
- }
- }
- else {
- if ( ::toupper(*filter) == ::toupper(*name) ) {
- found = true;
- }
- }
- filter++;
- }
- }
- if (!found) {
- return false;
- }
- while(*filter) {
- if ( *filter == ']' && *(filter+1) != ']' ) {
- break;
- }
- filter++;
- }
- filter++;
- name++;
- }
- }
- else {
- if (casesensitive) {
- if (*filter != *name) {
- return false;
- }
- }
- else {
- if ( ::toupper(*filter) != ::toupper(*name) ) {
- return false;
- }
- }
- filter++;
- name++;
- }
- }
- return true;
- }
- /*
- =============
- idStr::StripMediaName
- makes the string lower case, replaces backslashes with forward slashes, and removes extension
- =============
- */
- void idStr::StripMediaName( const char *name, idStr &mediaName ) {
- char c;
- mediaName.Empty();
- for ( c = *name; c; c = *(++name) ) {
- // truncate at an extension
- if ( c == '.' ) {
- break;
- }
- // convert backslashes to forward slashes
- if ( c == '\\' ) {
- mediaName.Append( '/' );
- } else {
- mediaName.Append( idStr::ToLower( c ) );
- }
- }
- }
- /*
- =============
- idStr::CheckExtension
- =============
- */
- bool idStr::CheckExtension( const char *name, const char *ext ) {
- const char *s1 = name + Length( name ) - 1;
- const char *s2 = ext + Length( ext ) - 1;
- int c1, c2, d;
- do {
- c1 = *s1--;
- c2 = *s2--;
- d = c1 - c2;
- while( d ) {
- if ( c1 <= 'Z' && c1 >= 'A' ) {
- d += ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c2 <= 'Z' && c2 >= 'A' ) {
- d -= ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- return false;
- }
- } while( s1 > name && s2 > ext );
- return ( s1 >= name );
- }
- /*
- =============
- idStr::FloatArrayToString
- =============
- */
- const char *idStr::FloatArrayToString( const float *array, const int length, const int precision ) {
- static int index = 0;
- static char str[4][16384]; // in case called by nested functions
- int i, n;
- char format[16], *s;
- // use an array of string so that multiple calls won't collide
- s = str[ index ];
- index = (index + 1) & 3;
- idStr::snPrintf( format, sizeof( format ), "%%.%df", precision );
- n = idStr::snPrintf( s, sizeof( str[0] ), format, array[0] );
- if ( precision > 0 ) {
- while( n > 0 && s[n-1] == '0' ) s[--n] = '\0';
- while( n > 0 && s[n-1] == '.' ) s[--n] = '\0';
- }
- idStr::snPrintf( format, sizeof( format ), " %%.%df", precision );
- for ( i = 1; i < length; i++ ) {
- n += idStr::snPrintf( s + n, sizeof( str[0] ) - n, format, array[i] );
- if ( precision > 0 ) {
- while( n > 0 && s[n-1] == '0' ) s[--n] = '\0';
- while( n > 0 && s[n-1] == '.' ) s[--n] = '\0';
- }
- }
- return s;
- }
- /*
- ========================
- idStr::CStyleQuote
- ========================
- */
- const char *idStr::CStyleQuote( const char *str ) {
- static int index = 0;
- static char buffers[4][16384]; // in case called by nested functions
- unsigned int i;
- char *buf;
- buf = buffers[index];
- index = ( index + 1 ) & 3;
- buf[0] = '\"';
- for ( i = 1; i < sizeof( buffers[0] ) - 2; i++ ) {
- int c = *str++;
- switch( c ) {
- case '\0': buf[i++] = '\"'; buf[i] = '\0'; return buf;
- case '\\': buf[i++] = '\\'; buf[i] = '\\'; break;
- case '\n': buf[i++] = '\\'; buf[i] = 'n'; break;
- case '\r': buf[i++] = '\\'; buf[i] = 'r'; break;
- case '\t': buf[i++] = '\\'; buf[i] = 't'; break;
- case '\v': buf[i++] = '\\'; buf[i] = 'v'; break;
- case '\b': buf[i++] = '\\'; buf[i] = 'b'; break;
- case '\f': buf[i++] = '\\'; buf[i] = 'f'; break;
- case '\a': buf[i++] = '\\'; buf[i] = 'a'; break;
- case '\'': buf[i++] = '\\'; buf[i] = '\''; break;
- case '\"': buf[i++] = '\\'; buf[i] = '\"'; break;
- case '\?': buf[i++] = '\\'; buf[i] = '\?'; break;
- default: buf[i] = c; break;
- }
- }
- buf[i++] = '\"';
- buf[i] = '\0';
- return buf;
- }
- /*
- ========================
- idStr::CStyleUnQuote
- ========================
- */
- const char *idStr::CStyleUnQuote( const char *str ) {
- if ( str[0] != '\"' ) {
- return str;
- }
- static int index = 0;
- static char buffers[4][16384]; // in case called by nested functions
- unsigned int i;
- char *buf;
- buf = buffers[index];
- index = ( index + 1 ) & 3;
- str++;
- for ( i = 0; i < sizeof( buffers[0] ) - 1; i++ ) {
- int c = *str++;
- if ( c == '\0' ) {
- break;
- } else if ( c == '\\' ) {
- c = *str++;
- switch( c ) {
- case '\\': buf[i] = '\\'; break;
- case 'n': buf[i] = '\n'; break;
- case 'r': buf[i] = '\r'; break;
- case 't': buf[i] = '\t'; break;
- case 'v': buf[i] = '\v'; break;
- case 'b': buf[i] = '\b'; break;
- case 'f': buf[i] = '\f'; break;
- case 'a': buf[i] = '\a'; break;
- case '\'': buf[i] = '\''; break;
- case '\"': buf[i] = '\"'; break;
- case '\?': buf[i] = '\?'; break;
- }
- } else {
- buf[i] = c;
- }
- }
- assert( buf[i-1] == '\"' );
- buf[i-1] = '\0';
- return buf;
- }
- /*
- ============
- idStr::Last
- returns -1 if not found otherwise the index of the char
- ============
- */
- int idStr::Last( const char c ) const {
- int i;
-
- for( i = Length(); i > 0; i-- ) {
- if ( data[ i - 1 ] == c ) {
- return i - 1;
- }
- }
- return -1;
- }
- /*
- ========================
- idStr::Format
- perform a threadsafe sprintf to the string
- ========================
- */
- void idStr::Format( const char *fmt, ... ) {
- va_list argptr;
- char text[MAX_PRINT_MSG];
- va_start( argptr, fmt );
- int len = idStr::vsnPrintf( text, sizeof( text ) - 1, fmt, argptr );
- va_end( argptr );
- text[ sizeof( text ) - 1 ] = '\0';
- if ( (size_t)len >= sizeof( text ) - 1 ) {
- idLib::common->FatalError( "Tried to set a large buffer using %s", fmt );
- }
- *this = text;
- }
- /*
- ========================
- idStr::FormatInt
- Formats integers with commas for readability.
- ========================
- */
- idStr idStr::FormatInt( const int num, bool isCash ) {
- idStr val = va( "%d", num );
- int len = val.Length();
- for ( int i = 0 ; i < ( ( len - 1 ) / 3 ); i++ ) {
- int pos = val.Length() - ( ( i + 1 ) * 3 + i );
- if ( pos > 1 || val[0] != '-' ) {
- val.Insert( ',', pos );
- }
- }
- if ( isCash ) {
- val.Insert( '$', val[0] == '-' ? 1 : 0 );
- }
- return val;
- }
- /*
- ============
- idStr::StripLeading
- ============
- */
- void idStr::StripLeading( const char c ) {
- while( data[ 0 ] == c ) {
- memmove( &data[ 0 ], &data[ 1 ], len );
- len--;
- }
- }
- /*
- ============
- idStr::StripLeading
- ============
- */
- void idStr::StripLeading( const char *string ) {
- int l;
- l = strlen( string );
- if ( l > 0 ) {
- while ( !Cmpn( string, l ) ) {
- memmove( data, data + l, len - l + 1 );
- len -= l;
- }
- }
- }
- /*
- ============
- idStr::StripLeadingOnce
- ============
- */
- bool idStr::StripLeadingOnce( const char *string ) {
- int l;
- l = strlen( string );
- if ( ( l > 0 ) && !Cmpn( string, l ) ) {
- memmove( data, data + l, len - l + 1 );
- len -= l;
- return true;
- }
- return false;
- }
- /*
- ============
- idStr::StripTrailing
- ============
- */
- void idStr::StripTrailing( const char c ) {
- int i;
-
- for( i = Length(); i > 0 && data[ i - 1 ] == c; i-- ) {
- data[ i - 1 ] = '\0';
- len--;
- }
- }
- /*
- ============
- idStr::StripLeading
- ============
- */
- void idStr::StripTrailing( const char *string ) {
- int l;
- l = strlen( string );
- if ( l > 0 ) {
- while ( ( len >= l ) && !Cmpn( string, data + len - l, l ) ) {
- len -= l;
- data[len] = '\0';
- }
- }
- }
- /*
- ============
- idStr::StripTrailingOnce
- ============
- */
- bool idStr::StripTrailingOnce( const char *string ) {
- int l;
- l = strlen( string );
- if ( ( l > 0 ) && ( len >= l ) && !Cmpn( string, data + len - l, l ) ) {
- len -= l;
- data[len] = '\0';
- return true;
- }
- return false;
- }
- /*
- ============
- idStr::Replace
- ============
- */
- bool idStr::ReplaceChar( const char old, const char nw ) {
- bool replaced = false;
- for ( int i = 0; i < Length(); i++ ) {
- if ( data[i] == old ) {
- data[i] = nw;
- replaced = true;
- }
- }
- return replaced;
- }
- /*
- ============
- idStr::Replace
- ============
- */
- bool idStr::Replace( const char *old, const char *nw ) {
- int oldLen = strlen( old );
- int newLen = strlen( nw );
- // Work out how big the new string will be
- int count = 0;
- for ( int i = 0; i < Length(); i++ ) {
- if ( idStr::Cmpn( &data[i], old, oldLen ) == 0 ) {
- count++;
- i += oldLen - 1;
- }
- }
- if ( count ) {
- idStr oldString( data );
- EnsureAlloced( len + ( ( newLen - oldLen ) * count ) + 2, false );
- // Replace the old data with the new data
- int j = 0;
- for ( int i = 0; i < oldString.Length(); i++ ) {
- if ( idStr::Cmpn( &oldString[i], old, oldLen ) == 0 ) {
- memcpy( data + j, nw, newLen );
- i += oldLen - 1;
- j += newLen;
- } else {
- data[j] = oldString[i];
- j++;
- }
- }
- data[j] = 0;
- len = strlen( data );
- return true;
- }
- return false;
- }
- /*
- ============
- idStr::Mid
- ============
- */
- const char *idStr::Mid( int start, int len, idStr &result ) const {
- int i;
- result.Empty();
- i = Length();
- if ( i == 0 || len <= 0 || start >= i ) {
- return NULL;
- }
- if ( start + len >= i ) {
- len = i - start;
- }
- result.Append( &data[ start ], len );
- return result;
- }
- /*
- ============
- idStr::Mid
- ============
- */
- idStr idStr::Mid( int start, int len ) const {
- int i;
- idStr result;
- i = Length();
- if ( i == 0 || len <= 0 || start >= i ) {
- return result;
- }
- if ( start + len >= i ) {
- len = i - start;
- }
- result.Append( &data[ start ], len );
- return result;
- }
- /*
- ============
- idStr::StripTrailingWhitespace
- ============
- */
- void idStr::StripTrailingWhitespace() {
- int i;
-
- // cast to unsigned char to prevent stripping off high-ASCII characters
- for( i = Length(); i > 0 && (unsigned char)(data[ i - 1 ]) <= ' '; i-- ) {
- data[ i - 1 ] = '\0';
- len--;
- }
- }
- /*
- ============
- idStr::StripQuotes
- Removes the quotes from the beginning and end of the string
- ============
- */
- idStr& idStr::StripQuotes ()
- {
- if ( data[0] != '\"' )
- {
- return *this;
- }
-
- // Remove the trailing quote first
- if ( data[len-1] == '\"' )
- {
- data[len-1] = '\0';
- len--;
- }
- // Strip the leading quote now
- len--;
- memmove( &data[ 0 ], &data[ 1 ], len );
- data[len] = '\0';
-
- return *this;
- }
- /*
- =====================================================================
- filename methods
- =====================================================================
- */
- /*
- ============
- idStr::FileNameHash
- ============
- */
- int idStr::FileNameHash() const {
- int i;
- long hash;
- char letter;
- hash = 0;
- i = 0;
- while( data[i] != '\0' ) {
- letter = idStr::ToLower( data[i] );
- if ( letter == '.' ) {
- break; // don't include extension
- }
- if ( letter =='\\' ) {
- letter = '/';
- }
- hash += (long)(letter)*(i+119);
- i++;
- }
- hash &= (FILE_HASH_SIZE-1);
- return hash;
- }
- /*
- ============
- idStr::BackSlashesToSlashes
- ============
- */
- idStr &idStr::BackSlashesToSlashes() {
- int i;
- for ( i = 0; i < len; i++ ) {
- if ( data[ i ] == '\\' ) {
- data[ i ] = '/';
- }
- }
- return *this;
- }
- /*
- ============
- idStr::SlashesToBackSlashes
- ============
- */
- idStr &idStr::SlashesToBackSlashes() {
- int i;
- for ( i = 0; i < len; i++ ) {
- if ( data[ i ] == '/' ) {
- data[ i ] = '\\';
- }
- }
- return *this;
- }
- /*
- ============
- idStr::SetFileExtension
- ============
- */
- idStr &idStr::SetFileExtension( const char *extension ) {
- StripFileExtension();
- if ( *extension != '.' ) {
- Append( '.' );
- }
- Append( extension );
- return *this;
- }
- /*
- ============
- idStr::StripFileExtension
- ============
- */
- idStr &idStr::StripFileExtension() {
- int i;
- for ( i = len-1; i >= 0; i-- ) {
- if ( data[i] == '.' ) {
- data[i] = '\0';
- len = i;
- break;
- }
- }
- return *this;
- }
- /*
- ============
- idStr::StripAbsoluteFileExtension
- ============
- */
- idStr &idStr::StripAbsoluteFileExtension() {
- int i;
- for ( i = 0; i < len; i++ ) {
- if ( data[i] == '.' ) {
- data[i] = '\0';
- len = i;
- break;
- }
- }
- return *this;
- }
- /*
- ==================
- idStr::DefaultFileExtension
- ==================
- */
- idStr &idStr::DefaultFileExtension( const char *extension ) {
- int i;
- // do nothing if the string already has an extension
- for ( i = len-1; i >= 0; i-- ) {
- if ( data[i] == '.' ) {
- return *this;
- }
- }
- if ( *extension != '.' ) {
- Append( '.' );
- }
- Append( extension );
- return *this;
- }
- /*
- ==================
- idStr::DefaultPath
- ==================
- */
- idStr &idStr::DefaultPath( const char *basepath ) {
- if ( ( ( *this )[ 0 ] == '/' ) || ( ( *this )[ 0 ] == '\\' ) ) {
- // absolute path location
- return *this;
- }
- *this = basepath + *this;
- return *this;
- }
- /*
- ====================
- idStr::AppendPath
- ====================
- */
- void idStr::AppendPath( const char *text ) {
- int pos;
- int i = 0;
- if ( text && text[i] ) {
- pos = len;
- EnsureAlloced( len + strlen( text ) + 2 );
- if ( pos ) {
- if ( data[ pos-1 ] != '/' ) {
- data[ pos++ ] = '/';
- }
- }
- if ( text[i] == '/' ) {
- i++;
- }
- for ( ; text[ i ]; i++ ) {
- if ( text[ i ] == '\\' ) {
- data[ pos++ ] = '/';
- } else {
- data[ pos++ ] = text[ i ];
- }
- }
- len = pos;
- data[ pos ] = '\0';
- }
- }
- /*
- ==================
- idStr::StripFilename
- ==================
- */
- idStr &idStr::StripFilename() {
- int pos;
- pos = Length() - 1;
- while( ( pos > 0 ) && ( ( *this )[ pos ] != '/' ) && ( ( *this )[ pos ] != '\\' ) ) {
- pos--;
- }
- if ( pos < 0 ) {
- pos = 0;
- }
- CapLength( pos );
- return *this;
- }
- /*
- ==================
- idStr::StripPath
- ==================
- */
- idStr &idStr::StripPath() {
- int pos;
- pos = Length();
- while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
- pos--;
- }
- *this = Right( Length() - pos );
- return *this;
- }
- /*
- ====================
- idStr::ExtractFilePath
- ====================
- */
- void idStr::ExtractFilePath( idStr &dest ) const {
- int pos;
- //
- // back up until a \ or the start
- //
- pos = Length();
- while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
- pos--;
- }
- Left( pos, dest );
- }
- /*
- ====================
- idStr::ExtractFileName
- ====================
- */
- void idStr::ExtractFileName( idStr &dest ) const {
- int pos;
- //
- // back up until a \ or the start
- //
- pos = Length() - 1;
- while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
- pos--;
- }
- Right( Length() - pos, dest );
- }
- /*
- ====================
- idStr::ExtractFileBase
- ====================
- */
- void idStr::ExtractFileBase( idStr &dest ) const {
- int pos;
- int start;
- //
- // back up until a \ or the start
- //
- pos = Length() - 1;
- while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '/' ) && ( ( *this )[ pos - 1 ] != '\\' ) ) {
- pos--;
- }
- start = pos;
- while( ( pos < Length() ) && ( ( *this )[ pos ] != '.' ) ) {
- pos++;
- }
- Mid( start, pos - start, dest );
- }
- /*
- ====================
- idStr::ExtractFileExtension
- ====================
- */
- void idStr::ExtractFileExtension( idStr &dest ) const {
- int pos;
- //
- // back up until a . or the start
- //
- pos = Length() - 1;
- while( ( pos > 0 ) && ( ( *this )[ pos - 1 ] != '.' ) ) {
- pos--;
- }
- if ( !pos ) {
- // no extension
- dest.Empty();
- } else {
- Right( Length() - pos, dest );
- }
- }
- /*
- =====================================================================
- char * methods to replace library functions
- =====================================================================
- */
- /*
- ============
- idStr::IsNumeric
- Checks a string to see if it contains only numerical values.
- ============
- */
- bool idStr::IsNumeric( const char *s ) {
- int i;
- bool dot;
- if ( *s == '-' ) {
- s++;
- }
- dot = false;
- for ( i = 0; s[i]; i++ ) {
- if ( !isdigit( (const unsigned char)s[i] ) ) {
- if ( ( s[ i ] == '.' ) && !dot ) {
- dot = true;
- continue;
- }
- return false;
- }
- }
- return true;
- }
- /*
- ============
- idStr::HasLower
- Checks if a string has any lowercase chars
- ============
- */
- bool idStr::HasLower( const char *s ) {
- if ( !s ) {
- return false;
- }
-
- while ( *s ) {
- if ( CharIsLower( *s ) ) {
- return true;
- }
- s++;
- }
-
- return false;
- }
- /*
- ============
- idStr::HasUpper
-
- Checks if a string has any uppercase chars
- ============
- */
- bool idStr::HasUpper( const char *s ) {
- if ( !s ) {
- return false;
- }
-
- while ( *s ) {
- if ( CharIsUpper( *s ) ) {
- return true;
- }
- s++;
- }
-
- return false;
- }
- /*
- ================
- idStr::Cmp
- ================
- */
- int idStr::Cmp( const char *s1, const char *s2 ) {
- int c1, c2, d;
- do {
- c1 = *s1++;
- c2 = *s2++;
- d = c1 - c2;
- if ( d ) {
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0; // strings are equal
- }
- /*
- ================
- idStr::Cmpn
- ================
- */
- int idStr::Cmpn( const char *s1, const char *s2, int n ) {
- int c1, c2, d;
- assert( n >= 0 );
- do {
- c1 = *s1++;
- c2 = *s2++;
- if ( !n-- ) {
- return 0; // strings are equal until end point
- }
- d = c1 - c2;
- if ( d ) {
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0; // strings are equal
- }
- /*
- ================
- idStr::Icmp
- ================
- */
- int idStr::Icmp( const char *s1, const char *s2 ) {
- int c1, c2, d;
- do {
- c1 = *s1++;
- c2 = *s2++;
- d = c1 - c2;
- while( d ) {
- if ( c1 <= 'Z' && c1 >= 'A' ) {
- d += ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c2 <= 'Z' && c2 >= 'A' ) {
- d -= ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0; // strings are equal
- }
- /*
- ================
- idStr::Icmpn
- ================
- */
- int idStr::Icmpn( const char *s1, const char *s2, int n ) {
- int c1, c2, d;
- assert( n >= 0 );
- do {
- c1 = *s1++;
- c2 = *s2++;
- if ( !n-- ) {
- return 0; // strings are equal until end point
- }
- d = c1 - c2;
- while( d ) {
- if ( c1 <= 'Z' && c1 >= 'A' ) {
- d += ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c2 <= 'Z' && c2 >= 'A' ) {
- d -= ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0; // strings are equal
- }
- /*
- ================
- idStr::Icmp
- ================
- */
- int idStr::IcmpNoColor( const char *s1, const char *s2 ) {
- int c1, c2, d;
- do {
- while ( idStr::IsColor( s1 ) ) {
- s1 += 2;
- }
- while ( idStr::IsColor( s2 ) ) {
- s2 += 2;
- }
- c1 = *s1++;
- c2 = *s2++;
- d = c1 - c2;
- while( d ) {
- if ( c1 <= 'Z' && c1 >= 'A' ) {
- d += ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c2 <= 'Z' && c2 >= 'A' ) {
- d -= ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0; // strings are equal
- }
- /*
- ================
- idStr::IcmpPath
- ================
- */
- int idStr::IcmpPath( const char *s1, const char *s2 ) {
- int c1, c2, d;
- #if 0
- //#if !defined( ID_PC_WIN )
- idLib::common->Printf( "WARNING: IcmpPath used on a case-sensitive filesystem?\n" );
- #endif
- do {
- c1 = *s1++;
- c2 = *s2++;
- d = c1 - c2;
- while( d ) {
- if ( c1 <= 'Z' && c1 >= 'A' ) {
- d += ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c1 == '\\' ) {
- d += ('/' - '\\');
- if ( !d ) {
- break;
- }
- }
- if ( c2 <= 'Z' && c2 >= 'A' ) {
- d -= ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c2 == '\\' ) {
- d -= ('/' - '\\');
- if ( !d ) {
- break;
- }
- }
- // make sure folders come first
- while( c1 ) {
- if ( c1 == '/' || c1 == '\\' ) {
- break;
- }
- c1 = *s1++;
- }
- while( c2 ) {
- if ( c2 == '/' || c2 == '\\' ) {
- break;
- }
- c2 = *s2++;
- }
- if ( c1 && !c2 ) {
- return -1;
- } else if ( !c1 && c2 ) {
- return 1;
- }
- // same folder depth so use the regular compare
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0;
- }
- /*
- ================
- idStr::IcmpnPath
- ================
- */
- int idStr::IcmpnPath( const char *s1, const char *s2, int n ) {
- int c1, c2, d;
- #if 0
- //#if !defined( ID_PC_WIN )
- idLib::common->Printf( "WARNING: IcmpPath used on a case-sensitive filesystem?\n" );
- #endif
- assert( n >= 0 );
- do {
- c1 = *s1++;
- c2 = *s2++;
- if ( !n-- ) {
- return 0; // strings are equal until end point
- }
- d = c1 - c2;
- while( d ) {
- if ( c1 <= 'Z' && c1 >= 'A' ) {
- d += ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c1 == '\\' ) {
- d += ('/' - '\\');
- if ( !d ) {
- break;
- }
- }
- if ( c2 <= 'Z' && c2 >= 'A' ) {
- d -= ('a' - 'A');
- if ( !d ) {
- break;
- }
- }
- if ( c2 == '\\' ) {
- d -= ('/' - '\\');
- if ( !d ) {
- break;
- }
- }
- // make sure folders come first
- while( c1 ) {
- if ( c1 == '/' || c1 == '\\' ) {
- break;
- }
- c1 = *s1++;
- }
- while( c2 ) {
- if ( c2 == '/' || c2 == '\\' ) {
- break;
- }
- c2 = *s2++;
- }
- if ( c1 && !c2 ) {
- return -1;
- } else if ( !c1 && c2 ) {
- return 1;
- }
- // same folder depth so use the regular compare
- return ( INT32_SIGNBITNOTSET( d ) << 1 ) - 1;
- }
- } while( c1 );
- return 0;
- }
- /*
- =============
- idStr::Copynz
-
- Safe strncpy that ensures a trailing zero
- =============
- */
- void idStr::Copynz( char *dest, const char *src, int destsize ) {
- if ( !src ) {
- idLib::common->Warning( "idStr::Copynz: NULL src" );
- return;
- }
- if ( destsize < 1 ) {
- idLib::common->Warning( "idStr::Copynz: destsize < 1" );
- return;
- }
- strncpy( dest, src, destsize-1 );
- dest[destsize-1] = 0;
- }
- /*
- ================
- idStr::Append
- never goes past bounds or leaves without a terminating 0
- ================
- */
- void idStr::Append( char *dest, int size, const char *src ) {
- int l1;
- l1 = strlen( dest );
- if ( l1 >= size ) {
- idLib::common->Error( "idStr::Append: already overflowed" );
- }
- idStr::Copynz( dest + l1, src, size - l1 );
- }
- /*
- ========================
- idStr::IsValidUTF8
- ========================
- */
- bool idStr::IsValidUTF8( const uint8 * s, const int maxLen, utf8Encoding_t & encoding ) {
- struct local_t {
- static int GetNumEncodedUTF8Bytes( const uint8 c ) {
- if ( c < 0x80 ) {
- return 1;
- } else if ( ( c >> 5 ) == 0x06 ) {
- // 2 byte encoding - the next byte must begin with
- return 2;
- } else if ( ( c >> 4 ) == 0x0E ) {
- // 3 byte encoding
- return 3;
- } else if ( ( c >> 5 ) == 0x1E ) {
- // 4 byte encoding
- return 4;
- }
- // this isnt' a valid UTF-8 precursor character
- return 0;
- }
- static bool RemainingCharsAreUTF8FollowingBytes( const uint8 * s, const int curChar, const int maxLen, const int num ) {
- if ( maxLen - curChar < num ) {
- return false;
- }
- for ( int i = curChar + 1; i <= curChar + num; i++ ) {
- if ( s[ i ] == '\0' ) {
- return false;
- }
- if ( ( s[ i ] >> 6 ) != 0x02 ) {
- return false;
- }
- }
- return true;
- }
- };
- // check for byte-order-marker
- encoding = UTF8_PURE_ASCII;
- utf8Encoding_t utf8Type = UTF8_ENCODED_NO_BOM;
- if ( maxLen > 3 && s[ 0 ] == 0xEF && s[ 1 ] == 0xBB && s[ 2 ] == 0xBF ) {
- utf8Type = UTF8_ENCODED_BOM;
- }
- for ( int i = 0; s[ i ] != '\0' && i < maxLen; i++ ) {
- int numBytes = local_t::GetNumEncodedUTF8Bytes( s[ i ] );
- if ( numBytes == 1 ) {
- continue; // just low ASCII
- } else if ( numBytes == 2 ) {
- // 2 byte encoding - the next byte must begin with bit pattern 10
- if ( !local_t::RemainingCharsAreUTF8FollowingBytes( s, i, maxLen, 1 ) ) {
- return false;
- }
- // skip over UTF-8 character
- i += 1;
- encoding = utf8Type;
- } else if ( numBytes == 3 ) {
- // 3 byte encoding - the next 2 bytes must begin with bit pattern 10
- if ( !local_t::RemainingCharsAreUTF8FollowingBytes( s, i, maxLen, 2 ) ) {
- return false;
- }
- // skip over UTF-8 character
- i += 2;
- encoding = utf8Type;
- } else if ( numBytes == 4 ) {
- // 4 byte encoding - the next 3 bytes must begin with bit pattern 10
- if ( !local_t::RemainingCharsAreUTF8FollowingBytes( s, i, maxLen, 3 ) ) {
- return false;
- }
- // skip over UTF-8 character
- i += 3;
- encoding = utf8Type;
- } else {
- // this isnt' a valid UTF-8 character
- if ( utf8Type == UTF8_ENCODED_BOM ) {
- encoding = UTF8_INVALID_BOM;
- } else {
- encoding = UTF8_INVALID;
- }
- return false;
- }
- }
- return true;
- }
- /*
- ========================
- idStr::UTF8Length
- ========================
- */
- int idStr::UTF8Length( const byte * s ) {
- int mbLen = 0;
- int charLen = 0;
- while ( s[ mbLen ] != '\0' ) {
- uint32 cindex;
- cindex = s[ mbLen ];
- if ( cindex < 0x80 ) {
- mbLen++;
- } else {
- int trailing = 0;
- if ( cindex >= 0xc0 ) {
- static const byte trailingBytes[ 64 ] = {
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
- };
- trailing = trailingBytes[ cindex - 0xc0 ];
- }
- mbLen += trailing + 1;
- }
- charLen++;
- }
- return charLen;
- }
- /*
- ========================
- idStr::AppendUTF8Char
- ========================
- */
- void idStr::AppendUTF8Char( uint32 c ) {
- if ( c < 0x80 ) {
- Append( ( char)c );
- } else if ( c < 0x800 ) { // 11 bits
- Append( (char)( 0xC0 | ( c >> 6 ) ) );
- Append( (char)( 0x80 | ( c & 0x3F ) ) );
- } else if ( c < 0x10000 ) { // 16 bits
- Append( (char)( 0xE0 | ( c >> 12 ) ) );
- Append( (char)( 0x80 | ( ( c >> 6 ) & 0x3F ) ) );
- Append( (char)( 0x80 | ( c & 0x3F ) ) );
- } else if ( c < 0x200000 ) { // 21 bits
- Append( (char)( 0xF0 | ( c >> 18 ) ) );
- Append( (char)( 0x80 | ( ( c >> 12 ) & 0x3F ) ) );
- Append( (char)( 0x80 | ( ( c >> 6 ) & 0x3F ) ) );
- Append( (char)( 0x80 | ( c & 0x3F ) ) );
- } else {
- // UTF-8 can encode up to 6 bytes. Why don't we support that?
- // This is an invalid Unicode character
- Append( '?' );
- }
- }
- /*
- ========================
- idStr::UTF8Char
- ========================
- */
- uint32 idStr::UTF8Char( const byte * s, int & idx ) {
- if ( idx >= 0 ) {
- while ( s[ idx ] != '\0' ) {
- uint32 cindex = s[ idx ];
- if ( cindex < 0x80 ) {
- idx++;
- return cindex;
- }
- int trailing = 0;
- if ( cindex >= 0xc0 ) {
- static const byte trailingBytes[ 64 ] = {
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
- };
- trailing = trailingBytes[ cindex - 0xc0 ];
- }
- static const uint32 trailingMask[ 6 ] = { 0x0000007f, 0x0000001f, 0x0000000f, 0x00000007, 0x00000003, 0x00000001 };
- cindex &= trailingMask[ trailing ];
- while ( trailing-- > 0 ) {
- cindex <<= 6;
- cindex += s[ ++idx ] & 0x0000003f;
- }
- idx++;
- return cindex;
- }
- }
- idx++;
- return 0; // return a null terminator if out of range
- }
- /*
- ================
- idStr::LengthWithoutColors
- ================
- */
- int idStr::LengthWithoutColors( const char *s ) {
- int len;
- const char *p;
- if ( !s ) {
- return 0;
- }
- len = 0;
- p = s;
- while( *p ) {
- if ( idStr::IsColor( p ) ) {
- p += 2;
- continue;
- }
- p++;
- len++;
- }
- return len;
- }
- /*
- ================
- idStr::RemoveColors
- ================
- */
- char *idStr::RemoveColors( char *string ) {
- char *d;
- char *s;
- int c;
- s = string;
- d = string;
- while( (c = *s) != 0 ) {
- if ( idStr::IsColor( s ) ) {
- s++;
- }
- else {
- *d++ = c;
- }
- s++;
- }
- *d = '\0';
- return string;
- }
- /*
- ================
- idStr::snPrintf
- ================
- */
- int idStr::snPrintf( char *dest, int size, const char *fmt, ...) {
- int len;
- va_list argptr;
- char buffer[32000]; // big, but small enough to fit in PPC stack
- va_start( argptr, fmt );
- len = vsprintf( buffer, fmt, argptr );
- va_end( argptr );
- if ( len >= sizeof( buffer ) ) {
- idLib::common->Error( "idStr::snPrintf: overflowed buffer" );
- }
- if ( len >= size ) {
- idLib::common->Warning( "idStr::snPrintf: overflow of %i in %i\n", len, size );
- len = size;
- }
- idStr::Copynz( dest, buffer, size );
- return len;
- }
- /*
- ============
- idStr::vsnPrintf
- vsnprintf portability:
- C99 standard: vsnprintf returns the number of characters (excluding the trailing
- '\0') which would have been written to the final string if enough space had been available
- snprintf and vsnprintf do not write more than size bytes (including the trailing '\0')
- win32: _vsnprintf returns the number of characters written, not including the terminating null character,
- or a negative value if an output error occurs. If the number of characters to write exceeds count, then count
- characters are written and -1 is returned and no trailing '\0' is added.
- idStr::vsnPrintf: always appends a trailing '\0', returns number of characters written (not including terminal \0)
- or returns -1 on failure or if the buffer would be overflowed.
- ============
- */
- int idStr::vsnPrintf( char *dest, int size, const char *fmt, va_list argptr ) {
- int ret;
- #undef _vsnprintf
- ret = _vsnprintf( dest, size-1, fmt, argptr );
- #define _vsnprintf use_idStr_vsnPrintf
- dest[size-1] = '\0';
- if ( ret < 0 || ret >= size ) {
- return -1;
- }
- return ret;
- }
- /*
- ============
- sprintf
- Sets the value of the string using a printf interface.
- ============
- */
- int sprintf( idStr &string, const char *fmt, ... ) {
- int l;
- va_list argptr;
- char buffer[32000];
-
- va_start( argptr, fmt );
- l = idStr::vsnPrintf( buffer, sizeof(buffer)-1, fmt, argptr );
- va_end( argptr );
- buffer[sizeof(buffer)-1] = '\0';
- string = buffer;
- return l;
- }
- /*
- ============
- vsprintf
- Sets the value of the string using a vprintf interface.
- ============
- */
- int vsprintf( idStr &string, const char *fmt, va_list argptr ) {
- int l;
- char buffer[32000];
-
- l = idStr::vsnPrintf( buffer, sizeof(buffer)-1, fmt, argptr );
- buffer[sizeof(buffer)-1] = '\0';
-
- string = buffer;
- return l;
- }
- /*
- ============
- va
- does a varargs printf into a temp buffer
- NOTE: not thread safe
- ============
- */
- char *va( const char *fmt, ... ) {
- va_list argptr;
- static int index = 0;
- static char string[4][16384]; // in case called by nested functions
- char *buf;
- buf = string[index];
- index = (index + 1) & 3;
- va_start( argptr, fmt );
- vsprintf( buf, fmt, argptr );
- va_end( argptr );
- return buf;
- }
- /*
- ============
- idStr::BestUnit
- ============
- */
- int idStr::BestUnit( const char *format, float value, Measure_t measure ) {
- int unit = 1;
- while ( unit <= 3 && ( 1 << ( unit * 10 ) < value ) ) {
- unit++;
- }
- unit--;
- value /= 1 << ( unit * 10 );
- sprintf( *this, format, value );
- *this += " ";
- *this += units[ measure ][ unit ];
- return unit;
- }
- /*
- ============
- idStr::SetUnit
- ============
- */
- void idStr::SetUnit( const char *format, float value, int unit, Measure_t measure ) {
- value /= 1 << ( unit * 10 );
- sprintf( *this, format, value );
- *this += " ";
- *this += units[ measure ][ unit ];
- }
- /*
- ================
- idStr::InitMemory
- ================
- */
- void idStr::InitMemory() {
- #ifdef USE_STRING_DATA_ALLOCATOR
- stringDataAllocator.Init();
- #endif
- }
- /*
- ================
- idStr::ShutdownMemory
- ================
- */
- void idStr::ShutdownMemory() {
- #ifdef USE_STRING_DATA_ALLOCATOR
- stringDataAllocator.Shutdown();
- #endif
- }
- /*
- ================
- idStr::PurgeMemory
- ================
- */
- void idStr::PurgeMemory() {
- #ifdef USE_STRING_DATA_ALLOCATOR
- stringDataAllocator.FreeEmptyBaseBlocks();
- #endif
- }
- /*
- ================
- idStr::ShowMemoryUsage_f
- ================
- */
- void idStr::ShowMemoryUsage_f( const idCmdArgs &args ) {
- #ifdef USE_STRING_DATA_ALLOCATOR
- idLib::common->Printf( "%6d KB string memory (%d KB free in %d blocks, %d empty base blocks)\n",
- stringDataAllocator.GetBaseBlockMemory() >> 10, stringDataAllocator.GetFreeBlockMemory() >> 10,
- stringDataAllocator.GetNumFreeBlocks(), stringDataAllocator.GetNumEmptyBaseBlocks() );
- #endif
- }
- /*
- ================
- idStr::FormatNumber
- ================
- */
- struct formatList_t {
- int gran;
- int count;
- };
- // elements of list need to decend in size
- formatList_t formatList[] = {
- { 1000000000, 0 },
- { 1000000, 0 },
- { 1000, 0 }
- };
- int numFormatList = sizeof(formatList) / sizeof( formatList[0] );
- idStr idStr::FormatNumber( int number ) {
- idStr string;
- bool hit;
- // reset
- for ( int i = 0; i < numFormatList; i++ ) {
- formatList_t *li = formatList + i;
- li->count = 0;
- }
- // main loop
- do {
- hit = false;
- for ( int i = 0; i < numFormatList; i++ ) {
- formatList_t *li = formatList + i;
- if ( number >= li->gran ) {
- li->count++;
- number -= li->gran;
- hit = true;
- break;
- }
- }
- } while ( hit );
- // print out
- bool found = false;
- for ( int i = 0; i < numFormatList; i++ ) {
- formatList_t *li = formatList + i;
- if ( li->count ) {
- if ( !found ) {
- string += va( "%i,", li->count );
- } else {
- string += va( "%3.3i,", li->count );
- }
- found = true;
- }
- else if ( found ) {
- string += va( "%3.3i,", li->count );
- }
- }
- if ( found ) {
- string += va( "%3.3i", number );
- }
- else {
- string += va( "%i", number );
- }
- // pad to proper size
- int count = 11 - string.Length();
- for ( int i = 0; i < count; i++ ) {
- string.Insert( " ", 0 );
- }
- return string;
- }
- CONSOLE_COMMAND( testStrId, "prints a localized string", 0 ) {
- if ( args.Argc() != 2 ) {
- idLib::Printf( "need a str id like 'STR_SWF_ACCEPT' without the hash, it gets parsed as a separate argument\n" );
- return;
- }
- idStrId str( va( "#%s", args.Argv( 1 ) ) );
- idLib::Printf( "%s = %s\n", args.Argv( 1 ), str.GetLocalizedString() );
- }
|