Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# tinystr.h

Go to the documentation of this file.

    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 #pragma warning( disable : 4514 )
    00034
    00035 #include <assert.h>
    00036
    00037 /*
    00038    TiXmlString is an emulation of the std::string template.
    00039    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
    00040    Only the member functions relevant to the TinyXML project have been implemented.
    00041    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
    00042    a string and there's no more room, we allocate a buffer twice as big as we need.
    00043 */
    00044 class TiXmlString
    00045 {
    00046   public :
    00047     // TiXmlString constructor, based on a string
    00048     TiXmlString (const char * instring);
    00049
    00050     // TiXmlString empty constructor
    00051     TiXmlString ()
    00052     {
    00053         allocated = 0;
    00054         cstring = NULL;
    00055         current_length = 0;
    00056     }
    00057
    00058     // TiXmlString copy constructor
    00059     TiXmlString (const TiXmlString& copy);
    00060
    00061     // TiXmlString destructor
    00062     ~ TiXmlString ()
    00063     {
    00064         empty_it ();
    00065     }
    00066
    00067     // Convert a TiXmlString into a classical char *
    00068     const char * c_str () const
    00069     {
    00070         if (allocated)
    00071             return cstring;
    00072         return "";
    00073     }
    00074
    00075     // Return the length of a TiXmlString
    00076     unsigned length () const
    00077     {
    00078         return ( allocated ) ? current_length : 0;
    00079     }
    00080
    00081     // TiXmlString = operator
    00082     void operator = (const char * content);
    00083
    00084     // = operator
    00085     void operator = (const TiXmlString & copy);
    00086
    00087     // += operator. Maps to append
    00088     TiXmlString& operator += (const char * suffix)
    00089     {
    00090         append (suffix);
    00091         return *this;
    00092     }
    00093
    00094     // += operator. Maps to append
    00095     TiXmlString& operator += (char single)
    00096     {
    00097         append (single);
    00098         return *this;
    00099     }
    00100
    00101     // += operator. Maps to append
    00102     TiXmlString& operator += (TiXmlString & suffix)
    00103     {
    00104         append (suffix);
    00105         return *this;
    00106     }
    00107     bool operator == (const TiXmlString & compare) const;
    00108     bool operator < (const TiXmlString & compare) const;
    00109     bool operator > (const TiXmlString & compare) const;
    00110
    00111     // Checks if a TiXmlString is empty
    00112     bool empty () const
    00113     {
    00114         return length () ? false : true;
    00115     }
    00116
    00117     // Checks if a TiXmlString contains only whitespace (same rules as isspace)
    00118     // Not actually used in tinyxml. Conflicts with a C macro, "isblank",
    00119     // which is a problem. Commenting out. -lee
    00120 //    bool isblank () const;
    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, int len );
    00165
    00166   protected :
    00167
    00168     // The base string
    00169     char * cstring;
    00170     // Number of chars allocated
    00171     unsigned allocated;
    00172     // Current string size
    00173     unsigned current_length;
    00174
    00175     // New size computation. It is simplistic right now : it returns twice the amount
    00176     // we need
    00177     unsigned assign_new_size (unsigned minimum_to_allocate)
    00178     {
    00179         //return minimum_to_allocate * 2;
    00180         for (unsigned pow2 = 2; pow2 < minimum_to_allocate; pow2 *= 2)
    00181         {
    00182         }
    00183         return pow2;
    00184     }
    00185
    00186     // Internal function that clears the content of a TiXmlString
    00187     void empty_it ()
    00188     {
    00189         if (cstring)
    00190             delete [] cstring;
    00191         cstring = NULL;
    00192         allocated = 0;
    00193         current_length = 0;
    00194     }
    00195
    00196     void append (const char *suffix );
    00197
    00198     // append function for another TiXmlString
    00199     void append (const TiXmlString & suffix)
    00200     {
    00201         append (suffix . c_str ());
    00202     }
    00203
    00204     // append for a single char. This could be improved a lot if needed
    00205     void append (char single)
    00206     {
    00207         char smallstr [2];
    00208         smallstr [0] = single;
    00209         smallstr [1] = 0;
    00210         append (smallstr);
    00211     }
    00212
    00213 } ;
    00214
    00215 /*
    00216    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
    00217    Only the operators that we need for TinyXML have been developped.
    00218 */
    00219 class TiXmlOutStream : public TiXmlString
    00220 {
    00221 public :
    00222     TiXmlOutStream () : TiXmlString () {}
    00223
    00224     // TiXmlOutStream << operator. Maps to TiXmlString::append
    00225     TiXmlOutStream & operator << (const char * in)
    00226     {
    00227         append (in);
    00228         return (* this);
    00229     }
    00230
    00231     // TiXmlOutStream << operator. Maps to TiXmlString::append
    00232     TiXmlOutStream & operator << (const TiXmlString & in)
    00233     {
    00234         append (in . c_str ());
    00235         return (* this);
    00236     }
    00237 } ;
    00238
    00239 #endif  // TIXML_STRING_INCLUDED
    00240 #endif  // TIXML_USE_STL