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