1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * text_t.cpp -- a simple 16-bit character string class
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | * $Id: text_t.cpp 10140 2005-06-16 22:49:44Z kjdon $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.25 2005/06/16 22:49:44 kjdon
|
---|
31 | added starts_with and ends_with functions
|
---|
32 |
|
---|
33 | Revision 1.24 2005/04/07 04:27:18 kjdon
|
---|
34 | added some x++ -> ++x changes submitted by Emanuel Dejanu
|
---|
35 |
|
---|
36 | Revision 1.23 2004/12/02 22:21:34 kjdon
|
---|
37 | added some changes made by Emanuel Dejanu (Simple Words)
|
---|
38 |
|
---|
39 | Revision 1.22 2004/05/24 03:43:22 mdewsnip
|
---|
40 | (Human Info) Added const text_t g_EmptyText("") to use instead of plain ol' "".
|
---|
41 |
|
---|
42 | Revision 1.21 2001/06/01 02:51:28 sjboddie
|
---|
43 | Changes to get phind working under windows
|
---|
44 |
|
---|
45 | Revision 1.20 2001/01/25 18:26:44 cs025
|
---|
46 | Included CORBA branch for first time
|
---|
47 |
|
---|
48 | Revision 1.15.2.2 2000/04/05 10:19:38 syeates
|
---|
49 | added automatic conversion to allow text_t's to be <<'ed to ostreams
|
---|
50 |
|
---|
51 | Revision 1.15.2.1 2000/04/04 15:02:29 cs025
|
---|
52 | Corba first commit
|
---|
53 |
|
---|
54 | Revision 1.15 1999/10/14 22:52:39 sjboddie
|
---|
55 | joinchar can join using text_t string now too
|
---|
56 |
|
---|
57 | Revision 1.14 1999/09/24 02:30:03 rjmcnab
|
---|
58 | added function has_unicode_letdig
|
---|
59 |
|
---|
60 | Revision 1.13 1999/09/07 04:57:43 sjboddie
|
---|
61 | added gpl notice
|
---|
62 |
|
---|
63 | Revision 1.12 1999/08/31 08:04:41 rjmcnab
|
---|
64 | Fixed a small but hard to find bug in getcarr
|
---|
65 |
|
---|
66 | Revision 1.11 1999/07/01 04:05:09 rjmcnab
|
---|
67 | Optimised append functions slightly and added a reserve function.
|
---|
68 |
|
---|
69 | Revision 1.10 1999/04/26 03:58:03 sjboddie
|
---|
70 | added is_number function
|
---|
71 |
|
---|
72 | Revision 1.9 1999/04/06 22:17:24 rjmcnab
|
---|
73 | Added splits and joins using text_tset.
|
---|
74 |
|
---|
75 | Revision 1.8 1999/02/28 23:14:41 rjmcnab
|
---|
76 |
|
---|
77 | Added uc and lc to convert to uppercase and lowercase.
|
---|
78 |
|
---|
79 | Revision 1.7 1999/02/21 22:26:39 rjmcnab
|
---|
80 |
|
---|
81 | Made getint() a constant function.
|
---|
82 |
|
---|
83 | Revision 1.6 1999/02/03 01:13:26 sjboddie
|
---|
84 |
|
---|
85 | Got interface to handle subcollections and language subcollections -
|
---|
86 | committed changes made to some of the collections
|
---|
87 |
|
---|
88 | Revision 1.5 1999/01/19 01:38:14 rjmcnab
|
---|
89 |
|
---|
90 | Made the source more portable.
|
---|
91 |
|
---|
92 | Revision 1.4 1999/01/12 01:51:00 rjmcnab
|
---|
93 |
|
---|
94 | Standard header.
|
---|
95 |
|
---|
96 | Revision 1.3 1999/01/08 02:33:16 rjmcnab
|
---|
97 |
|
---|
98 | Added standard header to source files.
|
---|
99 |
|
---|
100 | */
|
---|
101 |
|
---|
102 | #include "text_t.h"
|
---|
103 |
|
---|
104 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
105 | # include <ospace\std\algorithm>
|
---|
106 | #elif defined(GSDL_USE_STL_H)
|
---|
107 | # if defined(GSDL_USE_ALGO_H)
|
---|
108 | # include <algo.h>
|
---|
109 | # else
|
---|
110 | # include <algorithm.h>
|
---|
111 | # endif
|
---|
112 | #else
|
---|
113 | # include <algorithm>
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | #ifdef HAVE_CONFIG_H
|
---|
117 | # ifdef __WIN32__
|
---|
118 | # include "WIN32cfg.h"
|
---|
119 | # else
|
---|
120 | # include "config.h"
|
---|
121 | # endif
|
---|
122 | #endif
|
---|
123 |
|
---|
124 |
|
---|
125 | #include "unitool.h"
|
---|
126 |
|
---|
127 | const text_t g_EmptyText("");
|
---|
128 |
|
---|
129 | ////////////////////////////////////
|
---|
130 | // text_t methods
|
---|
131 | ////////////////////////////////////
|
---|
132 |
|
---|
133 | // new stream converter ...
|
---|
134 | ostream& operator<< (ostream &o, const text_t &text)
|
---|
135 | {
|
---|
136 | text_t::const_iterator ithere = text.begin();
|
---|
137 | text_t::const_iterator itend = text.end();
|
---|
138 |
|
---|
139 | while (ithere != itend)
|
---|
140 | {
|
---|
141 | if (*ithere < 256)
|
---|
142 | {
|
---|
143 | o << (unsigned char)(*ithere);
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | // put a space or a question mark depending on what
|
---|
148 | // the character is. Question marks tell the user that
|
---|
149 | // they are missing some information.
|
---|
150 | if (is_unicode_space (*ithere))
|
---|
151 | o << ' ';
|
---|
152 | else
|
---|
153 | o << '?';
|
---|
154 | }
|
---|
155 | ++ithere;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return o;
|
---|
159 | }
|
---|
160 |
|
---|
161 | text_t::text_t ()
|
---|
162 | {
|
---|
163 | setencoding(0);
|
---|
164 | clear ();
|
---|
165 | }
|
---|
166 |
|
---|
167 | text_t::text_t (int i)
|
---|
168 | {
|
---|
169 | setencoding(0);
|
---|
170 | clear ();
|
---|
171 | appendint (i);
|
---|
172 | }
|
---|
173 |
|
---|
174 | text_t::text_t (const char *s)
|
---|
175 | {
|
---|
176 | setencoding(0);
|
---|
177 | clear ();
|
---|
178 | appendcstr (s);
|
---|
179 | }
|
---|
180 |
|
---|
181 | text_t::text_t (const char *s, size_type nLength)
|
---|
182 | {
|
---|
183 | setencoding(0);
|
---|
184 | clear ();
|
---|
185 | appendcarr(s, nLength);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | void text_t::append (const text_t &t)
|
---|
190 | {
|
---|
191 | text.insert(text.end(), t.begin(), t.end());
|
---|
192 | }
|
---|
193 |
|
---|
194 | void text_t::appendrange (iterator first, iterator last)
|
---|
195 | {
|
---|
196 | text.insert(text.end(), first, last);
|
---|
197 | }
|
---|
198 |
|
---|
199 | void text_t::appendrange (const_iterator first, const_iterator last)
|
---|
200 | {
|
---|
201 | text.insert(text.end(), first, last);
|
---|
202 | }
|
---|
203 |
|
---|
204 | void text_t::appendint (int i)
|
---|
205 | {
|
---|
206 | // deal with zeros and negatives
|
---|
207 | if (i == 0)
|
---|
208 | {
|
---|
209 | text.push_back('0');
|
---|
210 | return;
|
---|
211 | }
|
---|
212 | else if (i < 0)
|
---|
213 | {
|
---|
214 | text.push_back('-');
|
---|
215 | i *= -1;
|
---|
216 | }
|
---|
217 |
|
---|
218 | // get a buffer for the conversion
|
---|
219 | int maxbuflen = sizeof(int)*3;
|
---|
220 | char *buf = new char[maxbuflen];
|
---|
221 | int len = 0;
|
---|
222 |
|
---|
223 | // get the number in reverse
|
---|
224 | while (i > 0)
|
---|
225 | {
|
---|
226 | buf[len++] = '0'+ (i%10);
|
---|
227 | i = i/10;
|
---|
228 | }
|
---|
229 |
|
---|
230 | // reverse the number
|
---|
231 | while (len > 0)
|
---|
232 | {
|
---|
233 | text.push_back(buf[--len]);
|
---|
234 | }
|
---|
235 |
|
---|
236 | delete []buf;
|
---|
237 | }
|
---|
238 |
|
---|
239 | int text_t::getint () const
|
---|
240 | {
|
---|
241 | int i = 0;
|
---|
242 | int mult = 1; // become -1 for negative numbers
|
---|
243 |
|
---|
244 | const_iterator here = text.begin();
|
---|
245 | const_iterator end = text.end();
|
---|
246 |
|
---|
247 | // do plus and minus signs
|
---|
248 | if (here != end)
|
---|
249 | {
|
---|
250 | if (*here == '-')
|
---|
251 | {
|
---|
252 | mult = -1;
|
---|
253 | ++here;
|
---|
254 | }
|
---|
255 | else if (*here == '+')
|
---|
256 | {
|
---|
257 | mult = 1;
|
---|
258 | ++here;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | // deal with the number
|
---|
263 | while ((here != end) && (*here >= '0') && (*here <= '9'))
|
---|
264 | {
|
---|
265 | i = 10*i + (*here - '0');
|
---|
266 | ++here;
|
---|
267 | }
|
---|
268 |
|
---|
269 | i *= mult;
|
---|
270 | return i;
|
---|
271 | }
|
---|
272 |
|
---|
273 | unsigned long text_t::getulong () const
|
---|
274 | {
|
---|
275 | unsigned long i = 0;
|
---|
276 |
|
---|
277 | const_iterator here = text.begin();
|
---|
278 | const_iterator end = text.end();
|
---|
279 |
|
---|
280 | while ((here != end) && (*here >= '0') && (*here <= '9'))
|
---|
281 | {
|
---|
282 | i = 10*i + (*here - '0');
|
---|
283 | ++here;
|
---|
284 | }
|
---|
285 |
|
---|
286 | return i;
|
---|
287 | }
|
---|
288 |
|
---|
289 | void text_t::appendcarr (const char *s, size_type len)
|
---|
290 | {
|
---|
291 | unsigned char *us = (unsigned char *)s;
|
---|
292 | if (text.capacity() < (text.size() + len + 2)) {
|
---|
293 | text.reserve(text.size() + len + 2);
|
---|
294 | }
|
---|
295 |
|
---|
296 | while (len > 0)
|
---|
297 | {
|
---|
298 | text.push_back (*us); // append this character
|
---|
299 | ++us;
|
---|
300 | --len;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | void text_t::appendcstr (const char *s)
|
---|
305 | {
|
---|
306 | size_t len = strlen(s);
|
---|
307 | if (text.capacity() < (text.size() + len + 2)) {
|
---|
308 | text.reserve(text.size() + len + 2);
|
---|
309 | }
|
---|
310 |
|
---|
311 | unsigned char *us = (unsigned char *)s;
|
---|
312 | while (*us != '\0')
|
---|
313 | {
|
---|
314 | text.push_back (*us); // append this character
|
---|
315 | ++us;
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | // strings returned from getcarr and getcstr become the callers
|
---|
321 | // responsibility and should be deallocated with "delete []"
|
---|
322 |
|
---|
323 | char *text_t::getcarr(size_type &len) const
|
---|
324 | {
|
---|
325 | unsigned char *cstr = new unsigned char[size()];
|
---|
326 | len = 0;
|
---|
327 |
|
---|
328 | const_iterator ithere = begin();
|
---|
329 | const_iterator itend = end();
|
---|
330 | while (ithere != itend)
|
---|
331 | {
|
---|
332 | if (*ithere < 256) cstr[len] = (unsigned char)(*ithere);
|
---|
333 | else {
|
---|
334 | // put a space or a question mark depending on what
|
---|
335 | // the character is. Question marks tell the user that
|
---|
336 | // they are missing some information.
|
---|
337 | if (is_unicode_space (*ithere)) cstr[len] = ' ';
|
---|
338 | else cstr[len] = '?';
|
---|
339 | }
|
---|
340 | ++len;
|
---|
341 | ++ithere;
|
---|
342 | }
|
---|
343 |
|
---|
344 | return (char *)cstr;
|
---|
345 | }
|
---|
346 |
|
---|
347 | char *text_t::getcstr() const
|
---|
348 | {
|
---|
349 | unsigned char *cstr = new unsigned char[size() + 1];
|
---|
350 | const_iterator ithere = begin();
|
---|
351 | const_iterator itend = end();
|
---|
352 | int len = 0;
|
---|
353 |
|
---|
354 | while (ithere != itend)
|
---|
355 | {
|
---|
356 | if (*ithere < 256) cstr[len] = (unsigned char)(*ithere);
|
---|
357 | else {
|
---|
358 | // put a space or a question mark depending on what
|
---|
359 | // the character is. Question marks tell the user that
|
---|
360 | // they are missing some information.
|
---|
361 | if (is_unicode_space (*ithere)) cstr[len] = ' ';
|
---|
362 | else cstr[len] = '?';
|
---|
363 | }
|
---|
364 | ++len;
|
---|
365 | ++ithere;
|
---|
366 | }
|
---|
367 |
|
---|
368 | cstr[len] = '\0';
|
---|
369 |
|
---|
370 | return (char *)cstr;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | // general functions which work on text_ts
|
---|
375 |
|
---|
376 | // find a character within a range
|
---|
377 | text_t::const_iterator findchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
378 | unsigned short c)
|
---|
379 | {
|
---|
380 | while (first != last)
|
---|
381 | {
|
---|
382 | if (*first == c) break;
|
---|
383 | ++first;
|
---|
384 | }
|
---|
385 | return first;
|
---|
386 | }
|
---|
387 |
|
---|
388 | text_t::iterator findchar (text_t::iterator first, text_t::iterator last,
|
---|
389 | unsigned short c)
|
---|
390 | {
|
---|
391 | while (first != last)
|
---|
392 | {
|
---|
393 | if (*first == c) break;
|
---|
394 | ++first;
|
---|
395 | }
|
---|
396 | return first;
|
---|
397 | }
|
---|
398 |
|
---|
399 | text_t::iterator findword (text_t::iterator first,
|
---|
400 | text_t::iterator last,
|
---|
401 | const text_t& word)
|
---|
402 | {
|
---|
403 | text_t::const_iterator word_begin = word.begin();
|
---|
404 | text_t::const_iterator word_end = word.end();
|
---|
405 |
|
---|
406 | while (first != last)
|
---|
407 | {
|
---|
408 | text_t::iterator char_match = first;
|
---|
409 | text_t::const_iterator word_here = word_begin;
|
---|
410 | while (word_here!=word_end)
|
---|
411 | {
|
---|
412 | if (*char_match != *word_here)
|
---|
413 | {
|
---|
414 | break;
|
---|
415 | }
|
---|
416 | ++char_match;
|
---|
417 | ++word_here;
|
---|
418 | }
|
---|
419 | if (word_here==word_end)
|
---|
420 | {
|
---|
421 | return first;
|
---|
422 | }
|
---|
423 | ++first;
|
---|
424 | }
|
---|
425 | return last; // get to here only if there is no match
|
---|
426 | }
|
---|
427 |
|
---|
428 | // get a string up to the next delimiter (which is skipped)
|
---|
429 | text_t::const_iterator getdelimitstr (text_t::const_iterator first,
|
---|
430 | text_t::const_iterator last,
|
---|
431 | unsigned short c, text_t &outstr)
|
---|
432 | {
|
---|
433 | text_t::const_iterator here = first;
|
---|
434 | here = findchar (first, last, c);
|
---|
435 | outstr.clear();
|
---|
436 | outstr.appendrange (first, here);
|
---|
437 | if (here != last) ++here; // skip c
|
---|
438 | return here;
|
---|
439 | }
|
---|
440 |
|
---|
441 | text_t::iterator getdelimitstr (text_t::iterator first, text_t::iterator last,
|
---|
442 | unsigned short c, text_t &outstr)
|
---|
443 | {
|
---|
444 | text_t::iterator here = first;
|
---|
445 | here = findchar (first, last, c);
|
---|
446 | outstr.clear();
|
---|
447 | outstr.appendrange (first, here);
|
---|
448 | if (here != last) ++here; // skip c
|
---|
449 | return here;
|
---|
450 | }
|
---|
451 |
|
---|
452 | // split a string with a character
|
---|
453 | void splitchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
454 | unsigned short c, text_tset &outlist)
|
---|
455 | {
|
---|
456 | outlist.erase(outlist.begin(), outlist.end());
|
---|
457 |
|
---|
458 | text_t t;
|
---|
459 |
|
---|
460 | while (first != last)
|
---|
461 | {
|
---|
462 | first = getdelimitstr (first, last, c, t);
|
---|
463 | outlist.insert (t);
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | void splitchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
468 | unsigned short c, text_tlist &outlist)
|
---|
469 | {
|
---|
470 | outlist.erase(outlist.begin(), outlist.end());
|
---|
471 |
|
---|
472 | text_t t;
|
---|
473 |
|
---|
474 | while (first != last)
|
---|
475 | {
|
---|
476 | first = getdelimitstr (first, last, c, t);
|
---|
477 | outlist.push_back (t);
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | void splitchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
482 | unsigned short c, text_tarray &outlist)
|
---|
483 | {
|
---|
484 | outlist.erase(outlist.begin(), outlist.end());
|
---|
485 |
|
---|
486 | text_t t;
|
---|
487 |
|
---|
488 | while (first != last)
|
---|
489 | {
|
---|
490 | first = getdelimitstr (first, last, c, t);
|
---|
491 | outlist.push_back (t);
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | // join a string using a character
|
---|
496 | void joinchar (const text_tset &inlist, unsigned short c, text_t &outtext)
|
---|
497 | {
|
---|
498 | outtext.clear ();
|
---|
499 |
|
---|
500 | text_tset::const_iterator here = inlist.begin ();
|
---|
501 | text_tset::const_iterator end = inlist.end ();
|
---|
502 |
|
---|
503 | if (here != end) {
|
---|
504 | outtext += *here; ++here;
|
---|
505 | while (here != end) {
|
---|
506 | outtext.push_back (c);
|
---|
507 | outtext += *here;
|
---|
508 | ++here;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | void joinchar (const text_tlist &inlist, unsigned short c, text_t &outtext)
|
---|
514 | {
|
---|
515 | outtext.clear ();
|
---|
516 |
|
---|
517 | text_tlist::const_iterator here = inlist.begin ();
|
---|
518 | text_tlist::const_iterator end = inlist.end ();
|
---|
519 | if (here != end) {
|
---|
520 | outtext += *here; ++here;
|
---|
521 | while (here != end) {
|
---|
522 | outtext.push_back (c);
|
---|
523 | outtext += *here;
|
---|
524 | ++here;
|
---|
525 | }
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | void joinchar (const text_tarray &inlist, unsigned short c, text_t &outtext)
|
---|
530 | {
|
---|
531 | outtext.clear ();
|
---|
532 |
|
---|
533 | text_tarray::const_iterator here = inlist.begin ();
|
---|
534 | text_tarray::const_iterator end = inlist.end ();
|
---|
535 | if (here != end) {
|
---|
536 | outtext += *here; ++here;
|
---|
537 | while (here != end) {
|
---|
538 | outtext.push_back (c);
|
---|
539 | outtext += *here;
|
---|
540 | ++here;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | void joinchar (const text_tlist &inlist, const text_t &c, text_t &outtext)
|
---|
546 | {
|
---|
547 | outtext.clear ();
|
---|
548 |
|
---|
549 | text_tlist::const_iterator here = inlist.begin ();
|
---|
550 | text_tlist::const_iterator end = inlist.end ();
|
---|
551 | if (here != end) {
|
---|
552 | outtext += *here; ++here;
|
---|
553 | while (here != end) {
|
---|
554 | outtext += c;
|
---|
555 | outtext += *here;
|
---|
556 | ++here;
|
---|
557 | }
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | void joinchar (const text_tset &inlist, const text_t &c, text_t &outtext)
|
---|
562 | {
|
---|
563 | outtext.clear ();
|
---|
564 |
|
---|
565 | text_tset::const_iterator here = inlist.begin ();
|
---|
566 | text_tset::const_iterator end = inlist.end ();
|
---|
567 | if (here != end) {
|
---|
568 | outtext += *here; ++here;
|
---|
569 | while (here != end) {
|
---|
570 | outtext += c;
|
---|
571 | outtext += *here;
|
---|
572 | ++here;
|
---|
573 | }
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | void joinchar (const text_tarray &inlist, const text_t &c, text_t &outtext)
|
---|
578 | {
|
---|
579 | outtext.clear ();
|
---|
580 |
|
---|
581 | text_tarray::const_iterator here = inlist.begin ();
|
---|
582 | text_tarray::const_iterator end = inlist.end ();
|
---|
583 | if (here != end) {
|
---|
584 | outtext += *here; ++here;
|
---|
585 | while (here != end) {
|
---|
586 | outtext += c;
|
---|
587 | outtext += *here;
|
---|
588 | ++here;
|
---|
589 | }
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | // count the occurances of a character within a range
|
---|
594 | int countchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
595 | unsigned short c)
|
---|
596 | {
|
---|
597 | int count = 0;
|
---|
598 | while (first != last) {
|
---|
599 | if (*first == c) ++count;
|
---|
600 | ++first;
|
---|
601 | }
|
---|
602 | return count;
|
---|
603 | }
|
---|
604 |
|
---|
605 | // return a substring of string from first up to but not including last
|
---|
606 | text_t substr (text_t::const_iterator first, text_t::const_iterator last) {
|
---|
607 |
|
---|
608 | text_t substr; substr.reserve(last - first + 2);
|
---|
609 | while (first != last) {
|
---|
610 | substr.push_back(*first);
|
---|
611 | ++first;
|
---|
612 | }
|
---|
613 | return substr;
|
---|
614 | }
|
---|
615 |
|
---|
616 |
|
---|
617 | // convert to lowercase
|
---|
618 | void lc (text_t::iterator first, text_t::iterator last) {
|
---|
619 | while (first != last) {
|
---|
620 | *first = unicode_tolower(*first);
|
---|
621 | ++first;
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | // convert to uppercase
|
---|
626 | void uc (text_t::iterator first, text_t::iterator last) {
|
---|
627 | while (first != last) {
|
---|
628 | *first = unicode_toupper(*first);
|
---|
629 | ++first;
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | // checks to see if it is a number (i.e. contains only 0-9)
|
---|
635 | bool is_number (const text_t &text) {
|
---|
636 |
|
---|
637 | text_t::const_iterator here = text.begin();
|
---|
638 | text_t::const_iterator end = text.end();
|
---|
639 |
|
---|
640 | while (here != end) {
|
---|
641 | if ((*here!='0') && (*here!='1') && (*here!='2') &&
|
---|
642 | (*here!='3') && (*here!='4') && (*here!='5') &&
|
---|
643 | (*here!='6') && (*here!='7') && (*here!='8') &&
|
---|
644 | (*here!='9')) return false;
|
---|
645 | ++here;
|
---|
646 | }
|
---|
647 | return true;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | // checks to see if the text has any letters or digits
|
---|
652 | bool has_unicode_letdig (const text_t &text) {
|
---|
653 | if (text.empty()) return false;
|
---|
654 |
|
---|
655 | text_t::const_iterator here = text.begin();
|
---|
656 | text_t::const_iterator end = text.end();
|
---|
657 | while (here != end) {
|
---|
658 | if (is_unicode_letdig (*here)) return true;
|
---|
659 | ++here;
|
---|
660 | }
|
---|
661 |
|
---|
662 | return false;
|
---|
663 | }
|
---|
664 |
|
---|
665 | // checks to see if a text_t starts with the specified prefix
|
---|
666 | bool starts_with(const text_t& text, const text_t& prefix) {
|
---|
667 | if (prefix.empty()) return true;
|
---|
668 | if (text.empty() || text.size()<prefix.size()) return false;
|
---|
669 | text_t substring = substr(text.begin(), text.begin()+prefix.size());
|
---|
670 | return substring == prefix;
|
---|
671 | }
|
---|
672 | // checks to see if a text_t ends with the specified suffix
|
---|
673 | bool ends_with(const text_t& text, const text_t& suffix) {
|
---|
674 | if (suffix.empty()) return true;
|
---|
675 | if (text.empty() || text.size() < suffix.size()) return false;
|
---|
676 | text_t substring = substr(text.end()-suffix.size(),text.end());
|
---|
677 | return substring == suffix;
|
---|
678 |
|
---|
679 | }
|
---|
680 |
|
---|
681 |
|
---|
682 | ////////////////////////////////////
|
---|
683 | // convertclass methods
|
---|
684 | ////////////////////////////////////
|
---|
685 |
|
---|
686 | // conversion classes used for getting information in to and out of
|
---|
687 | // the text_t class.
|
---|
688 |
|
---|
689 | convertclass::convertclass ()
|
---|
690 | {
|
---|
691 | // nothing to do
|
---|
692 | }
|
---|
693 |
|
---|
694 | void convertclass::reset ()
|
---|
695 | {
|
---|
696 | // nothing to do
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | ////////////////////////////////////
|
---|
701 | // inconvertclass methods
|
---|
702 | ////////////////////////////////////
|
---|
703 |
|
---|
704 | // convert from a char stream to the text_t class
|
---|
705 | // the default version assumes the input is a ascii
|
---|
706 | // character array
|
---|
707 |
|
---|
708 | inconvertclass::inconvertclass ()
|
---|
709 | {
|
---|
710 | start = NULL;
|
---|
711 | len = 0;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | void inconvertclass::reset ()
|
---|
716 | {
|
---|
717 | start = NULL;
|
---|
718 | len = 0;
|
---|
719 | }
|
---|
720 |
|
---|
721 | void inconvertclass::setinput (char *thestart, size_t thelen)
|
---|
722 | {
|
---|
723 | start = thestart;
|
---|
724 | len = thelen;
|
---|
725 | }
|
---|
726 |
|
---|
727 | void inconvertclass::convert (text_t &output, status_t &status)
|
---|
728 | {
|
---|
729 | output.clear();
|
---|
730 |
|
---|
731 | if (start == NULL || len == 0)
|
---|
732 | {
|
---|
733 | status = finished;
|
---|
734 | return;
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (output.capacity() < len + 2)
|
---|
738 | output.reserve(len + 2);
|
---|
739 |
|
---|
740 | // don't want any funny sign conversions happening
|
---|
741 | unsigned char *here = (unsigned char *)start;
|
---|
742 | while (len > 0)
|
---|
743 | {
|
---|
744 | output.push_back (*here); // append this character
|
---|
745 | ++here;
|
---|
746 | --len;
|
---|
747 | }
|
---|
748 |
|
---|
749 | start = (char *)here; // save current position
|
---|
750 | status = finished;
|
---|
751 | }
|
---|
752 |
|
---|
753 | // will treat the text_t as a 8-bit string and convert
|
---|
754 | // it to a 16-bit string using the about convert method.
|
---|
755 | text_t inconvertclass::convert (const text_t &t) {
|
---|
756 | text_t out;
|
---|
757 | text_t tmpout;
|
---|
758 | status_t status;
|
---|
759 | text_t::const_iterator here = t.begin();
|
---|
760 | text_t::const_iterator end = t.end();
|
---|
761 | unsigned char cbuf[256];
|
---|
762 | size_t cbuflen = 0;
|
---|
763 |
|
---|
764 | out.clear();
|
---|
765 | if (out.capacity() < t.size() + 2)
|
---|
766 | out.reserve(t.size() + 2);
|
---|
767 | while (here != end) {
|
---|
768 | while (here != end && cbuflen < 256) {
|
---|
769 | cbuf[cbuflen++] = (unsigned char)(*here & 0xff);
|
---|
770 | ++here;
|
---|
771 | }
|
---|
772 |
|
---|
773 | if (cbuflen > 0) {
|
---|
774 | setinput ((char *)cbuf, cbuflen);
|
---|
775 | status = unfinished;
|
---|
776 | while (status == unfinished) {
|
---|
777 | convert (tmpout, status);
|
---|
778 | out += tmpout;
|
---|
779 | }
|
---|
780 | cbuflen = 0;
|
---|
781 | }
|
---|
782 | }
|
---|
783 |
|
---|
784 | out.setencoding (0); // unicode
|
---|
785 |
|
---|
786 | return out;
|
---|
787 | }
|
---|
788 |
|
---|
789 | // an instance of the default inconvertclass to do simple
|
---|
790 | // conversions. Note that any functions that use this are
|
---|
791 | // not reentrant. If a function needs to be reentrant it
|
---|
792 | // should declare its own instance.
|
---|
793 | inconvertclass ascii2text_t;
|
---|
794 |
|
---|
795 |
|
---|
796 | ////////////////////////////////////
|
---|
797 | // outconvertclass methods
|
---|
798 | ////////////////////////////////////
|
---|
799 |
|
---|
800 | // Convert from a text_t class to a char stream
|
---|
801 | // This default version assumes the output is a ascii
|
---|
802 | // character array. If you set the output stream you
|
---|
803 | // can use this class to output to a stream using the
|
---|
804 | // << operator. The << operator can also be conveniently
|
---|
805 | // used to set the output stream by doing something like
|
---|
806 | //
|
---|
807 | // cout << text_t2ascii << text_tstr << anothertext_tstr;
|
---|
808 | //
|
---|
809 | outconvertclass::outconvertclass ()
|
---|
810 | {
|
---|
811 | input = NULL;
|
---|
812 | outs = NULL;
|
---|
813 | }
|
---|
814 |
|
---|
815 | void outconvertclass::reset ()
|
---|
816 | {
|
---|
817 | input = NULL;
|
---|
818 | outs = NULL;
|
---|
819 | }
|
---|
820 |
|
---|
821 | void outconvertclass::setinput (text_t *theinput)
|
---|
822 | {
|
---|
823 | input = theinput;
|
---|
824 | if (input != NULL) texthere = input->begin();
|
---|
825 | }
|
---|
826 |
|
---|
827 | void outconvertclass::setdata(text_t *theinput, text_t::iterator thetexthere)
|
---|
828 | {
|
---|
829 | input = theinput;
|
---|
830 | texthere = thetexthere;
|
---|
831 | }
|
---|
832 |
|
---|
833 | void outconvertclass::convert (char *output, size_t maxlen,
|
---|
834 | size_t &len, status_t &status)
|
---|
835 | {
|
---|
836 | if (input == NULL || output == NULL)
|
---|
837 | {
|
---|
838 | status = finished;
|
---|
839 | return;
|
---|
840 | }
|
---|
841 |
|
---|
842 | // don't want any funny sign conversions happening
|
---|
843 | unsigned char *uoutput = (unsigned char *)output;
|
---|
844 | text_t::iterator textend = input->end();
|
---|
845 | len = 0;
|
---|
846 | while ((len < maxlen) && (texthere != textend))
|
---|
847 | {
|
---|
848 | if (*texthere < 256) *uoutput = (unsigned char)(*texthere);
|
---|
849 | else {
|
---|
850 | // put a space or a question mark depending on what
|
---|
851 | // the character is. Question marks tell the user that
|
---|
852 | // they are missing some information.
|
---|
853 | if (is_unicode_space (*texthere)) *uoutput = ' ';
|
---|
854 | else *uoutput = '?';
|
---|
855 | }
|
---|
856 | ++uoutput;
|
---|
857 | ++len;
|
---|
858 | ++texthere;
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (texthere == textend) status = finished;
|
---|
862 | else status = unfinished;
|
---|
863 | }
|
---|
864 |
|
---|
865 | // will convert the 16-bit string to a 8-bit stream
|
---|
866 | // and place the result in a text_t. This method uses
|
---|
867 | // the above convert function.
|
---|
868 | text_t outconvertclass::convert (const text_t &t) {
|
---|
869 | text_t out;
|
---|
870 | unsigned char cbuf[256];
|
---|
871 | size_t cbuflen = 0;
|
---|
872 | status_t status = unfinished;
|
---|
873 |
|
---|
874 | out.clear();
|
---|
875 | if (out.capacity() < t.size() + 2)
|
---|
876 | out.reserve(t.size() + 2);
|
---|
877 | setinput ((text_t *)&t); // discard constant
|
---|
878 | while (status == unfinished) {
|
---|
879 | convert ((char *)cbuf, 256, cbuflen, status);
|
---|
880 | out.appendcarr ((char *)cbuf, cbuflen);
|
---|
881 | }
|
---|
882 |
|
---|
883 | out.setencoding (1); // other encoding
|
---|
884 |
|
---|
885 | return out;
|
---|
886 | }
|
---|
887 |
|
---|
888 |
|
---|
889 | void outconvertclass::setostream (ostream *theouts)
|
---|
890 | {
|
---|
891 | outs = theouts;
|
---|
892 | }
|
---|
893 |
|
---|
894 | ostream *outconvertclass::getostream ()
|
---|
895 | {
|
---|
896 | return outs;
|
---|
897 | }
|
---|
898 |
|
---|
899 |
|
---|
900 |
|
---|
901 |
|
---|
902 | // an instance of the default outconvertclass to do simple
|
---|
903 | // conversions
|
---|
904 | outconvertclass text_t2ascii;
|
---|
905 |
|
---|
906 |
|
---|
907 |
|
---|
908 | // stream operators for the output class
|
---|
909 |
|
---|
910 | outconvertclass &operator<< (ostream &theouts, outconvertclass &outconverter)
|
---|
911 | {
|
---|
912 | outconverter.setostream(&theouts);
|
---|
913 | return outconverter;
|
---|
914 | }
|
---|
915 |
|
---|
916 |
|
---|
917 | #define STREAMBUFSIZE 256
|
---|
918 | outconvertclass &operator<< (outconvertclass &outconverter, const text_t &t)
|
---|
919 | {
|
---|
920 | ostream *outstream = outconverter.getostream();
|
---|
921 |
|
---|
922 | if (outstream == NULL) return outconverter;
|
---|
923 |
|
---|
924 | char outbuf[STREAMBUFSIZE];
|
---|
925 | size_t len;
|
---|
926 | outconvertclass::status_t status = outconvertclass::unfinished;
|
---|
927 |
|
---|
928 | // assume that there is no data needing converting
|
---|
929 | // left in the converter
|
---|
930 | outconverter.setinput ((text_t *)(&t)); // note the const -> nonconst conversion
|
---|
931 |
|
---|
932 | while (status == outconvertclass::unfinished)
|
---|
933 | {
|
---|
934 | outconverter.convert (outbuf, STREAMBUFSIZE, len, status);
|
---|
935 | if (len > 0) outstream->write(outbuf, len);
|
---|
936 | }
|
---|
937 |
|
---|
938 | return outconverter;
|
---|
939 | }
|
---|