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