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