openEmbroider  0.1
an open source embroidery software
 All Classes Functions Enumerations
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 //#pragma warning( disable : 4514 )
34 
35 #include <assert.h>
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
48  TiXmlString (const char * instring);
49 
50  // TiXmlString empty constructor
51  TiXmlString ()
52  {
53  allocated = 0;
54  cstring = NULL;
55  current_length = 0;
56  }
57 
58  // TiXmlString copy constructor
59  TiXmlString (const TiXmlString& copy);
60 
61  // TiXmlString destructor
62  ~ TiXmlString ()
63  {
64  empty_it ();
65  }
66 
67  // Convert a TiXmlString into a classical char *
68  const char * c_str () const
69  {
70  if (allocated)
71  return cstring;
72  return "";
73  }
74 
75  // Return the length of a TiXmlString
76  unsigned length () const
77  {
78  return ( allocated ) ? current_length : 0;
79  }
80 
81  // TiXmlString = operator
82  void operator = (const char * content);
83 
84  // = operator
85  void operator = (const TiXmlString & copy);
86 
87  // += operator. Maps to append
88  TiXmlString& operator += (const char * suffix)
89  {
90  append (suffix);
91  return *this;
92  }
93 
94  // += operator. Maps to append
95  TiXmlString& operator += (char single)
96  {
97  append (single);
98  return *this;
99  }
100 
101  // += operator. Maps to append
102  TiXmlString& operator += (TiXmlString & suffix)
103  {
104  append (suffix);
105  return *this;
106  }
107  bool operator == (const TiXmlString & compare) const;
108  bool operator < (const TiXmlString & compare) const;
109  bool operator > (const TiXmlString & compare) const;
110 
111  // Checks if a TiXmlString is empty
112  bool empty () const
113  {
114  return length () ? false : true;
115  }
116 
117  // Checks if a TiXmlString contains only whitespace (same rules as isspace)
118  // Not actually used in tinyxml. Conflicts with a C macro, "isblank",
119  // which is a problem. Commenting out. -lee
120 // bool isblank () const;
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, int len );
165 
166  protected :
167 
168  // The base string
169  char * cstring;
170  // Number of chars allocated
171  unsigned allocated;
172  // Current string size
173  unsigned current_length;
174 
175  // New size computation. It is simplistic right now : it returns twice the amount
176  // we need
177  unsigned assign_new_size (unsigned 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. This could be improved a lot if needed
201  void append (char single)
202  {
203  char smallstr [2];
204  smallstr [0] = single;
205  smallstr [1] = 0;
206  append (smallstr);
207  }
208 
209 } ;
210 
211 /*
212  TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
213  Only the operators that we need for TinyXML have been developped.
214 */
216 {
217 public :
218  TiXmlOutStream () : TiXmlString () {}
219 
220  // TiXmlOutStream << operator. Maps to TiXmlString::append
221  TiXmlOutStream & operator << (const char * in)
222  {
223  append (in);
224  return (* this);
225  }
226 
227  // TiXmlOutStream << operator. Maps to TiXmlString::append
228  TiXmlOutStream & operator << (const TiXmlString & in)
229  {
230  append (in . c_str ());
231  return (* this);
232  }
233 } ;
234 
235 #endif // TIXML_STRING_INCLUDED
236 #endif // TIXML_USE_STL
Definition: tinystr.h:44
Definition: tinystr.h:215
void append(const char *str, int len)
Checks if a TiXmlString contains only whitespace (same rules as isspace)
Definition: tinystr.cpp:138