Nebula
Loading...
Searching...
No Matches
tinystr.h
Go to the documentation of this file.
1/*
2www.sourceforge.net/projects/tinyxml
3Original file by Yves Berquin.
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any
7damages arising from the use of this software.
8
9Permission is granted to anyone to use this software for any
10purpose, including commercial applications, and to alter it and
11redistribute it freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must
14not claim that you wrote the original software. If you use this
15software in a product, an acknowledgment in the product documentation
16would be appreciated but is not required.
17
182. Altered source versions must be plainly marked as such, and
19must not be misrepresented as being the original software.
20
213. This notice may not be removed or altered from any source
22distribution.
23*/
24
25/*
26 * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
27 *
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+().
35 */
36
37#ifndef TIXML_USE_STL
38
39#ifndef TIXML_STRING_INCLUDED
40#define TIXML_STRING_INCLUDED
41
42#include <assert.h>
43#include <string.h>
44
45// [mse] new/delete operator problem in programming configuration
46//#include "memory/memory.h"
47
48/* The support for explicit isn't that universal, and it isn't really
49 required - it is used to check that the TiXmlString class isn't incorrectly
50 used. Be nice to old compilers and macro it here:
51*/
52#if defined(_MSC_VER) && (_MSC_VER >= 1200 )
53 // Microsoft visual studio, version 6 and higher.
54 #define TIXML_EXPLICIT explicit
55#elif defined(__GNUC__) && (__GNUC__ >= 3 )
56 // GCC version 3 and higher.s
57 #define TIXML_EXPLICIT explicit
58#else
59 #define TIXML_EXPLICIT
60#endif
61
62
63/*
64 TiXmlString is an emulation of a subset of the std::string template.
65 Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
66 Only the member functions relevant to the TinyXML project have been implemented.
67 The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
68 a string and there's no more room, we allocate a buffer twice as big as we need.
69*/
71{
72 public :
73 // The size type used
74 typedef size_t size_type;
75
76 // Error value for find primitive
77 static const size_type npos; // = -1;
78
79
80 // TiXmlString empty constructor
82 {
83 }
84
85 // TiXmlString copy constructor
86 TiXmlString ( const TiXmlString & copy)
87 {
88 init(copy.length());
89 memcpy(start(), copy.data(), length());
90 }
91
92 // TiXmlString constructor, based on a string
93 TIXML_EXPLICIT TiXmlString ( const char * copy)
94 {
95 init( static_cast<size_type>( strlen(copy) ));
96 memcpy(start(), copy, length());
97 }
98
99 // TiXmlString constructor, based on a string
100 TIXML_EXPLICIT TiXmlString ( const char * str, size_type len)
101 {
102 init(len);
103 memcpy(start(), str, len);
104 }
105
106 // TiXmlString destructor
108 {
109 quit();
110 }
111
112 // = operator
113 TiXmlString& operator = (const char * copy)
114 {
115 return assign( copy, (size_type)strlen(copy));
116 }
117
118 // = operator
120 {
121 return assign(copy.start(), copy.length());
122 }
123
124
125 // += operator. Maps to append
126 TiXmlString& operator += (const char * suffix)
127 {
128 return append(suffix, static_cast<size_type>( strlen(suffix) ));
129 }
130
131 // += operator. Maps to append
133 {
134 return append(&single, 1);
135 }
136
137 // += operator. Maps to append
139 {
140 return append(suffix.data(), suffix.length());
141 }
142
143
144 // Convert a TiXmlString into a null-terminated char *
145 const char * c_str () const { return rep_->str; }
146
147 // Convert a TiXmlString into a char * (need not be null terminated).
148 const char * data () const { return rep_->str; }
149
150 // Return the length of a TiXmlString
151 size_type length () const { return rep_->size; }
152
153 // Alias for length()
154 size_type size () const { return rep_->size; }
155
156 // Checks if a TiXmlString is empty
157 bool empty () const { return rep_->size == 0; }
158
159 // Return capacity of string
160 size_type capacity () const { return rep_->capacity; }
161
162
163 // single char extraction
164 const char& at (size_type index) const
165 {
166 assert( index < length() );
167 return rep_->str[ index ];
168 }
169
170 // [] operator
171 char& operator [] (size_type index) const
172 {
173 assert( index < length() );
174 return rep_->str[ index ];
175 }
176
177 // find a char in a string. Return TiXmlString::npos if not found
178 size_type find (char lookup) const
179 {
180 return find(lookup, 0);
181 }
182
183 // find a char in a string from an offset. Return TiXmlString::npos if not found
184 size_type find (char tofind, size_type offset) const
185 {
186 if (offset >= length()) return npos;
187
188 for (const char* p = c_str() + offset; *p != '\0'; ++p)
189 {
190 if (*p == tofind) return static_cast< size_type >( p - c_str() );
191 }
192 return npos;
193 }
194
195 void clear ()
196 {
197 //Lee:
198 //The original was just too strange, though correct:
199 // TiXmlString().swap(*this);
200 //Instead use the quit & re-init:
201 quit();
202 init(0,0);
203 }
204
205 /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
206 function DOES NOT clear the content of the TiXmlString if any exists.
207 */
208 void reserve (size_type cap);
209
210 TiXmlString& assign (const char* str, size_type len);
211
212 TiXmlString& append (const char* str, size_type len);
213
214 void swap (TiXmlString& other)
215 {
216 Rep* r = rep_;
217 rep_ = other.rep_;
218 other.rep_ = r;
219 }
220
221 private:
222
223 void init(size_type sz) { init(sz, sz); }
224 void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
225 char* start() const { return rep_->str; }
226 char* finish() const { return rep_->str + rep_->size; }
227
228 struct Rep
229 {
231 char str[1];
232 };
233
235 {
236 if (cap)
237 {
238 // Lee: the original form:
239 // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
240 // doesn't work in some cases of new being overloaded. Switching
241 // to the normal allocation, although use an 'int' for systems
242 // that are overly picky about structure alignment.
243 const size_type bytesNeeded = sizeof(Rep) + cap;
244 const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
245 rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
246
247 rep_->str[ rep_->size = sz ] = '\0';
248 rep_->capacity = cap;
249 }
250 else
251 {
252 rep_ = &nullrep_;
253 }
254 }
255
256 void quit()
257 {
258 if (rep_ != &nullrep_)
259 {
260 // The rep_ is really an array of ints. (see the allocator, above).
261 // Cast it back before delete, so the compiler won't incorrectly call destructors.
262 delete [] ( reinterpret_cast<int*>( rep_ ) );
263 }
264 }
265
267 static Rep nullrep_;
268
269} ;
270
271
272inline bool operator == (const TiXmlString & a, const TiXmlString & b)
273{
274 return ( a.length() == b.length() ) // optimization on some platforms
275 && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
276}
277inline bool operator < (const TiXmlString & a, const TiXmlString & b)
278{
279 return strcmp(a.c_str(), b.c_str()) < 0;
280}
281
282inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
283inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
284inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
285inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
286
287inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
288inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
289inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
290inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
291
292TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
293TiXmlString operator + (const TiXmlString & a, const char* b);
294TiXmlString operator + (const char* a, const TiXmlString & b);
295
296
297/*
298 TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
299 Only the operators that we need for TinyXML have been developped.
300*/
302{
303public :
304
305 // TiXmlOutStream << operator.
307 {
308 *this += in;
309 return *this;
310 }
311
312 // TiXmlOutStream << operator.
313 TiXmlOutStream & operator << (const char * in)
314 {
315 *this += in;
316 return *this;
317 }
318
319} ;
320
321#endif // TIXML_STRING_INCLUDED
322#endif // TIXML_USE_STL
Definition tinystr.h:302
TiXmlOutStream & operator<<(const TiXmlString &in)
Definition tinystr.h:306
Definition tinystr.h:71
size_type capacity() const
Definition tinystr.h:160
const char * data() const
Definition tinystr.h:148
size_type find(char lookup) const
Definition tinystr.h:178
size_type find(char tofind, size_type offset) const
Definition tinystr.h:184
bool empty() const
Definition tinystr.h:157
TiXmlString()
Definition tinystr.h:81
size_t size_type
Definition tinystr.h:74
size_type size() const
Definition tinystr.h:154
TIXML_EXPLICIT TiXmlString(const char *str, size_type len)
Definition tinystr.h:100
void set_size(size_type sz)
Definition tinystr.h:224
size_type length() const
Definition tinystr.h:151
void init(size_type sz)
Definition tinystr.h:223
const char & at(size_type index) const
Definition tinystr.h:164
TiXmlString & operator+=(const char *suffix)
Definition tinystr.h:126
~TiXmlString()
Definition tinystr.h:107
char * start() const
Definition tinystr.h:225
const char * c_str() const
Definition tinystr.h:145
void reserve(size_type cap)
Definition tinystr.cc:40
static const size_type npos
Definition tinystr.h:77
void swap(TiXmlString &other)
Definition tinystr.h:214
TIXML_EXPLICIT TiXmlString(const char *copy)
Definition tinystr.h:93
TiXmlString & operator=(const char *copy)
Definition tinystr.h:113
void quit()
Definition tinystr.h:256
void clear()
Definition tinystr.h:195
TiXmlString & assign(const char *str, size_type len)
Definition tinystr.cc:52
Rep * rep_
Definition tinystr.h:266
TiXmlString(const TiXmlString &copy)
Definition tinystr.h:86
TiXmlString & append(const char *str, size_type len)
Definition tinystr.cc:71
char * finish() const
Definition tinystr.h:226
char & operator[](size_type index) const
Definition tinystr.h:171
void init(size_type sz, size_type cap)
Definition tinystr.h:234
static Rep nullrep_
Definition tinystr.h:37
Definition tinystr.h:229
size_type size
Definition tinystr.h:230
char str[1]
Definition tinystr.h:231
size_type capacity
Definition tinystr.h:230
bool operator>(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:283
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:277
TiXmlString operator+(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.cc:84
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:272
bool operator<=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:284
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:282
bool operator>=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:285
#define TIXML_EXPLICIT
Definition tinystr.h:59