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