Game Develop C++ Platform: tinystr.h Source File

| Game Develop C++ Platform Platform for developing native games based on OpenGL and SFML with Game Develop

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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