Crazy Eddies GUI System: tinystr.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- 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 #include "tinyxml.h"
    00026
    00027
    00028 #ifndef TIXML_USE_STL
    00029
    00030 #ifndef TIXML_STRING_INCLUDED
    00031 #define TIXML_STRING_INCLUDED
    00032
    00033 #ifdef _MSC_VER
    00034 #pragma warning( disable : 4786 )       // Debugger truncating names.
    00035 #endif
    00036
    00037 #include <assert.h>
    00038
    00039 /*
    00040    TiXmlString is an emulation of the std::string template.
    00041    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
    00042    Only the member functions relevant to the TinyXML project have been implemented.
    00043    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
    00044    a string and there's no more room, we allocate a buffer twice as big as we need.
    00045 */
    00046 class TiXmlString
    00047 {
    00048   public :
    00049     // TiXmlString constructor, based on a string
    00050     TiXmlString (const char * instring);
    00051
    00052     // TiXmlString empty constructor
    00053     TiXmlString ()
    00054     {
    00055         allocated = 0;
    00056         cstring = NULL;
    00057         current_length = 0;
    00058     }
    00059
    00060     // TiXmlString copy constructor
    00061     TiXmlString (const TiXmlString& copy);
    00062
    00063     // TiXmlString destructor
    00064     ~ TiXmlString ()
    00065     {
    00066         empty_it ();
    00067     }
    00068
    00069     // Convert a TiXmlString into a classical char *
    00070     const char * c_str () const
    00071     {
    00072         if (allocated)
    00073             return cstring;
    00074         return "";
    00075     }
    00076
    00077     // Return the length of a TiXmlString
    00078     unsigned length () const
    00079         {
    00080                 return ( allocated ) ? current_length : 0;
    00081         }
    00082
    00083     // TiXmlString = operator
    00084     void operator = (const char * content);
    00085
    00086     // = operator
    00087     void operator = (const TiXmlString & copy);
    00088
    00089     // += operator. Maps to append
    00090     TiXmlString& operator += (const char * suffix)
    00091     {
    00092         append (suffix);
    00093                 return *this;
    00094     }
    00095
    00096     // += operator. Maps to append
    00097     TiXmlString& operator += (char single)
    00098     {
    00099         append (single);
    00100                 return *this;
    00101     }
    00102
    00103     // += operator. Maps to append
    00104     TiXmlString& operator += (TiXmlString & suffix)
    00105     {
    00106         append (suffix);
    00107                 return *this;
    00108     }
    00109     bool operator == (const TiXmlString & compare) const;
    00110     bool operator == (const char* compare) const;
    00111     bool operator < (const TiXmlString & compare) const;
    00112     bool operator > (const TiXmlString & compare) const;
    00113
    00114     // Checks if a TiXmlString is empty
    00115     bool empty () const
    00116     {
    00117         return length () ? false : true;
    00118     }
    00119
    00120     // single char extraction
    00121     const char& at (unsigned index) const
    00122     {
    00123         assert( index < length ());
    00124         return cstring [index];
    00125     }
    00126
    00127     // find a char in a string. Return TiXmlString::notfound if not found
    00128     unsigned find (char lookup) const
    00129     {
    00130         return find (lookup, 0);
    00131     }
    00132
    00133     // find a char in a string from an offset. Return TiXmlString::notfound if not found
    00134     unsigned find (char tofind, unsigned offset) const;
    00135
    00136     /*  Function to reserve a big amount of data when we know we'll need it. Be aware that this
    00137                 function clears the content of the TiXmlString if any exists.
    00138     */
    00139     void reserve (unsigned size)
    00140     {
    00141         empty_it ();
    00142         if (size)
    00143         {
    00144             allocated = size;
    00145             cstring = new char [size];
    00146             cstring [0] = 0;
    00147             current_length = 0;
    00148         }
    00149     }
    00150
    00151     // [] operator
    00152     char& operator [] (unsigned index) const
    00153     {
    00154         assert( index < length ());
    00155         return cstring [index];
    00156     }
    00157
    00158     // Error value for find primitive
    00159     enum {      notfound = 0xffffffff,
    00160             npos = notfound };
    00161
    00162     void append (const char *str, int len );
    00163
    00164   protected :
    00165
    00166     // The base string
    00167     char * cstring;
    00168     // Number of chars allocated
    00169     unsigned allocated;
    00170     // Current string size
    00171     unsigned current_length;
    00172
    00173     // New size computation. It is simplistic right now : it returns twice the amount
    00174     // we need
    00175     unsigned assign_new_size (unsigned minimum_to_allocate)
    00176     {
    00177         return minimum_to_allocate * 2;
    00178     }
    00179
    00180     // Internal function that clears the content of a TiXmlString
    00181     void empty_it ()
    00182     {
    00183         if (cstring)
    00184             delete [] cstring;
    00185         cstring = NULL;
    00186         allocated = 0;
    00187         current_length = 0;
    00188     }
    00189
    00190     void append (const char *suffix );
    00191
    00192     // append function for another TiXmlString
    00193     void append (const TiXmlString & suffix)
    00194     {
    00195         append (suffix . c_str ());
    00196     }
    00197
    00198     // append for a single char.
    00199     void append (char single)
    00200     {
    00201         if ( cstring && current_length < (allocated-1) )
    00202                 {
    00203                         cstring[ current_length ] = single;
    00204                         ++current_length;
    00205                         cstring[ current_length ] = 0;
    00206                 }
    00207                 else
    00208                 {
    00209                         char smallstr [2];
    00210                         smallstr [0] = single;
    00211                         smallstr [1] = 0;
    00212                         append (smallstr);
    00213                 }
    00214     }
    00215
    00216 } ;
    00217
    00218 inline bool TiXmlString::operator == (const char* compare) const
    00219 {
    00220         if ( allocated )
    00221         {
    00222                 assert( cstring );
    00223                 return ( strcmp( cstring, compare ) == 0 );
    00224         }
    00225         return false;
    00226 }
    00227
    00228 /*
    00229    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
    00230    Only the operators that we need for TinyXML have been developped.
    00231 */
    00232 class TiXmlOutStream : public TiXmlString
    00233 {
    00234 public :
    00235     TiXmlOutStream () : TiXmlString () {}
    00236
    00237     // TiXmlOutStream << operator. Maps to TiXmlString::append
    00238     TiXmlOutStream & operator << (const char * in)
    00239     {
    00240         append (in);
    00241         return (* this);
    00242     }
    00243
    00244     // TiXmlOutStream << operator. Maps to TiXmlString::append
    00245     TiXmlOutStream & operator << (const TiXmlString & in)
    00246     {
    00247         append (in . c_str ());
    00248         return (* this);
    00249     }
    00250 } ;
    00251
    00252 #endif  // TIXML_STRING_INCLUDED
    00253 #endif  // TIXML_USE_STL