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