Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/tinystr.cpp

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 #include "stdafx.h" 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 // TiXmlString constructor, based on a C string 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 newlen = strlen (instring) + 1; 00050 newstring = new char [newlen]; 00051 memcpy (newstring, instring, newlen); 00052 // strcpy (newstring, instring); 00053 allocated = newlen; 00054 cstring = newstring; 00055 current_length = newlen - 1; 00056 } 00057 00058 // TiXmlString copy constructor 00059 TiXmlString::TiXmlString (const TiXmlString& copy) 00060 { 00061 unsigned newlen; 00062 char * newstring; 00063 00064 // Prevent copy to self! 00065 if ( &copy == this ) 00066 return; 00067 00068 if (! copy . allocated) 00069 { 00070 allocated = 0; 00071 cstring = NULL; 00072 current_length = 0; 00073 return; 00074 } 00075 newlen = copy . length () + 1; 00076 newstring = new char [newlen]; 00077 // strcpy (newstring, copy . cstring); 00078 memcpy (newstring, copy . cstring, newlen); 00079 allocated = newlen; 00080 cstring = newstring; 00081 current_length = newlen - 1; 00082 } 00083 00084 // TiXmlString = operator. Safe when assign own content 00085 void TiXmlString ::operator = (const char * content) 00086 { 00087 unsigned newlen; 00088 char * newstring; 00089 00090 if (! content) 00091 { 00092 empty_it (); 00093 return; 00094 } 00095 newlen = strlen (content) + 1; 00096 newstring = new char [newlen]; 00097 // strcpy (newstring, content); 00098 memcpy (newstring, content, newlen); 00099 empty_it (); 00100 allocated = newlen; 00101 cstring = newstring; 00102 current_length = newlen - 1; 00103 } 00104 00105 // = operator. Safe when assign own content 00106 void TiXmlString ::operator = (const TiXmlString & copy) 00107 { 00108 unsigned newlen; 00109 char * newstring; 00110 00111 if (! copy . length ()) 00112 { 00113 empty_it (); 00114 return; 00115 } 00116 newlen = copy . length () + 1; 00117 newstring = new char [newlen]; 00118 // strcpy (newstring, copy . c_str ()); 00119 memcpy (newstring, copy . c_str (), newlen); 00120 empty_it (); 00121 allocated = newlen; 00122 cstring = newstring; 00123 current_length = newlen - 1; 00124 } 00125 00126 00127 // Append a const char * to an existing TiXmlString 00128 void TiXmlString::Append( const char* str, int len ) 00129 { 00130 char * new_string; 00131 unsigned new_alloc, new_size, size_suffix; 00132 00133 // don't use strlen - it can overrun the len passed in! 00134 const char* p = str; 00135 size_suffix = 0; 00136 00137 while ( *p && size_suffix < (unsigned)len ) 00138 { 00139 ++p; 00140 ++size_suffix; 00141 } 00142 if ( !size_suffix) 00143 return; 00144 00145 new_size = length () + size_suffix + 1; 00146 // check if we need to expand 00147 if (new_size > allocated) 00148 { 00149 // compute new size 00150 new_alloc = assign_new_size (new_size); 00151 00152 // allocate new buffer 00153 new_string = new char [new_alloc]; 00154 new_string [0] = 0; 00155 00156 // copy the previous allocated buffer into this one 00157 if (allocated && cstring) 00158 // strcpy (new_string, cstring); 00159 memcpy (new_string, cstring, length ()); 00160 00161 // Append the suffix. It does exist, otherwize we wouldn't be expanding 00162 // strncat (new_string, str, len); 00163 memcpy (new_string + length (), 00164 str, 00165 size_suffix); 00166 00167 // return previsously allocated buffer if any 00168 if (allocated && cstring) 00169 delete [] cstring; 00170 00171 // update member variables 00172 cstring = new_string; 00173 allocated = new_alloc; 00174 } 00175 else 00176 { 00177 // we know we can safely Append the new string 00178 // strncat (cstring, str, len); 00179 memcpy (cstring + length (), 00180 str, 00181 size_suffix); 00182 } 00183 current_length = new_size - 1; 00184 cstring [current_length] = 0; 00185 } 00186 00187 00188 // Append a const char * to an existing TiXmlString 00189 void TiXmlString::Append( const char * suffix ) 00190 { 00191 char * new_string; 00192 unsigned new_alloc, new_size; 00193 00194 new_size = length () + strlen (suffix) + 1; 00195 // check if we need to expand 00196 if (new_size > allocated) 00197 { 00198 // compute new size 00199 new_alloc = assign_new_size (new_size); 00200 00201 // allocate new buffer 00202 new_string = new char [new_alloc]; 00203 new_string [0] = 0; 00204 00205 // copy the previous allocated buffer into this one 00206 if (allocated && cstring) 00207 memcpy (new_string, cstring, 1 + length ()); 00208 // strcpy (new_string, cstring); 00209 00210 // Append the suffix. It does exist, otherwize we wouldn't be expanding 00211 // strcat (new_string, suffix); 00212 memcpy (new_string + length (), 00213 suffix, 00214 strlen (suffix) + 1); 00215 00216 // return previsously allocated buffer if any 00217 if (allocated && cstring) 00218 delete [] cstring; 00219 00220 // update member variables 00221 cstring = new_string; 00222 allocated = new_alloc; 00223 } 00224 else 00225 { 00226 // we know we can safely Append the new string 00227 // strcat (cstring, suffix); 00228 memcpy (cstring + length (), 00229 suffix, 00230 strlen (suffix) + 1); 00231 } 00232 current_length = new_size - 1; 00233 } 00234 00235 // Check for TiXmlString equuivalence 00236 //bool TiXmlString::operator == (const TiXmlString & compare) const 00237 //{ 00238 // return (! strcmp (c_str (), compare . c_str ())); 00239 //} 00240 00241 //unsigned TiXmlString::length () const 00242 //{ 00243 // if (allocated) 00244 // // return strlen (cstring); 00245 // return current_length; 00246 // return 0; 00247 //} 00248 00249 00250 unsigned TiXmlString::find (char tofind, unsigned offset) const 00251 { 00252 char * lookup; 00253 00254 if (offset >= length ()) 00255 return (unsigned) notfound; 00256 for (lookup = cstring + offset; * lookup; lookup++) 00257 if (* lookup == tofind) 00258 return lookup - cstring; 00259 return (unsigned) notfound; 00260 } 00261 00262 00263 bool TiXmlString::operator == (const TiXmlString & compare) const 00264 { 00265 if ( allocated && compare.allocated ) 00266 { 00267 assert( cstring ); 00268 assert( compare.cstring ); 00269 return ( strcmp( cstring, compare.cstring ) == 0 ); 00270 } 00271 return false; 00272 } 00273 00274 00275 bool TiXmlString::operator < (const TiXmlString & compare) const 00276 { 00277 if ( allocated && compare.allocated ) 00278 { 00279 assert( cstring ); 00280 assert( compare.cstring ); 00281 return ( strcmp( cstring, compare.cstring ) > 0 ); 00282 } 00283 return false; 00284 } 00285 00286 00287 bool TiXmlString::operator > (const TiXmlString & compare) const 00288 { 00289 if ( allocated && compare.allocated ) 00290 { 00291 assert( cstring ); 00292 assert( compare.cstring ); 00293 return ( strcmp( cstring, compare.cstring ) < 0 ); 00294 } 00295 return false; 00296 } 00297 00298 00299 #endif // TIXML_USE_STL

Generated on Sun Jul 17 21:34:28 2005 for OpenGL GUI by doxygen 1.3.8