zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tinystr.h
Go to the documentation of this file.
1 /*
2 www.sourceforge.net/projects/tinyxml
3 Original file by Yves Berquin.
4 
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.
8 
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:
12 
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.
17 
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20 
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24 
25 /*
26  * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
27  *
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+().
35  */
36 
37 #ifndef TIXML_USE_STL
38 
39 #ifndef TIXML_STRING_INCLUDED
40 #define TIXML_STRING_INCLUDED
41 
42 #include <assert.h>
43 #include <string.h>
44 
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:
48 */
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
58 
59 /*
60  TiXmlString is an emulation of a subset of the std::string template.
61  Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
62  Only the member functions relevant to the TinyXML project have been implemented.
63  The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
64  a string and there's no more room, we allocate a buffer twice as big as we need.
65 */
66 class TINYXML_DLL TiXmlString
67 {
68  public :
69  // The size type used
70  typedef size_t size_type;
71 
72  // Error value for find primitive
73  static const size_type npos; // = -1;
74 
75 
76  // TiXmlString empty constructor
77  TiXmlString () : rep_(&nullrep_)
78  {
79  }
80 
81  // TiXmlString copy constructor
82  TiXmlString ( const TiXmlString & copy) : rep_(0)
83  {
84  init(copy.length());
85  memcpy(start(), copy.data(), length());
86  }
87 
88  // TiXmlString constructor, based on a string
89  TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
90  {
91  init( static_cast<size_type>( strlen(copy) ));
92  memcpy(start(), copy, length());
93  }
94 
95  // TiXmlString constructor, based on a string
96  TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
97  {
98  init(len);
99  memcpy(start(), str, len);
100  }
101 
102  // TiXmlString destructor
104  {
105  quit();
106  }
107 
108  // = operator
109  TiXmlString& operator = (const char * copy)
110  {
111  return assign( copy, (size_type)strlen(copy));
112  }
113 
114  // = operator
115  TiXmlString& operator = (const TiXmlString & copy)
116  {
117  return assign(copy.start(), copy.length());
118  }
119 
120 
121  // += operator. Maps to append
122  TiXmlString& operator += (const char * suffix)
123  {
124  return append(suffix, static_cast<size_type>( strlen(suffix) ));
125  }
126 
127  // += operator. Maps to append
128  TiXmlString& operator += (char single)
129  {
130  return append(&single, 1);
131  }
132 
133  // += operator. Maps to append
134  TiXmlString& operator += (const TiXmlString & suffix)
135  {
136  return append(suffix.data(), suffix.length());
137  }
138 
139 
140  // Convert a TiXmlString into a null-terminated char *
141  const char * c_str () const { return rep_->str; }
142 
143  // Convert a TiXmlString into a char * (need not be null terminated).
144  const char * data () const { return rep_->str; }
145 
146  // Return the length of a TiXmlString
147  size_type length () const { return rep_->size; }
148 
149  // Alias for length()
150  size_type size () const { return rep_->size; }
151 
152  // Checks if a TiXmlString is empty
153  bool empty () const { return rep_->size == 0; }
154 
155  // Return capacity of string
156  size_type capacity () const { return rep_->capacity; }
157 
158 
159  // single char extraction
160  const char& at (size_type index) const
161  {
162  assert( index < length() );
163  return rep_->str[ index ];
164  }
165 
166  // [] operator
167  char& operator [] (size_type index) const
168  {
169  assert( index < length() );
170  return rep_->str[ index ];
171  }
172 
173  // find a char in a string. Return TiXmlString::npos if not found
174  size_type find (char lookup) const
175  {
176  return find(lookup, 0);
177  }
178 
179  // find a char in a string from an offset. Return TiXmlString::npos if not found
180  size_type find (char tofind, size_type offset) const
181  {
182  if (offset >= length()) return npos;
183 
184  for (const char* p = c_str() + offset; *p != '\0'; ++p)
185  {
186  if (*p == tofind) return static_cast< size_type >( p - c_str() );
187  }
188  return npos;
189  }
190 
191  void clear ()
192  {
193  //Lee:
194  //The original was just too strange, though correct:
195  // TiXmlString().swap(*this);
196  //Instead use the quit & re-init:
197  quit();
198  init(0,0);
199  }
200 
201  /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
202  function DOES NOT clear the content of the TiXmlString if any exists.
203  */
204  void reserve (size_type cap);
205 
206  TiXmlString& assign (const char* str, size_type len);
207 
208  TiXmlString& append (const char* str, size_type len);
209 
210  void swap (TiXmlString& other)
211  {
212  Rep* r = rep_;
213  rep_ = other.rep_;
214  other.rep_ = r;
215  }
216 
217  private:
218 
219  void init(size_type sz) { init(sz, sz); }
220  void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
221  char* start() const { return rep_->str; }
222  char* finish() const { return rep_->str + rep_->size; }
223 
224  struct Rep
225  {
226  size_type size, capacity;
227  char str[1];
228  };
229 
230  void init(size_type sz, size_type cap)
231  {
232  if (cap)
233  {
234  // Lee: the original form:
235  // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
236  // doesn't work in some cases of new being overloaded. Switching
237  // to the normal allocation, although use an 'int' for systems
238  // that are overly picky about structure alignment.
239  const size_type bytesNeeded = sizeof(Rep) + cap;
240  const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
241  rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
242 
243  rep_->str[ rep_->size = sz ] = '\0';
244  rep_->capacity = cap;
245  }
246  else
247  {
248  rep_ = &nullrep_;
249  }
250  }
251 
252  void quit()
253  {
254  if (rep_ != &nullrep_)
255  {
256  // The rep_ is really an array of ints. (see the allocator, above).
257  // Cast it back before delete, so the compiler won't incorrectly call destructors.
258  delete [] ( reinterpret_cast<int*>( rep_ ) );
259  }
260  }
261 
262  Rep * rep_;
263  static Rep nullrep_;
264 
265 } ;
266 
267 
268 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
269 {
270  return ( a.length() == b.length() ) // optimization on some platforms
271  && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
272 }
273 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
274 {
275  return strcmp(a.c_str(), b.c_str()) < 0;
276 }
277 
278 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
279 inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
280 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
281 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
282 
283 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
284 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
285 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
286 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
287 
289 TiXmlString operator + (const TiXmlString & a, const char* b);
290 TiXmlString operator + (const char* a, const TiXmlString & b);
291 
292 
293 /*
294  TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
295  Only the operators that we need for TinyXML have been developped.
296 */
297 class TINYXML_DLL TiXmlOutStream : public TiXmlString
298 {
299 public :
300 
301  // TiXmlOutStream << operator.
303  {
304  *this += in;
305  return *this;
306  }
307 
308  // TiXmlOutStream << operator.
310  {
311  *this += in;
312  return *this;
313  }
314 
315 } ;
316 
317 #endif // TIXML_STRING_INCLUDED
318 #endif // TIXML_USE_STL
size_type find(char lookup) const
Definition: tinystr.h:174
const char & at(size_type index) const
Definition: tinystr.h:160
~TiXmlString()
Definition: tinystr.h:103
bool empty() const
Definition: tinystr.h:153
bool operator>(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:279
TiXmlString()
Definition: tinystr.h:77
TIXML_EXPLICIT TiXmlString(const char *copy)
Definition: tinystr.h:89
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:273
GLuint start
Definition: glew.h:1239
bool operator==(const SourceLocation &lhs, const SourceLocation &rhs)
size_type length() const
Definition: tinystr.h:147
void swap(TiXmlString &other)
Definition: tinystr.h:210
static void init(struct bs2b *bs2b)
Definition: bs2b.c:46
GLuint in
Definition: glew.h:10672
TiXmlString operator+(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.cpp:86
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
const char * c_str() const
Definition: tinystr.h:141
#define assert(x)
Definition: SDL_malloc.c:1234
GLenum GLsizei len
Definition: glew.h:7035
size_type size() const
Definition: tinystr.h:150
size_type find(char tofind, size_type offset) const
Definition: tinystr.h:180
size_t size_type
Definition: tinystr.h:70
size_type capacity() const
Definition: tinystr.h:156
GLenum cap
Definition: glew.h:10666
void clear()
Definition: tinystr.h:191
int
Definition: SDL_systhread.c:37
GLsizei GLsizei * length
Definition: gl2ext.h:792
GLfloat GLfloat p
Definition: glew.h:14938
GLuint index
Definition: glew.h:1800
TiXmlString(const TiXmlString &copy)
Definition: tinystr.h:82
bool operator!=(const SourceLocation &lhs, const SourceLocation &rhs)
std::ostream & operator<<(std::ostream &out, const Token &token)
Definition: Token.cpp:74
static const size_type npos
Definition: tinystr.h:73
bool operator>=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:281
GLintptr offset
Definition: glew.h:1668
const char * data() const
Definition: tinystr.h:144
#define memcpy
Definition: SDL_malloc.c:634
GLdouble GLdouble GLdouble r
Definition: glew.h:1392
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
#define str(s)
#define TIXML_EXPLICIT
Definition: tinystr.h:56
bool operator<=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:280
TIXML_EXPLICIT TiXmlString(const char *str, size_type len)
Definition: tinystr.h:96
GLsizei size
Definition: gl2ext.h:1467