Code

11b301066c7dea939854d578c28ce5efec8a1352
[inkscape.git] / src / dom / domstring.cpp
1 /**
2  * Phoebe DOM Implementation.
3  *
4  * This is a C++ approximation of the W3C DOM model, which follows
5  * fairly closely the specifications in the various .idl files, copies of
6  * which are provided for reference.  Most important is this one:
7  *
8  * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
9  *
10  * Authors:
11  *   Bob Jamison
12  *
13  * Copyright (C) 2005 Bob Jamison
14  *
15  *  This library is free software; you can redistribute it and/or
16  *  modify it under the terms of the GNU Lesser General Public
17  *  License as published by the Free Software Foundation; either
18  *  version 2.1 of the License, or (at your option) any later version.
19  *
20  *  This library is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  *  Lesser General Public License for more details.
24  *
25  *  You should have received a copy of the GNU Lesser General Public
26  *  License along with this library; if not, write to the Free Software
27  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
31 #include "domstring.h"
33 namespace org
34 {
35 namespace w3c
36 {
37 namespace dom
38 {
41 //#########################################################################
42 //# C O N S T R U C T O R S
43 //#########################################################################
45 DOMString::DOMString()
46 {
47     init();
48 }
50 DOMString::DOMString(const DOMString &other)
51 {
52     init();
53     chars = other.chars;
54 }
56 DOMString::DOMString(const char *str)
57 {
58     init();
59     append(str);
60 }
63 DOMString::~DOMString()
64 {
65     if (cstring)
66         free(cstring);
67 }
70 /**
71  * Called only by Constructors
72  */
73 void DOMString::init()
74 {
75     cstring = NULL;
76     chars.clear();
77 }
79 //#########################################################################
80 //# M O D I F Y
81 //#########################################################################
83 DOMString &DOMString::append(const DOMString &str)
84 {
85     unsigned int len = str.size();
86     for (unsigned int i=0 ; i<len ; i++)
87         {
88         XMLCh ch = str.charAt(i);
89         chars.push_back(ch);
90         }
91     return *this;
92 }
94 DOMString &DOMString::append(const char *str)
95 {
96     if (!str)
97         return *this;
98     char *s = (char *)str;
99     while (*s)
100         {
101         XMLCh ch = (XMLCh) *s++;
102         chars.push_back(ch);
103         }
104     return *this;
107 DOMString &DOMString::append(const std::string &str)
109     unsigned int len = str.size();
110     for (unsigned int i=0 ; i<len ; i++)
111         {
112         XMLCh ch = str[i];
113         chars.push_back(ch);
114         }
115     return *this;
118 DOMString &DOMString::assign(const DOMString &str)
120     clear();
121     append(str);
122     return *this;
125 DOMString &DOMString::assign(const char *str)
127     clear();
128     append(str);
129     return *this;
132 DOMString &DOMString::assign(const std::string &str)
134     clear();
135     append(str);
136     return *this;
140 void DOMString::erase(unsigned long pos, unsigned long count)
142     std::vector<XMLCh>::iterator iter = chars.begin();
143     chars.erase(iter, iter + count);
146 DOMString &DOMString::insert(unsigned long pos, const DOMString &str)
148     DOMString a = substr(0, pos);
149     DOMString b = substr(pos, size());
150     clear();
151     append(a);
152     append(str);
153     append(b);
154     return *this;
157 DOMString &DOMString::insert(unsigned long pos, const char *str)
159     DOMString a = substr(0, pos);
160     DOMString b = substr(pos, size());
161     clear();
162     append(a);
163     append(str);
164     append(b);
165     return *this;
168 DOMString &DOMString::insert(unsigned long pos, const std::string &str)
170     DOMString a = substr(0, pos);
171     DOMString b = substr(pos, size());
172     clear();
173     append(a);
174     append(str);
175     append(b);
176     return *this;
180 DOMString &DOMString::prepend(const DOMString &str)
182     DOMString tmp = *this;
183     clear();
184     append(str);
185     append(tmp);
186     return *this;
189 DOMString &DOMString::prepend(const char *str)
191     DOMString tmp = *this;
192     clear();
193     append(str);
194     append(tmp);
195     return *this;
198 DOMString &DOMString::prepend(const std::string &str)
200     DOMString tmp = *this;
201     clear();
202     append(str);
203     append(tmp);
204     return *this;
207 DOMString &DOMString::replace(unsigned long pos, unsigned long count,
208                               const DOMString &str)
210     DOMString a = substr(0, pos);
211     DOMString b = substr(pos+count, size());
212     clear();
213     append(a);
214     append(str);
215     append(b);
216     return *this;
219 DOMString &DOMString::replace(unsigned long pos, unsigned long count,
220                               const char *str)
222     DOMString a = substr(0, pos);
223     DOMString b = substr(pos+count, size());
224     clear();
225     append(a);
226     append(str);
227     append(b);
228     return *this;
231 DOMString &DOMString::replace(unsigned long pos, unsigned long count,
232                               const std::string &str)
234     DOMString a = substr(0, pos);
235     DOMString b = substr(pos+count, size());
236     clear();
237     append(a);
238     append(str);
239     append(b);
240     return *this;
244 DOMString &DOMString::push_back(XMLCh ch)
246     chars.push_back(ch);
247     return *this;
252 void DOMString::clear()
254     chars.clear();
255     if (cstring)
256         {
257         free(cstring);
258         cstring = NULL;
259         }
262 //#########################################################################
263 //# Q U E R Y
264 //#########################################################################
266 XMLCh DOMString::charAt(unsigned long index) const
268     return chars[index];
271 XMLCh DOMString::operator[](unsigned long index) const
273     return chars[index];
276 DOMString DOMString::substr(unsigned long start, unsigned long end) const
278     DOMString ret;
279     for (unsigned long i = start; i<end ; i++)
280         ret.push_back(chars[i]);
281     return ret;
284 const char *DOMString::c_str()
286     if (cstring)
287         free(cstring);
289     int length = chars.size();
291     cstring = (char *)malloc(length + 1);
293     int i=0;
294     for ( ; i<length ; i++ )
295         cstring[i] = (char) chars[i];
297     cstring[i] = '\0';
299     return cstring;
303 unsigned long DOMString::size() const
305     return chars.size();
310 //#########################################################################
311 //# C O M P A R I S O N
312 //#########################################################################
314 int DOMString::compare(const DOMString &str) const
316     int asize = chars.size();
317     int bsize = str.size();
319     int diff = 0;
320     int index = 0;
321     while (index < asize && index < bsize)
322         {
323         int a = (int) chars[index];
324         int b = (int) str[index];
325         diff = a - b;
326         if (diff)
327             return diff;
328         index++;
329         }
331     //equal for their common length. lets see which is longer
332     diff = asize - bsize;
334     return diff;
338 int DOMString::compare(const char *str) const
340     if (!str)
341         return 1;
343     int asize = chars.size();
345     int diff = 0;
346     int index = 0;
347     while (index < asize)
348         {
349         int a = (int) chars[index];
350         int b = (int) str[index];
351         if (!b)
352             return a;
354         diff = a - b;
355         if (diff)
356             return diff;
357         index++;
358         }
360     diff = str[index]; //char or terminating 0
362     return diff;
367 int DOMString::compare(const std::string &str) const
369     int asize = chars.size();
370     int bsize = str.size();
372     int diff = 0;
373     int index = 0;
374     while (index < asize && index < bsize)
375         {
376         int a = (int) chars[index];
377         int b = (int) str[index];
378         diff = a - b;
379         if (diff)
380             return diff;
381         index++;
382         }
384     diff = asize - bsize;
386     return diff;
394 //#########################################################################
395 //# O P E R A T O R S
396 //#########################################################################
397 DOMString &operator +(DOMString &a, const char *b)
398     { return a.append(b); }
400 DOMString &operator +(const char *b, DOMString &a)
401     { return a.prepend(b); }
405 }  //namespace dom
406 }  //namespace w3c
407 }  //namespace org
409 /*#########################################################################
410 ## E N D    O F    F I L E
411 #########################################################################*/