Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

tinystr.h

00001 /*
00002 www.sourceforge.net/projects/tinyxml
00003 Original file by Yves Berquin.
00004 
00005 This software is provided 'as-is', without any express or implied
00006 warranty. In no event will the authors be held liable for any
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any
00010 purpose, including commercial applications, and to alter it and
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must
00014 not claim that you wrote the original software. If you use this
00015 software in a product, an acknowledgment in the product documentation
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source
00022 distribution.
00023 */
00024 
00025 /*
00026  * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
00027  *
00028  * - completely rewritten. compact, clean, and fast implementation.
00029  * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
00030  * - fixed reserve() to work as per specification.
00031  * - fixed buggy compares operator==(), operator<(), and operator>()
00032  * - fixed operator+=() to take a const ref argument, following spec.
00033  * - added "copy" constructor with length, and most compare operators.
00034  * - added swap(), clear(), size(), capacity(), operator+().
00035  */
00036 
00037 #ifndef TIXML_USE_STL
00038 
00039 #ifndef TIXML_STRING_INCLUDED
00040 #define TIXML_STRING_INCLUDED
00041 
00042 #ifdef _MSC_VER
00043 #pragma warning( disable : 4786 )       // Debugger truncating names.
00044 #endif
00045 
00046 #include <assert.h>
00047 #include <string.h>
00048 
00049 /*
00050    TiXmlString is an emulation of a subset of the std::string template.
00051    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
00052    Only the member functions relevant to the TinyXML project have been implemented.
00053    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
00054    a string and there's no more room, we allocate a buffer twice as big as we need.
00055 */
00056 class TiXmlString
00057 {
00058   public :
00059         // The size type used
00060         typedef unsigned int size_type;
00061 
00062         // Error value for find primitive
00063         static const size_type npos; // = -1;
00064 
00065 
00066         // TiXmlString empty constructor
00067         TiXmlString () : rep_(&nullrep_)
00068         {
00069         }
00070 
00071         // TiXmlString copy constructor
00072         TiXmlString (const TiXmlString & copy)
00073         {
00074                 init(copy.length());
00075                 memcpy(start(), copy.data(), length());
00076         }
00077 
00078         // TiXmlString constructor, based on a string
00079         TiXmlString (const char * copy)
00080         {
00081                 init( static_cast<size_type>( strlen(copy) ));
00082                 memcpy(start(), copy, length());
00083         }
00084 
00085         // TiXmlString constructor, based on a string
00086         TiXmlString (const char * str, size_type len)
00087         {
00088                 init(len);
00089                 memcpy(start(), str, len);
00090         }
00091 
00092         // TiXmlString destructor
00093         ~TiXmlString ()
00094         {
00095                 quit();
00096         }
00097 
00098         // = operator
00099         TiXmlString& operator = (const char * copy)
00100         {
00101                 return assign( copy, (size_type)strlen(copy));
00102         }
00103 
00104         // = operator
00105         TiXmlString& operator = (const TiXmlString & copy)
00106         {
00107                 return assign(copy.start(), copy.length());
00108         }
00109 
00110 
00111         // += operator. Maps to append
00112         TiXmlString& operator += (const char * suffix)
00113         {
00114                 return append(suffix, static_cast<size_type>( strlen(suffix) ));
00115         }
00116 
00117         // += operator. Maps to append
00118         TiXmlString& operator += (char single)
00119         {
00120                 return append(&single, 1);
00121         }
00122 
00123         // += operator. Maps to append
00124         TiXmlString& operator += (const TiXmlString & suffix)
00125         {
00126                 return append(suffix.data(), suffix.length());
00127         }
00128 
00129 
00130         // Convert a TiXmlString into a null-terminated char *
00131         const char * c_str () const { return rep_->str; }
00132 
00133         // Convert a TiXmlString into a char * (need not be null terminated).
00134         const char * data () const { return rep_->str; }
00135 
00136         // Return the length of a TiXmlString
00137         size_type length () const { return rep_->size; }
00138 
00139         // Alias for length()
00140         size_type size () const { return rep_->size; }
00141 
00142         // Checks if a TiXmlString is empty
00143         bool empty () const { return rep_->size == 0; }
00144 
00145         // Return capacity of string
00146         size_type capacity () const { return rep_->capacity; }
00147 
00148 
00149         // single char extraction
00150         const char& at (size_type index) const
00151         {
00152                 assert( index < length() );
00153                 return rep_->str[ index ];
00154         }
00155 
00156         // [] operator
00157         char& operator [] (size_type index) const
00158         {
00159                 assert( index < length() );
00160                 return rep_->str[ index ];
00161         }
00162 
00163         // find a char in a string. Return TiXmlString::npos if not found
00164         size_type find (char lookup) const
00165         {
00166                 return find(lookup, 0);
00167         }
00168 
00169         // find a char in a string from an offset. Return TiXmlString::npos if not found
00170         size_type find (char tofind, size_type offset) const
00171         {
00172                 if (offset >= length()) return npos;
00173 
00174                 for (const char* p = c_str() + offset; *p != '\0'; ++p)
00175                 {
00176                    if (*p == tofind) return static_cast< size_type >( p - c_str() );
00177                 }
00178                 return npos;
00179         }
00180 
00181         void clear ()
00182         {
00183                 //Lee:
00184                 //The original was just too strange, though correct:
00185                 //      TiXmlString().swap(*this);
00186                 //Instead use the quit & re-init:
00187                 quit();
00188                 init(0,0);
00189         }
00190 
00191         /*      Function to reserve a big amount of data when we know we'll need it. Be aware that this
00192                 function DOES NOT clear the content of the TiXmlString if any exists.
00193         */
00194         void reserve (size_type cap);
00195 
00196         TiXmlString& assign (const char* str, size_type len);
00197 
00198         TiXmlString& append (const char* str, size_type len);
00199 
00200         void swap (TiXmlString& other)
00201         {
00202                 Rep* r = rep_;
00203                 rep_ = other.rep_;
00204                 other.rep_ = r;
00205         }
00206 
00207   private:
00208 
00209         void init(size_type sz) { init(sz, sz); }
00210         void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
00211         char* start() const { return rep_->str; }
00212         char* finish() const { return rep_->str + rep_->size; }
00213 
00214         struct Rep
00215         {
00216                 size_type size, capacity;
00217                 char str[1];
00218         };
00219 
00220         void init(size_type sz, size_type cap)
00221         {
00222                 if (cap)
00223                 {
00224                         rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
00225                         rep_->str[ rep_->size = sz ] = '\0';
00226                         rep_->capacity = cap;
00227                 }
00228                 else
00229                 {
00230                         rep_ = &nullrep_;
00231                 }
00232         }
00233 
00234         void quit()
00235         {
00236                 if (rep_ != &nullrep_)
00237                 {
00238                         operator delete(rep_);
00239                 }
00240         }
00241 
00242         Rep * rep_;
00243         static Rep nullrep_;
00244 
00245 } ;
00246 
00247 
00248 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
00249 {
00250         return    ( a.length() == b.length() )                          // optimization on some platforms
00251                && ( strcmp(a.c_str(), b.c_str()) == 0 );        // actual compare
00252 }
00253 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
00254 {
00255         return strcmp(a.c_str(), b.c_str()) < 0;
00256 }
00257 
00258 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
00259 inline bool operator >  (const TiXmlString & a, const TiXmlString & b) { return b < a; }
00260 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
00261 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
00262 
00263 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
00264 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
00265 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
00266 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
00267 
00268 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
00269 TiXmlString operator + (const TiXmlString & a, const char* b);
00270 TiXmlString operator + (const char* a, const TiXmlString & b);
00271 
00272 
00273 /*
00274    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
00275    Only the operators that we need for TinyXML have been developped.
00276 */
00277 class TiXmlOutStream : public TiXmlString
00278 {
00279 public :
00280 
00281         // TiXmlOutStream << operator.
00282         TiXmlOutStream & operator << (const TiXmlString & in)
00283         {
00284                 *this += in;
00285                 return *this;
00286         }
00287 
00288         // TiXmlOutStream << operator.
00289         TiXmlOutStream & operator << (const char * in)
00290         {
00291                 *this += in;
00292                 return *this;
00293         }
00294 
00295 } ;
00296 
00297 #endif  // TIXML_STRING_INCLUDED
00298 #endif  // TIXML_USE_STL

Generated on Sun Sep 25 16:25:49 2005 for Kyra by  doxygen 1.4.3