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