2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 //need to rewrite this
24 #ifndef __UTIL_STR_H__
25 #define __UTIL_STR_H__
32 #pragma warning(disable : 4710) // function 'blah' not inlined
35 void TestStringClass ();
40 strdata () : len( 0 ), refcount ( 0 ), data ( NULL ), alloced ( 0 ) {}
47 void AddRef () { refcount++; }
48 bool DelRef () // True if killed
69 void EnsureAlloced ( int, bool keepold = true );
70 void EnsureDataWritable ();
75 idStr( const char *text );
76 idStr( const idStr& string );
77 idStr( const idStr string, int start, int end );
78 idStr( const char ch );
79 idStr( const int num );
80 idStr( const float num );
81 idStr( const unsigned num );
82 int length( void ) const;
83 int allocated( void ) const;
84 const char * c_str( void ) const;
86 void append( const char *text );
87 void append( const idStr& text );
88 char operator[]( int index ) const;
89 char& operator[]( int index );
91 void operator=( const idStr& text );
92 void operator=( const char *text );
94 friend idStr operator+( const idStr& a, const idStr& b );
95 friend idStr operator+( const idStr& a, const char *b );
96 friend idStr operator+( const char *a, const idStr& b );
98 friend idStr operator+( const idStr& a, const float b );
99 friend idStr operator+( const idStr& a, const int b );
100 friend idStr operator+( const idStr& a, const unsigned b );
101 friend idStr operator+( const idStr& a, const bool b );
102 friend idStr operator+( const idStr& a, const char b );
104 idStr& operator+=( const idStr& a );
105 idStr& operator+=( const char *a );
106 idStr& operator+=( const float a );
107 idStr& operator+=( const char a );
108 idStr& operator+=( const int a );
109 idStr& operator+=( const unsigned a );
110 idStr& operator+=( const bool a );
112 friend bool operator==( const idStr& a, const idStr& b );
113 friend bool operator==( const idStr& a, const char *b );
114 friend bool operator==( const char *a, const idStr& b );
116 friend bool operator!=( const idStr& a, const idStr& b );
117 friend bool operator!=( const idStr& a, const char *b );
118 friend bool operator!=( const char *a, const idStr& b );
120 operator const char * () const;
121 operator const char * ();
123 int icmpn( const char *text, int n ) const;
124 int icmpn( const idStr& text, int n ) const;
125 int icmp( const char *text ) const;
126 int icmp( const idStr& text ) const;
127 int cmpn( const char *text, int n ) const;
128 int cmpn( const idStr& text, int n ) const;
129 int cmp( const char *text ) const;
130 int cmp( const idStr& text ) const;
132 void tolower( void );
133 void toupper( void );
135 static char *tolower( char *s1 );
136 static char *toupper( char *s1 );
138 static int icmpn( const char *s1, const char *s2, int n );
139 static int icmp( const char *s1, const char *s2 );
140 static int cmpn( const char *s1, const char *s2, int n );
141 static int cmp( const char *s1, const char *s2 );
143 static void snprintf ( char *dst, int size, const char *fmt, ... );
145 static bool isNumeric( const char *str );
146 bool isNumeric( void ) const;
148 void CapLength ( int );
150 void BackSlashesToSlashes ();
154 inline idStr::~idStr()
163 inline idStr::idStr() : m_data ( NULL )
166 m_data->data[ 0 ] = 0;
181 len = strlen( text );
182 EnsureAlloced ( len + 1 );
183 strcpy( m_data->data, text );
189 m_data->data[ 0 ] = 0;
200 m_data = text.m_data;
215 if ( end > text.length() )
220 if ( start > text.length() )
222 start = text.length();
231 EnsureAlloced ( len + 1 );
233 for( i = 0; i < len; i++ )
235 m_data->data[ i ] = text[ start + i ];
238 m_data->data[ len ] = 0;
250 m_data->data[ 0 ] = ch;
251 m_data->data[ 1 ] = 0;
264 sprintf( text, "%.3f", num );
265 len = strlen( text );
266 EnsureAlloced( len + 1 );
267 strcpy( m_data->data, text );
280 sprintf( text, "%d", num );
281 len = strlen( text );
282 EnsureAlloced( len + 1 );
283 strcpy( m_data->data, text );
296 sprintf( text, "%u", num );
297 len = strlen( text );
298 EnsureAlloced( len + 1 );
299 strcpy( m_data->data, text );
303 inline int idStr::length( void ) const
305 return ( m_data != NULL ) ? m_data->len : 0;
308 inline int idStr::allocated( void ) const
310 return ( m_data != NULL ) ? m_data->alloced + sizeof( *m_data ) : 0;
313 inline const char *idStr::c_str( void ) const
320 inline void idStr::append
332 len = length() + strlen( text );
333 EnsureAlloced( len + 1 );
335 strcat( m_data->data, text );
340 inline void idStr::append
348 len = length() + text.length();
349 EnsureAlloced ( len + 1 );
351 strcat ( m_data->data, text.c_str () );
355 inline char idStr::operator[]( int index ) const
362 // don't include the '/0' in the test, because technically, it's out of bounds
363 assert( ( index >= 0 ) && ( index < m_data->len ) );
365 // In release mode, give them a null character
366 // don't include the '/0' in the test, because technically, it's out of bounds
367 if ( ( index < 0 ) || ( index >= m_data->len ) )
372 return m_data->data[ index ];
375 inline char& idStr::operator[]
381 // Used for result for invalid indices
382 static char dummy = 0;
385 // We don't know if they'll write to it or not
386 // if it's not a const object
387 EnsureDataWritable ();
392 // don't include the '/0' in the test, because technically, it's out of bounds
393 assert( ( index >= 0 ) && ( index < m_data->len ) );
395 // In release mode, let them change a safe variable
396 // don't include the '/0' in the test, because technically, it's out of bounds
397 if ( ( index < 0 ) || ( index >= m_data->len ) )
402 return m_data->data[ index ];
405 inline void idStr::operator=
411 // adding the reference before deleting our current reference prevents
412 // us from deleting our string if we are copying from ourself
413 text.m_data->AddRef();
415 m_data = text.m_data;
418 inline void idStr::operator=
430 // safe behaviour if NULL
431 EnsureAlloced ( 1, false );
439 len = strlen ( text );
440 EnsureAlloced( len + 1, false );
441 strcpy ( m_data->data, text );
446 if ( text == m_data->data )
447 return; // Copying same thing. Punt.
449 // If we alias and I don't do this, I could corrupt other strings... This
450 // will get called with EnsureAlloced anyway
451 EnsureDataWritable ();
453 // Now we need to check if we're aliasing..
454 if ( text >= m_data->data && text <= m_data->data + m_data->len )
456 // Great, we're aliasing. We're copying from inside ourselves.
457 // This means that I don't have to ensure that anything is alloced,
458 // though I'll assert just in case.
459 int diff = text - m_data->data;
462 assert ( strlen ( text ) < (unsigned) m_data->len );
464 for ( i = 0; text[i]; i++ )
466 m_data->data[i] = text[i];
476 len = strlen( text );
477 EnsureAlloced ( len + 1, false );
478 strcpy( m_data->data, text );
482 inline idStr operator+
496 inline idStr operator+
510 inline idStr operator+
524 inline idStr operator+
533 result.append( b ? "true" : "false" );
538 inline idStr operator+
553 inline idStr& idStr::operator+=
563 inline idStr& idStr::operator+=
573 inline idStr& idStr::operator+=
588 inline idStr& idStr::operator+=
594 append( a ? "true" : "false" );
598 inline bool operator==
605 return ( !strcmp( a.c_str(), b.c_str() ) );
608 inline bool operator==
620 return ( !strcmp( a.c_str(), b ) );
623 inline bool operator==
635 return ( !strcmp( a, b.c_str() ) );
638 inline bool operator!=
648 inline bool operator!=
658 inline bool operator!=
668 inline int idStr::icmpn
678 return idStr::icmpn( m_data->data, text, n );
681 inline int idStr::icmpn
689 assert( text.m_data );
691 return idStr::icmpn( m_data->data, text.m_data->data, n );
694 inline int idStr::icmp
703 return idStr::icmp( m_data->data, text );
706 inline int idStr::icmp
713 assert( text.c_str () );
715 return idStr::icmp( c_str () , text.c_str () );
718 inline int idStr::cmp
727 return idStr::cmp( m_data->data, text );
730 inline int idStr::cmp
737 assert( text.c_str () );
739 return idStr::cmp( c_str () , text.c_str () );
742 inline int idStr::cmpn
752 return idStr::cmpn( c_str () , text, n );
755 inline int idStr::cmpn
763 assert( text.c_str () );
765 return idStr::cmpn( c_str () , text.c_str () , n );
768 inline void idStr::tolower
776 EnsureDataWritable ();
778 idStr::tolower( m_data->data );
781 inline void idStr::toupper
789 EnsureDataWritable ();
791 idStr::toupper( m_data->data );
794 inline bool idStr::isNumeric
801 return idStr::isNumeric( m_data->data );
804 inline idStr::operator const char *() {
808 inline idStr::operator const char *