source: trunk/gsdl/lib/text_t.h@ 1860

Last change on this file since 1860 was 1860, checked in by cs025, 23 years ago

Included CORBA branch for first time

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/**********************************************************************
2 *
3 * text_t.h -- 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.h 1860 2001-01-25 18:26:45Z cs025 $
25 *
26 *********************************************************************/
27
28
29#ifndef TEXT_T_H
30#define TEXT_T_H
31
32#include "gsdlconf.h"
33
34#if defined(GSDL_USE_OBJECTSPACE)
35# include <ospace\std\vector>
36# include <ospace\std\list>
37# include <ospace\std\set>
38# include <ospace\std\map>
39#elif defined(GSDL_USE_STL_H)
40# include <vector.h>
41# include <list.h>
42# include <set.h>
43# include <map.h>
44#else
45# include <vector>
46# include <list>
47# include <set>
48# include <map>
49#endif
50
51// use the standard namespace
52#if !defined (GSDL_NAMESPACE_BROKEN)
53#if defined(GSDL_USE_OBJECTSPACE)
54using namespace ospace::std;
55#else
56using namespace std;
57#endif
58#endif
59
60// class prototypes
61class inconvertclass;
62class outconvertclass;
63
64
65// for those stupid compilers which need it
66#if defined(GSDL_NEED_DESTROY_USHORT)
67inline void destroy(unsigned short *) {};
68inline void destroy(int *) {};
69#endif
70
71typedef vector<unsigned short> usvector;
72
73// The class text_t can handle long strings which may contain
74// null characters. It uses unsigned shorts to represent up to
75// 64K character values.
76class text_t {
77 public:
78 //type support for ucvector
79 typedef usvector::iterator iterator;
80 typedef usvector::const_iterator const_iterator;
81 typedef usvector::reference reference;
82 typedef usvector::const_reference const_reference;
83 typedef usvector::size_type size_type;
84 typedef usvector::difference_type difference_type;
85 typedef usvector::const_reverse_iterator const_reverse_iterator;
86 typedef usvector::reverse_iterator reverse_iterator;
87
88protected:
89 usvector text;
90 unsigned short encoding; // 0 = unicode, 1 = other
91
92public:
93 // constructors
94 text_t ();
95 text_t (int i);
96 text_t (char *s); // assumed to be a normal c string
97
98 void setencoding (unsigned short theencoding) {encoding=theencoding;};
99 unsigned short getencoding () {return encoding;};
100
101 usvector& text_as_usvector() { return text ; };
102 const usvector& text_as_usvector() const { return text ; };
103
104 // basic container support
105 iterator begin () {return text.begin();}
106 const_iterator begin () const {return text.begin();}
107 iterator end () {return text.end();}
108 const_iterator end () const {return text.end();}
109
110 void erase(iterator pos) {text.erase(pos);}
111 void erase(iterator first, iterator last) {text.erase(first, last);}
112 void push_back(unsigned short c) {text.push_back(c);}
113 void pop_back() {text.pop_back();}
114 text_t &operator=(const text_t &x) {text=x.text; encoding=x.encoding; return *this;}
115 reference operator[](size_type n) {return text[n];};
116 const_reference operator[](size_type n) const {return text[n];};
117
118 void reserve (size_type n) {text.reserve(n);}
119
120 bool empty () const {return text.empty();}
121 size_type size() const {return text.size();}
122 friend inline bool operator!=(const text_t& x, const text_t& y)
123 {return (x.text != y.text);}
124 friend inline bool operator==(const text_t& x, const text_t& y)
125 {return (x.text == y.text);}
126 friend inline bool operator<(const text_t& x, const text_t& y)
127 {return (x.text < y.text);}
128 friend inline bool operator>(const text_t& x, const text_t& y)
129 {return (x.text > y.text);}
130 friend inline bool operator>=(const text_t& x, const text_t& y)
131 {return (x.text >= y.text);}
132 friend inline bool operator<=(const text_t& x, const text_t& y)
133 {return (x.text <= y.text);}
134
135 // added functionality
136 void clear () {text.erase(text.begin(),text.end());}
137 void append (const text_t &t);
138 void appendrange (iterator first, iterator last);
139 void appendrange (const_iterator first, const_iterator last);
140 text_t &operator+= (const text_t &t) {append(t);return *this;}
141
142 // support for integers
143 void appendint (int i);
144 void setint (int i) {clear();appendint(i);}
145 text_t &operator=(int i) {setint (i);return *this;}
146 text_t &operator+= (int i) {appendint(i);return *this;}
147 int getint () const;
148
149 // support for arrays of chars
150 void appendcarr (char *s, size_type len);
151 void setcarr (char *s, size_type len) {clear();appendcarr(s,len);}
152
153 // support for null-terminated C strings
154 void appendcstr (char *s);
155 void setcstr (char *s) {clear();appendcstr(s);}
156 text_t &operator= (char *s) {setcstr(s);return *this;} // c string
157 text_t &operator+= (char *s) {appendcstr(s);return *this;} // c string
158
159 // support for const null-terminated C string
160 void appendcstr (const char *s) {appendcstr((char *)s);}
161 void setcstr (const char *s) {clear();appendcstr((char *) s);}
162 text_t &operator= (const char *s) {setcstr((char *) s);return *this;} // c string
163 text_t &operator+= (const char *s) {appendcstr((char *) s);return *this;} // c string
164
165 // strings returned from getcarr and getcstr become the callers
166 // responsibility and should be deallocated with "delete"
167 char *getcarr(size_type &len) const;
168 char *getcstr() const;
169
170};
171
172// new stream converter ...
173ostream& operator<< (ostream &o, const text_t text);
174
175inline text_t operator+(const text_t &t1, const text_t &t2)
176{
177 text_t tnew = t1;
178 tnew.append(t2);
179 return tnew;
180}
181
182inline text_t operator+(const text_t &t1, int i1)
183{
184 text_t tnew = t1;
185 tnew.appendint(i1);
186 return tnew;
187}
188
189inline text_t operator+(const text_t &t1, char *s1)
190{
191 text_t tnew = t1;
192 tnew.appendcstr(s1);
193 return tnew;
194}
195
196
197
198
199struct eqtext_t
200{
201 bool operator()(const text_t &t1, const text_t &t2) const
202 { return t1 == t2; }
203};
204
205struct lttext_t
206{
207 bool operator()(const text_t &t1, const text_t &t2) const
208 { return t1 < t2; }
209};
210
211
212// frequently used derived types
213typedef set<text_t,lttext_t> text_tset;
214typedef list<text_t> text_tlist; // more efficient for insertions/deletions
215typedef vector<text_t> text_tarray; // more space efficient than text_tlist
216typedef map<text_t, text_t, lttext_t> text_tmap;
217
218
219// general functions which work on text_ts
220
221// find a character within a range
222text_t::const_iterator findchar (text_t::const_iterator first, text_t::const_iterator last,
223 unsigned short c);
224text_t::iterator findchar (text_t::iterator first, text_t::iterator last,
225 unsigned short c);
226
227text_t::iterator findword (text_t::iterator first, text_t::iterator last,
228 const text_t &word);
229
230// get a string up to the next delimiter (which is skipped)
231text_t::const_iterator getdelimitstr (text_t::const_iterator first,
232 text_t::const_iterator last,
233 unsigned short c, text_t &outstr);
234text_t::iterator getdelimitstr (text_t::iterator first, text_t::iterator last,
235 unsigned short c, text_t &outstr);
236
237// split a string with a character
238void splitchar (text_t::const_iterator first, text_t::const_iterator last,
239 unsigned short c, text_tset &outlist);
240void splitchar (text_t::const_iterator first, text_t::const_iterator last,
241 unsigned short c, text_tlist &outlist);
242void splitchar (text_t::const_iterator first, text_t::const_iterator last,
243 unsigned short c, text_tarray &outlist);
244
245// join a string using a character
246void joinchar (const text_tset &inlist, unsigned short c, text_t &outtext);
247void joinchar (const text_tlist &inlist, unsigned short c, text_t &outtext);
248void joinchar (const text_tarray &inlist, unsigned short c, text_t &outtext);
249void joinchar (const text_tset &inlist, text_t c, text_t &outtext);
250void joinchar (const text_tlist &inlist, text_t c, text_t &outtext);
251void joinchar (const text_tarray &inlist, text_t c, text_t &outtext);
252
253// count the occurances of a character within a range
254int countchar (text_t::const_iterator first, text_t::const_iterator last,
255 unsigned short c);
256
257// return a substring of string from first up to but not including last
258text_t substr (text_t::const_iterator first, text_t::const_iterator last);
259
260// convert to lowercase
261void lc (text_t::iterator first, text_t::iterator last);
262inline void lc (text_t &t) {lc (t.begin(), t.end());}
263
264// convert to uppercase
265void uc (text_t::iterator first, text_t::iterator last);
266inline void uc (text_t &t) {uc (t.begin(), t.end());}
267
268// checks to see if it is a number (i.e. contains only 0-9)
269bool is_number (const text_t &text);
270
271// checks to see if the text has any letters or digits
272bool has_unicode_letdig (const text_t &text);
273
274
275// conversion classes used for getting information in to and out of
276// the text_t class.
277
278class convertclass
279{
280public:
281 enum status_t {finished, stopped, unfinished};
282
283 convertclass ();
284 virtual void reset ();
285};
286
287
288
289// convert from a char stream to the text_t class
290// the default version assumes the input is a ascii
291// character array
292class inconvertclass : public convertclass
293{
294public:
295 inconvertclass ();
296 void reset ();
297 void setinput (char *thestart, size_t thelen);
298
299 // output will be cleared before the conversion
300 virtual void convert (text_t &output, status_t &status);
301
302 // will treat the text_t as a 8-bit string and convert
303 // it to a 16-bit string using the about convert method.
304 text_t convert (const text_t &t);
305
306protected:
307 char *start;
308 size_t len;
309};
310
311// to get something which will do the conversion
312// to ascii declare a (non global!) instance like
313// this
314// inconvertclass ascii2text_t;
315
316#if defined(GSDL_USE_IOS_H)
317#include <ostream.h>
318#else
319#include <ostream>
320#endif
321
322// Convert from a text_t class to a char stream
323// This default version assumes the output is a ascii
324// character array. If you set the output stream you
325// can use this class to output to a stream using the
326// << operator. The << operator can also be conveniently
327// used to set the output stream by doing something like
328//
329// cout << text_t2ascii << textstr << anothertextstr;
330//
331// this class assumes that the input text doesn't change
332// while the conversion takes place
333class outconvertclass : public convertclass
334{
335public:
336 outconvertclass ();
337 void reset ();
338 void setinput (text_t *theinput);
339 // note that convert does not null-terminate the
340 // output array of characters
341 virtual void convert (char *output, size_t maxlen,
342 size_t &len, status_t &status);
343
344 // will convert the 16-bit string to a 8-bit stream
345 // and place the result in a text_t. This method uses
346 // the above convert function.
347 text_t convert (const text_t &t);
348
349 void setostream (ostream *theouts);
350 ostream *getostream ();
351
352protected:
353 text_t *input;
354 text_t::iterator texthere; // only valid if input is valid
355
356 ostream *outs;
357};
358
359// to get something which will do the conversion
360// to text_t declare a (non global!) instance like
361// this
362// outconvertclass text_t2ascii;
363
364
365// stream operators for the output class
366outconvertclass &operator<< (ostream &theouts, outconvertclass &outconverter);
367outconvertclass &operator<< (outconvertclass &outconverter, const text_t &t);
368
369#endif
Note: See TracBrowser for help on using the repository browser.