Chameleon

Chameleon Svn Source Tree

Root/branches/xZenu/src/modules/TinyXML/include/tinystr.h

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

Archive Download this file

Revision: 1307