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