Debian Codegraph @

src/tinyxml/tinystr.h

raw symbols SCIP
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
37 #ifndef TIXML_USE_STL
38
39 #ifndef TIXML_STRING_INCLUDED
40 #define TIXML_STRING_INCLUDED
41
42 #include <cassert>
43 #include <string>
44 #include <cstring> // Added by neoneurone on 27th november, 2007
45
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:
49 */
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
56 #else
57 #define TIXML_EXPLICIT
58 #endif
59
60
61 /*
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.
67 */
68 class TiXmlString
69 {
70 public :
71 // The size type used
72 typedef size_t size_type;
73
74 // Error value for find primitive
75 static const size_type npos; // = -1;
76
77
78 // TiXmlString empty constructor
79 TiXmlString () : rep_(&nullrep_)
80 {
81 }
82
83 // TiXmlString copy constructor
84 TiXmlString ( const TiXmlString & copy) : rep_(0)
85 {
86 init(copy.length());
87 memcpy(start(), copy.data(), length());
88 }
89
90 // TiXmlString constructor, based on a string
91 TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
92 {
93 init( static_cast<size_type>( strlen(copy) ));
94 memcpy(start(), copy, length());
95 }
96
97 // TiXmlString constructor, based on a string
98 TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
99 {
100 init(len);
101 memcpy(start(), str, len);
102 }
103
104 // TiXmlString destructor
105 ~TiXmlString ()
106 {
107 quit();
108 }
109
110 // = operator
111 TiXmlString& operator = (const char * copy)
112 {
113 return assign( copy, (size_type)strlen(copy));
114 }
115
116 // = operator
117 TiXmlString& operator = (const TiXmlString & copy)
118 {
119 return assign(copy.start(), copy.length());
120 }
121
122
123 // += operator. Maps to append
124 TiXmlString& operator += (const char * suffix)
125 {
126 return append(suffix, static_cast<size_type>( strlen(suffix) ));
127 }
128
129 // += operator. Maps to append
130 TiXmlString& operator += (char single)
131 {
132 return append(&single, 1);
133 }
134
135 // += operator. Maps to append
136 TiXmlString& operator += (const TiXmlString & suffix)
137 {
138 return append(suffix.data(), suffix.length());
139 }
140
141
142 // Convert a TiXmlString into a null-terminated char *
143 const char * c_str () const { return rep_->str; }
144
145 // Convert a TiXmlString into a char * (need not be null terminated).
146 const char * data () const { return rep_->str; }
147
148 // Return the length of a TiXmlString
149 size_type length () const { return rep_->size; }
150
151 // Alias for length()
152 size_type size () const { return rep_->size; }
153
154 // Checks if a TiXmlString is empty
155 bool empty () const { return rep_->size == 0; }
156
157 // Return capacity of string
158 size_type capacity () const { return rep_->capacity; }
159
160
161 // single char extraction
162 const char& at (size_type index) const
163 {
164 assert( index < length() );
165 return rep_->str[ index ];
166 }
167
168 // [] operator
169 char& operator [] (size_type index) const
170 {
171 assert( index < length() );
172 return rep_->str[ index ];
173 }
174
175 // find a char in a string. Return TiXmlString::npos if not found
176 size_type find (char lookup) const
177 {
178 return find(lookup, 0);
179 }
180
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
183 {
184 if (offset >= length()) return npos;
185
186 for (const char* p = c_str() + offset; *p != '\0'; ++p)
187 {
188 if (*p == tofind) return static_cast< size_type >( p - c_str() );
189 }
190 return npos;
191 }
192
193 void clear ()
194 {
195 //Lee:
196 //The original was just too strange, though correct:
197 // TiXmlString().swap(*this);
198 //Instead use the quit & re-init:
199 quit();
200 init(0,0);
201 }
202
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.
205 */
206 void reserve (size_type cap);
207
208 TiXmlString& assign (const char* str, size_type len);
209
210 TiXmlString& append (const char* str, size_type len);
211
212 void swap (TiXmlString& other)
213 {
214 Rep* r = rep_;
215 rep_ = other.rep_;
216 other.rep_ = r;
217 }
218
219 private:
220
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; }
225
226 struct Rep
227 {
228 size_type size, capacity;
229 char str[1];
230 };
231
232 void init(size_type sz, size_type cap)
233 {
234 if (cap)
235 {
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 ] );
244
245 rep_->str[ rep_->size = sz ] = '\0';
246 rep_->capacity = cap;
247 }
248 else
249 {
250 rep_ = &nullrep_;
251 }
252 }
253
254 void quit()
255 {
256 if (rep_ != &nullrep_)
257 {
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_ ) );
261 }
262 }
263
264 Rep * rep_;
265 static Rep nullrep_;
266
267 } ;
268
269
270 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
271 {
272 return ( a.length() == b.length() ) // optimization on some platforms
273 && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
274 }
275 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
276 {
277 return strcmp(a.c_str(), b.c_str()) < 0;
278 }
279
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); }
284
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); }
289
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);
293
294
295 /*
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.
298 */
299 class TiXmlOutStream : public TiXmlString
300 {
301 public :
302
303 // TiXmlOutStream << operator.
304 TiXmlOutStream & operator << (const TiXmlString & in)
305 {
306 *this += in;
307 return *this;
308 }
309
310 // TiXmlOutStream << operator.
311 TiXmlOutStream & operator << (const char * in)
312 {
313 *this += in;
314 return *this;
315 }
316
317 } ;
318
319 #endif // TIXML_STRING_INCLUDED
320 #endif // TIXML_USE_STL