Game Develop Core: tinystr.h Source File

| Game Develop Core Core library for developing platforms and tools compatible with Game Develop.

- Main Page
- Modules
- Classes

- File List
- File Members

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