ddc
tinystr.h
Go to the documentation of this file.
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 : 4786 ) // Debugger truncating names.
35 #endif
36 
37 #include <assert.h>
38 
39 /*
40  TiXmlString is an emulation of the std::string template.
41  Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
42  Only the member functions relevant to the TinyXML project have been implemented.
43  The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
44  a string and there's no more room, we allocate a buffer twice as big as we need.
45 */
47 {
48  public :
49  // TiXmlString constructor, based on a string
50  TiXmlString (const char * instring);
51 
52  // TiXmlString empty constructor
54  {
55  allocated = 0;
56  cstring = NULL;
57  current_length = 0;
58  }
59 
60  // TiXmlString copy constructor
61  TiXmlString (const TiXmlString& copy);
62 
63  // TiXmlString destructor
65  {
66  empty_it ();
67  }
68 
69  // Convert a TiXmlString into a classical char *
70  const char * c_str () const
71  {
72  if (allocated)
73  return cstring;
74  return "";
75  }
76 
77  // Return the length of a TiXmlString
78  unsigned length () const
79  {
80  return ( allocated ) ? current_length : 0;
81  }
82 
83  // TiXmlString = operator
84  void operator = (const char * content);
85 
86  // = operator
87  void operator = (const TiXmlString & copy);
88 
89  // += operator. Maps to append
90  TiXmlString& operator += (const char * suffix)
91  {
92  append (suffix);
93  return *this;
94  }
95 
96  // += operator. Maps to append
97  TiXmlString& operator += (char single)
98  {
99  append (single);
100  return *this;
101  }
102 
103  // += operator. Maps to append
105  {
106  append (suffix);
107  return *this;
108  }
109  bool operator == (const TiXmlString & 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,
160 
161  void append (const char *str, int len );
162 
163  protected :
164 
165  // The base string
166  char * cstring;
167  // Number of chars allocated
168  unsigned allocated;
169  // Current string size
170  unsigned current_length;
171 
172  // New size computation. It is simplistic right now : it returns twice the amount
173  // we need
174  unsigned assign_new_size (unsigned 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 :
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
235  {
236  append (in . c_str ());
237  return (* this);
238  }
239 } ;
240 
241 #endif // TIXML_STRING_INCLUDED
242 #endif // TIXML_USE_STL
243 
244 /*--- emacs style variables ---
245  * Local Variables:
246  * mode: C++
247  * c-file-style: "ellemtel"
248  * c-basic-offset: 4
249  * tab-width: 8
250  * indent-tabs-mode: nil
251  * End:
252  */
bool operator<(const TiXmlString &compare) const
Definition: tinystr.cpp:275
char * cstring
Definition: tinystr.h:166
void append(const TiXmlString &suffix)
Definition: tinystr.h:192
unsigned assign_new_size(unsigned minimum_to_allocate)
Definition: tinystr.h:174
TiXmlString & operator+=(const char *suffix)
Definition: tinystr.h:90
TiXmlOutStream()
Definition: tinystr.h:224
void append(char single)
Definition: tinystr.h:198
unsigned current_length
Definition: tinystr.h:170
bool empty() const
Definition: tinystr.h:114
bool operator==(const TiXmlString &compare) const
Definition: tinystr.cpp:263
unsigned allocated
Definition: tinystr.h:168
Definition: tinystr.h:158
~ TiXmlString()
Definition: tinystr.h:64
bool operator>(const TiXmlString &compare) const
Definition: tinystr.cpp:287
Definition: tinystr.h:159
Definition: tinystr.h:221
const char * c_str() const
Definition: tinystr.h:70
char & operator[](unsigned index) const
Definition: tinystr.h:151
TiXmlOutStream & operator<<(TiXmlOutStream &out, const TiXmlNode &base)
Definition: tinyxml.cpp:1296
void empty_it()
Definition: tinystr.h:180
void reserve(unsigned size)
Definition: tinystr.h:138
unsigned find(char lookup) const
Definition: tinystr.h:127
void append(const char *str, int len)
Definition: tinystr.cpp:128
unsigned length() const
Definition: tinystr.h:78
void operator=(const char *content)
Definition: tinystr.cpp:85
const char & at(unsigned index) const
Definition: tinystr.h:120
Definition: tinystr.h:46
TiXmlString()
Definition: tinystr.h:53