Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tinystr.cpp

Go to the documentation of this file.
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 
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         //*ME:  warning C4267: convert 'size_t' to 'unsigned int'
00050         //*ME:  Use Cast: (unsigned)
00051     newlen = (unsigned)strlen (instring) + 1;
00052     newstring = new char [newlen];
00053     memcpy (newstring, instring, newlen);
00054     // strcpy (newstring, instring);
00055     allocated = newlen;
00056     cstring = newstring;
00057     current_length = newlen - 1;
00058 }
00059 
00060 // TiXmlString copy constructor
00061 TiXmlString::TiXmlString (const TiXmlString& copy)
00062 {
00063     unsigned newlen;
00064     char * newstring;
00065 
00066         // Prevent copy to self!
00067         if ( &copy == 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     // strcpy (newstring, copy . cstring);
00080     memcpy (newstring, copy . cstring, newlen);
00081     allocated = newlen;
00082     cstring = newstring;
00083     current_length = newlen - 1;
00084 }
00085 
00086 // TiXmlString = operator. Safe when assign own content
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     // strcpy (newstring, content);
00100     memcpy (newstring, content, newlen);
00101     empty_it ();
00102     allocated = newlen;
00103     cstring = newstring;
00104     current_length = newlen - 1;
00105 }
00106 
00107 // = operator. Safe when assign own content
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     // strcpy (newstring, copy . c_str ());
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 // append a const char * to an existing TiXmlString
00130 void TiXmlString::append( const char* str, int len )
00131 {
00132     char * new_string;
00133     unsigned new_alloc, new_size, size_suffix;
00134         
00135         // don't use strlen - it can overrun the len passed in!
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     // check if we need to expand
00149     if (new_size > allocated)
00150     {
00151         // compute new size
00152         new_alloc = assign_new_size (new_size);
00153 
00154         // allocate new buffer
00155         new_string = new char [new_alloc];        
00156         new_string [0] = 0;
00157 
00158         // copy the previous allocated buffer into this one
00159         if (allocated && cstring)
00160             // strcpy (new_string, cstring);
00161             memcpy (new_string, cstring, length ());
00162 
00163         // append the suffix. It does exist, otherwize we wouldn't be expanding 
00164         // strncat (new_string, str, len);
00165         memcpy (new_string + length (), 
00166                 str,
00167                 size_suffix);
00168 
00169         // return previsously allocated buffer if any
00170         if (allocated && cstring)
00171             delete [] cstring;
00172 
00173         // update member variables
00174         cstring = new_string;
00175         allocated = new_alloc;
00176     }
00177     else
00178     {
00179         // we know we can safely append the new string
00180         // strncat (cstring, str, len);
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 // append a const char * to an existing TiXmlString
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     // check if we need to expand
00198     if (new_size > allocated)
00199     {
00200         // compute new size
00201         new_alloc = assign_new_size (new_size);
00202 
00203         // allocate new buffer
00204         new_string = new char [new_alloc];        
00205         new_string [0] = 0;
00206 
00207         // copy the previous allocated buffer into this one
00208         if (allocated && cstring)
00209             memcpy (new_string, cstring, 1 + length ());
00210             // strcpy (new_string, cstring);
00211 
00212         // append the suffix. It does exist, otherwize we wouldn't be expanding 
00213         // strcat (new_string, suffix);
00214         memcpy (new_string + length (), 
00215                 suffix,
00216                 strlen (suffix) + 1);
00217 
00218         // return previsously allocated buffer if any
00219         if (allocated && cstring)
00220             delete [] cstring;
00221 
00222         // update member variables
00223         cstring = new_string;
00224         allocated = new_alloc;
00225     }
00226     else
00227     {
00228         // we know we can safely append the new string
00229         // strcat (cstring, suffix);
00230         memcpy (cstring + length (), 
00231                 suffix, 
00232                 strlen (suffix) + 1);
00233     }
00234     current_length = new_size - 1;
00235 }
00236 
00237 // Check for TiXmlString equuivalence
00238 //bool TiXmlString::operator == (const TiXmlString & compare) const
00239 //{
00240 //    return (! strcmp (c_str (), compare . c_str ()));
00241 //}
00242 
00243 //unsigned TiXmlString::length () const
00244 //{
00245 //    if (allocated)
00246 //        // return strlen (cstring);
00247 //        return current_length;
00248 //    return 0;
00249 //}
00250 
00251 
00252 unsigned TiXmlString::find (char tofind, unsigned offset) const
00253 {
00254         //*ME:  warning C4244: convert '__w64 int' to 'unsigned'
00255         //*ME:  Use Array-Arithmetic instead of Pointer
00256         //  char * lookup;
00257 
00258     if (offset >= length ())
00259         return (unsigned) notfound;
00260         //  for (lookup = cstring + offset; * lookup; lookup++)
00261         //      if (* lookup == tofind)
00262         //          return lookup - cstring;
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

Generated on Wed Sep 7 09:56:35 2005 for Crazy Eddies GUI System by  doxygen 1.4.3