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