source: trunk/VUT/OcclusionCullingSceneManager/tinyxml/tinystr.cpp @ 37

Revision 37, 6.2 KB checked in by mattausch, 20 years ago (diff)
Line 
1/*
2www.sourceforge.net/projects/tinyxml
3Original file by Yves Berquin.
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any
7damages arising from the use of this software.
8
9Permission is granted to anyone to use this software for any
10purpose, including commercial applications, and to alter it and
11redistribute it freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must
14not claim that you wrote the original software. If you use this
15software in a product, an acknowledgment in the product documentation
16would be appreciated but is not required.
17
182. Altered source versions must be plainly marked as such, and
19must not be misrepresented as being the original software.
20
213. This notice may not be removed or altered from any source
22distribution.
23*/
24
25#include "tinyxml.h"
26
27#ifndef TIXML_USE_STL
28
29
30#include <stdlib.h>
31#include <string.h>
32#include <ctype.h>
33
34#include "tinystr.h"
35
36// TiXmlString constructor, based on a C string
37TiXmlString::TiXmlString (const char* instring)
38{
39    unsigned newlen;
40    char * newstring;
41
42    if (!instring)
43    {
44        allocated = 0;
45        cstring = NULL;
46        return;
47    }
48    newlen = strlen (instring) + 1;
49    newstring = new char [newlen];
50    strcpy (newstring, instring);
51    allocated = newlen;
52    cstring = newstring;
53}
54
55// TiXmlString copy constructor
56TiXmlString::TiXmlString (const TiXmlString& copy)
57{
58    unsigned newlen;
59    char * newstring;
60
61        // Prevent copy to self!
62        if ( &copy == this )
63                return;
64
65    if (! copy . allocated)
66    {
67        allocated = 0;
68        cstring = NULL;
69        return;
70    }
71    newlen = strlen (copy . cstring) + 1;
72    newstring = new char [newlen];
73    strcpy (newstring, copy . cstring);
74    allocated = newlen;
75    cstring = newstring;
76}
77
78// TiXmlString = operator. Safe when assign own content
79void TiXmlString ::operator = (const char * content)
80{
81    unsigned newlen;
82    char * newstring;
83
84    if (! content)
85    {
86        empty_it ();
87        return;
88    }
89    newlen = strlen (content) + 1;
90    newstring = new char [newlen];
91    strcpy (newstring, content);
92    empty_it ();
93    allocated = newlen;
94    cstring = newstring;
95}
96
97// = operator. Safe when assign own content
98void TiXmlString ::operator = (const TiXmlString & copy)
99{
100    unsigned newlen;
101    char * newstring;
102
103    if (! copy . length ())
104    {
105        empty_it ();
106        return;
107    }
108    newlen = copy . length () + 1;
109    newstring = new char [newlen];
110    strcpy (newstring, copy . c_str ());
111    empty_it ();
112    allocated = newlen;
113    cstring = newstring;
114}
115
116
117//// Checks if a TiXmlString contains only whitespace (same rules as isspace)
118//bool TiXmlString::isblank () const
119//{
120//    char * lookup;
121//    for (lookup = cstring; * lookup; lookup++)
122//        if (! isspace (* lookup))
123//            return false;
124//    return true;
125//}
126
127// append a const char * to an existing TiXmlString
128void TiXmlString::append( const char* str, int len )
129{
130    char * new_string;
131    unsigned new_alloc, new_size;
132
133    new_size = length () + len + 1;
134    // check if we need to expand
135    if (new_size > allocated)
136    {
137        // compute new size
138        new_alloc = assign_new_size (new_size);
139
140        // allocate new buffer
141        new_string = new char [new_alloc];       
142        new_string [0] = 0;
143
144        // copy the previous allocated buffer into this one
145        if (allocated && cstring)
146            strcpy (new_string, cstring);
147
148        // append the suffix. It does exist, otherwize we wouldn't be expanding
149        strncat (new_string, str, len);
150
151        // return previsously allocated buffer if any
152        if (allocated && cstring)
153            delete [] cstring;
154
155        // update member variables
156        cstring = new_string;
157        allocated = new_alloc;
158    }
159    else
160        // we know we can safely append the new string
161        strncat (cstring, str, len);
162}
163
164
165// append a const char * to an existing TiXmlString
166void TiXmlString::append( const char * suffix )
167{
168    char * new_string;
169    unsigned new_alloc, new_size;
170
171    new_size = length () + strlen (suffix) + 1;
172    // check if we need to expand
173    if (new_size > allocated)
174    {
175        // compute new size
176        new_alloc = assign_new_size (new_size);
177
178        // allocate new buffer
179        new_string = new char [new_alloc];       
180        new_string [0] = 0;
181
182        // copy the previous allocated buffer into this one
183        if (allocated && cstring)
184            strcpy (new_string, cstring);
185
186        // append the suffix. It does exist, otherwize we wouldn't be expanding
187        strcat (new_string, suffix);
188
189        // return previsously allocated buffer if any
190        if (allocated && cstring)
191            delete [] cstring;
192
193        // update member variables
194        cstring = new_string;
195        allocated = new_alloc;
196    }
197    else
198        // we know we can safely append the new string
199        strcat (cstring, suffix);
200}
201
202// Check for TiXmlString equuivalence
203//bool TiXmlString::operator == (const TiXmlString & compare) const
204//{
205//    return (! strcmp (c_str (), compare . c_str ()));
206//}
207
208unsigned TiXmlString::length () const
209{
210    if (allocated)
211        return strlen (cstring);
212    return 0;
213}
214
215
216unsigned TiXmlString::find (char tofind, unsigned offset) const
217{
218    char * lookup;
219
220    if (offset >= length ())
221        return (unsigned) notfound;
222    for (lookup = cstring + offset; * lookup; lookup++)
223        if (* lookup == tofind)
224            return lookup - cstring;
225    return (unsigned) notfound;
226}
227
228
229bool TiXmlString::operator == (const TiXmlString & compare) const
230{
231        if ( allocated && compare.allocated )
232        {
233                assert( cstring );
234                assert( compare.cstring );
235                return ( strcmp( cstring, compare.cstring ) == 0 );
236        }
237        return false;
238}
239
240
241bool TiXmlString::operator < (const TiXmlString & compare) const
242{
243        if ( allocated && compare.allocated )
244        {
245                assert( cstring );
246                assert( compare.cstring );
247                return ( strcmp( cstring, compare.cstring ) > 0 );
248        }
249        return false;
250}
251
252
253bool TiXmlString::operator > (const TiXmlString & compare) const
254{
255        if ( allocated && compare.allocated )
256        {
257                assert( cstring );
258                assert( compare.cstring );
259                return ( strcmp( cstring, compare.cstring ) < 0 );
260        }
261        return false;
262}
263
264
265#endif  // TIXML_USE_STL
Note: See TracBrowser for help on using the repository browser.