2 www.sourceforge.net/projects/tinyxml
3 Original file by Yves Berquin.
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source
26 * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
28 * - completely rewritten. compact, clean, and fast implementation.
29 * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
30 * - fixed reserve() to work as per specification.
31 * - fixed buggy compares operator==(), operator<(), and operator>()
32 * - fixed operator+=() to take a const ref argument, following spec.
33 * - added "copy" constructor with length, and most compare operators.
34 * - added swap(), clear(), size(), capacity(), operator+().
39 #ifndef TIXML_STRING_INCLUDED
40 #define TIXML_STRING_INCLUDED
44 #include <tinyxml/cpPlugins_tinyxml_Export.h>
46 /* The support for explicit isn't that universal, and it isn't really
47 required - it is used to check that the TiXmlString class isn't incorrectly
48 used. Be nice to old compilers and macro it here:
50 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
51 // Microsoft visual studio, version 6 and higher.
52 #define TIXML_EXPLICIT explicit
53 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
54 // GCC version 3 and higher.s
55 #define TIXML_EXPLICIT explicit
57 #define TIXML_EXPLICIT
62 TiXmlString is an emulation of a subset of the std::string template.
63 Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
64 Only the member functions relevant to the TinyXML project have been implemented.
65 The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
66 a string and there's no more room, we allocate a buffer twice as big as we need.
68 class cpPlugins_tinyxml_EXPORT TiXmlString
72 typedef size_t size_type;
74 // Error value for find primitive
75 static const size_type npos; // = -1;
78 // TiXmlString empty constructor
79 TiXmlString () : rep_(&nullrep_)
83 // TiXmlString copy constructor
84 TiXmlString ( const TiXmlString & copy) : rep_(0)
87 memcpy(start(), copy.data(), length());
90 // TiXmlString constructor, based on a string
91 TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
93 init( static_cast<size_type>( strlen(copy) ));
94 memcpy(start(), copy, length());
97 // TiXmlString constructor, based on a string
98 TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
101 memcpy(start(), str, len);
104 // TiXmlString destructor
111 TiXmlString& operator = (const char * copy)
113 return assign( copy, (size_type)strlen(copy));
117 TiXmlString& operator = (const TiXmlString & copy)
119 return assign(copy.start(), copy.length());
123 // += operator. Maps to append
124 TiXmlString& operator += (const char * suffix)
126 return append(suffix, static_cast<size_type>( strlen(suffix) ));
129 // += operator. Maps to append
130 TiXmlString& operator += (char single)
132 return append(&single, 1);
135 // += operator. Maps to append
136 TiXmlString& operator += (const TiXmlString & suffix)
138 return append(suffix.data(), suffix.length());
142 // Convert a TiXmlString into a null-terminated char *
143 const char * c_str () const { return rep_->str; }
145 // Convert a TiXmlString into a char * (need not be null terminated).
146 const char * data () const { return rep_->str; }
148 // Return the length of a TiXmlString
149 size_type length () const { return rep_->size; }
151 // Alias for length()
152 size_type size () const { return rep_->size; }
154 // Checks if a TiXmlString is empty
155 bool empty () const { return rep_->size == 0; }
157 // Return capacity of string
158 size_type capacity () const { return rep_->capacity; }
161 // single char extraction
162 const char& at (size_type index) const
164 assert( index < length() );
165 return rep_->str[ index ];
169 char& operator [] (size_type index) const
171 assert( index < length() );
172 return rep_->str[ index ];
175 // find a char in a string. Return TiXmlString::npos if not found
176 size_type find (char lookup) const
178 return find(lookup, 0);
181 // find a char in a string from an offset. Return TiXmlString::npos if not found
182 size_type find (char tofind, size_type offset) const
184 if (offset >= length()) return npos;
186 for (const char* p = c_str() + offset; *p != '\0'; ++p)
188 if (*p == tofind) return static_cast< size_type >( p - c_str() );
196 //The original was just too strange, though correct:
197 // TiXmlString().swap(*this);
198 //Instead use the quit & re-init:
203 /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
204 function DOES NOT clear the content of the TiXmlString if any exists.
206 void reserve (size_type cap);
208 TiXmlString& assign (const char* str, size_type len);
210 TiXmlString& append (const char* str, size_type len);
212 void swap (TiXmlString& other)
221 void init(size_type sz) { init(sz, sz); }
222 void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
223 char* start() const { return rep_->str; }
224 char* finish() const { return rep_->str + rep_->size; }
228 size_type size, capacity;
232 void init(size_type sz, size_type cap)
236 // Lee: the original form:
237 // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
238 // doesn't work in some cases of new being overloaded. Switching
239 // to the normal allocation, although use an 'int' for systems
240 // that are overly picky about structure alignment.
241 const size_type bytesNeeded = sizeof(Rep) + cap;
242 const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
243 rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
245 rep_->str[ rep_->size = sz ] = '\0';
246 rep_->capacity = cap;
256 if (rep_ != &nullrep_)
258 // The rep_ is really an array of ints. (see the allocator, above).
259 // Cast it back before delete, so the compiler won't incorrectly call destructors.
260 delete [] ( reinterpret_cast<int*>( rep_ ) );
270 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
272 return ( a.length() == b.length() ) // optimization on some platforms
273 && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
275 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
277 return strcmp(a.c_str(), b.c_str()) < 0;
280 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
281 inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
282 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
283 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
285 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
286 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
287 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
288 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
290 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
291 TiXmlString operator + (const TiXmlString & a, const char* b);
292 TiXmlString operator + (const char* a, const TiXmlString & b);
296 TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
297 Only the operators that we need for TinyXML have been developped.
299 class TiXmlOutStream : public TiXmlString
303 // TiXmlOutStream << operator.
304 TiXmlOutStream & operator << (const TiXmlString & in)
310 // TiXmlOutStream << operator.
311 TiXmlOutStream & operator << (const char * in)
319 #endif // TIXML_STRING_INCLUDED
320 #endif // TIXML_USE_STL