| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#ifndef TIXML_USE_STL |
| 38 |
|
| 39 |
#ifndef TIXML_STRING_INCLUDED |
| 40 |
#define TIXML_STRING_INCLUDED |
| 41 |
|
| 42 |
#include <cassert> |
| 43 |
#include <string> |
| 44 |
#include <cstring> |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#if defined(_MSC_VER) && (_MSC_VER >= 1200 ) |
| 51 |
|
| 52 |
#define TIXML_EXPLICIT explicit |
| 53 |
#elif defined(__GNUC__) && (__GNUC__ >= 3 ) |
| 54 |
|
| 55 |
#define TIXML_EXPLICIT explicit |
| 56 |
#else |
| 57 |
#define TIXML_EXPLICIT |
| 58 |
#endif |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
class TiXmlString |
| 69 |
{ |
| 70 |
public : |
| 71 |
|
| 72 |
typedef size_t size_type; |
| 73 |
|
| 74 |
|
| 75 |
static const size_type npos; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
TiXmlString () : rep_(&nullrep_) |
| 80 |
{ |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
TiXmlString ( const TiXmlString & copy) : rep_(0) |
| 85 |
{ |
| 86 |
init(copy.length()); |
| 87 |
memcpy(start(), copy.data(), length()); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0) |
| 92 |
{ |
| 93 |
init( static_cast<size_type>( strlen(copy) )); |
| 94 |
memcpy(start(), copy, length()); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0) |
| 99 |
{ |
| 100 |
init(len); |
| 101 |
memcpy(start(), str, len); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
~TiXmlString () |
| 106 |
{ |
| 107 |
quit(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
TiXmlString& operator = (const char * copy) |
| 112 |
{ |
| 113 |
return assign( copy, (size_type)strlen(copy)); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
TiXmlString& operator = (const TiXmlString & copy) |
| 118 |
{ |
| 119 |
return assign(copy.start(), copy.length()); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
TiXmlString& operator += (const char * suffix) |
| 125 |
{ |
| 126 |
return append(suffix, static_cast<size_type>( strlen(suffix) )); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
TiXmlString& operator += (char single) |
| 131 |
{ |
| 132 |
return append(&single, 1); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
TiXmlString& operator += (const TiXmlString & suffix) |
| 137 |
{ |
| 138 |
return append(suffix.data(), suffix.length()); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
const char * c_str () const { return rep_->str; } |
| 144 |
|
| 145 |
|
| 146 |
const char * data () const { return rep_->str; } |
| 147 |
|
| 148 |
|
| 149 |
size_type length () const { return rep_->size; } |
| 150 |
|
| 151 |
|
| 152 |
size_type size () const { return rep_->size; } |
| 153 |
|
| 154 |
|
| 155 |
bool empty () const { return rep_->size == 0; } |
| 156 |
|
| 157 |
|
| 158 |
size_type capacity () const { return rep_->capacity; } |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
const char& at (size_type index) const |
| 163 |
{ |
| 164 |
assert( index < length() ); |
| 165 |
return rep_->str[ index ]; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
char& operator [] (size_type index) const |
| 170 |
{ |
| 171 |
assert( index < length() ); |
| 172 |
return rep_->str[ index ]; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
size_type find (char lookup) const |
| 177 |
{ |
| 178 |
return find(lookup, 0); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
size_type find (char tofind, size_type offset) const |
| 183 |
{ |
| 184 |
if (offset >= length()) return npos; |
| 185 |
|
| 186 |
for (const char* p = c_str() + offset; *p != '\0'; ++p) |
| 187 |
{ |
| 188 |
if (*p == tofind) return static_cast< size_type >( p - c_str() ); |
| 189 |
} |
| 190 |
return npos; |
| 191 |
} |
| 192 |
|
| 193 |
void clear () |
| 194 |
{ |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
quit(); |
| 200 |
init(0,0); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
void reserve (size_type cap); |
| 207 |
|
| 208 |
TiXmlString& assign (const char* str, size_type len); |
| 209 |
|
| 210 |
TiXmlString& append (const char* str, size_type len); |
| 211 |
|
| 212 |
void swap (TiXmlString& other) |
| 213 |
{ |
| 214 |
Rep* r = rep_; |
| 215 |
rep_ = other.rep_; |
| 216 |
other.rep_ = r; |
| 217 |
} |
| 218 |
|
| 219 |
private: |
| 220 |
|
| 221 |
void init(size_type sz) { init(sz, sz); } |
| 222 |
void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; } |
| 223 |
char* start() const { return rep_->str; } |
| 224 |
char* finish() const { return rep_->str + rep_->size; } |
| 225 |
|
| 226 |
struct Rep |
| 227 |
{ |
| 228 |
size_type size, capacity; |
| 229 |
char str[1]; |
| 230 |
}; |
| 231 |
|
| 232 |
void init(size_type sz, size_type cap) |
| 233 |
{ |
| 234 |
if (cap) |
| 235 |
{ |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
const size_type bytesNeeded = sizeof(Rep) + cap; |
| 242 |
const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int ); |
| 243 |
rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] ); |
| 244 |
|
| 245 |
rep_->str[ rep_->size = sz ] = '\0'; |
| 246 |
rep_->capacity = cap; |
| 247 |
} |
| 248 |
else |
| 249 |
{ |
| 250 |
rep_ = &nullrep_; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void quit() |
| 255 |
{ |
| 256 |
if (rep_ != &nullrep_) |
| 257 |
{ |
| 258 |
|
| 259 |
|
| 260 |
delete [] ( reinterpret_cast<int*>( rep_ ) ); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
Rep * rep_; |
| 265 |
static Rep nullrep_; |
| 266 |
|
| 267 |
} ; |
| 268 |
|
| 269 |
|
| 270 |
inline bool operator == (const TiXmlString & a, const TiXmlString & b) |
| 271 |
{ |
| 272 |
return ( a.length() == b.length() ) |
| 273 |
&& ( strcmp(a.c_str(), b.c_str()) == 0 ); |
| 274 |
} |
| 275 |
inline bool operator < (const TiXmlString & a, const TiXmlString & b) |
| 276 |
{ |
| 277 |
return strcmp(a.c_str(), b.c_str()) < 0; |
| 278 |
} |
| 279 |
|
| 280 |
inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); } |
| 281 |
inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; } |
| 282 |
inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); } |
| 283 |
inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); } |
| 284 |
|
| 285 |
inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; } |
| 286 |
inline bool operator == (const char* a, const TiXmlString & b) { return b == a; } |
| 287 |
inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); } |
| 288 |
inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); } |
| 289 |
|
| 290 |
TiXmlString operator + (const TiXmlString & a, const TiXmlString & b); |
| 291 |
TiXmlString operator + (const TiXmlString & a, const char* b); |
| 292 |
TiXmlString operator + (const char* a, const TiXmlString & b); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
class TiXmlOutStream : public TiXmlString |
| 300 |
{ |
| 301 |
public : |
| 302 |
|
| 303 |
|
| 304 |
TiXmlOutStream & operator << (const TiXmlString & in) |
| 305 |
{ |
| 306 |
*this += in; |
| 307 |
return *this; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
TiXmlOutStream & operator << (const char * in) |
| 312 |
{ |
| 313 |
*this += in; |
| 314 |
return *this; |
| 315 |
} |
| 316 |
|
| 317 |
} ; |
| 318 |
|
| 319 |
#endif |
| 320 |
#endif |