blob: aedd2f9055d468dec2d0dadc202a2ba7a182bcb4 [file] [log] [blame]
The Android Open Source Project562be062009-03-03 19:30:48 -08001/*
2www.sourceforge.net/projects/tinyxml
Dan Albert0238a202014-08-22 00:52:41 +00003Original file by Yves Berquin.
The Android Open Source Project562be062009-03-03 19:30:48 -08004
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
Dan Albert0238a202014-08-22 00:52:41 +000025/*
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 */
The Android Open Source Project562be062009-03-03 19:30:48 -080036
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/*
46 TiXmlString is an emulation of a subset of the std::string template.
47 Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
48 Only the member functions relevant to the TinyXML project have been implemented.
49 The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
50 a string and there's no more room, we allocate a buffer twice as big as we need.
51*/
52class TiXmlString
53{
54 public :
55 // The size type used
Dan Albert0238a202014-08-22 00:52:41 +000056 typedef unsigned int size_type;
The Android Open Source Project562be062009-03-03 19:30:48 -080057
58 // Error value for find primitive
59 static const size_type npos; // = -1;
60
61
62 // TiXmlString empty constructor
63 TiXmlString () : rep_(&nullrep_)
64 {
65 }
66
67 // TiXmlString copy constructor
Dan Albert0238a202014-08-22 00:52:41 +000068 TiXmlString (const TiXmlString & copy)
The Android Open Source Project562be062009-03-03 19:30:48 -080069 {
70 init(copy.length());
71 memcpy(start(), copy.data(), length());
72 }
73
74 // TiXmlString constructor, based on a string
Dan Albert0238a202014-08-22 00:52:41 +000075 TiXmlString (const char * copy)
The Android Open Source Project562be062009-03-03 19:30:48 -080076 {
77 init( static_cast<size_type>( strlen(copy) ));
78 memcpy(start(), copy, length());
79 }
80
81 // TiXmlString constructor, based on a string
Dan Albert0238a202014-08-22 00:52:41 +000082 TiXmlString (const char * str, size_type len)
The Android Open Source Project562be062009-03-03 19:30:48 -080083 {
84 init(len);
85 memcpy(start(), str, len);
86 }
87
88 // TiXmlString destructor
89 ~TiXmlString ()
90 {
91 quit();
92 }
93
Dan Albert0238a202014-08-22 00:52:41 +000094 // = operator
The Android Open Source Project562be062009-03-03 19:30:48 -080095 TiXmlString& operator = (const char * copy)
96 {
97 return assign( copy, (size_type)strlen(copy));
98 }
99
Dan Albert0238a202014-08-22 00:52:41 +0000100 // = operator
The Android Open Source Project562be062009-03-03 19:30:48 -0800101 TiXmlString& operator = (const TiXmlString & copy)
102 {
103 return assign(copy.start(), copy.length());
104 }
105
106
107 // += operator. Maps to append
108 TiXmlString& operator += (const char * suffix)
109 {
110 return append(suffix, static_cast<size_type>( strlen(suffix) ));
111 }
112
113 // += operator. Maps to append
114 TiXmlString& operator += (char single)
115 {
116 return append(&single, 1);
117 }
118
119 // += operator. Maps to append
120 TiXmlString& operator += (const TiXmlString & suffix)
121 {
122 return append(suffix.data(), suffix.length());
123 }
124
125
126 // Convert a TiXmlString into a null-terminated char *
127 const char * c_str () const { return rep_->str; }
128
129 // Convert a TiXmlString into a char * (need not be null terminated).
130 const char * data () const { return rep_->str; }
131
132 // Return the length of a TiXmlString
133 size_type length () const { return rep_->size; }
134
135 // Alias for length()
136 size_type size () const { return rep_->size; }
137
138 // Checks if a TiXmlString is empty
139 bool empty () const { return rep_->size == 0; }
140
141 // Return capacity of string
142 size_type capacity () const { return rep_->capacity; }
143
144
145 // single char extraction
146 const char& at (size_type index) const
147 {
148 assert( index < length() );
149 return rep_->str[ index ];
150 }
151
152 // [] operator
153 char& operator [] (size_type index) const
154 {
155 assert( index < length() );
156 return rep_->str[ index ];
157 }
158
159 // find a char in a string. Return TiXmlString::npos if not found
160 size_type find (char lookup) const
161 {
162 return find(lookup, 0);
163 }
164
165 // find a char in a string from an offset. Return TiXmlString::npos if not found
166 size_type find (char tofind, size_type offset) const
167 {
168 if (offset >= length()) return npos;
169
170 for (const char* p = c_str() + offset; *p != '\0'; ++p)
171 {
172 if (*p == tofind) return static_cast< size_type >( p - c_str() );
173 }
174 return npos;
175 }
176
177 void clear ()
178 {
179 //Lee:
180 //The original was just too strange, though correct:
181 // TiXmlString().swap(*this);
182 //Instead use the quit & re-init:
183 quit();
184 init(0,0);
185 }
186
187 /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
188 function DOES NOT clear the content of the TiXmlString if any exists.
189 */
190 void reserve (size_type cap);
191
192 TiXmlString& assign (const char* str, size_type len);
193
194 TiXmlString& append (const char* str, size_type len);
195
196 void swap (TiXmlString& other)
197 {
198 Rep* r = rep_;
199 rep_ = other.rep_;
200 other.rep_ = r;
201 }
202
203 private:
204
205 void init(size_type sz) { init(sz, sz); }
206 void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
207 char* start() const { return rep_->str; }
208 char* finish() const { return rep_->str + rep_->size; }
209
210 struct Rep
211 {
212 size_type size, capacity;
213 char str[1];
214 };
215
216 void init(size_type sz, size_type cap)
217 {
218 if (cap)
219 {
Dan Albert0238a202014-08-22 00:52:41 +0000220 rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
The Android Open Source Project562be062009-03-03 19:30:48 -0800221 rep_->str[ rep_->size = sz ] = '\0';
222 rep_->capacity = cap;
223 }
224 else
225 {
226 rep_ = &nullrep_;
227 }
228 }
229
230 void quit()
231 {
232 if (rep_ != &nullrep_)
233 {
Dan Albert0238a202014-08-22 00:52:41 +0000234 operator delete(rep_);
The Android Open Source Project562be062009-03-03 19:30:48 -0800235 }
236 }
237
238 Rep * rep_;
239 static Rep nullrep_;
240
241} ;
242
243
244inline bool operator == (const TiXmlString & a, const TiXmlString & b)
245{
246 return ( a.length() == b.length() ) // optimization on some platforms
247 && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
248}
249inline bool operator < (const TiXmlString & a, const TiXmlString & b)
250{
251 return strcmp(a.c_str(), b.c_str()) < 0;
252}
253
254inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
255inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
256inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
257inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
258
259inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
260inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
261inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
262inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
263
264TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
265TiXmlString operator + (const TiXmlString & a, const char* b);
266TiXmlString operator + (const char* a, const TiXmlString & b);
267
268
269/*
270 TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
271 Only the operators that we need for TinyXML have been developped.
272*/
273class TiXmlOutStream : public TiXmlString
274{
275public :
276
277 // TiXmlOutStream << operator.
278 TiXmlOutStream & operator << (const TiXmlString & in)
279 {
280 *this += in;
281 return *this;
282 }
283
284 // TiXmlOutStream << operator.
285 TiXmlOutStream & operator << (const char * in)
286 {
287 *this += in;
288 return *this;
289 }
290
291} ;
292
293#endif // TIXML_STRING_INCLUDED
294#endif // TIXML_USE_STL