Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mac_osx2/src/external/tinyxml/tinystr.h @ 10256

Last change on this file since 10256 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

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