Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/parser/tinyxml/tinystr.h @ 8105

Last change on this file since 8105 was 5819, checked in by bensch, 19 years ago

orxonox/trunk: merged branches world_entities to trunk again
merged with command
svn merge -r5795:HEAD branches/world_entities/ trunk/
no conflicts (what a wonder)

File size: 8.1 KB
Line 
1/*
2www.sourceforge.net/projects/tinyxml
3Original file by Yves Berquin.
4
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
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 <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
56        typedef unsigned int size_type;
57
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
68        TiXmlString (const TiXmlString & copy)
69        {
70                init(copy.length());
71                memcpy(start(), copy.data(), length());
72        }
73
74        // TiXmlString constructor, based on a string
75        TiXmlString (const char * copy)
76        {
77                init( static_cast<size_type>( strlen(copy) ));
78                memcpy(start(), copy, length());
79        }
80
81        // TiXmlString constructor, based on a string
82        TiXmlString (const char * str, size_type len)
83        {
84                init(len);
85                memcpy(start(), str, len);
86        }
87
88        // TiXmlString destructor
89        ~TiXmlString ()
90        {
91                quit();
92        }
93
94        // = operator
95        TiXmlString& operator = (const char * copy)
96        {
97                return assign( copy, (size_type)strlen(copy));
98        }
99
100        // = operator
101        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                {
220                        // Lee: the original form:
221                        //      rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
222                        // doesn't work in some cases of new being overloaded. Switching
223                        // to the normal allocation, although use an 'int' for systems
224                        // that are overly picky about structure alignment.
225                        const size_type bytesNeeded = sizeof(Rep) + cap;
226                        const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int ); 
227                        rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
228
229                        rep_->str[ rep_->size = sz ] = '\0';
230                        rep_->capacity = cap;
231                }
232                else
233                {
234                        rep_ = &nullrep_;
235                }
236        }
237
238        void quit()
239        {
240                if (rep_ != &nullrep_)
241                {
242                        // The rep_ is really an array of ints. (see the allocator, above).
243                        // Cast it back before delete, so the compiler won't incorrectly call destructors.
244                        delete [] ( reinterpret_cast<int*>( rep_ ) );
245                }
246        }
247
248        Rep * rep_;
249        static Rep nullrep_;
250
251} ;
252
253
254inline bool operator == (const TiXmlString & a, const TiXmlString & b)
255{
256        return    ( a.length() == b.length() )                          // optimization on some platforms
257               && ( strcmp(a.c_str(), b.c_str()) == 0 );        // actual compare
258}
259inline bool operator < (const TiXmlString & a, const TiXmlString & b)
260{
261        return strcmp(a.c_str(), b.c_str()) < 0;
262}
263
264inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
265inline bool operator >  (const TiXmlString & a, const TiXmlString & b) { return b < a; }
266inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
267inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
268
269inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
270inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
271inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
272inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
273
274TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
275TiXmlString operator + (const TiXmlString & a, const char* b);
276TiXmlString operator + (const char* a, const TiXmlString & b);
277
278
279/*
280   TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
281   Only the operators that we need for TinyXML have been developped.
282*/
283class TiXmlOutStream : public TiXmlString
284{
285public :
286
287        // TiXmlOutStream << operator.
288        TiXmlOutStream & operator << (const TiXmlString & in)
289        {
290                *this += in;
291                return *this;
292        }
293
294        // TiXmlOutStream << operator.
295        TiXmlOutStream & operator << (const char * in)
296        {
297                *this += in;
298                return *this;
299        }
300
301} ;
302
303#endif  // TIXML_STRING_INCLUDED
304#endif  // TIXML_USE_STL
Note: See TracBrowser for help on using the repository browser.