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