| 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 |         current_length = 0; | 
|---|
| 47 |         return; | 
|---|
| 48 |     } | 
|---|
| 49 |         //*ME:  warning C4267: convert 'size_t' to 'unsigned int' | 
|---|
| 50 |         //*ME:  Use Cast: (unsigned) | 
|---|
| 51 |     newlen = (unsigned)strlen (instring) + 1; | 
|---|
| 52 |     newstring = new char [newlen]; | 
|---|
| 53 |     memcpy (newstring, instring, newlen); | 
|---|
| 54 |     // strcpy (newstring, instring); | 
|---|
| 55 |     allocated = newlen; | 
|---|
| 56 |     cstring = newstring; | 
|---|
| 57 |     current_length = newlen - 1; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | // TiXmlString copy constructor | 
|---|
| 61 | TiXmlString::TiXmlString (const TiXmlString& copy) | 
|---|
| 62 | { | 
|---|
| 63 |     unsigned newlen; | 
|---|
| 64 |     char * newstring; | 
|---|
| 65 |  | 
|---|
| 66 |         // Prevent copy to self! | 
|---|
| 67 |         if ( © == this ) | 
|---|
| 68 |                 return; | 
|---|
| 69 |  | 
|---|
| 70 |     if (! copy . allocated) | 
|---|
| 71 |     { | 
|---|
| 72 |         allocated = 0; | 
|---|
| 73 |         cstring = NULL; | 
|---|
| 74 |         current_length = 0; | 
|---|
| 75 |         return; | 
|---|
| 76 |     } | 
|---|
| 77 |     newlen = copy . length () + 1; | 
|---|
| 78 |     newstring = new char [newlen]; | 
|---|
| 79 |     // strcpy (newstring, copy . cstring); | 
|---|
| 80 |     memcpy (newstring, copy . cstring, newlen); | 
|---|
| 81 |     allocated = newlen; | 
|---|
| 82 |     cstring = newstring; | 
|---|
| 83 |     current_length = newlen - 1; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | // TiXmlString = operator. Safe when assign own content | 
|---|
| 87 | void TiXmlString ::operator = (const char * content) | 
|---|
| 88 | { | 
|---|
| 89 |     unsigned newlen; | 
|---|
| 90 |     char * newstring; | 
|---|
| 91 |  | 
|---|
| 92 |     if (! content) | 
|---|
| 93 |     { | 
|---|
| 94 |         empty_it (); | 
|---|
| 95 |         return; | 
|---|
| 96 |     } | 
|---|
| 97 |     newlen = (unsigned)strlen (content) + 1; | 
|---|
| 98 |     newstring = new char [newlen]; | 
|---|
| 99 |     // strcpy (newstring, content); | 
|---|
| 100 |     memcpy (newstring, content, newlen); | 
|---|
| 101 |     empty_it (); | 
|---|
| 102 |     allocated = newlen; | 
|---|
| 103 |     cstring = newstring; | 
|---|
| 104 |     current_length = newlen - 1; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | // = operator. Safe when assign own content | 
|---|
| 108 | void TiXmlString ::operator = (const TiXmlString & copy) | 
|---|
| 109 | { | 
|---|
| 110 |     unsigned newlen; | 
|---|
| 111 |     char * newstring; | 
|---|
| 112 |  | 
|---|
| 113 |     if (! copy . length ()) | 
|---|
| 114 |     { | 
|---|
| 115 |         empty_it (); | 
|---|
| 116 |         return; | 
|---|
| 117 |     } | 
|---|
| 118 |     newlen = copy . length () + 1; | 
|---|
| 119 |     newstring = new char [newlen]; | 
|---|
| 120 |     // strcpy (newstring, copy . c_str ()); | 
|---|
| 121 |     memcpy (newstring, copy . c_str (), newlen); | 
|---|
| 122 |     empty_it (); | 
|---|
| 123 |     allocated = newlen; | 
|---|
| 124 |     cstring = newstring; | 
|---|
| 125 |     current_length = newlen - 1; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | // append a const char * to an existing TiXmlString | 
|---|
| 130 | void TiXmlString::append( const char* str, int len ) | 
|---|
| 131 | { | 
|---|
| 132 |     char * new_string; | 
|---|
| 133 |     unsigned new_alloc, new_size, size_suffix; | 
|---|
| 134 |          | 
|---|
| 135 |         // don't use strlen - it can overrun the len passed in! | 
|---|
| 136 |         const char* p = str; | 
|---|
| 137 |         size_suffix = 0; | 
|---|
| 138 |  | 
|---|
| 139 |         while ( *p && size_suffix < (unsigned)len ) | 
|---|
| 140 |         { | 
|---|
| 141 |                 ++p; | 
|---|
| 142 |                 ++size_suffix; | 
|---|
| 143 |         } | 
|---|
| 144 |     if ( !size_suffix) | 
|---|
| 145 |         return; | 
|---|
| 146 |  | 
|---|
| 147 |     new_size = length () + size_suffix + 1; | 
|---|
| 148 |     // check if we need to expand | 
|---|
| 149 |     if (new_size > allocated) | 
|---|
| 150 |     { | 
|---|
| 151 |         // compute new size | 
|---|
| 152 |         new_alloc = assign_new_size (new_size); | 
|---|
| 153 |  | 
|---|
| 154 |         // allocate new buffer | 
|---|
| 155 |         new_string = new char [new_alloc];         | 
|---|
| 156 |         new_string [0] = 0; | 
|---|
| 157 |  | 
|---|
| 158 |         // copy the previous allocated buffer into this one | 
|---|
| 159 |         if (allocated && cstring) | 
|---|
| 160 |             // strcpy (new_string, cstring); | 
|---|
| 161 |             memcpy (new_string, cstring, length ()); | 
|---|
| 162 |  | 
|---|
| 163 |         // append the suffix. It does exist, otherwize we wouldn't be expanding  | 
|---|
| 164 |         // strncat (new_string, str, len); | 
|---|
| 165 |         memcpy (new_string + length (),  | 
|---|
| 166 |                 str, | 
|---|
| 167 |                 size_suffix); | 
|---|
| 168 |  | 
|---|
| 169 |         // return previsously allocated buffer if any | 
|---|
| 170 |         if (allocated && cstring) | 
|---|
| 171 |             delete [] cstring; | 
|---|
| 172 |  | 
|---|
| 173 |         // update member variables | 
|---|
| 174 |         cstring = new_string; | 
|---|
| 175 |         allocated = new_alloc; | 
|---|
| 176 |     } | 
|---|
| 177 |     else | 
|---|
| 178 |     { | 
|---|
| 179 |         // we know we can safely append the new string | 
|---|
| 180 |         // strncat (cstring, str, len); | 
|---|
| 181 |         memcpy (cstring + length (),  | 
|---|
| 182 |                 str, | 
|---|
| 183 |                 size_suffix); | 
|---|
| 184 |     } | 
|---|
| 185 |     current_length = new_size - 1; | 
|---|
| 186 |     cstring [current_length] = 0; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 |  | 
|---|
| 190 | // append a const char * to an existing TiXmlString | 
|---|
| 191 | void TiXmlString::append( const char * suffix ) | 
|---|
| 192 | { | 
|---|
| 193 |     char * new_string; | 
|---|
| 194 |     unsigned new_alloc, new_size; | 
|---|
| 195 |  | 
|---|
| 196 |     new_size = length () + strlen (suffix) + 1; | 
|---|
| 197 |     // check if we need to expand | 
|---|
| 198 |     if (new_size > allocated) | 
|---|
| 199 |     { | 
|---|
| 200 |         // compute new size | 
|---|
| 201 |         new_alloc = assign_new_size (new_size); | 
|---|
| 202 |  | 
|---|
| 203 |         // allocate new buffer | 
|---|
| 204 |         new_string = new char [new_alloc];         | 
|---|
| 205 |         new_string [0] = 0; | 
|---|
| 206 |  | 
|---|
| 207 |         // copy the previous allocated buffer into this one | 
|---|
| 208 |         if (allocated && cstring) | 
|---|
| 209 |             memcpy (new_string, cstring, 1 + length ()); | 
|---|
| 210 |             // strcpy (new_string, cstring); | 
|---|
| 211 |  | 
|---|
| 212 |         // append the suffix. It does exist, otherwize we wouldn't be expanding  | 
|---|
| 213 |         // strcat (new_string, suffix); | 
|---|
| 214 |         memcpy (new_string + length (),  | 
|---|
| 215 |                 suffix, | 
|---|
| 216 |                 strlen (suffix) + 1); | 
|---|
| 217 |  | 
|---|
| 218 |         // return previsously allocated buffer if any | 
|---|
| 219 |         if (allocated && cstring) | 
|---|
| 220 |             delete [] cstring; | 
|---|
| 221 |  | 
|---|
| 222 |         // update member variables | 
|---|
| 223 |         cstring = new_string; | 
|---|
| 224 |         allocated = new_alloc; | 
|---|
| 225 |     } | 
|---|
| 226 |     else | 
|---|
| 227 |     { | 
|---|
| 228 |         // we know we can safely append the new string | 
|---|
| 229 |         // strcat (cstring, suffix); | 
|---|
| 230 |         memcpy (cstring + length (),  | 
|---|
| 231 |                 suffix,  | 
|---|
| 232 |                 strlen (suffix) + 1); | 
|---|
| 233 |     } | 
|---|
| 234 |     current_length = new_size - 1; | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | // Check for TiXmlString equuivalence | 
|---|
| 238 | //bool TiXmlString::operator == (const TiXmlString & compare) const | 
|---|
| 239 | //{ | 
|---|
| 240 | //    return (! strcmp (c_str (), compare . c_str ())); | 
|---|
| 241 | //} | 
|---|
| 242 |  | 
|---|
| 243 | //unsigned TiXmlString::length () const | 
|---|
| 244 | //{ | 
|---|
| 245 | //    if (allocated) | 
|---|
| 246 | //        // return strlen (cstring); | 
|---|
| 247 | //        return current_length; | 
|---|
| 248 | //    return 0; | 
|---|
| 249 | //} | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 | unsigned TiXmlString::find (char tofind, unsigned offset) const | 
|---|
| 253 | { | 
|---|
| 254 |         //*ME:  warning C4244: convert '__w64 int' to 'unsigned' | 
|---|
| 255 |         //*ME:  Use Array-Arithmetic instead of Pointer | 
|---|
| 256 |         //  char * lookup; | 
|---|
| 257 |  | 
|---|
| 258 |     if (offset >= length ()) | 
|---|
| 259 |         return (unsigned) notfound; | 
|---|
| 260 |         //  for (lookup = cstring + offset; * lookup; lookup++) | 
|---|
| 261 |         //      if (* lookup == tofind) | 
|---|
| 262 |         //          return lookup - cstring; | 
|---|
| 263 |     for( unsigned n=offset ; cstring[n] != '\0' ; n++ ) | 
|---|
| 264 |         if( cstring[n] == tofind ) | 
|---|
| 265 |             return  n ; | 
|---|
| 266 |     return (unsigned) notfound; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 |  | 
|---|
| 270 | bool TiXmlString::operator == (const TiXmlString & compare) const | 
|---|
| 271 | { | 
|---|
| 272 |         if ( allocated && compare.allocated ) | 
|---|
| 273 |         { | 
|---|
| 274 |                 assert( cstring ); | 
|---|
| 275 |                 assert( compare.cstring ); | 
|---|
| 276 |                 return ( strcmp( cstring, compare.cstring ) == 0 ); | 
|---|
| 277 |         } | 
|---|
| 278 |         return false; | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 |  | 
|---|
| 282 | bool TiXmlString::operator < (const TiXmlString & compare) const | 
|---|
| 283 | { | 
|---|
| 284 |         if ( allocated && compare.allocated ) | 
|---|
| 285 |         { | 
|---|
| 286 |                 assert( cstring ); | 
|---|
| 287 |                 assert( compare.cstring ); | 
|---|
| 288 |                 return ( strcmp( cstring, compare.cstring ) > 0 ); | 
|---|
| 289 |         } | 
|---|
| 290 |         return false; | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 |  | 
|---|
| 294 | bool TiXmlString::operator > (const TiXmlString & compare) const | 
|---|
| 295 | { | 
|---|
| 296 |         if ( allocated && compare.allocated ) | 
|---|
| 297 |         { | 
|---|
| 298 |                 assert( cstring ); | 
|---|
| 299 |                 assert( compare.cstring ); | 
|---|
| 300 |                 return ( strcmp( cstring, compare.cstring ) < 0 ); | 
|---|
| 301 |         } | 
|---|
| 302 |         return false; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 |  | 
|---|
| 306 | #endif  // TIXML_USE_STL | 
|---|