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


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Thu Sep 8 2022 02:30:19