?? tinystr.cpp
字號:
/*www.sourceforge.net/projects/tinyxmlOriginal file by Yves Berquin.This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.2. Altered source versions must be plainly marked as such, andmust not be misrepresented as being the original software.3. This notice may not be removed or altered from any source distribution.*//** @file tinystr.cpp @modified by Raul Chirinos @date 2004-08-26 Changes: - Replaced string.h routines for their RMF equivalents - Replaced character buffer allocations with RMMalloc - Replaced ctype.h routines with macro equivalents not to use locale since this support is removed from ucLinux - Introduced use of a fixed string buffer. If string to allocate fits in the fixed buffer this buffer will be used and no allocation will be made. This was done because releasing the large amount of strings causes a long delay in ucLinux.*/#include "tinyxml.h"#ifndef TIXML_USE_STL#include <stdlib.h>#include <string.h>//#include <ctype.h>#include "tinystr.h"//#define RMALLOC_DEBUG#ifdef RMALLOC_DEBUGstatic RMuint32 g_bAllocated = 0;static RMuint32 g_nAllocated = 0;static RMuint16 g_maxsize = 0;static RMuint32 g_fixed = 0;#endif// TiXmlString constructor, based on a C stringTiXmlString::TiXmlString (const char* instring){ unsigned newlen; allocated = 0; cstring = NULL; current_length = 0; fixed = 0; buffer[0] = 0; if (!instring) return; newlen = strlen (instring) + 1; //newstring = new char [newlen]; [RC changed] if(newlen < TINY_FIXED_BUFFER){#ifdef RMALLOC_DEBUG// printf("less than buffer %ld\n", ++g_fixed);#endif memcpy (buffer, instring, newlen); fixed = newlen; current_length = newlen - 1; return; } // proceed to allocate char * newstring; newstring = (RMascii*)MALLOC(newlen);//////////////////////////////////////////////////////#ifdef RMALLOC_DEBUG g_bAllocated += newlen; RMbool changed = FALSE; if(newlen > g_maxsize){ g_maxsize = newlen; changed = TRUE; } ++g_nAllocated; if(changed) printf("1....count %ld max %d\n", g_nAllocated, g_maxsize);#endif////////////////////////////////////////////////////// memcpy (newstring, instring, newlen); // strcpy (newstring, instring); allocated = newlen; cstring = newstring; current_length = newlen - 1;}// TiXmlString copy constructorTiXmlString::TiXmlString (const TiXmlString& copy){ unsigned newlen; // Prevent copy to self! if ( © == this ) return; allocated = 0; cstring = NULL; current_length = 0; fixed = 0; buffer[0] = 0; if (copy.allocated){ char * newstring; newlen = copy.length () + 1; newstring = (RMascii*)MALLOC(newlen);//new char [newlen];/////////////////////////////////////////////////#ifdef RMALLOC_DEBUG g_bAllocated += newlen; RMbool changed = FALSE; if(newlen > g_maxsize){ g_maxsize = newlen; changed = TRUE; } ++g_nAllocated; if(changed) printf("2.....count %ld max %d\n", g_nAllocated, g_maxsize);#endif///////////////////////////////////////////////// // strcpy (newstring, copy . cstring); memcpy (newstring, copy.cstring, newlen); allocated = newlen; cstring = newstring; current_length = newlen - 1; return; } else if(copy.fixed){ memcpy (buffer, copy.buffer, newlen); fixed = newlen; current_length = newlen - 1; } }// TiXmlString = operator. Safe when assign own contentvoid TiXmlString ::operator = (const char * content){ unsigned newlen; if (! content || content == ""){ empty_it (); return; } newlen = strlen (content) + 1; if(newlen < TINY_FIXED_BUFFER){#ifdef RMALLOC_DEBUG// printf("less than buffer %ld\n", ++g_fixed);#endif empty_it(); memcpy (buffer, content, newlen); fixed = newlen; current_length = newlen -1; return; } // proceed to allocate char * newstring; newstring = (RMascii*)MALLOC(newlen);//new char [newlen]; /////////////////////////////////////////////////#ifdef RMALLOC_DEBUG g_bAllocated += newlen; RMbool changed = FALSE; if(newlen > g_maxsize){ g_maxsize = newlen; changed = TRUE; } ++g_nAllocated; if(changed) printf("3.....count %ld max %d\n", g_nAllocated, g_maxsize);#endif//////////////////////////////////////////////// // strcpy (newstring, content); memcpy (newstring, content, newlen); empty_it (); allocated = newlen; cstring = newstring; current_length = newlen - 1;}// = operator. Safe when assign own contentvoid TiXmlString ::operator = (const TiXmlString & copy){ unsigned newlen; if (! copy.length ()){ empty_it (); return; } newlen = copy.length () + 1; if(newlen < TINY_FIXED_BUFFER){#ifdef RMALLOC_DEBUG// printf("less than buffer %ld\n", ++g_fixed);#endif empty_it(); memcpy (buffer, copy.c_str(), newlen); fixed = newlen; current_length = newlen - 1; return; } // proceed to allocate char * newstring; newstring = (RMascii*)MALLOC(newlen);//new char [newlen];////////////////////////////////////////////////#ifdef RMALLOC_DEBUG g_bAllocated += newlen; RMbool changed = FALSE; if(newlen > g_maxsize){ g_maxsize = newlen; changed = TRUE; } ++g_nAllocated; if(changed) printf("4......count %ld max %d\n", g_nAllocated, g_maxsize);#endif///////////////////////////////////////////////// // strcpy (newstring, copy . c_str ()); memcpy (newstring, copy.c_str(), newlen); empty_it(); allocated = newlen; cstring = newstring; current_length = newlen - 1;}// append a const char * to an existing TiXmlStringvoid TiXmlString::append( const char* str, int len ){ unsigned new_alloc, new_size, size_suffix; // don't use strlen - it can overrun the len passed in! const char* p = str; size_suffix = 0; while ( *p && size_suffix < (unsigned)len ){ ++p; ++size_suffix; } if ( !size_suffix) return; new_size = length () + size_suffix + 1; // check if we can use the fixed buffer if(new_size < TINY_FIXED_BUFFER && allocated == 0){#ifdef RMALLOC_DEBUG// printf("less than buffer %ld\n", ++g_fixed);#endif // compute new size new_alloc = assign_new_size (new_size); // set buffer // append the suffix. It does exist, otherwize we wouldn't be expanding // strncat (new_string, str, len); memcpy (buffer + length (), str, size_suffix); // update member variables fixed = new_size; current_length = new_size - 1; buffer[current_length] = 0; return; } // proceed to allocate (if necessary) // check if we need to expand if (new_size > allocated){ // compute new size new_alloc = assign_new_size (new_size); char * new_string; new_string = (RMascii*)MALLOC(new_alloc);//new char [new_alloc]; //////////////////////////////////////////////////#ifdef RMALLOC_DEBUG g_bAllocated += new_alloc; RMbool changed = FALSE; if(new_alloc > g_maxsize){ g_maxsize = new_alloc; changed = TRUE; } ++g_nAllocated; if(changed) printf("5......count %ld max %d\n", g_nAllocated, g_maxsize);#endif///////////////////////////////////////////////// new_string [0] = 0; // copy the previous allocated buffer into this one if (allocated && cstring) // strcpy (new_string, cstring); memcpy (new_string, cstring, length ()); else{ // see it we were using fixed buffer and switch if(fixed > 0) memcpy(new_string, buffer, length()); } // append the suffix. It does exist, otherwize we wouldn't be expanding // strncat (new_string, str, len); memcpy (new_string + length (), str, size_suffix); // return previsously allocated buffer if any if (allocated && cstring) //delete [] cstring; RFREE(cstring); // update member variables cstring = new_string; allocated = new_alloc; fixed = 0, buffer[0] = 0; } else{ // we know we can safely append the new string // strncat (cstring, str, len); memcpy (cstring + length (), str, size_suffix); } current_length = new_size - 1; cstring [current_length] = 0;}// append a const char * to an existing TiXmlStringvoid TiXmlString::append( const char * suffix ){ char * new_string; unsigned new_alloc, new_size; new_size = length () + strlen (suffix) + 1; // check if we can use the fixed buffer if(new_size < TINY_FIXED_BUFFER && allocated == 0){#ifdef RMALLOC_DEBUG// printf("less than buffer %ld\n", ++g_fixed);#endif // compute new size new_alloc = assign_new_size (new_size); // append the suffix. It does exist, otherwize we wouldn't be expanding // strcat (new_string, suffix); memcpy(buffer + length (), suffix, strlen (suffix) + 1); // update member variables fixed = new_size; current_length = new_size - 1; return; } // proceed to allocate (if necessary) // check if we need to expand if (new_size > allocated){ // compute new size new_alloc = assign_new_size (new_size); // allocate new buffer new_string = (RMascii*)MALLOC(new_alloc);//new char [new_alloc];//////////////////////////////////////////////////#ifdef RMALLOC_DEBUG g_bAllocated += new_alloc; RMbool changed = FALSE; if(new_alloc > g_maxsize){ g_maxsize = new_alloc; changed = TRUE; } ++g_nAllocated; if(changed) printf("6.......count %ld max %d\n", g_nAllocated, g_maxsize);#endif///////////////////////////////////////////////// new_string [0] = 0; // copy the previous allocated buffer into this one if (allocated && cstring) memcpy (new_string, cstring, 1 + length ()); else{ // see it we were using fixed buffer and switch if(fixed > 0) memcpy(new_string, buffer, 1 + length()); } // strcpy (new_string, cstring); // append the suffix. It does exist, otherwise we wouldn't be expanding // strcat (new_string, suffix); memcpy (new_string + length (), suffix, strlen (suffix) + 1); // return previsously allocated buffer if any if (allocated && cstring) //delete [] cstring; RFREE(cstring); // update member variables cstring = new_string; allocated = new_alloc; fixed = 0, buffer[0] = 0; } else{ // we know we can safely append the new string // strcat (cstring, suffix); memcpy (cstring + length (), suffix, strlen (suffix) + 1); } current_length = new_size - 1;}// Check for TiXmlString equuivalence//bool TiXmlString::operator == (const TiXmlString & compare) const//{// return (! strcmp (c_str (), compare . c_str ()));//}//unsigned TiXmlString::length () const//{// if (allocated)// // return strlen (cstring);// return current_length;// return 0;//}unsigned TiXmlString::find (char tofind, unsigned offset) const{ char * lookup; if (offset >= length ()) return (unsigned) notfound; if(allocated){ for (lookup = cstring + offset; * lookup; lookup++) if (* lookup == tofind) return lookup - cstring; } else{ for (lookup = (char*)buffer + offset; * lookup; lookup++) if (* lookup == tofind) return lookup - buffer; } return (unsigned) notfound;}bool TiXmlString::operator == (const TiXmlString & compare) const{ if ( allocated && compare.allocated ) { assert( cstring ); assert( compare.cstring ); //return ( strcmp( cstring, compare.cstring ) == 0 ); return ( RMCompareAscii( cstring, compare.cstring )); } else if(fixed && compare.fixed){ //assert( buffer[0] ); //assert( compare.buffer[0] ); //return ( strcmp( cstring, compare.cstring ) == 0 ); return ( RMCompareAscii( buffer, compare.buffer )); } return false;}bool TiXmlString::operator < (const TiXmlString & compare) const{ if ( allocated && compare.allocated ) { assert( cstring ); assert( compare.cstring ); return ( strcmp( cstring, compare.cstring ) > 0 ); } else if ( fixed && compare.fixed ) { //assert( buffer[0] ); //assert( compare.buffer[0] ); return ( strcmp( buffer, compare.buffer ) > 0 ); } return false;}bool TiXmlString::operator > (const TiXmlString & compare) const{ if ( allocated && compare.allocated ) { assert( cstring ); assert( compare.cstring ); return ( strcmp( cstring, compare.cstring ) < 0 ); } else if ( fixed && compare.fixed ) { assert( buffer[0] ); assert( compare.buffer[0] ); return ( strcmp( buffer, compare.buffer ) < 0 ); } return false;}#endif // TIXML_USE_STL
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -