Main Page | Class Hierarchy | Class List | File List | Class Members

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