Warning, file /include/XML/tinystr.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 #ifndef XML_TINYSTR_H
0002 #define XML_TINYSTR_H
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #ifndef TIXML_USE_STL
0041
0042 #ifndef TIXML_STRING_INCLUDED
0043 #define TIXML_STRING_INCLUDED
0044
0045 #include <assert.h>
0046 #include <string.h>
0047
0048
0049
0050
0051
0052 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
0053
0054 #define TIXML_EXPLICIT explicit
0055 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
0056
0057 #define TIXML_EXPLICIT explicit
0058 #else
0059 #define TIXML_EXPLICIT
0060 #endif
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 class TiXmlString{
0071 public :
0072
0073 typedef size_t size_type;
0074
0075
0076 static const size_type npos;
0077
0078
0079
0080 TiXmlString () : rep_(&nullrep_)
0081 {
0082 }
0083
0084
0085 TiXmlString ( const TiXmlString & copy) : rep_(0)
0086 {
0087 init(copy.length());
0088 memcpy(start(), copy.data(), length());
0089 }
0090
0091
0092 TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
0093 {
0094 init( static_cast<size_type>( strlen(copy) ));
0095 memcpy(start(), copy, length());
0096 }
0097
0098
0099 TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
0100 {
0101 init(len);
0102 memcpy(start(), str, len);
0103 }
0104
0105
0106 ~TiXmlString ()
0107 {
0108 quit();
0109 }
0110
0111
0112 TiXmlString& operator = (const char * copy)
0113 {
0114 return assign( copy, (size_type)strlen(copy));
0115 }
0116
0117
0118 TiXmlString& operator = (const TiXmlString & copy)
0119 {
0120 return assign(copy.start(), copy.length());
0121 }
0122
0123
0124
0125 TiXmlString& operator += (const char * suffix)
0126 {
0127 return append(suffix, static_cast<size_type>( strlen(suffix) ));
0128 }
0129
0130
0131 TiXmlString& operator += (char single)
0132 {
0133 return append(&single, 1);
0134 }
0135
0136
0137 TiXmlString& operator += (const TiXmlString & suffix)
0138 {
0139 return append(suffix.data(), suffix.length());
0140 }
0141
0142
0143
0144 const char * c_str () const { return rep_->str; }
0145
0146
0147 const char * data () const { return rep_->str; }
0148
0149
0150 size_type length () const { return rep_->size; }
0151
0152
0153 size_type size () const { return rep_->size; }
0154
0155
0156 bool empty () const { return rep_->size == 0; }
0157
0158
0159 size_type capacity () const { return rep_->capacity; }
0160
0161
0162
0163 const char& at (size_type index) const
0164 {
0165 assert( index < length() );
0166 return rep_->str[ index ];
0167 }
0168
0169
0170 char& operator [] (size_type index) const
0171 {
0172 assert( index < length() );
0173 return rep_->str[ index ];
0174 }
0175
0176
0177 size_type find (char lookup) const
0178 {
0179 return find(lookup, 0);
0180 }
0181
0182
0183 size_type find (char tofind, size_type offset) const
0184 {
0185 if (offset >= length()) return npos;
0186
0187 for (const char* p = c_str() + offset; *p != '\0'; ++p)
0188 {
0189 if (*p == tofind) return static_cast< size_type >( p - c_str() );
0190 }
0191 return npos;
0192 }
0193
0194 void clear ()
0195 {
0196
0197
0198
0199
0200 quit();
0201 init(0,0);
0202 }
0203
0204
0205
0206
0207 void reserve (size_type cap);
0208
0209 TiXmlString& assign (const char* str, size_type len);
0210
0211 TiXmlString& append (const char* str, size_type len);
0212
0213 void swap (TiXmlString& other)
0214 {
0215 Rep* r = rep_;
0216 rep_ = other.rep_;
0217 other.rep_ = r;
0218 }
0219
0220 private:
0221
0222 void init(size_type sz) { init(sz, sz); }
0223 void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
0224 char* start() const { return rep_->str; }
0225 char* finish() const { return rep_->str + rep_->size; }
0226
0227 struct Rep
0228 {
0229 size_type size, capacity;
0230 char str[1];
0231 };
0232
0233 void init(size_type sz, size_type cap)
0234 {
0235 if (cap)
0236 {
0237
0238
0239
0240
0241
0242 const size_type bytesNeeded = sizeof(Rep) + cap;
0243 const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
0244 rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
0245
0246 rep_->str[ rep_->size = sz ] = '\0';
0247 rep_->capacity = cap;
0248 }
0249 else
0250 {
0251 rep_ = &nullrep_;
0252 }
0253 }
0254
0255 void quit()
0256 {
0257 if (rep_ != &nullrep_)
0258 {
0259
0260
0261 delete [] ( reinterpret_cast<int*>( rep_ ) );
0262 }
0263 }
0264
0265 Rep * rep_;
0266 static Rep nullrep_;
0267
0268 } ;
0269
0270
0271 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
0272 {
0273 return ( a.length() == b.length() )
0274 && ( strcmp(a.c_str(), b.c_str()) == 0 );
0275 }
0276 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
0277 {
0278 return strcmp(a.c_str(), b.c_str()) < 0;
0279 }
0280
0281 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
0282 inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
0283 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
0284 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
0285
0286 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
0287 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
0288 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
0289 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
0290
0291 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
0292 TiXmlString operator + (const TiXmlString & a, const char* b);
0293 TiXmlString operator + (const char* a, const TiXmlString & b);
0294
0295
0296
0297
0298
0299
0300 class TiXmlOutStream : public TiXmlString
0301 {
0302 public :
0303
0304
0305 TiXmlOutStream & operator << (const TiXmlString & in)
0306 {
0307 *this += in;
0308 return *this;
0309 }
0310
0311
0312 TiXmlOutStream & operator << (const char * in)
0313 {
0314 *this += in;
0315 return *this;
0316 }
0317
0318 } ;
0319
0320 #endif
0321 #endif
0322
0323 #endif