tinystr.h

Go to the documentation of this file.
00001 
00002 /*
00003 www.sourceforge.net/projects/tinyxml
00004 Original file by Yves Berquin.
00005 
00006 This software is provided 'as-is', without any express or implied 
00007 warranty. In no event will the authors be held liable for any 
00008 damages arising from the use of this software.
00009 
00010 Permission is granted to anyone to use this software for any 
00011 purpose, including commercial applications, and to alter it and 
00012 redistribute it freely, subject to the following restrictions:
00013 
00014 1. The origin of this software must not be misrepresented; you must 
00015 not claim that you wrote the original software. If you use this 
00016 software in a product, an acknowledgment in the product documentation 
00017 would be appreciated but is not required.
00018 
00019 2. Altered source versions must be plainly marked as such, and
00020 must not be misrepresented as being the original software.
00021 
00022 3. This notice may not be removed or altered from any source 
00023 distribution.
00024 */
00025 
00026 #include "tinyxml.h"
00027 
00028 
00029 #ifndef TIXML_USE_STL
00030 
00031 #ifndef TIXML_STRING_INCLUDED
00032 #define TIXML_STRING_INCLUDED
00033 
00034 #pragma warning( disable : 4514 )
00035 
00036 #include <assert.h>
00037 
00038 /*
00039    TiXmlString is an emulation of the std::string template.
00040    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
00041    Only the member functions relevant to the TinyXML project have been implemented.
00042    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
00043    a string and there's no more room, we allocate a buffer twice as big as we need.
00044 */
00045 class TiXmlString
00046 {
00047     public:
00048         // TiXmlString constructor, based on a string
00049     TiXmlString(const char *instring);
00050 
00051     // TiXmlString empty constructor
00052     TiXmlString()
00053     {
00054         allocated = 0;
00055         cstring = NULL;
00056         current_length = 0;
00057     }
00058 
00059     // TiXmlString copy constructor
00060     TiXmlString(const TiXmlString & copy);
00061 
00062     // TiXmlString destructor
00063     ~TiXmlString()
00064     {
00065         empty_it();
00066     }
00067 
00068     // Convert a TiXmlString into a classical char *
00069     const char *c_str() const
00070     {
00071         if(allocated)
00072             return cstring;
00073         return "";
00074     }
00075 
00076     // Return the length of a TiXmlString
00077     unsigned length() const;
00078 
00079     // TiXmlString = operator
00080     void operator =(const char *content);
00081 
00082     // = operator
00083     void operator =(const TiXmlString & copy);
00084 
00085     // += operator. Maps to append
00086     TiXmlString & operator +=(const char *suffix)
00087     {
00088         append(suffix);
00089         return *this;
00090     }
00091 
00092     // += operator. Maps to append
00093     TiXmlString & operator +=(char single)
00094     {
00095         append(single);
00096         return *this;
00097     }
00098 
00099     // += operator. Maps to append
00100     TiXmlString & operator +=(TiXmlString & suffix)
00101     {
00102         append(suffix);
00103         return *this;
00104     }
00105     bool operator ==(const TiXmlString & compare) const;
00106     bool operator <(const TiXmlString & compare) const;
00107     bool operator >(const TiXmlString & compare) const;
00108 
00109     // Checks if a TiXmlString is empty
00110     bool empty() const
00111     {
00112         return length() ? false : true;
00113     }
00114 
00115     // Checks if a TiXmlString contains only whitespace (same rules as isspace)
00116     // Not actually used in tinyxml. Conflicts with a C macro, "isblank",
00117     // which is a problem. Commenting out. -lee
00118 //    bool isblank () const;
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 
00147             cstring[0] = 0;
00148             current_length = 0;
00149         }
00150     }
00151 
00152     // [] operator 
00153     char &operator [] (unsigned index) const
00154     {
00155         assert(index < length());
00156         return cstring[index];
00157     }
00158 
00159     // Error value for find primitive 
00160     enum
00161     { notfound = 0xffffffff,
00162         npos = notfound
00163     };
00164 
00165     void append(const char *str, int len);
00166 
00167   protected:
00168 
00169     // The base string
00170     char *cstring;
00171 
00172     // Number of chars allocated
00173     unsigned allocated;
00174 
00175     // Current string size
00176     unsigned current_length;
00177 
00178     // New size computation. It is simplistic right now : it returns twice the amount
00179     // we need
00180     unsigned assign_new_size(unsigned minimum_to_allocate)
00181     {
00182         return minimum_to_allocate * 2;
00183     }
00184 
00185     // Internal function that clears the content of a TiXmlString
00186     void empty_it()
00187     {
00188         if(cstring)
00189             delete[]cstring;
00190         cstring = NULL;
00191         allocated = 0;
00192         current_length = 0;
00193     }
00194 
00195     void append(const char *suffix);
00196 
00197     // append function for another TiXmlString
00198     void append(const TiXmlString & suffix)
00199     {
00200         append(suffix.c_str());
00201     }
00202 
00203     // append for a single char. This could be improved a lot if needed
00204     void append(char single)
00205     {
00206         char smallstr[2];
00207 
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:TiXmlOutStream():TiXmlString()
00222     {
00223     }
00224 
00225     // TiXmlOutStream << operator. Maps to TiXmlString::append
00226     TiXmlOutStream & operator <<(const char *in)
00227     {
00228         append(in);
00229         return (*this);
00230     }
00231 
00232     // TiXmlOutStream << operator. Maps to TiXmlString::append
00233     TiXmlOutStream & operator <<(const TiXmlString & in)
00234     {
00235         append(in.c_str());
00236         return (*this);
00237     }
00238 };
00239 
00240 #endif // TIXML_STRING_INCLUDED
00241 #endif // TIXML_USE_STL

Generated on Sun Mar 11 15:33:23 2007 for MetalWarriors by  doxygen 1.4.7