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