00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef TIXML_USE_STL
00038
00039 #ifndef TIXML_STRING_INCLUDED
00040 #define TIXML_STRING_INCLUDED
00041
00042 #include <assert.h>
00043 #include <string.h>
00044
00045
00046
00047
00048
00049
00050
00051
00052 class TiXmlString
00053 {
00054 public :
00055
00056 typedef unsigned int size_type;
00057
00058
00059 static const size_type npos;
00060
00061
00062
00063 TiXmlString () : rep_(&nullrep_)
00064 {
00065 }
00066
00067
00068 TiXmlString (const TiXmlString & copy)
00069 {
00070 init(copy.length());
00071 memcpy(start(), copy.data(), length());
00072 }
00073
00074
00075 TiXmlString (const char * copy)
00076 {
00077 init( static_cast<size_type>( strlen(copy) ));
00078 memcpy(start(), copy, length());
00079 }
00080
00081
00082 TiXmlString (const char * str, size_type len)
00083 {
00084 init(len);
00085 memcpy(start(), str, len);
00086 }
00087
00088
00089 ~TiXmlString ()
00090 {
00091 quit();
00092 }
00093
00094
00095 TiXmlString& operator = (const char * copy)
00096 {
00097 return assign( copy, (size_type)strlen(copy));
00098 }
00099
00100
00101 TiXmlString& operator = (const TiXmlString & copy)
00102 {
00103 return assign(copy.start(), copy.length());
00104 }
00105
00106
00107
00108 TiXmlString& operator += (const char * suffix)
00109 {
00110 return append(suffix, static_cast<size_type>( strlen(suffix) ));
00111 }
00112
00113
00114 TiXmlString& operator += (char single)
00115 {
00116 return append(&single, 1);
00117 }
00118
00119
00120 TiXmlString& operator += (const TiXmlString & suffix)
00121 {
00122 return append(suffix.data(), suffix.length());
00123 }
00124
00125
00126
00127 const char * c_str () const { return rep_->str; }
00128
00129
00130 const char * data () const { return rep_->str; }
00131
00132
00133 size_type length () const { return rep_->size; }
00134
00135
00136 size_type size () const { return rep_->size; }
00137
00138
00139 bool empty () const { return rep_->size == 0; }
00140
00141
00142 size_type capacity () const { return rep_->capacity; }
00143
00144
00145
00146 const char& at (size_type index) const
00147 {
00148 assert( index < length() );
00149 return rep_->str[ index ];
00150 }
00151
00152
00153 char& operator [] (size_type index) const
00154 {
00155 assert( index < length() );
00156 return rep_->str[ index ];
00157 }
00158
00159
00160 size_type find (char lookup) const
00161 {
00162 return find(lookup, 0);
00163 }
00164
00165
00166 size_type find (char tofind, size_type offset) const
00167 {
00168 if (offset >= length()) return npos;
00169
00170 for (const char* p = c_str() + offset; *p != '\0'; ++p)
00171 {
00172 if (*p == tofind) return static_cast< size_type >( p - c_str() );
00173 }
00174 return npos;
00175 }
00176
00177 void clear ()
00178 {
00179
00180
00181
00182
00183 quit();
00184 init(0,0);
00185 }
00186
00187
00188
00189
00190 void reserve (size_type cap);
00191
00192 TiXmlString& assign (const char* str, size_type len);
00193
00194 TiXmlString& append (const char* str, size_type len);
00195
00196 void swap (TiXmlString& other)
00197 {
00198 Rep* r = rep_;
00199 rep_ = other.rep_;
00200 other.rep_ = r;
00201 }
00202
00203 private:
00204
00205 void init(size_type sz) { init(sz, sz); }
00206 void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
00207 char* start() const { return rep_->str; }
00208 char* finish() const { return rep_->str + rep_->size; }
00209
00210 struct Rep
00211 {
00212 size_type size, capacity;
00213 char str[1];
00214 };
00215
00216 void init(size_type sz, size_type cap)
00217 {
00218 if (cap)
00219 {
00220
00221
00222
00223
00224
00225 const size_type bytesNeeded = sizeof(Rep) + cap;
00226 const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
00227 rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
00228
00229 rep_->str[ rep_->size = sz ] = '\0';
00230 rep_->capacity = cap;
00231 }
00232 else
00233 {
00234 rep_ = &nullrep_;
00235 }
00236 }
00237
00238 void quit()
00239 {
00240 if (rep_ != &nullrep_)
00241 {
00242
00243
00244 delete [] ( reinterpret_cast<int*>( rep_ ) );
00245 }
00246 }
00247
00248 Rep * rep_;
00249 static Rep nullrep_;
00250
00251 } ;
00252
00253
00254 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
00255 {
00256 return ( a.length() == b.length() )
00257 && ( strcmp(a.c_str(), b.c_str()) == 0 );
00258 }
00259 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
00260 {
00261 return strcmp(a.c_str(), b.c_str()) < 0;
00262 }
00263
00264 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
00265 inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
00266 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
00267 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
00268
00269 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
00270 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
00271 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
00272 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
00273
00274 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
00275 TiXmlString operator + (const TiXmlString & a, const char* b);
00276 TiXmlString operator + (const char* a, const TiXmlString & b);
00277
00278
00279
00280
00281
00282
00283 class TiXmlOutStream : public TiXmlString
00284 {
00285 public :
00286
00287
00288 TiXmlOutStream & operator << (const TiXmlString & in)
00289 {
00290 *this += in;
00291 return *this;
00292 }
00293
00294
00295 TiXmlOutStream & operator << (const char * in)
00296 {
00297 *this += in;
00298 return *this;
00299 }
00300
00301 } ;
00302
00303 #endif // TIXML_STRING_INCLUDED
00304 #endif // TIXML_USE_STL