source: trunk/gsdl/src/library/text_t.h@ 4

Last change on this file since 4 was 4, checked in by sjboddie, 25 years ago

Initial revision

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/**************************************************************************
2 *
3 * text_t.h -- a simple 16-bit charater string class
4 * Copyright (c) 1998 -- Rodger McNab
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: text_t.h 4 1998-11-17 01:10:39Z sjboddie $
21 *
22 **************************************************************************/
23
24
25
26#ifndef TEXT_T_H
27#define TEXT_T_H
28
29#ifndef USE_OBJECTSPACE
30# include <vector>
31# include <list>
32# include <set>
33#else
34# include <ospace\std\vector>
35# include <ospace\std\list>
36# include <ospace\std\set>
37#endif
38
39// use the standard namespace
40#ifdef __GNUG__
41// namespaces are broken in current version of gcc
42#else
43# ifndef USE_OBJECTSPACE
44using namespace std;
45# else
46# ifndef OS_NO_NAMESPACE
47using namespace ospace::std;
48# endif
49# endif
50#endif
51
52
53// class prototypes
54class inconvertclass;
55class outconvertclass;
56
57
58typedef vector<unsigned short> usvector;
59
60// The class text_t can handle long strings which may contain
61// null characters. It uses unsigned shorts to represent up to
62// 64K character values.
63class text_t {
64 public:
65 //type support for ucvector
66 typedef usvector::iterator iterator;
67 typedef usvector::const_iterator const_iterator;
68 typedef usvector::reference reference;
69 typedef usvector::const_reference const_reference;
70 typedef usvector::size_type size_type;
71 typedef usvector::difference_type difference_type;
72 typedef usvector::const_reverse_iterator const_reverse_iterator;
73 typedef usvector::reverse_iterator reverse_iterator;
74
75protected:
76 usvector text;
77
78public:
79 // constructors
80 text_t ();
81 text_t (int i);
82 text_t (char *s); // assumed to be a normal c string
83
84 // basic container support
85 iterator begin () {return text.begin();}
86 const_iterator begin () const {return text.begin();}
87 iterator end () {return text.end();}
88 const_iterator end () const {return text.end();}
89
90 void erase(iterator pos) {text.erase(pos);}
91 void erase(iterator first, iterator last) {text.erase(first, last);}
92 void push_back(unsigned short c) {text.push_back(c);}
93 void pop_back() {text.pop_back();}
94 text_t &operator=(const text_t &x) {text=x.text;return *this;}
95 reference operator[](size_type n) {return text[n];};
96 const_reference operator[](size_type n) const {return text[n];};
97
98 bool empty () const {return text.empty();}
99 size_type size() const {return text.size();}
100 friend bool operator!=(const text_t& x, const text_t& y);
101 friend bool operator==(const text_t& x, const text_t& y);
102 friend bool operator<(const text_t& x, const text_t& y);
103 friend bool operator>(const text_t& x, const text_t& y);
104
105 // added functionality
106 void clear () {text.erase(text.begin(),text.end());}
107 void append (const text_t &t);
108 void appendrange (iterator first, iterator last);
109 void appendrange (const_iterator first, const_iterator last);
110 text_t &operator+= (const text_t &t) {append(t);return *this;}
111
112 // support for integers
113 void appendint (int i);
114 void setint (int i) {clear();appendint(i);}
115 text_t &operator=(int i) {setint (i);return *this;}
116 text_t &operator+= (int i) {appendint(i);return *this;}
117 int getint ();
118
119 // support for arrays of chars
120 void appendcarr (char *s, size_type len);
121 void setcarr (char *s, size_type len) {clear();appendcarr(s,len);}
122
123 // support for null-terminated C strings
124 void appendcstr (char *s);
125 void setcstr (char *s) {clear();appendcstr(s);}
126 text_t &operator= (char *s) {setcstr(s);return *this;} // c string
127 text_t &operator+= (char *s) {appendcstr(s);return *this;} // c string
128
129 // strings returned from getcarr and getcstr become the callers
130 // responsibility and should be deallocated with "delete"
131 char *getcarr(size_type &len) const;
132 char *getcstr() const;
133};
134
135
136inline text_t operator+(const text_t &t1, const text_t &t2)
137{
138 text_t tnew = t1;
139 tnew.append(t2);
140 return tnew;
141}
142
143inline text_t operator+(const text_t &t1, int i1)
144{
145 text_t tnew = t1;
146 tnew.appendint(i1);
147 return tnew;
148}
149
150inline text_t operator+(const text_t &t1, char *s1)
151{
152 text_t tnew = t1;
153 tnew.appendcstr(s1);
154 return tnew;
155}
156
157
158inline bool operator!=(const text_t& x, const text_t& y)
159{
160 return (x.text != y.text);
161}
162
163inline bool operator==(const text_t& x, const text_t& y)
164{
165 return (x.text == y.text);
166}
167
168inline bool operator<(const text_t& x, const text_t& y)
169{
170 return (x.text < y.text);
171}
172
173inline bool operator>(const text_t& x, const text_t& y)
174{
175 return (x.text > y.text);
176}
177
178
179struct eqtext_t
180{
181 bool operator()(const text_t t1, const text_t t2) const
182 { return t1 == t2; }
183};
184
185struct lttext_t
186{
187 bool operator()(const text_t t1, const text_t t2) const
188 { return t1 < t2; }
189};
190
191
192// frequently used derived types
193typedef set<text_t,lttext_t> text_tset;
194typedef list<text_t> text_tlist; // more efficient for insertions/deletions
195typedef vector<text_t> text_tarray; // more space efficient than text_tlist
196
197
198// general functions which work on text_ts
199
200// find a character within a range
201text_t::const_iterator findchar (text_t::const_iterator first, text_t::const_iterator last,
202 unsigned short c);
203text_t::iterator findchar (text_t::iterator first, text_t::iterator last,
204 unsigned short c);
205
206// get a string up to the next delimiter (which is skipped)
207text_t::const_iterator getdelimitstr (text_t::const_iterator first,
208 text_t::const_iterator last,
209 unsigned short c, text_t &outstr);
210text_t::iterator getdelimitstr (text_t::iterator first, text_t::iterator last,
211 unsigned short c, text_t &outstr);
212
213// split a string with a character
214void splitchar (text_t::const_iterator first, text_t::const_iterator last,
215 unsigned short c, text_tlist &outlist);
216void splitchar (text_t::const_iterator first, text_t::const_iterator last,
217 unsigned short c, text_tarray &outlist);
218
219// join a string using a character
220void joinchar (const text_tlist &inlist, unsigned short c, text_t &outtext);
221void joinchar (const text_tarray &inlist, unsigned short c, text_t &outtext);
222
223// count the occurances of a character within a range
224int countchar (text_t::const_iterator first, text_t::const_iterator last,
225 unsigned short c);
226
227
228
229
230
231
232
233
234// conversion classes used for getting information in to and out of
235// the text_t class.
236
237class convertclass
238{
239public:
240 enum status_t {finished, stopped, unfinished};
241
242 convertclass ();
243 virtual void reset ();
244};
245
246
247
248// convert from a char stream to the text_t class
249// the default version assumes the input is a ascii
250// character array
251class inconvertclass : public convertclass
252{
253public:
254 inconvertclass ();
255 void reset ();
256 void setinput (char *thestart, size_t thelen);
257 virtual void convert (text_t &output, status_t &status);
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 void setostream (ostream *theouts);
293 ostream *getostream ();
294
295protected:
296 text_t *input;
297 text_t::iterator texthere; // only valid if input is valid
298
299 ostream *outs;
300};
301
302// to get something which will do the conversion
303// to text_t declare a (non global!) instance like
304// this
305// outconvertclass text_t2ascii;
306
307
308// stream operators for the output class
309outconvertclass &operator<< (ostream &theouts, outconvertclass &outconverter);
310outconvertclass &operator<< (outconvertclass &outconverter, const text_t &t);
311
312
313#endif
Note: See TracBrowser for help on using the repository browser.