2 www.sourceforge.net/projects/tinyxml
\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
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
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
17 2. Altered source versions must be plainly marked as such, and
\r
18 must not be misrepresented as being the original software.
\r
20 3. This notice may not be removed or altered from any source
\r
24 #ifndef TIXML_USE_STL
\r
26 #ifndef TIXML_STRING_INCLUDED
\r
27 #define TIXML_STRING_INCLUDED
\r
32 /* The support for explicit isn't that universal, and it isn't really
\r
33 required - it is used to check that the TiXmlString class isn't incorrectly
\r
34 used. Be nice to old compilers and macro it here:
\r
36 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
\r
37 // Microsoft visual studio, version 6 and higher.
\r
38 #define TIXML_EXPLICIT explicit
\r
39 #elif defined(__GNUC__) && (__GNUC__ >= 3)
\r
40 // GCC version 3 and higher.s
\r
41 #define TIXML_EXPLICIT explicit
\r
43 #define TIXML_EXPLICIT
\r
47 TiXmlString is an emulation of a subset of the std::string template.
\r
48 Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
\r
49 Only the member functions relevant to the TinyXML project have been implemented.
\r
50 The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
\r
51 a string and there's no more room, we allocate a buffer twice as big as we need.
\r
56 // The size type used
\r
57 typedef size_t size_type;
\r
59 // Error value for find primitive
\r
60 static const size_type npos; // = -1;
\r
62 // TiXmlString empty constructor
\r
68 // TiXmlString copy constructor
\r
69 TiXmlString(const TiXmlString ©)
\r
72 init(copy.length());
\r
73 memcpy(start(), copy.data(), length());
\r
76 // TiXmlString constructor, based on a string
\r
77 TIXML_EXPLICIT TiXmlString(const char *copy)
\r
80 init(static_cast<size_type>(strlen(copy)));
\r
81 memcpy(start(), copy, length());
\r
84 // TiXmlString constructor, based on a string
\r
85 TIXML_EXPLICIT TiXmlString(const char *str, size_type len)
\r
89 memcpy(start(), str, len);
\r
92 // TiXmlString destructor
\r
98 TiXmlString &operator=(const char *copy)
\r
100 return assign(copy, (size_type)strlen(copy));
\r
103 TiXmlString &operator=(const TiXmlString ©)
\r
105 return assign(copy.start(), copy.length());
\r
108 // += operator. Maps to append
\r
109 TiXmlString &operator+=(const char *suffix)
\r
111 return append(suffix, static_cast<size_type>(strlen(suffix)));
\r
114 // += operator. Maps to append
\r
115 TiXmlString &operator+=(char single)
\r
117 return append(&single, 1);
\r
120 // += operator. Maps to append
\r
121 TiXmlString &operator+=(const TiXmlString &suffix)
\r
123 return append(suffix.data(), suffix.length());
\r
126 // Convert a TiXmlString into a null-terminated char *
\r
127 const char *c_str() const
\r
132 // Convert a TiXmlString into a char * (need not be null terminated).
\r
133 const char *data() const
\r
138 // Return the length of a TiXmlString
\r
139 size_type length() const
\r
144 // Alias for length()
\r
145 size_type size() const
\r
150 // Checks if a TiXmlString is empty
\r
153 return rep_->size == 0;
\r
156 // Return capacity of string
\r
157 size_type capacity() const
\r
159 return rep_->capacity;
\r
162 // single char extraction
\r
163 const char &at(size_type index) const
\r
165 assert(index < length());
\r
166 return rep_->str[index];
\r
170 char &operator[](size_type index) const
\r
172 assert(index < length());
\r
173 return rep_->str[index];
\r
176 // find a char in a string. Return TiXmlString::npos if not found
\r
177 size_type find(char lookup) const
\r
179 return find(lookup, 0);
\r
182 // find a char in a string from an offset. Return TiXmlString::npos if not found
\r
183 size_type find(char tofind, size_type offset) const
\r
185 if (offset >= length())
\r
188 for (const char *p = c_str() + offset; *p != '\0'; ++p)
\r
191 return static_cast<size_type>(p - c_str());
\r
199 //The original was just too strange, though correct:
\r
200 // TiXmlString().swap(*this);
\r
201 //Instead use the quit & re-init:
\r
206 /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
\r
207 function DOES NOT clear the content of the TiXmlString if any exists.
\r
209 void reserve(size_type cap);
\r
211 TiXmlString &assign(const char *str, size_type len);
\r
213 TiXmlString &append(const char *str, size_type len);
\r
215 void swap(TiXmlString &other)
\r
223 void init(size_type sz)
\r
227 void set_size(size_type sz)
\r
229 rep_->str[rep_->size = sz] = '\0';
\r
231 char *start() const
\r
235 char *finish() const
\r
237 return rep_->str + rep_->size;
\r
242 size_type size, capacity;
\r
246 void init(size_type sz, size_type cap)
\r
250 // Lee: the original form:
\r
251 // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
\r
252 // doesn't work in some cases of new being overloaded. Switching
\r
253 // to the normal allocation, although use an 'int' for systems
\r
254 // that are overly picky about structure alignment.
\r
255 const size_type bytesNeeded = sizeof(Rep) + cap;
\r
256 const size_type intsNeeded = (bytesNeeded + sizeof(int) - 1) / sizeof(int);
\r
257 rep_ = reinterpret_cast<Rep *>(new int[intsNeeded]);
\r
259 rep_->str[rep_->size = sz] = '\0';
\r
260 rep_->capacity = cap;
\r
270 if (rep_ != &nullrep_)
\r
272 // The rep_ is really an array of ints. (see the allocator, above).
\r
273 // Cast it back before delete, so the compiler won't incorrectly call destructors.
\r
274 delete[](reinterpret_cast<int *>(rep_));
\r
279 static Rep nullrep_;
\r
282 inline bool operator==(const TiXmlString &a, const TiXmlString &b)
\r
284 return (a.length() == b.length()) // optimization on some platforms
\r
285 && (strcmp(a.c_str(), b.c_str()) == 0); // actual compare
\r
287 inline bool operator<(const TiXmlString &a, const TiXmlString &b)
\r
289 return strcmp(a.c_str(), b.c_str()) < 0;
\r
292 inline bool operator!=(const TiXmlString &a, const TiXmlString &b)
\r
296 inline bool operator>(const TiXmlString &a, const TiXmlString &b)
\r
300 inline bool operator<=(const TiXmlString &a, const TiXmlString &b)
\r
304 inline bool operator>=(const TiXmlString &a, const TiXmlString &b)
\r
309 inline bool operator==(const TiXmlString &a, const char *b)
\r
311 return strcmp(a.c_str(), b) == 0;
\r
313 inline bool operator==(const char *a, const TiXmlString &b)
\r
317 inline bool operator!=(const TiXmlString &a, const char *b)
\r
321 inline bool operator!=(const char *a, const TiXmlString &b)
\r
326 TiXmlString operator+(const TiXmlString &a, const TiXmlString &b);
\r
327 TiXmlString operator+(const TiXmlString &a, const char *b);
\r
328 TiXmlString operator+(const char *a, const TiXmlString &b);
\r
331 TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
\r
332 Only the operators that we need for TinyXML have been developped.
\r
334 class TiXmlOutStream : public TiXmlString
\r
337 // TiXmlOutStream << operator.
\r
338 TiXmlOutStream &operator<<(const TiXmlString &in)
\r
344 // TiXmlOutStream << operator.
\r
345 TiXmlOutStream &operator<<(const char *in)
\r
352 #endif // TIXML_STRING_INCLUDED
\r
353 #endif // TIXML_USE_STL
\r