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