Main Page   Class Hierarchy   Compound List   File List   Compound 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 #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     }
00056 
00057     // TiXmlString copy constructor
00058     TiXmlString (const TiXmlString& copy);
00059 
00060     // TiXmlString destructor
00061     ~ TiXmlString ()
00062     {
00063         empty_it ();
00064     }
00065 
00066     // Convert a TiXmlString into a classical char *
00067     const char * c_str () const
00068     {
00069         if (allocated)
00070             return cstring;
00071         return "";
00072     }
00073 
00074     // Return the length of a TiXmlString
00075     unsigned length () const;
00076 
00077     // TiXmlString = operator
00078     void operator = (const char * content);
00079 
00080     // = operator
00081     void operator = (const TiXmlString & copy);
00082 
00083     // += operator. Maps to append
00084     TiXmlString& operator += (const char * suffix)
00085     {
00086         append (suffix);
00087         return *this;
00088     }
00089 
00090     // += operator. Maps to append
00091     TiXmlString& operator += (char single)
00092     {
00093         append (single);
00094         return *this;
00095     }
00096 
00097     // += operator. Maps to append
00098     TiXmlString& operator += (TiXmlString & suffix)
00099     {
00100         append (suffix);
00101         return *this;
00102     }
00103     bool operator == (const TiXmlString & compare) const;
00104     bool operator < (const TiXmlString & compare) const;
00105     bool operator > (const TiXmlString & compare) const;
00106 
00107     // Checks if a TiXmlString is empty
00108     bool empty () const
00109     {
00110         return length () ? false : true;
00111     }
00112 
00113     // Checks if a TiXmlString contains only whitespace (same rules as isspace)
00114     // Not actually used in tinyxml. Conflicts with a C macro, "isblank",
00115     // which is a problem. Commenting out. -lee
00116 //    bool isblank () const;
00117 
00118     // single char extraction
00119     const char& at (unsigned index) const
00120     {
00121         assert( index < length ());
00122         return cstring [index];
00123     }
00124 
00125     // find a char in a string. Return TiXmlString::notfound if not found
00126     unsigned find (char lookup) const
00127     {
00128         return find (lookup, 0);
00129     }
00130 
00131     // find a char in a string from an offset. Return TiXmlString::notfound if not found
00132     unsigned find (char tofind, unsigned offset) const;
00133 
00134     /*  Function to reserve a big amount of data when we know we'll need it. Be aware that this
00135         function clears the content of the TiXmlString if any exists.
00136     */
00137     void reserve (unsigned size)
00138     {
00139         empty_it ();
00140         if (size)
00141         {
00142             allocated = size;
00143             cstring = new char [size];
00144             cstring [0] = 0;
00145         }
00146     }
00147 
00148     // [] operator 
00149     char& operator [] (unsigned index) const
00150     {
00151         assert( index < length ());
00152         return cstring [index];
00153     }
00154 
00155     // Error value for find primitive 
00156     enum {  notfound = 0xffffffff,
00157             npos = notfound };
00158 
00159     void append (const char *str, int len );
00160 
00161   protected :
00162 
00163     // The base string
00164     char * cstring;
00165     // Number of chars allocated
00166     unsigned allocated;
00167 
00168     // New size computation. It is simplistic right now : it returns twice the amount
00169     // we need
00170     unsigned assign_new_size (unsigned minimum_to_allocate)
00171     {
00172         return minimum_to_allocate * 2;
00173     }
00174 
00175     // Internal function that clears the content of a TiXmlString
00176     void empty_it ()
00177     {
00178         if (cstring)
00179             delete [] cstring;
00180         cstring = NULL;
00181         allocated = 0;
00182     }
00183 
00184     void append (const char *suffix );
00185 
00186     // append function for another TiXmlString
00187     void append (const TiXmlString & suffix)
00188     {
00189         append (suffix . c_str ());
00190     }
00191 
00192     // append for a single char. This could be improved a lot if needed
00193     void append (char single)
00194     {
00195         char smallstr [2];
00196         smallstr [0] = single;
00197         smallstr [1] = 0;
00198         append (smallstr);
00199     }
00200 
00201 } ;
00202 
00203 /* 
00204    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
00205    Only the operators that we need for TinyXML have been developped.
00206 */
00207 class TiXmlOutStream : public TiXmlString
00208 {
00209 public :
00210     TiXmlOutStream () : TiXmlString () {}
00211 
00212     // TiXmlOutStream << operator. Maps to TiXmlString::append
00213     TiXmlOutStream & operator << (const char * in)
00214     {
00215         append (in);
00216         return (* this);
00217     }
00218 
00219     // TiXmlOutStream << operator. Maps to TiXmlString::append
00220     TiXmlOutStream & operator << (const TiXmlString & in)
00221     {
00222         append (in . c_str ());
00223         return (* this);
00224     }
00225 } ;
00226 
00227 #endif  // TIXML_STRING_INCLUDED
00228 #endif  // TIXML_USE_STL

Generated on Sun Jun 23 17:57:47 2002 for TinyXml by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001