Back to Kbengine

tinystr.h

kbe/src/lib/dependencies/tinyxml/docs/tinystr_8h_source.html

2.5.129.3 KB
Original Source

tinystr.h

00001/\*00002www.sourceforge.net/projects/tinyxml0000300004This software is provided 'as-is', without any express or implied00005warranty. In no event will the authors be held liable for any00006damages arising from the use of this software.0000700008Permission is granted to anyone to use this software for any00009purpose, including commercial applications, and to alter it and00010redistribute it freely, subject to the following restrictions:00011000121. The origin of this software must not be misrepresented; you must00013not claim that you wrote the original software. If you use this00014software in a product, an acknowledgment in the product documentation00015would be appreciated but is not required.00016000172. Altered source versions must be plainly marked as such, and00018must not be misrepresented as being the original software.00019000203. This notice may not be removed or altered from any source00021distribution.00022\*/00023 
00024 
00025#ifndef TIXML\_USE\_STL0002600027#ifndef TIXML\_STRING\_INCLUDED00028#define TIXML\_STRING\_INCLUDED0002900030#include \<assert.h\>00031#include \<string.h\>00032 
00033/\* The support for explicit isn't that universal, and it isn't really00034 required - it is used to check that the TiXmlString class isn't incorrectly00035 used. Be nice to old compilers and macro it here:00036\*/00037#if defined(\_MSC\_VER) && (\_MSC\_VER \>= 1200 )00038// Microsoft visual studio, version 6 and higher.00039 #define TIXML\_EXPLICIT explicit00040#elif defined(\_\_GNUC\_\_) && (\_\_GNUC\_\_ \>= 3 )00041// GCC version 3 and higher.s00042 #define TIXML\_EXPLICIT explicit00043#else00044 #define TIXML\_EXPLICIT00045#endif0004600047 
00048/\*00049 TiXmlString is an emulation of a subset of the std::string template.00050 Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.00051 Only the member functions relevant to the TinyXML project have been implemented.00052 The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase00053 a string and there's no more room, we allocate a buffer twice as big as we need.00054\*/00055class TiXmlString
00056 {
00057public:
00058// The size type used00059typedefsize\_tsize_type;
00060 
00061// Error value for find primitive00062staticconstsize_type npos;// = -1;00063 
00064 
00065// TiXmlString empty constructor00066 TiXmlString () : rep_(&nullrep_)
00067 {
00068 }
00069 
00070// TiXmlString copy constructor00071 TiXmlString (constTiXmlString & copy) : rep_(0)
00072 {
00073 init(copy.length());
00074 memcpy(start(), copy.data(), length());
00075 }
00076 
00077// TiXmlString constructor, based on a string00078 TIXML_EXPLICIT TiXmlString (constchar* copy) : rep_(0)
00079 {
00080 init( static_cast<size_type>( strlen(copy) ));
00081 memcpy(start(), copy, length());
00082 }
00083 
00084// TiXmlString constructor, based on a string00085 TIXML_EXPLICIT TiXmlString (constchar* str, size_type len) : rep_(0)
00086 {
00087 init(len);
00088 memcpy(start(), str, len);
00089 }
00090 
00091// TiXmlString destructor00092 ~TiXmlString ()
00093 {
00094 quit();
00095 }
00096 
00097 TiXmlString& operator = (constchar* copy)
00098 {
00099returnassign( copy, (size_type)strlen(copy));
00100 }
00101 
00102 TiXmlString& operator = (constTiXmlString & copy)
00103 {
00104returnassign(copy.start(), copy.length());
00105 }
00106 
00107 
00108// += operator. Maps to append00109 TiXmlString& operator += (constchar* suffix)
00110 {
00111returnappend(suffix, static_cast<size_type>( strlen(suffix) ));
00112 }
00113 
00114// += operator. Maps to append00115 TiXmlString& operator += (charsingle)
00116 {
00117returnappend(&single, 1);
00118 }
00119 
00120// += operator. Maps to append00121 TiXmlString& operator += (constTiXmlString & suffix)
00122 {
00123returnappend(suffix.data(), suffix.length());
00124 }
00125 
00126 
00127// Convert a TiXmlString into a null-terminated char \*00128constchar* c_str () const {returnrep_->str; }
00129 
00130// Convert a TiXmlString into a char \* (need not be null terminated).00131constchar* data () const {returnrep_->str; }
00132 
00133// Return the length of a TiXmlString00134 size_type length () const {returnrep_->size; }
00135 
00136// Alias for length()00137 size_type size () const {returnrep_->size; }
00138 
00139// Checks if a TiXmlString is empty00140boolempty () const {returnrep_->size == 0; }
00141 
00142// Return capacity of string00143 size_type capacity () const {returnrep_->capacity; }
00144 
00145 
00146// single char extraction00147constchar& at (size_type index) const00148{
00149 assert( index < length() );
00150returnrep_->str[index];
00151 }
00152 
00153// [] operator00154char& operator [] (size_type index) const00155{
00156 assert( index < length() );
00157returnrep_->str[index];
00158 }
00159 
00160// find a char in a string. Return TiXmlString::npos if not found00161 size_type find (charlookup) const00162{
00163returnfind(lookup, 0);
00164 }
00165 
00166// find a char in a string from an offset. Return TiXmlString::npos if not found00167 size_type find (chartofind, size_type offset) const00168{
00169if(offset >= length())returnnpos;
00170 
00171for(constchar* p = c_str() + offset; *p !='\0'; ++p)
00172 {
00173if(*p == tofind)returnstatic\_cast\<size_type\>( p - c_str() );
00174 }
00175returnnpos;
00176 }
00177 
00178voidclear ()
00179 {
00180//Lee:00181//The original was just too strange, though correct:00182// TiXmlString().swap(\*this);00183//Instead use the quit & re-init:00184 quit();
00185 init(0,0);
00186 }
00187 
00188/\* Function to reserve a big amount of data when we know we'll need it. Be aware that this00189 function DOES NOT clear the content of the TiXmlString if any exists.00190 \*/00191voidreserve (size_type cap);
00192 
00193 TiXmlString& assign (constchar* str, size_type len);
00194 
00195 TiXmlString& append (constchar* str, size_type len);
00196 
00197voidswap (TiXmlString& other)
00198 {
00199 Rep* r = rep_;
00200 rep_ = other.rep_;
00201 other.rep_ = r;
00202 }
00203 
00204private:
00205 
00206voidinit(size_type sz) { init(sz, sz); }
00207voidset_size(size_type sz) { rep_->str[rep_->size = sz] ='\0'; }
00208char* start() const {returnrep_->str; }
00209char* finish() const {returnrep_->str + rep_->size; }
00210 
00211struct Rep
00212 {
00213 size_type size, capacity;
00214charstr[1];
00215 };
00216 
00217voidinit(size_type sz, size_type cap)
00218 {
00219if(cap)
00220 {
00221// Lee: the original form:00222// rep\_ = static\_cast\<Rep\*\>(operator new(sizeof(Rep) + cap));00223// doesn't work in some cases of new being overloaded. Switching00224// to the normal allocation, although use an 'int' for systems00225// that are overly picky about structure alignment.00226constsize_type bytesNeeded =sizeof(Rep) + cap;
00227constsize_type intsNeeded = ( bytesNeeded +sizeof(int) - 1 ) /sizeof( int ); 
00228 rep_ =reinterpret\_cast\<Rep*\>(newint[intsNeeded] );
00229 
00230 rep_->str[rep_->size = sz] ='\0';
00231 rep_->capacity = cap;
00232 }
00233else00234 {
00235 rep_ = &nullrep_;
00236 }
00237 }
00238 
00239voidquit()
00240 {
00241if(rep_ != &nullrep_)
00242 {
00243// The rep\_ is really an array of ints. (see the allocator, above).00244// Cast it back before delete, so the compiler won't incorrectly call destructors.00245delete[] (reinterpret\_cast\<int*\>( rep_ ) );
00246 }
00247 }
00248 
00249 Rep * rep_;
00250staticRep nullrep_;
00251 
00252 } ;
00253 
00254 
00255inlinebooloperator == (constTiXmlString & a,constTiXmlString & b)
00256 {
00257return( a.length() == b.length() )// optimization on some platforms00258 && ( strcmp(a.c_str(), b.c_str()) == 0 );// actual compare00259 }
00260inlinebooloperator < (constTiXmlString & a,constTiXmlString & b)
00261 {
00262returnstrcmp(a.c_str(), b.c_str()) < 0;
00263 }
00264 
00265inlinebooloperator != (constTiXmlString & a,constTiXmlString & b) {return!(a == b); }
00266inlinebooloperator > (constTiXmlString & a,constTiXmlString & b) {returnb < a; }
00267inlinebooloperator <= (constTiXmlString & a,constTiXmlString & b) {return!(b < a); }
00268inlinebooloperator >= (constTiXmlString & a,constTiXmlString & b) {return!(a < b); }
00269 
00270inlinebooloperator == (constTiXmlString & a,constchar* b) {returnstrcmp(a.c_str(), b) == 0; }
00271inlinebooloperator == (constchar* a,constTiXmlString & b) {returnb == a; }
00272inlinebooloperator != (constTiXmlString & a,constchar* b) {return!(a == b); }
00273inlinebooloperator != (constchar* a,constTiXmlString & b) {return!(b == a); }
00274 
00275 TiXmlString operator + (constTiXmlString & a,constTiXmlString & b);
00276 TiXmlString operator + (constTiXmlString & a,constchar* b);
00277 TiXmlString operator + (constchar* a,constTiXmlString & b);
00278 
00279 
00280/\*00281 TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.00282 Only the operators that we need for TinyXML have been developped.00283\*/00284class TiXmlOutStream :publicTiXmlString
00285 {
00286public:
00287 
00288// TiXmlOutStream \<\< operator.00289 TiXmlOutStream & operator << (constTiXmlString & in)
00290 {
00291 *this+= in;
00292return*this;
00293 }
00294 
00295// TiXmlOutStream \<\< operator.00296 TiXmlOutStream & operator << (constchar* in)
00297 {
00298 *this+= in;
00299return*this;
00300 }
00301 
00302 } ;
00303 
00304#endif // TIXML\_STRING\_INCLUDED00305#endif // TIXML\_USE\_STL

Generated by 1.6.2