DataDirector API
tinystr.h
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 #include "tinyxml.h"
26 
27 
28 #ifndef TIXML_USE_STL
29 
30 #ifndef TIXML_STRING_INCLUDED
31 #define TIXML_STRING_INCLUDED
32 
33 #ifdef _MSC_VER
34 #pragma warning( disable : 4530 )
35 #pragma warning( disable : 4786 )
36 #endif
37 
38 #include <assert.h>
39 
40 /*
41  TiXmlString is an emulation of the std::string template.
42  Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
43  Only the member functions relevant to the TinyXML project have been implemented.
44  The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
45  a string and there's no more room, we allocate a buffer twice as big as we need.
46 */
47 class DDD_EXPORT TiXmlString
48 {
49  public :
50  // TiXmlString constructor, based on a string, mark explicit to force
51  // us to find unnecessary casting.
52  explicit TiXmlString (const char * instring);
53 
54  // TiXmlString empty constructor
55  TiXmlString ()
56  {
57  allocated = 0;
58  cstring = NULL;
59  current_length = 0;
60  }
61 
62  // TiXmlString copy constructor
63  explicit TiXmlString (const TiXmlString& copy);
64 
65  // TiXmlString destructor
66  ~ TiXmlString ()
67  {
68  empty_it ();
69  }
70 
71  // Convert a TiXmlString into a classical char *
72  const char * c_str () const
73  {
74  if (allocated)
75  return cstring;
76  return "";
77  }
78 
79  // Return the length of a TiXmlString
80  size_t length () const
81  {
82  return ( allocated ) ? current_length : 0;
83  }
84 
85  // TiXmlString = operator
86  void operator = (const char * content);
87 
88  // = operator
89  void operator = (const TiXmlString & copy);
90 
91  // += operator. Maps to append
92  TiXmlString& operator += (const char * suffix)
93  {
94  append (suffix);
95  return *this;
96  }
97 
98  // += operator. Maps to append
99  TiXmlString& operator += (char single)
100  {
101  append (single);
102  return *this;
103  }
104 
105  // += operator. Maps to append
106  TiXmlString& operator += (TiXmlString & suffix)
107  {
108  append (suffix);
109  return *this;
110  }
111  bool operator == (const TiXmlString & compare) const;
112  bool operator == (const char* compare) const;
113  bool operator < (const TiXmlString & compare) const;
114  bool operator > (const TiXmlString & compare) const;
115 
116  // Checks if a TiXmlString is empty
117  bool empty () const
118  {
119  return length () ? false : true;
120  }
121 
122  // single char extraction
123  const char& at (unsigned index) const
124  {
125  assert( index < length ());
126  return cstring [index];
127  }
128 
129  // find a char in a string. Return TiXmlString::notfound if not found
130  unsigned find (char lookup) const
131  {
132  return find (lookup, 0);
133  }
134 
135  // find a char in a string from an offset. Return TiXmlString::notfound if not found
136  unsigned find (char tofind, unsigned offset) const;
137 
138  /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
139  function clears the content of the TiXmlString if any exists.
140  */
141  void reserve (unsigned size)
142  {
143  empty_it ();
144  if (size)
145  {
146  allocated = size;
147  cstring = new char [size];
148  cstring [0] = 0;
149  current_length = 0;
150  }
151  }
152 
153  // [] operator
154  char& operator [] (unsigned index) const
155  {
156  assert( index < length ());
157  return cstring [index];
158  }
159 
160  // Error value for find primitive
161  enum { notfound = 0xffffffff,
162  npos = notfound };
163 
164  void append (const char *str, size_t len );
165 
166  protected :
167 
168  // The base string
169  char * cstring;
170  // Number of chars allocated
171  size_t allocated;
172  // Current string size
173  size_t current_length;
174 
175  // New size computation. It is simplistic right now : it returns twice the amount
176  // we need
177  size_t assign_new_size (size_t minimum_to_allocate)
178  {
179  return minimum_to_allocate * 2;
180  }
181 
182  // Internal function that clears the content of a TiXmlString
183  void empty_it ()
184  {
185  if (cstring)
186  delete [] cstring;
187  cstring = NULL;
188  allocated = 0;
189  current_length = 0;
190  }
191 
192  void append (const char *suffix );
193 
194  // append function for another TiXmlString
195  void append (const TiXmlString & suffix)
196  {
197  append (suffix . c_str ());
198  }
199 
200  // append for a single char.
201  void append (char single)
202  {
203  if ( cstring && current_length < (allocated-1) )
204  {
205  cstring[ current_length ] = single;
206  ++current_length;
207  cstring[ current_length ] = 0;
208  }
209  else
210  {
211  char smallstr [2];
212  smallstr [0] = single;
213  smallstr [1] = 0;
214  append (smallstr);
215  }
216  }
217 
218 } ;
219 
220 /*
221  TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
222  Only the operators that we need for TinyXML have been developped.
223 */
224 class DDD_EXPORT TiXmlOutStream : public TiXmlString
225 {
226 public :
227  TiXmlOutStream () : TiXmlString () {}
228 
229  // TiXmlOutStream << operator. Maps to TiXmlString::append
230  TiXmlOutStream & operator << (const char * in)
231  {
232  append (in);
233  return (* this);
234  }
235 
236  // TiXmlOutStream << operator. Maps to TiXmlString::append
237  TiXmlOutStream & operator << (const TiXmlString & in)
238  {
239  append (in . c_str ());
240  return (* this);
241  }
242 } ;
243 
244 #ifdef _MSC_VER
245 #pragma warning( default : 4530 )
246 #pragma warning( default : 4786 )
247 #endif
248 
249 #endif // TIXML_STRING_INCLUDED
250 #endif // TIXML_USE_STL
251 
Definition: tinystr.h:224
Definition: tinystr.h:47