* Path for renames during restore and renames during share (thanks to Bryan Aldrich...
[vss2svn.git] / ssphys / utils / tinystr.h
blob6e3b9991e9bf61302279af75b488434719127d65
1 /*
2 www.sourceforge.net/projects/tinyxml
3 Original file by Yves Berquin.
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.
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:
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.
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source
22 distribution.
26 * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
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+().
37 #ifndef TIXML_USE_STL
39 #ifndef TIXML_STRING_INCLUDED
40 #define TIXML_STRING_INCLUDED
42 #include <assert.h>
43 #include <string.h>
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:
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
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.
67 class TiXmlString
69 public :
70 // The size type used
71 typedef size_t size_type;
73 // Error value for find primitive
74 static const size_type npos; // = -1;
77 // TiXmlString empty constructor
78 TiXmlString () : rep_(&nullrep_)
82 // TiXmlString copy constructor
83 TiXmlString ( const TiXmlString & copy)
85 init(copy.length());
86 memcpy(start(), copy.data(), length());
89 // TiXmlString constructor, based on a string
90 TIXML_EXPLICIT TiXmlString ( const char * copy)
92 init( static_cast<size_type>( strlen(copy) ));
93 memcpy(start(), copy, length());
96 // TiXmlString constructor, based on a string
97 TIXML_EXPLICIT TiXmlString ( const char * str, size_type len)
99 init(len);
100 memcpy(start(), str, len);
103 // TiXmlString destructor
104 ~TiXmlString ()
106 quit();
109 // = operator
110 TiXmlString& operator = (const char * copy)
112 return assign( copy, (size_type)strlen(copy));
115 // = operator
116 TiXmlString& operator = (const TiXmlString & copy)
118 return assign(copy.start(), copy.length());
122 // += operator. Maps to append
123 TiXmlString& operator += (const char * suffix)
125 return append(suffix, static_cast<size_type>( strlen(suffix) ));
128 // += operator. Maps to append
129 TiXmlString& operator += (char single)
131 return append(&single, 1);
134 // += operator. Maps to append
135 TiXmlString& operator += (const TiXmlString & suffix)
137 return append(suffix.data(), suffix.length());
141 // Convert a TiXmlString into a null-terminated char *
142 const char * c_str () const { return rep_->str; }
144 // Convert a TiXmlString into a char * (need not be null terminated).
145 const char * data () const { return rep_->str; }
147 // Return the length of a TiXmlString
148 size_type length () const { return rep_->size; }
150 // Alias for length()
151 size_type size () const { return rep_->size; }
153 // Checks if a TiXmlString is empty
154 bool empty () const { return rep_->size == 0; }
156 // Return capacity of string
157 size_type capacity () const { return rep_->capacity; }
160 // single char extraction
161 const char& at (size_type index) const
163 assert( index < length() );
164 return rep_->str[ index ];
167 // [] operator
168 char& operator [] (size_type index) const
170 assert( index < length() );
171 return rep_->str[ index ];
174 // find a char in a string. Return TiXmlString::npos if not found
175 size_type find (char lookup) const
177 return find(lookup, 0);
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
183 if (offset >= length()) return npos;
185 for (const char* p = c_str() + offset; *p != '\0'; ++p)
187 if (*p == tofind) return static_cast< size_type >( p - c_str() );
189 return npos;
192 void clear ()
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);
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.
205 void reserve (size_type cap);
207 TiXmlString& assign (const char* str, size_type len);
209 TiXmlString& append (const char* str, size_type len);
211 void swap (TiXmlString& other)
213 Rep* r = rep_;
214 rep_ = other.rep_;
215 other.rep_ = r;
218 private:
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; }
225 struct Rep
227 size_type size, capacity;
228 char str[1];
231 void init(size_type sz, size_type cap)
233 if (cap)
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 ] );
244 rep_->str[ rep_->size = sz ] = '\0';
245 rep_->capacity = cap;
247 else
249 rep_ = &nullrep_;
253 void quit()
255 if (rep_ != &nullrep_)
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_ ) );
263 Rep * rep_;
264 static Rep nullrep_;
269 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
271 return ( a.length() == b.length() ) // optimization on some platforms
272 && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
274 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
276 return strcmp(a.c_str(), b.c_str()) < 0;
279 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
280 inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
281 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
282 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
284 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
285 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
286 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
287 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
289 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
290 TiXmlString operator + (const TiXmlString & a, const char* b);
291 TiXmlString operator + (const char* a, const TiXmlString & b);
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.
298 class TiXmlOutStream : public TiXmlString
300 public :
302 // TiXmlOutStream << operator.
303 TiXmlOutStream & operator << (const TiXmlString & in)
305 *this += in;
306 return *this;
309 // TiXmlOutStream << operator.
310 TiXmlOutStream & operator << (const char * in)
312 *this += in;
313 return *this;
318 #endif // TIXML_STRING_INCLUDED
319 #endif // TIXML_USE_STL