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