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 */
30 #include <stdio.h>
31 #include <stdlib.h>
33 #include "domstring.h"
35 namespace org
36 {
37 namespace w3c
38 {
39 namespace dom
40 {
43 //#########################################################################
44 //# C O N S T R U C T O R S
45 //#########################################################################
47 DOMString::DOMString()
48 {
49 init();
50 }
52 DOMString::DOMString(const DOMString &other)
53 {
54 init();
55 chars = other.chars;
56 }
58 DOMString::DOMString(const char *str)
59 {
60 init();
61 append(str);
62 }
65 DOMString::~DOMString()
66 {
67 if (cstring)
68 delete cstring;
69 }
72 /**
73 * Called only by Constructors
74 */
75 void DOMString::init()
76 {
77 cstring = NULL;
78 chars.clear();
79 }
81 //#########################################################################
82 //# M O D I F Y
83 //#########################################################################
85 DOMString &DOMString::append(const DOMString &str)
86 {
87 unsigned int len = str.size();
88 for (unsigned int i=0 ; i<len ; i++)
89 {
90 XMLCh ch = str.charAt(i);
91 chars.push_back(ch);
92 }
93 return *this;
94 }
96 DOMString &DOMString::append(const char *str)
97 {
98 if (!str)
99 return *this;
100 char *s = (char *)str;
101 while (*s)
102 {
103 XMLCh ch = (XMLCh) *s++;
104 chars.push_back(ch);
105 }
106 return *this;
107 }
109 DOMString &DOMString::append(const std::string &str)
110 {
111 unsigned int len = str.size();
112 for (unsigned int i=0 ; i<len ; i++)
113 {
114 XMLCh ch = str[i];
115 chars.push_back(ch);
116 }
117 return *this;
118 }
120 DOMString &DOMString::assign(const DOMString &str)
121 {
122 clear();
123 append(str);
124 return *this;
125 }
127 DOMString &DOMString::operator=(const DOMString &str)
128 {
129 clear();
130 append(str);
131 return *this;
132 }
134 DOMString &DOMString::assign(const char *str)
135 {
136 clear();
137 append(str);
138 return *this;
139 }
141 DOMString &DOMString::assign(const std::string &str)
142 {
143 clear();
144 append(str);
145 return *this;
146 }
149 void DOMString::erase(unsigned long /*pos*/, unsigned long count)
150 {
151 std::vector<XMLCh>::iterator iter = chars.begin();
152 chars.erase(iter, iter + count);
153 }
155 DOMString &DOMString::insert(unsigned long pos, const DOMString &str)
156 {
157 DOMString a = substr(0, pos);
158 DOMString b = substr(pos, size());
159 clear();
160 append(a);
161 append(str);
162 append(b);
163 return *this;
164 }
166 DOMString &DOMString::insert(unsigned long pos, const char *str)
167 {
168 DOMString a = substr(0, pos);
169 DOMString b = substr(pos, size());
170 clear();
171 append(a);
172 append(str);
173 append(b);
174 return *this;
175 }
177 DOMString &DOMString::insert(unsigned long pos, const std::string &str)
178 {
179 DOMString a = substr(0, pos);
180 DOMString b = substr(pos, size());
181 clear();
182 append(a);
183 append(str);
184 append(b);
185 return *this;
186 }
189 DOMString &DOMString::prepend(const DOMString &str)
190 {
191 DOMString tmp = *this;
192 clear();
193 append(str);
194 append(tmp);
195 return *this;
196 }
198 DOMString &DOMString::prepend(const char *str)
199 {
200 DOMString tmp = *this;
201 clear();
202 append(str);
203 append(tmp);
204 return *this;
205 }
207 DOMString &DOMString::prepend(const std::string &str)
208 {
209 DOMString tmp = *this;
210 clear();
211 append(str);
212 append(tmp);
213 return *this;
214 }
216 DOMString &DOMString::replace(unsigned long pos, unsigned long count,
217 const DOMString &str)
218 {
219 DOMString a = substr(0, pos);
220 DOMString b = substr(pos+count, size());
221 clear();
222 append(a);
223 append(str);
224 append(b);
225 return *this;
226 }
228 DOMString &DOMString::replace(unsigned long pos, unsigned long count,
229 const char *str)
230 {
231 DOMString a = substr(0, pos);
232 DOMString b = substr(pos+count, size());
233 clear();
234 append(a);
235 append(str);
236 append(b);
237 return *this;
238 }
240 DOMString &DOMString::replace(unsigned long pos, unsigned long count,
241 const std::string &str)
242 {
243 DOMString a = substr(0, pos);
244 DOMString b = substr(pos+count, size());
245 clear();
246 append(a);
247 append(str);
248 append(b);
249 return *this;
250 }
253 DOMString &DOMString::push_back(XMLCh ch)
254 {
255 chars.push_back(ch);
256 return *this;
257 }
261 void DOMString::clear()
262 {
263 chars.clear();
264 if (cstring)
265 {
266 delete cstring;
267 cstring = NULL;
268 }
269 }
271 //#########################################################################
272 //# Q U E R Y
273 //#########################################################################
275 XMLCh DOMString::charAt(unsigned long index) const
276 {
277 return chars[index];
278 }
280 XMLCh DOMString::operator[](unsigned long index) const
281 {
282 return chars[index];
283 }
285 DOMString DOMString::substr(unsigned long start, unsigned long end) const
286 {
287 DOMString ret;
288 for (unsigned long i = start; i<end ; i++)
289 ret.push_back(chars[i]);
290 return ret;
291 }
293 const char *DOMString::c_str()
294 {
295 if (cstring)
296 delete cstring;
298 int length = chars.size();
300 cstring = new char[length+1];
302 int i=0;
303 for ( ; i<length ; i++ )
304 cstring[i] = (char) chars[i];
306 cstring[i] = '\0';
308 return cstring;
309 }
312 unsigned long DOMString::size() const
313 {
314 return chars.size();
315 }
319 //#########################################################################
320 //# C O M P A R I S O N
321 //#########################################################################
323 int DOMString::compare(const DOMString &str) const
324 {
325 int asize = chars.size();
326 int bsize = str.size();
328 int diff = 0;
329 int index = 0;
330 while (index < asize && index < bsize)
331 {
332 int a = (int) chars[index];
333 int b = (int) str[index];
334 diff = a - b;
335 if (diff)
336 return diff;
337 index++;
338 }
340 //equal for their common length. lets see which is longer
341 diff = asize - bsize;
343 return diff;
344 }
347 int DOMString::compare(const char *str) const
348 {
349 if (!str)
350 return 1;
352 int asize = chars.size();
354 int diff = 0;
355 int index = 0;
356 while (index < asize)
357 {
358 int a = (int) chars[index];
359 int b = (int) str[index];
360 if (!b)
361 return a;
363 diff = a - b;
364 if (diff)
365 return diff;
366 index++;
367 }
369 diff = str[index]; //char or terminating 0
371 return diff;
373 }
376 int DOMString::compare(const std::string &str) const
377 {
378 int asize = chars.size();
379 int bsize = str.size();
381 int diff = 0;
382 int index = 0;
383 while (index < asize && index < bsize)
384 {
385 int a = (int) chars[index];
386 int b = (int) str[index];
387 diff = a - b;
388 if (diff)
389 return diff;
390 index++;
391 }
393 diff = asize - bsize;
395 return diff;
399 }
403 //#########################################################################
404 //# O P E R A T O R S
405 //#########################################################################
406 DOMString &operator +(DOMString &a, const char *b)
407 { return a.append(b); }
409 DOMString &operator +(const char *b, DOMString &a)
410 { return a.prepend(b); }
414 } //namespace dom
415 } //namespace w3c
416 } //namespace org
418 /*#########################################################################
419 ## E N D O F F I L E
420 #########################################################################*/