source: gsdl/trunk/lib/text_t.cpp@ 15077

Last change on this file since 15077 was 15077, checked in by mdewsnip, 15 years ago

Oh dear... there were two pretty big problems with the replace() function: it wouldn't work correctly replacing strings longer than one character, and it wouldn't replace a match at the start of the string.

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