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

Last change on this file since 120 was 114, checked in by rjmcnab, 25 years ago

Made the source more portable.

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