[692] | 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 | #ifndef TIXML_USE_STL
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | #include <stdlib.h>
|
---|
| 31 | #include <string.h>
|
---|
| 32 | #include <ctype.h>
|
---|
| 33 |
|
---|
| 34 | #include "tinystr.h"
|
---|
| 35 |
|
---|
| 36 | // TiXmlString constructor, based on a C string
|
---|
| 37 | TiXmlString::TiXmlString (const char* instring)
|
---|
| 38 | {
|
---|
| 39 | unsigned newlen;
|
---|
| 40 | char * newstring;
|
---|
| 41 |
|
---|
| 42 | if (!instring)
|
---|
| 43 | {
|
---|
| 44 | allocated = 0;
|
---|
| 45 | cstring = NULL;
|
---|
| 46 | return;
|
---|
| 47 | }
|
---|
| 48 | newlen = strlen (instring) + 1;
|
---|
| 49 | newstring = new char [newlen];
|
---|
| 50 | strcpy (newstring, instring);
|
---|
| 51 | allocated = newlen;
|
---|
| 52 | cstring = newstring;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | // TiXmlString copy constructor
|
---|
| 56 | TiXmlString::TiXmlString (const TiXmlString& copy)
|
---|
| 57 | {
|
---|
| 58 | unsigned newlen;
|
---|
| 59 | char * newstring;
|
---|
| 60 |
|
---|
| 61 | // Prevent copy to self!
|
---|
| 62 | if ( © == this )
|
---|
| 63 | return;
|
---|
| 64 |
|
---|
| 65 | if (! copy . allocated)
|
---|
| 66 | {
|
---|
| 67 | allocated = 0;
|
---|
| 68 | cstring = NULL;
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 | newlen = strlen (copy . cstring) + 1;
|
---|
| 72 | newstring = new char [newlen];
|
---|
| 73 | strcpy (newstring, copy . cstring);
|
---|
| 74 | allocated = newlen;
|
---|
| 75 | cstring = newstring;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // TiXmlString = operator. Safe when assign own content
|
---|
| 79 | void TiXmlString ::operator = (const char * content)
|
---|
| 80 | {
|
---|
| 81 | unsigned newlen;
|
---|
| 82 | char * newstring;
|
---|
| 83 |
|
---|
| 84 | if (! content)
|
---|
| 85 | {
|
---|
| 86 | empty_it ();
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | newlen = strlen (content) + 1;
|
---|
| 90 | newstring = new char [newlen];
|
---|
| 91 | strcpy (newstring, content);
|
---|
| 92 | empty_it ();
|
---|
| 93 | allocated = newlen;
|
---|
| 94 | cstring = newstring;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | // = operator. Safe when assign own content
|
---|
| 98 | void TiXmlString ::operator = (const TiXmlString & copy)
|
---|
| 99 | {
|
---|
| 100 | unsigned newlen;
|
---|
| 101 | char * newstring;
|
---|
| 102 |
|
---|
| 103 | if (! copy . length ())
|
---|
| 104 | {
|
---|
| 105 | empty_it ();
|
---|
| 106 | return;
|
---|
| 107 | }
|
---|
| 108 | newlen = copy . length () + 1;
|
---|
| 109 | newstring = new char [newlen];
|
---|
| 110 | strcpy (newstring, copy . c_str ());
|
---|
| 111 | empty_it ();
|
---|
| 112 | allocated = newlen;
|
---|
| 113 | cstring = newstring;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | //// Checks if a TiXmlString contains only whitespace (same rules as isspace)
|
---|
| 118 | //bool TiXmlString::isblank () const
|
---|
| 119 | //{
|
---|
| 120 | // char * lookup;
|
---|
| 121 | // for (lookup = cstring; * lookup; lookup++)
|
---|
| 122 | // if (! isspace (* lookup))
|
---|
| 123 | // return false;
|
---|
| 124 | // return true;
|
---|
| 125 | //}
|
---|
| 126 |
|
---|
| 127 | // append a const char * to an existing TiXmlString
|
---|
| 128 | void TiXmlString::append( const char* str, int len )
|
---|
| 129 | {
|
---|
| 130 | char * new_string;
|
---|
| 131 | unsigned new_alloc, new_size;
|
---|
| 132 |
|
---|
| 133 | new_size = length () + len + 1;
|
---|
| 134 | // check if we need to expand
|
---|
| 135 | if (new_size > allocated)
|
---|
| 136 | {
|
---|
| 137 | // compute new size
|
---|
| 138 | new_alloc = assign_new_size (new_size);
|
---|
| 139 |
|
---|
| 140 | // allocate new buffer
|
---|
| 141 | new_string = new char [new_alloc];
|
---|
| 142 | new_string [0] = 0;
|
---|
| 143 |
|
---|
| 144 | // copy the previous allocated buffer into this one
|
---|
| 145 | if (allocated && cstring)
|
---|
| 146 | strcpy (new_string, cstring);
|
---|
| 147 |
|
---|
| 148 | // append the suffix. It does exist, otherwize we wouldn't be expanding
|
---|
| 149 | strncat (new_string, str, len);
|
---|
| 150 |
|
---|
| 151 | // return previsously allocated buffer if any
|
---|
| 152 | if (allocated && cstring)
|
---|
| 153 | delete [] cstring;
|
---|
| 154 |
|
---|
| 155 | // update member variables
|
---|
| 156 | cstring = new_string;
|
---|
| 157 | allocated = new_alloc;
|
---|
| 158 | }
|
---|
| 159 | else
|
---|
| 160 | // we know we can safely append the new string
|
---|
| 161 | strncat (cstring, str, len);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | // append a const char * to an existing TiXmlString
|
---|
| 166 | void TiXmlString::append( const char * suffix )
|
---|
| 167 | {
|
---|
| 168 | char * new_string;
|
---|
| 169 | unsigned new_alloc, new_size;
|
---|
| 170 |
|
---|
| 171 | new_size = length () + strlen (suffix) + 1;
|
---|
| 172 | // check if we need to expand
|
---|
| 173 | if (new_size > allocated)
|
---|
| 174 | {
|
---|
| 175 | // compute new size
|
---|
| 176 | new_alloc = assign_new_size (new_size);
|
---|
| 177 |
|
---|
| 178 | // allocate new buffer
|
---|
| 179 | new_string = new char [new_alloc];
|
---|
| 180 | new_string [0] = 0;
|
---|
| 181 |
|
---|
| 182 | // copy the previous allocated buffer into this one
|
---|
| 183 | if (allocated && cstring)
|
---|
| 184 | strcpy (new_string, cstring);
|
---|
| 185 |
|
---|
| 186 | // append the suffix. It does exist, otherwize we wouldn't be expanding
|
---|
| 187 | strcat (new_string, suffix);
|
---|
| 188 |
|
---|
| 189 | // return previsously allocated buffer if any
|
---|
| 190 | if (allocated && cstring)
|
---|
| 191 | delete [] cstring;
|
---|
| 192 |
|
---|
| 193 | // update member variables
|
---|
| 194 | cstring = new_string;
|
---|
| 195 | allocated = new_alloc;
|
---|
| 196 | }
|
---|
| 197 | else
|
---|
| 198 | // we know we can safely append the new string
|
---|
| 199 | strcat (cstring, suffix);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | // Check for TiXmlString equuivalence
|
---|
| 203 | //bool TiXmlString::operator == (const TiXmlString & compare) const
|
---|
| 204 | //{
|
---|
| 205 | // return (! strcmp (c_str (), compare . c_str ()));
|
---|
| 206 | //}
|
---|
| 207 |
|
---|
| 208 | unsigned TiXmlString::length () const
|
---|
| 209 | {
|
---|
| 210 | if (allocated)
|
---|
| 211 | return strlen (cstring);
|
---|
| 212 | return 0;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | unsigned TiXmlString::find (char tofind, unsigned offset) const
|
---|
| 217 | {
|
---|
| 218 | char * lookup;
|
---|
| 219 |
|
---|
| 220 | if (offset >= length ())
|
---|
| 221 | return (unsigned) notfound;
|
---|
| 222 | for (lookup = cstring + offset; * lookup; lookup++)
|
---|
| 223 | if (* lookup == tofind)
|
---|
| 224 | return lookup - cstring;
|
---|
| 225 | return (unsigned) notfound;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | bool TiXmlString::operator == (const TiXmlString & compare) const
|
---|
| 230 | {
|
---|
| 231 | if ( allocated && compare.allocated )
|
---|
| 232 | {
|
---|
| 233 | assert( cstring );
|
---|
| 234 | assert( compare.cstring );
|
---|
| 235 | return ( strcmp( cstring, compare.cstring ) == 0 );
|
---|
| 236 | }
|
---|
| 237 | return false;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | bool TiXmlString::operator < (const TiXmlString & compare) const
|
---|
| 242 | {
|
---|
| 243 | if ( allocated && compare.allocated )
|
---|
| 244 | {
|
---|
| 245 | assert( cstring );
|
---|
| 246 | assert( compare.cstring );
|
---|
| 247 | return ( strcmp( cstring, compare.cstring ) > 0 );
|
---|
| 248 | }
|
---|
| 249 | return false;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | bool TiXmlString::operator > (const TiXmlString & compare) const
|
---|
| 254 | {
|
---|
| 255 | if ( allocated && compare.allocated )
|
---|
| 256 | {
|
---|
| 257 | assert( cstring );
|
---|
| 258 | assert( compare.cstring );
|
---|
| 259 | return ( strcmp( cstring, compare.cstring ) < 0 );
|
---|
| 260 | }
|
---|
| 261 | return false;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 | #endif // TIXML_USE_STL
|
---|