00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "tinyxml.h"
00026
00027 #ifndef TIXML_USE_STL
00028
00029
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <ctype.h>
00033
00034 #include "tinystr.h"
00035
00036
00037 TiXmlString::TiXmlString (const char* instring)
00038 {
00039 unsigned newlen;
00040 char * newstring;
00041
00042 if (!instring)
00043 {
00044 allocated = 0;
00045 cstring = NULL;
00046 current_length = 0;
00047 return;
00048 }
00049
00050
00051 newlen = (unsigned)strlen (instring) + 1;
00052 newstring = new char [newlen];
00053 memcpy (newstring, instring, newlen);
00054
00055 allocated = newlen;
00056 cstring = newstring;
00057 current_length = newlen - 1;
00058 }
00059
00060
00061 TiXmlString::TiXmlString (const TiXmlString& copy)
00062 {
00063 unsigned newlen;
00064 char * newstring;
00065
00066
00067 if ( © == this )
00068 return;
00069
00070 if (! copy . allocated)
00071 {
00072 allocated = 0;
00073 cstring = NULL;
00074 current_length = 0;
00075 return;
00076 }
00077 newlen = copy . length () + 1;
00078 newstring = new char [newlen];
00079
00080 memcpy (newstring, copy . cstring, newlen);
00081 allocated = newlen;
00082 cstring = newstring;
00083 current_length = newlen - 1;
00084 }
00085
00086
00087 void TiXmlString ::operator = (const char * content)
00088 {
00089 unsigned newlen;
00090 char * newstring;
00091
00092 if (! content)
00093 {
00094 empty_it ();
00095 return;
00096 }
00097 newlen = (unsigned)strlen (content) + 1;
00098 newstring = new char [newlen];
00099
00100 memcpy (newstring, content, newlen);
00101 empty_it ();
00102 allocated = newlen;
00103 cstring = newstring;
00104 current_length = newlen - 1;
00105 }
00106
00107
00108 void TiXmlString ::operator = (const TiXmlString & copy)
00109 {
00110 unsigned newlen;
00111 char * newstring;
00112
00113 if (! copy . length ())
00114 {
00115 empty_it ();
00116 return;
00117 }
00118 newlen = copy . length () + 1;
00119 newstring = new char [newlen];
00120
00121 memcpy (newstring, copy . c_str (), newlen);
00122 empty_it ();
00123 allocated = newlen;
00124 cstring = newstring;
00125 current_length = newlen - 1;
00126 }
00127
00128
00129
00130 void TiXmlString::append( const char* str, int len )
00131 {
00132 char * new_string;
00133 unsigned new_alloc, new_size, size_suffix;
00134
00135
00136 const char* p = str;
00137 size_suffix = 0;
00138
00139 while ( *p && size_suffix < (unsigned)len )
00140 {
00141 ++p;
00142 ++size_suffix;
00143 }
00144 if ( !size_suffix)
00145 return;
00146
00147 new_size = length () + size_suffix + 1;
00148
00149 if (new_size > allocated)
00150 {
00151
00152 new_alloc = assign_new_size (new_size);
00153
00154
00155 new_string = new char [new_alloc];
00156 new_string [0] = 0;
00157
00158
00159 if (allocated && cstring)
00160
00161 memcpy (new_string, cstring, length ());
00162
00163
00164
00165 memcpy (new_string + length (),
00166 str,
00167 size_suffix);
00168
00169
00170 if (allocated && cstring)
00171 delete [] cstring;
00172
00173
00174 cstring = new_string;
00175 allocated = new_alloc;
00176 }
00177 else
00178 {
00179
00180
00181 memcpy (cstring + length (),
00182 str,
00183 size_suffix);
00184 }
00185 current_length = new_size - 1;
00186 cstring [current_length] = 0;
00187 }
00188
00189
00190
00191 void TiXmlString::append( const char * suffix )
00192 {
00193 char * new_string;
00194 unsigned new_alloc, new_size;
00195
00196 new_size = length () + strlen (suffix) + 1;
00197
00198 if (new_size > allocated)
00199 {
00200
00201 new_alloc = assign_new_size (new_size);
00202
00203
00204 new_string = new char [new_alloc];
00205 new_string [0] = 0;
00206
00207
00208 if (allocated && cstring)
00209 memcpy (new_string, cstring, 1 + length ());
00210
00211
00212
00213
00214 memcpy (new_string + length (),
00215 suffix,
00216 strlen (suffix) + 1);
00217
00218
00219 if (allocated && cstring)
00220 delete [] cstring;
00221
00222
00223 cstring = new_string;
00224 allocated = new_alloc;
00225 }
00226 else
00227 {
00228
00229
00230 memcpy (cstring + length (),
00231 suffix,
00232 strlen (suffix) + 1);
00233 }
00234 current_length = new_size - 1;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 unsigned TiXmlString::find (char tofind, unsigned offset) const
00253 {
00254
00255
00256
00257
00258 if (offset >= length ())
00259 return (unsigned) notfound;
00260
00261
00262
00263 for( unsigned n=offset ; cstring[n] != '\0' ; n++ )
00264 if( cstring[n] == tofind )
00265 return n ;
00266 return (unsigned) notfound;
00267 }
00268
00269
00270 bool TiXmlString::operator == (const TiXmlString & compare) const
00271 {
00272 if ( allocated && compare.allocated )
00273 {
00274 assert( cstring );
00275 assert( compare.cstring );
00276 return ( strcmp( cstring, compare.cstring ) == 0 );
00277 }
00278 return false;
00279 }
00280
00281
00282
00283 bool TiXmlString::operator < (const TiXmlString & compare) const
00284 {
00285 if ( allocated && compare.allocated )
00286 {
00287 assert( cstring );
00288 assert( compare.cstring );
00289 return ( strcmp( cstring, compare.cstring ) > 0 );
00290 }
00291 return false;
00292 }
00293
00294
00295 bool TiXmlString::operator > (const TiXmlString & compare) const
00296 {
00297 if ( allocated && compare.allocated )
00298 {
00299 assert( cstring );
00300 assert( compare.cstring );
00301 return ( strcmp( cstring, compare.cstring ) < 0 );
00302 }
00303 return false;
00304 }
00305
00306
00307 #endif // TIXML_USE_STL