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