[965] | 1 | /*
|
---|
| 2 | www.sourceforge.net/projects/tinyxml
|
---|
| 3 | Original file by Yves Berquin.
|
---|
| 4 |
|
---|
| 5 | This software is provided 'as-is', without any express or implied
|
---|
| 6 | warranty. In no event will the authors be held liable for any
|
---|
| 7 | damages arising from the use of this software.
|
---|
| 8 |
|
---|
| 9 | Permission is granted to anyone to use this software for any
|
---|
| 10 | purpose, including commercial applications, and to alter it and
|
---|
| 11 | redistribute it freely, subject to the following restrictions:
|
---|
| 12 |
|
---|
| 13 | 1. The origin of this software must not be misrepresented; you must
|
---|
| 14 | not claim that you wrote the original software. If you use this
|
---|
| 15 | software in a product, an acknowledgment in the product documentation
|
---|
| 16 | would be appreciated but is not required.
|
---|
| 17 |
|
---|
| 18 | 2. Altered source versions must be plainly marked as such, and
|
---|
| 19 | must not be misrepresented as being the original software.
|
---|
| 20 |
|
---|
| 21 | 3. This notice may not be removed or altered from any source
|
---|
| 22 | distribution.
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | #include "tinyxml.h"
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | #ifndef TIXML_USE_STL
|
---|
| 29 |
|
---|
| 30 | #ifndef TIXML_STRING_INCLUDED
|
---|
| 31 | #define TIXML_STRING_INCLUDED
|
---|
| 32 |
|
---|
| 33 | #pragma warning( disable : 4514 )
|
---|
| 34 |
|
---|
| 35 | #include <assert.h>
|
---|
| 36 |
|
---|
| 37 | /*
|
---|
| 38 | TiXmlString is an emulation of the std::string template.
|
---|
| 39 | Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
|
---|
| 40 | Only the member functions relevant to the TinyXML project have been implemented.
|
---|
| 41 | The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
|
---|
| 42 | a string and there's no more room, we allocate a buffer twice as big as we need.
|
---|
| 43 | */
|
---|
| 44 | class TiXmlString
|
---|
| 45 | {
|
---|
| 46 | public :
|
---|
| 47 | // TiXmlString constructor, based on a string
|
---|
| 48 | TiXmlString (const char * instring);
|
---|
| 49 |
|
---|
| 50 | // TiXmlString empty constructor
|
---|
| 51 | TiXmlString ()
|
---|
| 52 | {
|
---|
| 53 | allocated = 0;
|
---|
| 54 | cstring = NULL;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | // TiXmlString copy constructor
|
---|
| 58 | TiXmlString (const TiXmlString& copy);
|
---|
| 59 |
|
---|
| 60 | // TiXmlString destructor
|
---|
| 61 | ~ TiXmlString ()
|
---|
| 62 | {
|
---|
| 63 | empty_it ();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // Convert a TiXmlString into a classical char *
|
---|
| 67 | const char * c_str () const
|
---|
| 68 | {
|
---|
| 69 | if (allocated)
|
---|
| 70 | return cstring;
|
---|
| 71 | return "";
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | // Return the length of a TiXmlString
|
---|
| 75 | unsigned length () const;
|
---|
| 76 |
|
---|
| 77 | // TiXmlString = operator
|
---|
| 78 | void operator = (const char * content);
|
---|
| 79 |
|
---|
| 80 | // = operator
|
---|
| 81 | void operator = (const TiXmlString & copy);
|
---|
| 82 |
|
---|
| 83 | // += operator. Maps to append
|
---|
| 84 | TiXmlString& operator += (const char * suffix)
|
---|
| 85 | {
|
---|
| 86 | append (suffix);
|
---|
| 87 | return *this;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | // += operator. Maps to append
|
---|
| 91 | TiXmlString& operator += (char single)
|
---|
| 92 | {
|
---|
| 93 | append (single);
|
---|
| 94 | return *this;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | // += operator. Maps to append
|
---|
| 98 | TiXmlString& operator += (TiXmlString & suffix)
|
---|
| 99 | {
|
---|
| 100 | append (suffix);
|
---|
| 101 | return *this;
|
---|
| 102 | }
|
---|
| 103 | bool operator == (const TiXmlString & compare) const;
|
---|
| 104 | bool operator < (const TiXmlString & compare) const;
|
---|
| 105 | bool operator > (const TiXmlString & compare) const;
|
---|
| 106 |
|
---|
| 107 | // Checks if a TiXmlString is empty
|
---|
| 108 | bool empty () const
|
---|
| 109 | {
|
---|
| 110 | return length () ? false : true;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // Checks if a TiXmlString contains only whitespace (same rules as isspace)
|
---|
| 114 | // Not actually used in tinyxml. Conflicts with a C macro, "isblank",
|
---|
| 115 | // which is a problem. Commenting out. -lee
|
---|
| 116 | // bool isblank () const;
|
---|
| 117 |
|
---|
| 118 | // single char extraction
|
---|
| 119 | const char& at (unsigned index) const
|
---|
| 120 | {
|
---|
| 121 | assert( index < length ());
|
---|
| 122 | return cstring [index];
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // find a char in a string. Return TiXmlString::notfound if not found
|
---|
| 126 | unsigned find (char lookup) const
|
---|
| 127 | {
|
---|
| 128 | return find (lookup, 0);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | // find a char in a string from an offset. Return TiXmlString::notfound if not found
|
---|
| 132 | unsigned find (char tofind, unsigned offset) const;
|
---|
| 133 |
|
---|
| 134 | /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
|
---|
| 135 | function clears the content of the TiXmlString if any exists.
|
---|
| 136 | */
|
---|
| 137 | void reserve (unsigned size)
|
---|
| 138 | {
|
---|
| 139 | empty_it ();
|
---|
| 140 | if (size)
|
---|
| 141 | {
|
---|
| 142 | allocated = size;
|
---|
| 143 | cstring = new char [size];
|
---|
| 144 | cstring [0] = 0;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | // [] operator
|
---|
| 149 | char& operator [] (unsigned index) const
|
---|
| 150 | {
|
---|
| 151 | assert( index < length ());
|
---|
| 152 | return cstring [index];
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | // Error value for find primitive
|
---|
| 156 | enum { notfound = 0xffffffff,
|
---|
| 157 | npos = notfound };
|
---|
| 158 |
|
---|
| 159 | void append (const char *str, int len );
|
---|
| 160 |
|
---|
| 161 | protected :
|
---|
| 162 |
|
---|
| 163 | // The base string
|
---|
| 164 | char * cstring;
|
---|
| 165 | // Number of chars allocated
|
---|
| 166 | unsigned allocated;
|
---|
| 167 |
|
---|
| 168 | // New size computation. It is simplistic right now : it returns twice the amount
|
---|
| 169 | // we need
|
---|
| 170 | unsigned assign_new_size (unsigned minimum_to_allocate)
|
---|
| 171 | {
|
---|
| 172 | return minimum_to_allocate * 2;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | // Internal function that clears the content of a TiXmlString
|
---|
| 176 | void empty_it ()
|
---|
| 177 | {
|
---|
| 178 | if (cstring)
|
---|
| 179 | delete [] cstring;
|
---|
| 180 | cstring = NULL;
|
---|
| 181 | allocated = 0;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void append (const char *suffix );
|
---|
| 185 |
|
---|
| 186 | // append function for another TiXmlString
|
---|
| 187 | void append (const TiXmlString & suffix)
|
---|
| 188 | {
|
---|
| 189 | append (suffix . c_str ());
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | // append for a single char. This could be improved a lot if needed
|
---|
| 193 | void append (char single)
|
---|
| 194 | {
|
---|
| 195 | char smallstr [2];
|
---|
| 196 | smallstr [0] = single;
|
---|
| 197 | smallstr [1] = 0;
|
---|
| 198 | append (smallstr);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | } ;
|
---|
| 202 |
|
---|
| 203 | /*
|
---|
| 204 | TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
|
---|
| 205 | Only the operators that we need for TinyXML have been developped.
|
---|
| 206 | */
|
---|
| 207 | class TiXmlOutStream : public TiXmlString
|
---|
| 208 | {
|
---|
| 209 | public :
|
---|
| 210 | TiXmlOutStream () : TiXmlString () {}
|
---|
| 211 |
|
---|
| 212 | // TiXmlOutStream << operator. Maps to TiXmlString::append
|
---|
| 213 | TiXmlOutStream & operator << (const char * in)
|
---|
| 214 | {
|
---|
| 215 | append (in);
|
---|
| 216 | return (* this);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | // TiXmlOutStream << operator. Maps to TiXmlString::append
|
---|
| 220 | TiXmlOutStream & operator << (const TiXmlString & in)
|
---|
| 221 | {
|
---|
| 222 | append (in . c_str ());
|
---|
| 223 | return (* this);
|
---|
| 224 | }
|
---|
| 225 | } ;
|
---|
| 226 |
|
---|
| 227 | #endif // TIXML_STRING_INCLUDED
|
---|
| 228 | #endif // TIXML_USE_STL
|
---|