Orocos Real-Time Toolkit  2.9.0
tinystr.h
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens do nov 2 13:06:00 CET 2006 tinystr.h
3 
4  tinystr.h - description
5  -------------------
6  begin : do november 02 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@gmail.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 /*
40 www.sourceforge.net/projects/tinyxml
41 Original file by Yves Berquin.
42 
43 This software is provided 'as-is', without any express or implied
44 warranty. In no event will the authors be held liable for any
45 damages arising from the use of this software.
46 
47 Permission is granted to anyone to use this software for any
48 purpose, including commercial applications, and to alter it and
49 redistribute it freely, subject to the following restrictions:
50 
51 1. The origin of this software must not be misrepresented; you must
52 not claim that you wrote the original software. If you use this
53 software in a product, an acknowledgment in the product documentation
54 would be appreciated but is not required.
55 
56 2. Altered source versions must be plainly marked as such, and
57 must not be misrepresented as being the original software.
58 
59 3. This notice may not be removed or altered from any source
60 distribution.
61 */
62 
63 /*
64  * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
65  *
66  * - completely rewritten. compact, clean, and fast implementation.
67  * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
68  * - fixed reserve() to work as per specification.
69  * - fixed buggy compares operator==(), operator<(), and operator>()
70  * - fixed operator+=() to take a const ref argument, following spec.
71  * - added "copy" constructor with length, and most compare operators.
72  * - added swap(), clear(), size(), capacity(), operator+().
73  */
74 
75 #ifndef TIXML_USE_STL
76 
77 #ifndef TIXML_STRING_INCLUDED
78 #define TIXML_STRING_INCLUDED
79 
80 #include <assert.h>
81 #include <string.h>
82 
83 /* The support for explicit isn't that universal, and it isn't really
84  required - it is used to check that the TiXmlString class isn't incorrectly
85  used. Be nice to old compilers and macro it here:
86 */
87 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
88  // Microsoft visual studio, version 6 and higher.
89  #define TIXML_EXPLICIT explicit
90 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
91  // GCC version 3 and higher.s
92  #define TIXML_EXPLICIT explicit
93 #else
94  #define TIXML_EXPLICIT
95 #endif
96 
97 namespace RTT { namespace marsh {
98 
99 /*
100  TiXmlString is an emulation of a subset of the std::string template.
101  Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
102  Only the member functions relevant to the TinyXML project have been implemented.
103  The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
104  a string and there's no more room, we allocate a buffer twice as big as we need.
105 */
107 {
108  public :
109  // The size type used
110  typedef size_t size_type;
111 
112  // Error value for find primitive
113  static const size_type npos; // = -1;
114 
115 
116  // TiXmlString empty constructor
117  TiXmlString () : rep_(&nullrep_)
118  {
119  }
120 
121  // TiXmlString copy constructor
122  TiXmlString ( const TiXmlString & copy)
123  {
124  init(copy.length());
125  memcpy(start(), copy.data(), length());
126  }
127 
128  // TiXmlString constructor, based on a string
129  TIXML_EXPLICIT TiXmlString ( const char * copy)
130  {
131  init( static_cast<size_type>( strlen(copy) ));
132  memcpy(start(), copy, length());
133  }
134 
135  // TiXmlString constructor, based on a string
136  TIXML_EXPLICIT TiXmlString ( const char * str, size_type len)
137  {
138  init(len);
139  memcpy(start(), str, len);
140  }
141 
142  // TiXmlString destructor
144  {
145  quit();
146  }
147 
148  // = operator
149  TiXmlString& operator = (const char * copy)
150  {
151  return assign( copy, (size_type)strlen(copy));
152  }
153 
154  // = operator
156  {
157  return assign(copy.start(), copy.length());
158  }
159 
160 
161  // += operator. Maps to append
162  TiXmlString& operator += (const char * suffix)
163  {
164  return append(suffix, static_cast<size_type>( strlen(suffix) ));
165  }
166 
167  // += operator. Maps to append
168  TiXmlString& operator += (char single)
169  {
170  return append(&single, 1);
171  }
172 
173  // += operator. Maps to append
175  {
176  return append(suffix.data(), suffix.length());
177  }
178 
179 
180  // Convert a TiXmlString into a null-terminated char *
181  const char * c_str () const { return rep_->str; }
182 
183  // Convert a TiXmlString into a char * (need not be null terminated).
184  const char * data () const { return rep_->str; }
185 
186  // Return the length of a TiXmlString
187  size_type length () const { return rep_->size; }
188 
189  // Alias for length()
190  size_type size () const { return rep_->size; }
191 
192  // Checks if a TiXmlString is empty
193  bool empty () const { return rep_->size == 0; }
194 
195  // Return capacity of string
196  size_type capacity () const { return rep_->capacity; }
197 
198 
199  // single char extraction
200  const char& at (size_type index) const
201  {
202  assert( index < length() );
203  return rep_->str[ index ];
204  }
205 
206  // [] operator
207  char& operator [] (size_type index) const
208  {
209  assert( index < length() );
210  return rep_->str[ index ];
211  }
212 
213  // find a char in a string. Return TiXmlString::npos if not found
214  size_type find (char lookup) const
215  {
216  return find(lookup, 0);
217  }
218 
219  // find a char in a string from an offset. Return TiXmlString::npos if not found
220  size_type find (char tofind, size_type offset) const
221  {
222  if (offset >= length()) return npos;
223 
224  for (const char* p = c_str() + offset; *p != '\0'; ++p)
225  {
226  if (*p == tofind) return static_cast< size_type >( p - c_str() );
227  }
228  return npos;
229  }
230 
231  void clear ()
232  {
233  //Lee:
234  //The original was just too strange, though correct:
235  // TiXmlString().swap(*this);
236  //Instead use the quit & re-init:
237  quit();
238  init(0,0);
239  }
240 
241  /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
242  function DOES NOT clear the content of the TiXmlString if any exists.
243  */
244  void reserve (size_type cap);
245 
246  TiXmlString& assign (const char* str, size_type len);
247 
248  TiXmlString& append (const char* str, size_type len);
249 
250  void swap (TiXmlString& other)
251  {
252  Rep* r = rep_;
253  rep_ = other.rep_;
254  other.rep_ = r;
255  }
256 
257  private:
258 
259  void init(size_type sz) { init(sz, sz); }
260  void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
261  char* start() const { return rep_->str; }
262  char* finish() const { return rep_->str + rep_->size; }
263 
264  struct Rep
265  {
266  size_type size, capacity;
267  char str[1];
268  };
269 
270  void init(size_type sz, size_type cap)
271  {
272  if (cap)
273  {
274  // Lee: the original form:
275  // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
276  // doesn't work in some cases of new being overloaded. Switching
277  // to the normal allocation, although use an 'int' for systems
278  // that are overly picky about structure alignment.
279  const size_type bytesNeeded = sizeof(Rep) + cap;
280  const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
281  rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
282 
283  rep_->str[ rep_->size = sz ] = '\0';
284  rep_->capacity = cap;
285  }
286  else
287  {
288  rep_ = &nullrep_;
289  }
290  }
291 
292  void quit()
293  {
294  if (rep_ != &nullrep_)
295  {
296  // The rep_ is really an array of ints. (see the allocator, above).
297  // Cast it back before delete, so the compiler won't incorrectly call destructors.
298  delete [] ( reinterpret_cast<int*>( rep_ ) );
299  }
300  }
301 
302  Rep * rep_;
303  static Rep nullrep_;
304 
305 } ;
306 
307 
308 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
309 {
310  return ( a.length() == b.length() ) // optimization on some platforms
311  && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
312 }
313 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
314 {
315  return strcmp(a.c_str(), b.c_str()) < 0;
316 }
317 
318 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
319 inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
320 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
321 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
322 
323 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
324 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
325 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
326 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
327 
328 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
329 TiXmlString operator + (const TiXmlString & a, const char* b);
330 TiXmlString operator + (const char* a, const TiXmlString & b);
331 
332 
333 /*
334  TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
335  Only the operators that we need for TinyXML have been developped.
336 */
338 {
339 public :
340 
341  // TiXmlOutStream << operator.
343  {
344  *this += in;
345  return *this;
346  }
347 
348  // TiXmlOutStream << operator.
349  TiXmlOutStream & operator << (const char * in)
350  {
351  *this += in;
352  return *this;
353  }
354 
355 } ;
356 
357 }}
358 
359 #endif // TIXML_STRING_INCLUDED
360 #endif // TIXML_USE_STL
void swap(TiXmlString &other)
Definition: tinystr.h:250
const char * data() const
Definition: tinystr.h:184
size_type capacity() const
Definition: tinystr.h:196
TIXML_EXPLICIT TiXmlString(const char *copy)
Definition: tinystr.h:129
TiXmlString & operator=(const char *copy)
Definition: tinystr.h:149
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:313
TiXmlString operator+(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.cpp:125
bool operator>=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:321
size_type length() const
Definition: tinystr.h:187
TiXmlString & append(const char *str, size_type len)
Definition: tinystr.cpp:112
TiXmlString & operator+=(const char *suffix)
Definition: tinystr.h:162
char & operator[](size_type index) const
Definition: tinystr.h:207
TIXML_EXPLICIT TiXmlString(const char *str, size_type len)
Definition: tinystr.h:136
bool operator<=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:320
void reserve(size_type cap)
Definition: tinystr.cpp:81
const char * c_str() const
Definition: tinystr.h:181
bool empty() const
Definition: tinystr.h:193
TIXML_OSTREAM & operator<<(TIXML_OSTREAM &out, const TiXmlNode &base)
Definition: tinyxml.cpp:1698
TiXmlString(const TiXmlString &copy)
Definition: tinystr.h:122
bool operator>(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:319
const char & at(size_type index) const
Definition: tinystr.h:200
static const size_type npos
Definition: tinystr.h:113
size_type find(char tofind, size_type offset) const
Definition: tinystr.h:220
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:308
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
#define TIXML_EXPLICIT
Definition: tinystr.h:94
TiXmlString & assign(const char *str, size_type len)
Definition: tinystr.cpp:93
size_type find(char lookup) const
Definition: tinystr.h:214
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:318
size_type size() const
Definition: tinystr.h:190