doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Str.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 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 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __STR_H__
30 #define __STR_H__
31 
32 /*
33 ===============================================================================
34 
35  Character string
36 
37 ===============================================================================
38 */
39 
40 // these library functions should not be used for cross platform compatibility
41 #define strcmp idStr::Cmp // use_idStr_Cmp
42 #define strncmp use_idStr_Cmpn
43 
44 #if defined( StrCmpN )
45 #undef StrCmpN
46 #endif
47 #define StrCmpN use_idStr_Cmpn
48 
49 #if defined( strcmpi )
50 #undef strcmpi
51 #endif
52 #define strcmpi use_idStr_Icmp
53 
54 #if defined( StrCmpI )
55 #undef StrCmpI
56 #endif
57 #define StrCmpI use_idStr_Icmp
58 
59 #if defined( StrCmpNI )
60 #undef StrCmpNI
61 #endif
62 #define StrCmpNI use_idStr_Icmpn
63 
64 #define stricmp idStr::Icmp // use_idStr_Icmp
65 #define _stricmp use_idStr_Icmp
66 #define strcasecmp use_idStr_Icmp
67 #define strnicmp use_idStr_Icmpn
68 #define _strnicmp use_idStr_Icmpn
69 #define _memicmp use_idStr_Icmpn
70 #define snprintf use_idStr_snPrintf
71 #define _snprintf use_idStr_snPrintf
72 #define vsnprintf use_idStr_vsnPrintf
73 #define _vsnprintf use_idStr_vsnPrintf
74 
75 class idVec4;
76 
77 #ifndef FILE_HASH_SIZE
78 #define FILE_HASH_SIZE 1024
79 #endif
80 
81 // color escape character
82 const int C_COLOR_ESCAPE = '^';
83 const int C_COLOR_DEFAULT = '0';
84 const int C_COLOR_RED = '1';
85 const int C_COLOR_GREEN = '2';
86 const int C_COLOR_YELLOW = '3';
87 const int C_COLOR_BLUE = '4';
88 const int C_COLOR_CYAN = '5';
89 const int C_COLOR_MAGENTA = '6';
90 const int C_COLOR_WHITE = '7';
91 const int C_COLOR_GRAY = '8';
92 const int C_COLOR_BLACK = '9';
93 
94 // color escape string
95 #define S_COLOR_DEFAULT "^0"
96 #define S_COLOR_RED "^1"
97 #define S_COLOR_GREEN "^2"
98 #define S_COLOR_YELLOW "^3"
99 #define S_COLOR_BLUE "^4"
100 #define S_COLOR_CYAN "^5"
101 #define S_COLOR_MAGENTA "^6"
102 #define S_COLOR_WHITE "^7"
103 #define S_COLOR_GRAY "^8"
104 #define S_COLOR_BLACK "^9"
105 
106 // make idStr a multiple of 16 bytes long
107 // don't make too large to keep memory requirements to a minimum
108 const int STR_ALLOC_BASE = 20;
109 const int STR_ALLOC_GRAN = 32;
110 
111 typedef enum {
114 } Measure_t;
115 
116 class idStr {
117 
118 public:
119  idStr( void );
120  idStr( const idStr &text );
121  idStr( const idStr &text, int start, int end );
122  idStr( const char *text );
123  idStr( const char *text, int start, int end );
124  explicit idStr( const bool b );
125  explicit idStr( const char c );
126  explicit idStr( const int i );
127  explicit idStr( const unsigned u );
128  explicit idStr( const float f );
129  ~idStr( void );
130 
131  size_t Size( void ) const;
132  const char * c_str( void ) const;
133  operator const char *( void ) const;
134  operator const char *( void );
135 
136  char operator[]( int index ) const;
137  char & operator[]( int index );
138 
139  void operator=( const idStr &text );
140  void operator=( const char *text );
141 
142  friend idStr operator+( const idStr &a, const idStr &b );
143  friend idStr operator+( const idStr &a, const char *b );
144  friend idStr operator+( const char *a, const idStr &b );
145 
146  friend idStr operator+( const idStr &a, const float b );
147  friend idStr operator+( const idStr &a, const int b );
148  friend idStr operator+( const idStr &a, const unsigned b );
149  friend idStr operator+( const idStr &a, const bool b );
150  friend idStr operator+( const idStr &a, const char b );
151 
152  idStr & operator+=( const idStr &a );
153  idStr & operator+=( const char *a );
154  idStr & operator+=( const float a );
155  idStr & operator+=( const char a );
156  idStr & operator+=( const int a );
157  idStr & operator+=( const unsigned a );
158  idStr & operator+=( const bool a );
159 
160  // case sensitive compare
161  friend bool operator==( const idStr &a, const idStr &b );
162  friend bool operator==( const idStr &a, const char *b );
163  friend bool operator==( const char *a, const idStr &b );
164 
165  // case sensitive compare
166  friend bool operator!=( const idStr &a, const idStr &b );
167  friend bool operator!=( const idStr &a, const char *b );
168  friend bool operator!=( const char *a, const idStr &b );
169 
170  // case sensitive compare
171  int Cmp( const char *text ) const;
172  int Cmpn( const char *text, int n ) const;
173  int CmpPrefix( const char *text ) const;
174 
175  // case insensitive compare
176  int Icmp( const char *text ) const;
177  int Icmpn( const char *text, int n ) const;
178  int IcmpPrefix( const char *text ) const;
179 
180  // case insensitive compare ignoring color
181  int IcmpNoColor( const char *text ) const;
182 
183  // compares paths and makes sure folders come first
184  int IcmpPath( const char *text ) const;
185  int IcmpnPath( const char *text, int n ) const;
186  int IcmpPrefixPath( const char *text ) const;
187 
188  int Length( void ) const;
189  int Allocated( void ) const;
190  void Empty( void );
191  bool IsEmpty( void ) const;
192  void Clear( void );
193  void Append( const char a );
194  void Append( const idStr &text );
195  void Append( const char *text );
196  void Append( const char *text, int len );
197  void Insert( const char a, int index );
198  void Insert( const char *text, int index );
199  void ToLower( void );
200  void ToUpper( void );
201  bool IsNumeric( void ) const;
202  bool IsColor( void ) const;
203  bool HasLower( void ) const;
204  bool HasUpper( void ) const;
205  int LengthWithoutColors( void ) const;
206  idStr & RemoveColors( void );
207  void CapLength( int );
208  void Fill( const char ch, int newlen );
209 
210  int Find( const char c, int start = 0, int end = -1 ) const;
211  int Find( const char *text, bool casesensitive = true, int start = 0, int end = -1 ) const;
212  bool Filter( const char *filter, bool casesensitive ) const;
213  int Last( const char c ) const; // return the index to the last occurance of 'c', returns -1 if not found
214  const char * Left( int len, idStr &result ) const; // store the leftmost 'len' characters in the result
215  const char * Right( int len, idStr &result ) const; // store the rightmost 'len' characters in the result
216  const char * Mid( int start, int len, idStr &result ) const; // store 'len' characters starting at 'start' in result
217  idStr Left( int len ) const; // return the leftmost 'len' characters
218  idStr Right( int len ) const; // return the rightmost 'len' characters
219  idStr Mid( int start, int len ) const; // return 'len' characters starting at 'start'
220  void StripLeading( const char c ); // strip char from front as many times as the char occurs
221  void StripLeading( const char *string ); // strip string from front as many times as the string occurs
222  bool StripLeadingOnce( const char *string ); // strip string from front just once if it occurs
223  void StripTrailing( const char c ); // strip char from end as many times as the char occurs
224  void StripTrailing( const char *string ); // strip string from end as many times as the string occurs
225  bool StripTrailingOnce( const char *string ); // strip string from end just once if it occurs
226  void Strip( const char c ); // strip char from front and end as many times as the char occurs
227  void Strip( const char *string ); // strip string from front and end as many times as the string occurs
228  void StripTrailingWhitespace( void ); // strip trailing white space characters
229  idStr & StripQuotes( void ); // strip quotes around string
230  void Replace( const char *old, const char *nw );
231 
232  // file name methods
233  int FileNameHash( void ) const; // hash key for the filename (skips extension)
234  idStr & BackSlashesToSlashes( void ); // convert slashes
235  idStr & SetFileExtension( const char *extension ); // set the given file extension
236  idStr & StripFileExtension( void ); // remove any file extension
237  idStr & StripAbsoluteFileExtension( void ); // remove any file extension looking from front (useful if there are multiple .'s)
238  idStr & DefaultFileExtension( const char *extension ); // if there's no file extension use the default
239  idStr & DefaultPath( const char *basepath ); // if there's no path use the default
240  void AppendPath( const char *text ); // append a partial path
241  idStr & StripFilename( void ); // remove the filename from a path
242  idStr & StripPath( void ); // remove the path from the filename
243  void ExtractFilePath( idStr &dest ) const; // copy the file path to another string
244  void ExtractFileName( idStr &dest ) const; // copy the filename to another string
245  void ExtractFileBase( idStr &dest ) const; // copy the filename minus the extension to another string
246  void ExtractFileExtension( idStr &dest ) const; // copy the file extension to another string
247  bool CheckExtension( const char *ext );
248 
249  // char * methods to replace library functions
250  static int Length( const char *s );
251  static char * ToLower( char *s );
252  static char * ToUpper( char *s );
253  static bool IsNumeric( const char *s );
254  static bool IsColor( const char *s );
255  static bool HasLower( const char *s );
256  static bool HasUpper( const char *s );
257  static int LengthWithoutColors( const char *s );
258  static char * RemoveColors( char *s );
259  static int Cmp( const char *s1, const char *s2 );
260  static int Cmpn( const char *s1, const char *s2, int n );
261  static int Icmp( const char *s1, const char *s2 );
262  static int Icmpn( const char *s1, const char *s2, int n );
263  static int IcmpNoColor( const char *s1, const char *s2 );
264  static int IcmpPath( const char *s1, const char *s2 ); // compares paths and makes sure folders come first
265  static int IcmpnPath( const char *s1, const char *s2, int n ); // compares paths and makes sure folders come first
266  static void Append( char *dest, int size, const char *src );
267  static void Copynz( char *dest, const char *src, int destsize );
268  static int snPrintf( char *dest, int size, const char *fmt, ... ) id_attribute((format(printf,3,4)));
269  static int vsnPrintf( char *dest, int size, const char *fmt, va_list argptr );
270  static int FindChar( const char *str, const char c, int start = 0, int end = -1 );
271  static int FindText( const char *str, const char *text, bool casesensitive = true, int start = 0, int end = -1 );
272  static bool Filter( const char *filter, const char *name, bool casesensitive );
273  static void StripMediaName( const char *name, idStr &mediaName );
274  static bool CheckExtension( const char *name, const char *ext );
275  static const char * FloatArrayToString( const float *array, const int length, const int precision );
276 
277  // hash keys
278  static int Hash( const char *string );
279  static int Hash( const char *string, int length );
280  static int IHash( const char *string ); // case insensitive
281  static int IHash( const char *string, int length ); // case insensitive
282 
283  // character methods
284  static char ToLower( char c );
285  static char ToUpper( char c );
286  static bool CharIsPrintable( int c );
287  static bool CharIsLower( int c );
288  static bool CharIsUpper( int c );
289  static bool CharIsAlpha( int c );
290  static bool CharIsNumeric( int c );
291  static bool CharIsNewLine( char c );
292  static bool CharIsTab( char c );
293  static int ColorIndex( int c );
294  static idVec4 & ColorForIndex( int i );
295 
296  friend int sprintf( idStr &dest, const char *fmt, ... );
297  friend int vsprintf( idStr &dest, const char *fmt, va_list ap );
298 
299  void ReAllocate( int amount, bool keepold ); // reallocate string data buffer
300  void FreeData( void ); // free allocated string memory
301 
302  // format value in the given measurement with the best unit, returns the best unit
303  int BestUnit( const char *format, float value, Measure_t measure );
304  // format value in the requested unit and measurement
305  void SetUnit( const char *format, float value, int unit, Measure_t measure );
306 
307  static void InitMemory( void );
308  static void ShutdownMemory( void );
309  static void PurgeMemory( void );
310  static void ShowMemoryUsage_f( const idCmdArgs &args );
311 
312  int DynamicMemoryUsed() const;
313  static idStr FormatNumber( int number );
314 
315 protected:
316  int len;
317  char * data;
318  int alloced;
320 
321  void Init( void ); // initialize string using base buffer
322  void EnsureAlloced( int amount, bool keepold = true ); // ensure string data buffer is large anough
323 };
324 
325 char * va( const char *fmt, ... ) id_attribute((format(printf,1,2)));
326 
327 
328 ID_INLINE void idStr::EnsureAlloced( int amount, bool keepold ) {
329  if ( amount > alloced ) {
330  ReAllocate( amount, keepold );
331  }
332 }
333 
334 ID_INLINE void idStr::Init( void ) {
335  len = 0;
337  data = baseBuffer;
338  data[ 0 ] = '\0';
339 #ifdef ID_DEBUG_UNINITIALIZED_MEMORY
340  memset( baseBuffer, 0, sizeof( baseBuffer ) );
341 #endif
342 }
343 
344 ID_INLINE idStr::idStr( void ) {
345  Init();
346 }
347 
348 ID_INLINE idStr::idStr( const idStr &text ) {
349  int l;
350 
351  Init();
352  l = text.Length();
353  EnsureAlloced( l + 1 );
354  strcpy( data, text.data );
355  len = l;
356 }
357 
358 ID_INLINE idStr::idStr( const idStr &text, int start, int end ) {
359  int i;
360  int l;
361 
362  Init();
363  if ( end > text.Length() ) {
364  end = text.Length();
365  }
366  if ( start > text.Length() ) {
367  start = text.Length();
368  } else if ( start < 0 ) {
369  start = 0;
370  }
371 
372  l = end - start;
373  if ( l < 0 ) {
374  l = 0;
375  }
376 
377  EnsureAlloced( l + 1 );
378 
379  for ( i = 0; i < l; i++ ) {
380  data[ i ] = text[ start + i ];
381  }
382 
383  data[ l ] = '\0';
384  len = l;
385 }
386 
387 ID_INLINE idStr::idStr( const char *text ) {
388  int l;
389 
390  Init();
391  if ( text ) {
392  l = strlen( text );
393  EnsureAlloced( l + 1 );
394  strcpy( data, text );
395  len = l;
396  }
397 }
398 
399 ID_INLINE idStr::idStr( const char *text, int start, int end ) {
400  int i;
401  int l = strlen( text );
402 
403  Init();
404  if ( end > l ) {
405  end = l;
406  }
407  if ( start > l ) {
408  start = l;
409  } else if ( start < 0 ) {
410  start = 0;
411  }
412 
413  l = end - start;
414  if ( l < 0 ) {
415  l = 0;
416  }
417 
418  EnsureAlloced( l + 1 );
419 
420  for ( i = 0; i < l; i++ ) {
421  data[ i ] = text[ start + i ];
422  }
423 
424  data[ l ] = '\0';
425  len = l;
426 }
427 
428 ID_INLINE idStr::idStr( const bool b ) {
429  Init();
430  EnsureAlloced( 2 );
431  data[ 0 ] = b ? '1' : '0';
432  data[ 1 ] = '\0';
433  len = 1;
434 }
435 
436 ID_INLINE idStr::idStr( const char c ) {
437  Init();
438  EnsureAlloced( 2 );
439  data[ 0 ] = c;
440  data[ 1 ] = '\0';
441  len = 1;
442 }
443 
444 ID_INLINE idStr::idStr( const int i ) {
445  char text[ 64 ];
446  int l;
447 
448  Init();
449  l = sprintf( text, "%d", i );
450  EnsureAlloced( l + 1 );
451  strcpy( data, text );
452  len = l;
453 }
454 
455 ID_INLINE idStr::idStr( const unsigned u ) {
456  char text[ 64 ];
457  int l;
458 
459  Init();
460  l = sprintf( text, "%u", u );
461  EnsureAlloced( l + 1 );
462  strcpy( data, text );
463  len = l;
464 }
465 
466 ID_INLINE idStr::idStr( const float f ) {
467  char text[ 64 ];
468  int l;
469 
470  Init();
471  l = idStr::snPrintf( text, sizeof( text ), "%f", f );
472  while( l > 0 && text[l-1] == '0' ) text[--l] = '\0';
473  while( l > 0 && text[l-1] == '.' ) text[--l] = '\0';
474  EnsureAlloced( l + 1 );
475  strcpy( data, text );
476  len = l;
477 }
478 
479 ID_INLINE idStr::~idStr( void ) {
480  FreeData();
481 }
482 
483 ID_INLINE size_t idStr::Size( void ) const {
484  return sizeof( *this ) + Allocated();
485 }
486 
487 ID_INLINE const char *idStr::c_str( void ) const {
488  return data;
489 }
490 
491 ID_INLINE idStr::operator const char *( void ) {
492  return c_str();
493 }
494 
495 ID_INLINE idStr::operator const char *( void ) const {
496  return c_str();
497 }
498 
499 ID_INLINE char idStr::operator[]( int index ) const {
500  assert( ( index >= 0 ) && ( index <= len ) );
501  return data[ index ];
502 }
503 
504 ID_INLINE char &idStr::operator[]( int index ) {
505  assert( ( index >= 0 ) && ( index <= len ) );
506  return data[ index ];
507 }
508 
509 ID_INLINE void idStr::operator=( const idStr &text ) {
510  int l;
511 
512  l = text.Length();
513  EnsureAlloced( l + 1, false );
514  memcpy( data, text.data, l );
515  data[l] = '\0';
516  len = l;
517 }
518 
519 ID_INLINE idStr operator+( const idStr &a, const idStr &b ) {
520  idStr result( a );
521  result.Append( b );
522  return result;
523 }
524 
525 ID_INLINE idStr operator+( const idStr &a, const char *b ) {
526  idStr result( a );
527  result.Append( b );
528  return result;
529 }
530 
531 ID_INLINE idStr operator+( const char *a, const idStr &b ) {
532  idStr result( a );
533  result.Append( b );
534  return result;
535 }
536 
537 ID_INLINE idStr operator+( const idStr &a, const bool b ) {
538  idStr result( a );
539  result.Append( b ? "true" : "false" );
540  return result;
541 }
542 
543 ID_INLINE idStr operator+( const idStr &a, const char b ) {
544  idStr result( a );
545  result.Append( b );
546  return result;
547 }
548 
549 ID_INLINE idStr operator+( const idStr &a, const float b ) {
550  char text[ 64 ];
551  idStr result( a );
552 
553  sprintf( text, "%f", b );
554  result.Append( text );
555 
556  return result;
557 }
558 
559 ID_INLINE idStr operator+( const idStr &a, const int b ) {
560  char text[ 64 ];
561  idStr result( a );
562 
563  sprintf( text, "%d", b );
564  result.Append( text );
565 
566  return result;
567 }
568 
569 ID_INLINE idStr operator+( const idStr &a, const unsigned b ) {
570  char text[ 64 ];
571  idStr result( a );
572 
573  sprintf( text, "%u", b );
574  result.Append( text );
575 
576  return result;
577 }
578 
579 ID_INLINE idStr &idStr::operator+=( const float a ) {
580  char text[ 64 ];
581 
582  sprintf( text, "%f", a );
583  Append( text );
584 
585  return *this;
586 }
587 
588 ID_INLINE idStr &idStr::operator+=( const int a ) {
589  char text[ 64 ];
590 
591  sprintf( text, "%d", a );
592  Append( text );
593 
594  return *this;
595 }
596 
597 ID_INLINE idStr &idStr::operator+=( const unsigned a ) {
598  char text[ 64 ];
599 
600  sprintf( text, "%u", a );
601  Append( text );
602 
603  return *this;
604 }
605 
606 ID_INLINE idStr &idStr::operator+=( const idStr &a ) {
607  Append( a );
608  return *this;
609 }
610 
611 ID_INLINE idStr &idStr::operator+=( const char *a ) {
612  Append( a );
613  return *this;
614 }
615 
616 ID_INLINE idStr &idStr::operator+=( const char a ) {
617  Append( a );
618  return *this;
619 }
620 
621 ID_INLINE idStr &idStr::operator+=( const bool a ) {
622  Append( a ? "true" : "false" );
623  return *this;
624 }
625 
626 ID_INLINE bool operator==( const idStr &a, const idStr &b ) {
627  return ( !idStr::Cmp( a.data, b.data ) );
628 }
629 
630 ID_INLINE bool operator==( const idStr &a, const char *b ) {
631  assert( b );
632  return ( !idStr::Cmp( a.data, b ) );
633 }
634 
635 ID_INLINE bool operator==( const char *a, const idStr &b ) {
636  assert( a );
637  return ( !idStr::Cmp( a, b.data ) );
638 }
639 
640 ID_INLINE bool operator!=( const idStr &a, const idStr &b ) {
641  return !( a == b );
642 }
643 
644 ID_INLINE bool operator!=( const idStr &a, const char *b ) {
645  return !( a == b );
646 }
647 
648 ID_INLINE bool operator!=( const char *a, const idStr &b ) {
649  return !( a == b );
650 }
651 
652 ID_INLINE int idStr::Cmp( const char *text ) const {
653  assert( text );
654  return idStr::Cmp( data, text );
655 }
656 
657 ID_INLINE int idStr::Cmpn( const char *text, int n ) const {
658  assert( text );
659  return idStr::Cmpn( data, text, n );
660 }
661 
662 ID_INLINE int idStr::CmpPrefix( const char *text ) const {
663  assert( text );
664  return idStr::Cmpn( data, text, strlen( text ) );
665 }
666 
667 ID_INLINE int idStr::Icmp( const char *text ) const {
668  assert( text );
669  return idStr::Icmp( data, text );
670 }
671 
672 ID_INLINE int idStr::Icmpn( const char *text, int n ) const {
673  assert( text );
674  return idStr::Icmpn( data, text, n );
675 }
676 
677 ID_INLINE int idStr::IcmpPrefix( const char *text ) const {
678  assert( text );
679  return idStr::Icmpn( data, text, strlen( text ) );
680 }
681 
682 ID_INLINE int idStr::IcmpNoColor( const char *text ) const {
683  assert( text );
684  return idStr::IcmpNoColor( data, text );
685 }
686 
687 ID_INLINE int idStr::IcmpPath( const char *text ) const {
688  assert( text );
689  return idStr::IcmpPath( data, text );
690 }
691 
692 ID_INLINE int idStr::IcmpnPath( const char *text, int n ) const {
693  assert( text );
694  return idStr::IcmpnPath( data, text, n );
695 }
696 
697 ID_INLINE int idStr::IcmpPrefixPath( const char *text ) const {
698  assert( text );
699  return idStr::IcmpnPath( data, text, strlen( text ) );
700 }
701 
702 ID_INLINE int idStr::Length( void ) const {
703  return len;
704 }
705 
706 ID_INLINE int idStr::Allocated( void ) const {
707  if ( data != baseBuffer ) {
708  return alloced;
709  } else {
710  return 0;
711  }
712 }
713 
714 ID_INLINE void idStr::Empty( void ) {
715  EnsureAlloced( 1 );
716  data[ 0 ] = '\0';
717  len = 0;
718 }
719 
720 ID_INLINE bool idStr::IsEmpty( void ) const {
721  return ( idStr::Cmp( data, "" ) == 0 );
722 }
723 
724 ID_INLINE void idStr::Clear( void ) {
725  FreeData();
726  Init();
727 }
728 
729 ID_INLINE void idStr::Append( const char a ) {
730  EnsureAlloced( len + 2 );
731  data[ len ] = a;
732  len++;
733  data[ len ] = '\0';
734 }
735 
736 ID_INLINE void idStr::Append( const idStr &text ) {
737  int newLen;
738  int i;
739 
740  newLen = len + text.Length();
741  EnsureAlloced( newLen + 1 );
742  for ( i = 0; i < text.len; i++ ) {
743  data[ len + i ] = text[ i ];
744  }
745  len = newLen;
746  data[ len ] = '\0';
747 }
748 
749 ID_INLINE void idStr::Append( const char *text ) {
750  int newLen;
751  int i;
752 
753  if ( text ) {
754  newLen = len + strlen( text );
755  EnsureAlloced( newLen + 1 );
756  for ( i = 0; text[ i ]; i++ ) {
757  data[ len + i ] = text[ i ];
758  }
759  len = newLen;
760  data[ len ] = '\0';
761  }
762 }
763 
764 ID_INLINE void idStr::Append( const char *text, int l ) {
765  int newLen;
766  int i;
767 
768  if ( text && l ) {
769  newLen = len + l;
770  EnsureAlloced( newLen + 1 );
771  for ( i = 0; text[ i ] && i < l; i++ ) {
772  data[ len + i ] = text[ i ];
773  }
774  len = newLen;
775  data[ len ] = '\0';
776  }
777 }
778 
779 ID_INLINE void idStr::Insert( const char a, int index ) {
780  int i, l;
781 
782  if ( index < 0 ) {
783  index = 0;
784  } else if ( index > len ) {
785  index = len;
786  }
787 
788  l = 1;
789  EnsureAlloced( len + l + 1 );
790  for ( i = len; i >= index; i-- ) {
791  data[i+l] = data[i];
792  }
793  data[index] = a;
794  len++;
795 }
796 
797 ID_INLINE void idStr::Insert( const char *text, int index ) {
798  int i, l;
799 
800  if ( index < 0 ) {
801  index = 0;
802  } else if ( index > len ) {
803  index = len;
804  }
805 
806  l = strlen( text );
807  EnsureAlloced( len + l + 1 );
808  for ( i = len; i >= index; i-- ) {
809  data[i+l] = data[i];
810  }
811  for ( i = 0; i < l; i++ ) {
812  data[index+i] = text[i];
813  }
814  len += l;
815 }
816 
817 ID_INLINE void idStr::ToLower( void ) {
818  for (int i = 0; data[i]; i++ ) {
819  if ( CharIsUpper( data[i] ) ) {
820  data[i] += ( 'a' - 'A' );
821  }
822  }
823 }
824 
825 ID_INLINE void idStr::ToUpper( void ) {
826  for (int i = 0; data[i]; i++ ) {
827  if ( CharIsLower( data[i] ) ) {
828  data[i] -= ( 'a' - 'A' );
829  }
830  }
831 }
832 
833 ID_INLINE bool idStr::IsNumeric( void ) const {
834  return idStr::IsNumeric( data );
835 }
836 
837 ID_INLINE bool idStr::IsColor( void ) const {
838  return idStr::IsColor( data );
839 }
840 
841 ID_INLINE bool idStr::HasLower( void ) const {
842  return idStr::HasLower( data );
843 }
844 
845 ID_INLINE bool idStr::HasUpper( void ) const {
846  return idStr::HasUpper( data );
847 }
848 
849 ID_INLINE idStr &idStr::RemoveColors( void ) {
851  len = Length( data );
852  return *this;
853 }
854 
855 ID_INLINE int idStr::LengthWithoutColors( void ) const {
857 }
858 
859 ID_INLINE void idStr::CapLength( int newlen ) {
860  if ( len <= newlen ) {
861  return;
862  }
863  data[ newlen ] = 0;
864  len = newlen;
865 }
866 
867 ID_INLINE void idStr::Fill( const char ch, int newlen ) {
868  EnsureAlloced( newlen + 1 );
869  len = newlen;
870  memset( data, ch, len );
871  data[ len ] = 0;
872 }
873 
874 ID_INLINE int idStr::Find( const char c, int start, int end ) const {
875  if ( end == -1 ) {
876  end = len;
877  }
878  return idStr::FindChar( data, c, start, end );
879 }
880 
881 ID_INLINE int idStr::Find( const char *text, bool casesensitive, int start, int end ) const {
882  if ( end == -1 ) {
883  end = len;
884  }
885  return idStr::FindText( data, text, casesensitive, start, end );
886 }
887 
888 ID_INLINE bool idStr::Filter( const char *filter, bool casesensitive ) const {
889  return idStr::Filter( filter, data, casesensitive );
890 }
891 
892 ID_INLINE const char *idStr::Left( int len, idStr &result ) const {
893  return Mid( 0, len, result );
894 }
895 
896 ID_INLINE const char *idStr::Right( int len, idStr &result ) const {
897  if ( len >= Length() ) {
898  result = *this;
899  return result;
900  }
901  return Mid( Length() - len, len, result );
902 }
903 
904 ID_INLINE idStr idStr::Left( int len ) const {
905  return Mid( 0, len );
906 }
907 
908 ID_INLINE idStr idStr::Right( int len ) const {
909  if ( len >= Length() ) {
910  return *this;
911  }
912  return Mid( Length() - len, len );
913 }
914 
915 ID_INLINE void idStr::Strip( const char c ) {
916  StripLeading( c );
917  StripTrailing( c );
918 }
919 
920 ID_INLINE void idStr::Strip( const char *string ) {
921  StripLeading( string );
922  StripTrailing( string );
923 }
924 
925 ID_INLINE bool idStr::CheckExtension( const char *ext ) {
926  return idStr::CheckExtension( data, ext );
927 }
928 
929 ID_INLINE int idStr::Length( const char *s ) {
930  int i;
931  for ( i = 0; s[i]; i++ ) {}
932  return i;
933 }
934 
935 ID_INLINE char *idStr::ToLower( char *s ) {
936  for ( int i = 0; s[i]; i++ ) {
937  if ( CharIsUpper( s[i] ) ) {
938  s[i] += ( 'a' - 'A' );
939  }
940  }
941  return s;
942 }
943 
944 ID_INLINE char *idStr::ToUpper( char *s ) {
945  for ( int i = 0; s[i]; i++ ) {
946  if ( CharIsLower( s[i] ) ) {
947  s[i] -= ( 'a' - 'A' );
948  }
949  }
950  return s;
951 }
952 
953 ID_INLINE int idStr::Hash( const char *string ) {
954  int i, hash = 0;
955  for ( i = 0; *string != '\0'; i++ ) {
956  hash += ( *string++ ) * ( i + 119 );
957  }
958  return hash;
959 }
960 
961 ID_INLINE int idStr::Hash( const char *string, int length ) {
962  int i, hash = 0;
963  for ( i = 0; i < length; i++ ) {
964  hash += ( *string++ ) * ( i + 119 );
965  }
966  return hash;
967 }
968 
969 ID_INLINE int idStr::IHash( const char *string ) {
970  int i, hash = 0;
971  for( i = 0; *string != '\0'; i++ ) {
972  hash += ToLower( *string++ ) * ( i + 119 );
973  }
974  return hash;
975 }
976 
977 ID_INLINE int idStr::IHash( const char *string, int length ) {
978  int i, hash = 0;
979  for ( i = 0; i < length; i++ ) {
980  hash += ToLower( *string++ ) * ( i + 119 );
981  }
982  return hash;
983 }
984 
985 ID_INLINE bool idStr::IsColor( const char *s ) {
986  return ( s[0] == C_COLOR_ESCAPE && s[1] != '\0' && s[1] != ' ' );
987 }
988 
989 ID_INLINE char idStr::ToLower( char c ) {
990  if ( c <= 'Z' && c >= 'A' ) {
991  return ( c + ( 'a' - 'A' ) );
992  }
993  return c;
994 }
995 
996 ID_INLINE char idStr::ToUpper( char c ) {
997  if ( c >= 'a' && c <= 'z' ) {
998  return ( c - ( 'a' - 'A' ) );
999  }
1000  return c;
1001 }
1002 
1003 ID_INLINE bool idStr::CharIsPrintable( int c ) {
1004  // test for regular ascii and western European high-ascii chars
1005  return ( c >= 0x20 && c <= 0x7E ) || ( c >= 0xA1 && c <= 0xFF );
1006 }
1007 
1008 ID_INLINE bool idStr::CharIsLower( int c ) {
1009  // test for regular ascii and western European high-ascii chars
1010  return ( c >= 'a' && c <= 'z' ) || ( c >= 0xE0 && c <= 0xFF );
1011 }
1012 
1013 ID_INLINE bool idStr::CharIsUpper( int c ) {
1014  // test for regular ascii and western European high-ascii chars
1015  return ( c <= 'Z' && c >= 'A' ) || ( c >= 0xC0 && c <= 0xDF );
1016 }
1017 
1018 ID_INLINE bool idStr::CharIsAlpha( int c ) {
1019  // test for regular ascii and western European high-ascii chars
1020  return ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ||
1021  ( c >= 0xC0 && c <= 0xFF ) );
1022 }
1023 
1024 ID_INLINE bool idStr::CharIsNumeric( int c ) {
1025  return ( c <= '9' && c >= '0' );
1026 }
1027 
1028 ID_INLINE bool idStr::CharIsNewLine( char c ) {
1029  return ( c == '\n' || c == '\r' || c == '\v' );
1030 }
1031 
1032 ID_INLINE bool idStr::CharIsTab( char c ) {
1033  return ( c == '\t' );
1034 }
1035 
1036 ID_INLINE int idStr::ColorIndex( int c ) {
1037  return ( c & 15 );
1038 }
1039 
1040 ID_INLINE int idStr::DynamicMemoryUsed() const {
1041  return ( data == baseBuffer ) ? 0 : alloced;
1042 }
1043 
1044 #endif /* !__STR_H__ */
idStr(void)
Definition: Str.h:344
static int snPrintf(char *dest, int size, const char *fmt,...) id_attribute((format(printf
Definition: Str.cpp:1465
GLsizei const GLfloat * value
Definition: glext.h:3614
char * data
Definition: Str.h:317
idStr & SetFileExtension(const char *extension)
Definition: Str.cpp:743
Measure_t
Definition: Str.h:111
static bool CharIsUpper(int c)
Definition: Str.h:1013
assert(prefInfo.fullscreenBtn)
const int C_COLOR_DEFAULT
Definition: Str.h:83
int Cmp(const char *text) const
Definition: Str.h:652
int CmpPrefix(const char *text) const
Definition: Str.h:662
void ToLower(void)
Definition: Str.h:817
void StripLeading(const char c)
Definition: Str.cpp:469
idStr & DefaultPath(const char *basepath)
Definition: Str.cpp:815
void ToUpper(void)
Definition: Str.h:825
GLenum GLsizei GLenum format
Definition: glext.h:2846
idStr & StripQuotes(void)
Definition: Str.cpp:665
char * va(const char *fmt,...) id_attribute((format(printf
bool Filter(const char *filter, bool casesensitive) const
Definition: Str.h:888
ID_INLINE idStr operator+(const idStr &a, const idStr &b)
Definition: Str.h:519
int Length(void) const
Definition: Str.h:702
bool StripTrailingOnce(const char *string)
Definition: Str.cpp:546
void StripTrailingWhitespace(void)
Definition: Str.cpp:648
GLenum GLsizei n
Definition: glext.h:3705
bool IsEmpty(void) const
Definition: Str.h:720
void StripTrailing(const char c)
Definition: Str.cpp:515
void Strip(const char c)
Definition: Str.h:915
static int ColorIndex(int c)
Definition: Str.h:1036
friend bool operator!=(const idStr &a, const idStr &b)
Definition: Str.h:640
void ExtractFileBase(idStr &dest) const
Definition: Str.cpp:940
static const char * FloatArrayToString(const float *array, const int length, const int precision)
Definition: Str.cpp:418
static void StripMediaName(const char *name, idStr &mediaName)
Definition: Str.cpp:359
const char * Left(int len, idStr &result) const
Definition: Str.h:892
static void ShowMemoryUsage_f(const idCmdArgs &args)
Definition: Str.cpp:1654
GLdouble s
Definition: glext.h:2935
GLuint src
Definition: glext.h:5390
idStr & DefaultFileExtension(const char *extension)
Definition: Str.cpp:794
GLenum GLsizei len
Definition: glext.h:3472
int i
Definition: process.py:33
int Cmpn(const char *text, int n) const
Definition: Str.h:657
Boolean result
int IcmpNoColor(const char *text) const
Definition: Str.h:682
ID_INLINE bool operator==(const idStr &a, const idStr &b)
Definition: Str.h:626
static idVec4 & ColorForIndex(int i)
Definition: Str.cpp:71
const int C_COLOR_BLUE
Definition: Str.h:87
friend int sprintf(idStr &dest, const char *fmt,...)
Definition: Str.cpp:1528
int Icmp(const char *text) const
Definition: Str.h:667
int BestUnit(const char *format, float value, Measure_t measure)
Definition: Str.cpp:1591
idStr & BackSlashesToSlashes(void)
Definition: Str.cpp:727
list l
Definition: prepare.py:17
bool HasLower(void) const
Definition: Str.h:841
char operator[](int index) const
Definition: Str.h:499
void ExtractFilePath(idStr &dest) const
Definition: Str.cpp:902
static bool CharIsNumeric(int c)
Definition: Str.h:1024
static bool CharIsLower(int c)
Definition: Str.h:1008
idStr & StripPath(void)
Definition: Str.cpp:885
const int C_COLOR_GRAY
Definition: Str.h:91
void ReAllocate(int amount, bool keepold)
Definition: Str.cpp:80
int Icmpn(const char *text, int n) const
Definition: Str.h:672
const int C_COLOR_WHITE
Definition: Str.h:90
bool CheckExtension(const char *ext)
Definition: Str.h:925
size_t Size(void) const
Definition: Str.h:483
void ExtractFileName(idStr &dest) const
Definition: Str.cpp:921
const int C_COLOR_RED
Definition: Str.h:84
GLuint index
Definition: glext.h:3476
const GLubyte * c
Definition: glext.h:4677
Definition: Vector.h:808
idStr & StripFileExtension(void)
Definition: Str.cpp:757
static idStr FormatNumber(int number)
Definition: Str.cpp:1682
void Empty(void)
Definition: Str.h:714
GLuint GLuint end
Definition: glext.h:2845
static void InitMemory(void)
Definition: Str.cpp:1621
idStr & operator+=(const idStr &a)
Definition: Str.h:606
void EnsureAlloced(int amount, bool keepold=true)
Definition: Str.h:328
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
void CapLength(int)
Definition: Str.h:859
~idStr(void)
Definition: Str.h:479
static bool CharIsNewLine(char c)
Definition: Str.h:1028
idStr & StripAbsoluteFileExtension(void)
Definition: Str.cpp:775
static int FindText(const char *str, const char *text, bool casesensitive=true, int start=0, int end=-1)
Definition: Str.cpp:207
static void Copynz(char *dest, const char *src, int destsize)
Definition: Str.cpp:1376
const int STR_ALLOC_GRAN
Definition: Str.h:109
const int C_COLOR_YELLOW
Definition: Str.h:86
const int C_COLOR_MAGENTA
Definition: Str.h:89
void Insert(const char a, int index)
Definition: Str.h:779
void Clear(void)
Definition: Str.h:724
const char * Right(int len, idStr &result) const
Definition: Str.h:896
int Find(const char c, int start=0, int end=-1) const
Definition: Str.h:874
const int C_COLOR_CYAN
Definition: Str.h:88
idStr & RemoveColors(void)
Definition: Str.h:849
static int Hash(const char *string)
Definition: Str.h:953
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
void AppendPath(const char *text)
Definition: Str.cpp:830
friend int vsprintf(idStr &dest, const char *fmt, va_list ap)
Definition: Str.cpp:1549
int FileNameHash(void) const
Definition: Str.cpp:700
static int IHash(const char *string)
Definition: Str.h:969
GLubyte GLubyte b
Definition: glext.h:4662
int IcmpnPath(const char *text, int n) const
Definition: Str.h:692
bool IsNumeric(void) const
Definition: Str.h:833
void Init(void)
Definition: Str.h:334
static int FindChar(const char *str, const char c, int start=0, int end=-1)
Definition: Str.cpp:186
void operator=(const idStr &text)
Definition: Str.h:509
static void ShutdownMemory(void)
Definition: Str.cpp:1632
const char * Mid(int start, int len, idStr &result) const
Definition: Str.cpp:603
int len
Definition: Str.h:316
#define id_attribute(x)
Definition: sys_public.h:139
int alloced
Definition: Str.h:318
static int static int vsnPrintf(char *dest, int size, const char *fmt, va_list argptr)
Definition: Str.cpp:1502
friend idStr operator+(const idStr &a, const idStr &b)
Definition: Str.h:519
static bool CharIsAlpha(int c)
Definition: Str.h:1018
GLenum filter
Definition: glext.h:3704
tuple f
Definition: idal.py:89
const int C_COLOR_BLACK
Definition: Str.h:92
void Append(const char a)
Definition: Str.h:729
int Last(const char c) const
Definition: Str.cpp:452
const GLcharARB * name
Definition: glext.h:3629
GLsizeiptr size
Definition: glext.h:3112
ID_INLINE bool operator!=(const idStr &a, const idStr &b)
Definition: Str.h:640
Definition: Str.h:116
void Fill(const char ch, int newlen)
Definition: Str.h:867
GLsizei const GLcharARB const GLint * length
Definition: glext.h:3599
int IcmpPrefix(const char *text) const
Definition: Str.h:677
int DynamicMemoryUsed() const
Definition: Str.h:1040
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const char * c_str(void) const
Definition: Str.h:487
const int STR_ALLOC_BASE
Definition: Str.h:108
void SetUnit(const char *format, float value, int unit, Measure_t measure)
Definition: Str.cpp:1609
int IcmpPrefixPath(const char *text) const
Definition: Str.h:697
const int C_COLOR_GREEN
Definition: Str.h:85
friend bool operator==(const idStr &a, const idStr &b)
Definition: Str.h:626
void ExtractFileExtension(idStr &dest) const
Definition: Str.cpp:965
void FreeData(void)
Definition: Str.cpp:123
const int C_COLOR_ESCAPE
Definition: Str.h:82
static bool CharIsTab(char c)
Definition: Str.h:1032
char baseBuffer[STR_ALLOC_BASE]
Definition: Str.h:319
bool HasUpper(void) const
Definition: Str.h:845
void Replace(const char *old, const char *nw)
Definition: Str.cpp:563
int sprintf(idStr &string, const char *fmt,...)
Definition: Str.cpp:1528
int LengthWithoutColors(void) const
Definition: Str.h:855
idStr & StripFilename(void)
Definition: Str.cpp:864
GLuint start
Definition: glext.h:2845
static void PurgeMemory(void)
Definition: Str.cpp:1643
bool IsColor(void) const
Definition: Str.h:837
int IcmpPath(const char *text) const
Definition: Str.h:687
bool StripLeadingOnce(const char *string)
Definition: Str.cpp:498
int Allocated(void) const
Definition: Str.h:706
static bool CharIsPrintable(int c)
Definition: Str.h:1003