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