[1076] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * text_t.cpp -- 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.cpp 12504 2006-08-23 05:17:29Z kjdon $
|
---|
| 25 | *
|
---|
[1076] | 26 | *********************************************************************/
|
---|
| 27 |
|
---|
| 28 | #include "text_t.h"
|
---|
| 29 |
|
---|
| 30 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 31 | # include <ospace\std\algorithm>
|
---|
| 32 | #elif defined(GSDL_USE_STL_H)
|
---|
| 33 | # if defined(GSDL_USE_ALGO_H)
|
---|
| 34 | # include <algo.h>
|
---|
| 35 | # else
|
---|
| 36 | # include <algorithm.h>
|
---|
| 37 | # endif
|
---|
| 38 | #else
|
---|
| 39 | # include <algorithm>
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
[1860] | 42 | #ifdef HAVE_CONFIG_H
|
---|
| 43 | # ifdef __WIN32__
|
---|
| 44 | # include "WIN32cfg.h"
|
---|
| 45 | # else
|
---|
| 46 | # include "config.h"
|
---|
| 47 | # endif
|
---|
| 48 | #endif
|
---|
[1076] | 49 |
|
---|
[1860] | 50 |
|
---|
[1076] | 51 | #include "unitool.h"
|
---|
| 52 |
|
---|
[7382] | 53 | const text_t g_EmptyText("");
|
---|
| 54 |
|
---|
[1076] | 55 | ////////////////////////////////////
|
---|
| 56 | // text_t methods
|
---|
| 57 | ////////////////////////////////////
|
---|
| 58 |
|
---|
[1860] | 59 | // new stream converter ...
|
---|
[8727] | 60 | ostream& operator<< (ostream &o, const text_t &text)
|
---|
[1860] | 61 | {
|
---|
| 62 | text_t::const_iterator ithere = text.begin();
|
---|
| 63 | text_t::const_iterator itend = text.end();
|
---|
| 64 |
|
---|
| 65 | while (ithere != itend)
|
---|
| 66 | {
|
---|
| 67 | if (*ithere < 256)
|
---|
| 68 | {
|
---|
| 69 | o << (unsigned char)(*ithere);
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | {
|
---|
| 73 | // put a space or a question mark depending on what
|
---|
| 74 | // the character is. Question marks tell the user that
|
---|
| 75 | // they are missing some information.
|
---|
| 76 | if (is_unicode_space (*ithere))
|
---|
| 77 | o << ' ';
|
---|
| 78 | else
|
---|
| 79 | o << '?';
|
---|
| 80 | }
|
---|
[8727] | 81 | ++ithere;
|
---|
[1860] | 82 | }
|
---|
| 83 |
|
---|
| 84 | return o;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[1076] | 87 | text_t::text_t ()
|
---|
| 88 | {
|
---|
| 89 | setencoding(0);
|
---|
| 90 | clear ();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | text_t::text_t (int i)
|
---|
| 94 | {
|
---|
| 95 | setencoding(0);
|
---|
| 96 | clear ();
|
---|
| 97 | appendint (i);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[8727] | 100 | text_t::text_t (const char *s)
|
---|
[1076] | 101 | {
|
---|
| 102 | setencoding(0);
|
---|
| 103 | clear ();
|
---|
| 104 | appendcstr (s);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[8727] | 107 | text_t::text_t (const char *s, size_type nLength)
|
---|
| 108 | {
|
---|
| 109 | setencoding(0);
|
---|
| 110 | clear ();
|
---|
| 111 | appendcarr(s, nLength);
|
---|
| 112 | }
|
---|
[1860] | 113 |
|
---|
[8727] | 114 |
|
---|
[1076] | 115 | void text_t::append (const text_t &t)
|
---|
| 116 | {
|
---|
| 117 | text.insert(text.end(), t.begin(), t.end());
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | void text_t::appendrange (iterator first, iterator last)
|
---|
| 121 | {
|
---|
| 122 | text.insert(text.end(), first, last);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void text_t::appendrange (const_iterator first, const_iterator last)
|
---|
| 126 | {
|
---|
| 127 | text.insert(text.end(), first, last);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void text_t::appendint (int i)
|
---|
| 131 | {
|
---|
| 132 | // deal with zeros and negatives
|
---|
| 133 | if (i == 0)
|
---|
| 134 | {
|
---|
| 135 | text.push_back('0');
|
---|
| 136 | return;
|
---|
| 137 | }
|
---|
| 138 | else if (i < 0)
|
---|
| 139 | {
|
---|
| 140 | text.push_back('-');
|
---|
| 141 | i *= -1;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | // get a buffer for the conversion
|
---|
| 145 | int maxbuflen = sizeof(int)*3;
|
---|
| 146 | char *buf = new char[maxbuflen];
|
---|
| 147 | int len = 0;
|
---|
| 148 |
|
---|
| 149 | // get the number in reverse
|
---|
| 150 | while (i > 0)
|
---|
| 151 | {
|
---|
| 152 | buf[len++] = '0'+ (i%10);
|
---|
| 153 | i = i/10;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // reverse the number
|
---|
| 157 | while (len > 0)
|
---|
| 158 | {
|
---|
| 159 | text.push_back(buf[--len]);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[8727] | 162 | delete []buf;
|
---|
[1076] | 163 | }
|
---|
| 164 |
|
---|
| 165 | int text_t::getint () const
|
---|
| 166 | {
|
---|
| 167 | int i = 0;
|
---|
| 168 | int mult = 1; // become -1 for negative numbers
|
---|
| 169 |
|
---|
| 170 | const_iterator here = text.begin();
|
---|
| 171 | const_iterator end = text.end();
|
---|
| 172 |
|
---|
| 173 | // do plus and minus signs
|
---|
| 174 | if (here != end)
|
---|
| 175 | {
|
---|
| 176 | if (*here == '-')
|
---|
| 177 | {
|
---|
| 178 | mult = -1;
|
---|
[9593] | 179 | ++here;
|
---|
[1076] | 180 | }
|
---|
| 181 | else if (*here == '+')
|
---|
| 182 | {
|
---|
| 183 | mult = 1;
|
---|
[8727] | 184 | ++here;
|
---|
[1076] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | // deal with the number
|
---|
| 189 | while ((here != end) && (*here >= '0') && (*here <= '9'))
|
---|
| 190 | {
|
---|
| 191 | i = 10*i + (*here - '0');
|
---|
[8727] | 192 | ++here;
|
---|
[1076] | 193 | }
|
---|
| 194 |
|
---|
| 195 | i *= mult;
|
---|
| 196 | return i;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[2487] | 199 | unsigned long text_t::getulong () const
|
---|
| 200 | {
|
---|
| 201 | unsigned long i = 0;
|
---|
[1076] | 202 |
|
---|
[2487] | 203 | const_iterator here = text.begin();
|
---|
| 204 | const_iterator end = text.end();
|
---|
[1076] | 205 |
|
---|
[2487] | 206 | while ((here != end) && (*here >= '0') && (*here <= '9'))
|
---|
| 207 | {
|
---|
| 208 | i = 10*i + (*here - '0');
|
---|
[8727] | 209 | ++here;
|
---|
[2487] | 210 | }
|
---|
| 211 |
|
---|
| 212 | return i;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[8727] | 215 | void text_t::appendcarr (const char *s, size_type len)
|
---|
[1076] | 216 | {
|
---|
| 217 | unsigned char *us = (unsigned char *)s;
|
---|
[8727] | 218 | if (text.capacity() < (text.size() + len + 2)) {
|
---|
| 219 | text.reserve(text.size() + len + 2);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[1076] | 222 | while (len > 0)
|
---|
| 223 | {
|
---|
| 224 | text.push_back (*us); // append this character
|
---|
[8727] | 225 | ++us;
|
---|
| 226 | --len;
|
---|
[1076] | 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[8727] | 230 | void text_t::appendcstr (const char *s)
|
---|
[1076] | 231 | {
|
---|
[8727] | 232 | size_t len = strlen(s);
|
---|
| 233 | if (text.capacity() < (text.size() + len + 2)) {
|
---|
| 234 | text.reserve(text.size() + len + 2);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[1076] | 237 | unsigned char *us = (unsigned char *)s;
|
---|
| 238 | while (*us != '\0')
|
---|
| 239 | {
|
---|
| 240 | text.push_back (*us); // append this character
|
---|
[8727] | 241 | ++us;
|
---|
[1076] | 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 | // strings returned from getcarr and getcstr become the callers
|
---|
[8727] | 247 | // responsibility and should be deallocated with "delete []"
|
---|
[1076] | 248 |
|
---|
| 249 | char *text_t::getcarr(size_type &len) const
|
---|
| 250 | {
|
---|
| 251 | unsigned char *cstr = new unsigned char[size()];
|
---|
| 252 | len = 0;
|
---|
| 253 |
|
---|
| 254 | const_iterator ithere = begin();
|
---|
| 255 | const_iterator itend = end();
|
---|
| 256 | while (ithere != itend)
|
---|
| 257 | {
|
---|
| 258 | if (*ithere < 256) cstr[len] = (unsigned char)(*ithere);
|
---|
| 259 | else {
|
---|
| 260 | // put a space or a question mark depending on what
|
---|
| 261 | // the character is. Question marks tell the user that
|
---|
| 262 | // they are missing some information.
|
---|
| 263 | if (is_unicode_space (*ithere)) cstr[len] = ' ';
|
---|
| 264 | else cstr[len] = '?';
|
---|
| 265 | }
|
---|
[8727] | 266 | ++len;
|
---|
| 267 | ++ithere;
|
---|
[1076] | 268 | }
|
---|
| 269 |
|
---|
| 270 | return (char *)cstr;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | char *text_t::getcstr() const
|
---|
| 274 | {
|
---|
| 275 | unsigned char *cstr = new unsigned char[size() + 1];
|
---|
| 276 | const_iterator ithere = begin();
|
---|
| 277 | const_iterator itend = end();
|
---|
| 278 | int len = 0;
|
---|
| 279 |
|
---|
| 280 | while (ithere != itend)
|
---|
| 281 | {
|
---|
| 282 | if (*ithere < 256) cstr[len] = (unsigned char)(*ithere);
|
---|
| 283 | else {
|
---|
| 284 | // put a space or a question mark depending on what
|
---|
| 285 | // the character is. Question marks tell the user that
|
---|
| 286 | // they are missing some information.
|
---|
| 287 | if (is_unicode_space (*ithere)) cstr[len] = ' ';
|
---|
| 288 | else cstr[len] = '?';
|
---|
| 289 | }
|
---|
[8727] | 290 | ++len;
|
---|
| 291 | ++ithere;
|
---|
[1076] | 292 | }
|
---|
| 293 |
|
---|
| 294 | cstr[len] = '\0';
|
---|
| 295 |
|
---|
| 296 | return (char *)cstr;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 | // general functions which work on text_ts
|
---|
| 301 |
|
---|
| 302 | // find a character within a range
|
---|
| 303 | text_t::const_iterator findchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
| 304 | unsigned short c)
|
---|
| 305 | {
|
---|
| 306 | while (first != last)
|
---|
| 307 | {
|
---|
| 308 | if (*first == c) break;
|
---|
[8727] | 309 | ++first;
|
---|
[1076] | 310 | }
|
---|
| 311 | return first;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | text_t::iterator findchar (text_t::iterator first, text_t::iterator last,
|
---|
| 315 | unsigned short c)
|
---|
| 316 | {
|
---|
| 317 | while (first != last)
|
---|
| 318 | {
|
---|
| 319 | if (*first == c) break;
|
---|
[8727] | 320 | ++first;
|
---|
[1076] | 321 | }
|
---|
| 322 | return first;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[12504] | 325 | text_t::iterator findlastchar (text_t::iterator first, text_t::iterator last,
|
---|
| 326 | unsigned short c)
|
---|
| 327 | {
|
---|
| 328 | text_t::iterator current = last;
|
---|
| 329 | while (current != first) {
|
---|
| 330 | if (*current == c) break;
|
---|
| 331 | --current;
|
---|
| 332 | }
|
---|
| 333 | if (current == first) {
|
---|
| 334 | if (*current == c) return current;
|
---|
| 335 | return last;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | return current;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[8727] | 341 | text_t::iterator findword (text_t::iterator first,
|
---|
| 342 | text_t::iterator last,
|
---|
[1860] | 343 | const text_t& word)
|
---|
| 344 | {
|
---|
| 345 | text_t::const_iterator word_begin = word.begin();
|
---|
| 346 | text_t::const_iterator word_end = word.end();
|
---|
| 347 |
|
---|
| 348 | while (first != last)
|
---|
| 349 | {
|
---|
| 350 | text_t::iterator char_match = first;
|
---|
| 351 | text_t::const_iterator word_here = word_begin;
|
---|
| 352 | while (word_here!=word_end)
|
---|
| 353 | {
|
---|
| 354 | if (*char_match != *word_here)
|
---|
| 355 | {
|
---|
| 356 | break;
|
---|
| 357 | }
|
---|
[8727] | 358 | ++char_match;
|
---|
| 359 | ++word_here;
|
---|
[1860] | 360 | }
|
---|
| 361 | if (word_here==word_end)
|
---|
| 362 | {
|
---|
| 363 | return first;
|
---|
| 364 | }
|
---|
[8727] | 365 | ++first;
|
---|
[1860] | 366 | }
|
---|
| 367 | return last; // get to here only if there is no match
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[1076] | 370 | // get a string up to the next delimiter (which is skipped)
|
---|
| 371 | text_t::const_iterator getdelimitstr (text_t::const_iterator first,
|
---|
| 372 | text_t::const_iterator last,
|
---|
| 373 | unsigned short c, text_t &outstr)
|
---|
| 374 | {
|
---|
| 375 | text_t::const_iterator here = first;
|
---|
| 376 | here = findchar (first, last, c);
|
---|
| 377 | outstr.clear();
|
---|
| 378 | outstr.appendrange (first, here);
|
---|
[8727] | 379 | if (here != last) ++here; // skip c
|
---|
[1076] | 380 | return here;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | text_t::iterator getdelimitstr (text_t::iterator first, text_t::iterator last,
|
---|
| 384 | unsigned short c, text_t &outstr)
|
---|
| 385 | {
|
---|
| 386 | text_t::iterator here = first;
|
---|
| 387 | here = findchar (first, last, c);
|
---|
| 388 | outstr.clear();
|
---|
| 389 | outstr.appendrange (first, here);
|
---|
[8727] | 390 | if (here != last) ++here; // skip c
|
---|
[1076] | 391 | return here;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | // split a string with a character
|
---|
| 395 | void splitchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
| 396 | unsigned short c, text_tset &outlist)
|
---|
| 397 | {
|
---|
| 398 | outlist.erase(outlist.begin(), outlist.end());
|
---|
| 399 |
|
---|
| 400 | text_t t;
|
---|
| 401 |
|
---|
| 402 | while (first != last)
|
---|
| 403 | {
|
---|
| 404 | first = getdelimitstr (first, last, c, t);
|
---|
| 405 | outlist.insert (t);
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | void splitchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
| 410 | unsigned short c, text_tlist &outlist)
|
---|
| 411 | {
|
---|
| 412 | outlist.erase(outlist.begin(), outlist.end());
|
---|
| 413 |
|
---|
| 414 | text_t t;
|
---|
| 415 |
|
---|
| 416 | while (first != last)
|
---|
| 417 | {
|
---|
| 418 | first = getdelimitstr (first, last, c, t);
|
---|
| 419 | outlist.push_back (t);
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | void splitchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
| 424 | unsigned short c, text_tarray &outlist)
|
---|
| 425 | {
|
---|
| 426 | outlist.erase(outlist.begin(), outlist.end());
|
---|
| 427 |
|
---|
| 428 | text_t t;
|
---|
| 429 |
|
---|
| 430 | while (first != last)
|
---|
| 431 | {
|
---|
| 432 | first = getdelimitstr (first, last, c, t);
|
---|
| 433 | outlist.push_back (t);
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | // join a string using a character
|
---|
| 438 | void joinchar (const text_tset &inlist, unsigned short c, text_t &outtext)
|
---|
| 439 | {
|
---|
| 440 | outtext.clear ();
|
---|
| 441 |
|
---|
| 442 | text_tset::const_iterator here = inlist.begin ();
|
---|
| 443 | text_tset::const_iterator end = inlist.end ();
|
---|
[8727] | 444 |
|
---|
| 445 | if (here != end) {
|
---|
| 446 | outtext += *here; ++here;
|
---|
| 447 | while (here != end) {
|
---|
| 448 | outtext.push_back (c);
|
---|
[1076] | 449 | outtext += *here;
|
---|
[8727] | 450 | ++here;
|
---|
[1076] | 451 | }
|
---|
[8727] | 452 | }
|
---|
[1076] | 453 | }
|
---|
| 454 |
|
---|
| 455 | void joinchar (const text_tlist &inlist, unsigned short c, text_t &outtext)
|
---|
| 456 | {
|
---|
| 457 | outtext.clear ();
|
---|
| 458 |
|
---|
| 459 | text_tlist::const_iterator here = inlist.begin ();
|
---|
| 460 | text_tlist::const_iterator end = inlist.end ();
|
---|
[8727] | 461 | if (here != end) {
|
---|
| 462 | outtext += *here; ++here;
|
---|
| 463 | while (here != end) {
|
---|
| 464 | outtext.push_back (c);
|
---|
[1076] | 465 | outtext += *here;
|
---|
[8727] | 466 | ++here;
|
---|
[1076] | 467 | }
|
---|
[8727] | 468 | }
|
---|
[1076] | 469 | }
|
---|
| 470 |
|
---|
| 471 | void joinchar (const text_tarray &inlist, unsigned short c, text_t &outtext)
|
---|
| 472 | {
|
---|
| 473 | outtext.clear ();
|
---|
| 474 |
|
---|
| 475 | text_tarray::const_iterator here = inlist.begin ();
|
---|
| 476 | text_tarray::const_iterator end = inlist.end ();
|
---|
[8727] | 477 | if (here != end) {
|
---|
| 478 | outtext += *here; ++here;
|
---|
| 479 | while (here != end) {
|
---|
| 480 | outtext.push_back (c);
|
---|
[1076] | 481 | outtext += *here;
|
---|
[8727] | 482 | ++here;
|
---|
[1076] | 483 | }
|
---|
[8727] | 484 | }
|
---|
[1076] | 485 | }
|
---|
| 486 |
|
---|
[8727] | 487 | void joinchar (const text_tlist &inlist, const text_t &c, text_t &outtext)
|
---|
[1088] | 488 | {
|
---|
| 489 | outtext.clear ();
|
---|
| 490 |
|
---|
| 491 | text_tlist::const_iterator here = inlist.begin ();
|
---|
| 492 | text_tlist::const_iterator end = inlist.end ();
|
---|
[8727] | 493 | if (here != end) {
|
---|
| 494 | outtext += *here; ++here;
|
---|
| 495 | while (here != end) {
|
---|
| 496 | outtext += c;
|
---|
[1088] | 497 | outtext += *here;
|
---|
[8727] | 498 | ++here;
|
---|
[1088] | 499 | }
|
---|
[8727] | 500 | }
|
---|
[1088] | 501 | }
|
---|
| 502 |
|
---|
[8727] | 503 | void joinchar (const text_tset &inlist, const text_t &c, text_t &outtext)
|
---|
[1088] | 504 | {
|
---|
| 505 | outtext.clear ();
|
---|
| 506 |
|
---|
| 507 | text_tset::const_iterator here = inlist.begin ();
|
---|
| 508 | text_tset::const_iterator end = inlist.end ();
|
---|
[8727] | 509 | if (here != end) {
|
---|
| 510 | outtext += *here; ++here;
|
---|
| 511 | while (here != end) {
|
---|
| 512 | outtext += c;
|
---|
[1088] | 513 | outtext += *here;
|
---|
[8727] | 514 | ++here;
|
---|
[1088] | 515 | }
|
---|
[8727] | 516 | }
|
---|
[1088] | 517 | }
|
---|
| 518 |
|
---|
[8727] | 519 | void joinchar (const text_tarray &inlist, const text_t &c, text_t &outtext)
|
---|
[1076] | 520 | {
|
---|
| 521 | outtext.clear ();
|
---|
| 522 |
|
---|
| 523 | text_tarray::const_iterator here = inlist.begin ();
|
---|
| 524 | text_tarray::const_iterator end = inlist.end ();
|
---|
[8727] | 525 | if (here != end) {
|
---|
| 526 | outtext += *here; ++here;
|
---|
| 527 | while (here != end) {
|
---|
| 528 | outtext += c;
|
---|
[1076] | 529 | outtext += *here;
|
---|
[8727] | 530 | ++here;
|
---|
[1076] | 531 | }
|
---|
[8727] | 532 | }
|
---|
[1076] | 533 | }
|
---|
| 534 |
|
---|
| 535 | // count the occurances of a character within a range
|
---|
| 536 | int countchar (text_t::const_iterator first, text_t::const_iterator last,
|
---|
| 537 | unsigned short c)
|
---|
| 538 | {
|
---|
| 539 | int count = 0;
|
---|
| 540 | while (first != last) {
|
---|
[9593] | 541 | if (*first == c) ++count;
|
---|
| 542 | ++first;
|
---|
[1076] | 543 | }
|
---|
| 544 | return count;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | // return a substring of string from first up to but not including last
|
---|
| 548 | text_t substr (text_t::const_iterator first, text_t::const_iterator last) {
|
---|
| 549 |
|
---|
[8727] | 550 | text_t substr; substr.reserve(last - first + 2);
|
---|
[1076] | 551 | while (first != last) {
|
---|
| 552 | substr.push_back(*first);
|
---|
[8727] | 553 | ++first;
|
---|
[1076] | 554 | }
|
---|
| 555 | return substr;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 | // convert to lowercase
|
---|
| 560 | void lc (text_t::iterator first, text_t::iterator last) {
|
---|
| 561 | while (first != last) {
|
---|
| 562 | *first = unicode_tolower(*first);
|
---|
[8727] | 563 | ++first;
|
---|
[1076] | 564 | }
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | // convert to uppercase
|
---|
| 568 | void uc (text_t::iterator first, text_t::iterator last) {
|
---|
| 569 | while (first != last) {
|
---|
| 570 | *first = unicode_toupper(*first);
|
---|
[8727] | 571 | ++first;
|
---|
[1076] | 572 | }
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 |
|
---|
| 576 | // checks to see if it is a number (i.e. contains only 0-9)
|
---|
| 577 | bool is_number (const text_t &text) {
|
---|
| 578 |
|
---|
| 579 | text_t::const_iterator here = text.begin();
|
---|
| 580 | text_t::const_iterator end = text.end();
|
---|
| 581 |
|
---|
| 582 | while (here != end) {
|
---|
| 583 | if ((*here!='0') && (*here!='1') && (*here!='2') &&
|
---|
| 584 | (*here!='3') && (*here!='4') && (*here!='5') &&
|
---|
| 585 | (*here!='6') && (*here!='7') && (*here!='8') &&
|
---|
| 586 | (*here!='9')) return false;
|
---|
[8727] | 587 | ++here;
|
---|
[1076] | 588 | }
|
---|
| 589 | return true;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 |
|
---|
| 593 | // checks to see if the text has any letters or digits
|
---|
| 594 | bool has_unicode_letdig (const text_t &text) {
|
---|
| 595 | if (text.empty()) return false;
|
---|
| 596 |
|
---|
| 597 | text_t::const_iterator here = text.begin();
|
---|
| 598 | text_t::const_iterator end = text.end();
|
---|
| 599 | while (here != end) {
|
---|
| 600 | if (is_unicode_letdig (*here)) return true;
|
---|
[8727] | 601 | ++here;
|
---|
[1076] | 602 | }
|
---|
| 603 |
|
---|
| 604 | return false;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[10140] | 607 | // checks to see if a text_t starts with the specified prefix
|
---|
| 608 | bool starts_with(const text_t& text, const text_t& prefix) {
|
---|
| 609 | if (prefix.empty()) return true;
|
---|
| 610 | if (text.empty() || text.size()<prefix.size()) return false;
|
---|
| 611 | text_t substring = substr(text.begin(), text.begin()+prefix.size());
|
---|
| 612 | return substring == prefix;
|
---|
| 613 | }
|
---|
| 614 | // checks to see if a text_t ends with the specified suffix
|
---|
| 615 | bool ends_with(const text_t& text, const text_t& suffix) {
|
---|
| 616 | if (suffix.empty()) return true;
|
---|
| 617 | if (text.empty() || text.size() < suffix.size()) return false;
|
---|
| 618 | text_t substring = substr(text.end()-suffix.size(),text.end());
|
---|
| 619 | return substring == suffix;
|
---|
[1076] | 620 |
|
---|
[10140] | 621 | }
|
---|
[1076] | 622 |
|
---|
[10140] | 623 |
|
---|
[1076] | 624 | ////////////////////////////////////
|
---|
| 625 | // convertclass methods
|
---|
| 626 | ////////////////////////////////////
|
---|
| 627 |
|
---|
| 628 | // conversion classes used for getting information in to and out of
|
---|
| 629 | // the text_t class.
|
---|
| 630 |
|
---|
| 631 | convertclass::convertclass ()
|
---|
| 632 | {
|
---|
| 633 | // nothing to do
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | void convertclass::reset ()
|
---|
| 637 | {
|
---|
| 638 | // nothing to do
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 |
|
---|
| 642 | ////////////////////////////////////
|
---|
| 643 | // inconvertclass methods
|
---|
| 644 | ////////////////////////////////////
|
---|
| 645 |
|
---|
| 646 | // convert from a char stream to the text_t class
|
---|
| 647 | // the default version assumes the input is a ascii
|
---|
| 648 | // character array
|
---|
| 649 |
|
---|
| 650 | inconvertclass::inconvertclass ()
|
---|
| 651 | {
|
---|
| 652 | start = NULL;
|
---|
| 653 | len = 0;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 |
|
---|
| 657 | void inconvertclass::reset ()
|
---|
| 658 | {
|
---|
| 659 | start = NULL;
|
---|
| 660 | len = 0;
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | void inconvertclass::setinput (char *thestart, size_t thelen)
|
---|
| 664 | {
|
---|
| 665 | start = thestart;
|
---|
| 666 | len = thelen;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | void inconvertclass::convert (text_t &output, status_t &status)
|
---|
| 670 | {
|
---|
| 671 | output.clear();
|
---|
| 672 |
|
---|
| 673 | if (start == NULL || len == 0)
|
---|
| 674 | {
|
---|
| 675 | status = finished;
|
---|
| 676 | return;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
[8727] | 679 | if (output.capacity() < len + 2)
|
---|
| 680 | output.reserve(len + 2);
|
---|
| 681 |
|
---|
[1076] | 682 | // don't want any funny sign conversions happening
|
---|
| 683 | unsigned char *here = (unsigned char *)start;
|
---|
| 684 | while (len > 0)
|
---|
| 685 | {
|
---|
| 686 | output.push_back (*here); // append this character
|
---|
| 687 | ++here;
|
---|
| 688 | --len;
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | start = (char *)here; // save current position
|
---|
| 692 | status = finished;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | // will treat the text_t as a 8-bit string and convert
|
---|
| 696 | // it to a 16-bit string using the about convert method.
|
---|
| 697 | text_t inconvertclass::convert (const text_t &t) {
|
---|
| 698 | text_t out;
|
---|
| 699 | text_t tmpout;
|
---|
| 700 | status_t status;
|
---|
| 701 | text_t::const_iterator here = t.begin();
|
---|
| 702 | text_t::const_iterator end = t.end();
|
---|
| 703 | unsigned char cbuf[256];
|
---|
| 704 | size_t cbuflen = 0;
|
---|
| 705 |
|
---|
[8727] | 706 | out.clear();
|
---|
| 707 | if (out.capacity() < t.size() + 2)
|
---|
| 708 | out.reserve(t.size() + 2);
|
---|
[1076] | 709 | while (here != end) {
|
---|
| 710 | while (here != end && cbuflen < 256) {
|
---|
| 711 | cbuf[cbuflen++] = (unsigned char)(*here & 0xff);
|
---|
[8727] | 712 | ++here;
|
---|
[1076] | 713 | }
|
---|
| 714 |
|
---|
| 715 | if (cbuflen > 0) {
|
---|
| 716 | setinput ((char *)cbuf, cbuflen);
|
---|
| 717 | status = unfinished;
|
---|
| 718 | while (status == unfinished) {
|
---|
| 719 | convert (tmpout, status);
|
---|
| 720 | out += tmpout;
|
---|
| 721 | }
|
---|
| 722 | cbuflen = 0;
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | out.setencoding (0); // unicode
|
---|
| 727 |
|
---|
| 728 | return out;
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | // an instance of the default inconvertclass to do simple
|
---|
| 732 | // conversions. Note that any functions that use this are
|
---|
| 733 | // not reentrant. If a function needs to be reentrant it
|
---|
| 734 | // should declare its own instance.
|
---|
| 735 | inconvertclass ascii2text_t;
|
---|
| 736 |
|
---|
| 737 |
|
---|
| 738 | ////////////////////////////////////
|
---|
| 739 | // outconvertclass methods
|
---|
| 740 | ////////////////////////////////////
|
---|
| 741 |
|
---|
| 742 | // Convert from a text_t class to a char stream
|
---|
| 743 | // This default version assumes the output is a ascii
|
---|
| 744 | // character array. If you set the output stream you
|
---|
| 745 | // can use this class to output to a stream using the
|
---|
| 746 | // << operator. The << operator can also be conveniently
|
---|
| 747 | // used to set the output stream by doing something like
|
---|
| 748 | //
|
---|
| 749 | // cout << text_t2ascii << text_tstr << anothertext_tstr;
|
---|
| 750 | //
|
---|
| 751 | outconvertclass::outconvertclass ()
|
---|
| 752 | {
|
---|
| 753 | input = NULL;
|
---|
| 754 | outs = NULL;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | void outconvertclass::reset ()
|
---|
| 758 | {
|
---|
| 759 | input = NULL;
|
---|
| 760 | outs = NULL;
|
---|
| 761 | }
|
---|
| 762 |
|
---|
| 763 | void outconvertclass::setinput (text_t *theinput)
|
---|
| 764 | {
|
---|
| 765 | input = theinput;
|
---|
| 766 | if (input != NULL) texthere = input->begin();
|
---|
| 767 | }
|
---|
| 768 |
|
---|
[8727] | 769 | void outconvertclass::setdata(text_t *theinput, text_t::iterator thetexthere)
|
---|
| 770 | {
|
---|
| 771 | input = theinput;
|
---|
| 772 | texthere = thetexthere;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
[1076] | 775 | void outconvertclass::convert (char *output, size_t maxlen,
|
---|
| 776 | size_t &len, status_t &status)
|
---|
| 777 | {
|
---|
| 778 | if (input == NULL || output == NULL)
|
---|
| 779 | {
|
---|
| 780 | status = finished;
|
---|
| 781 | return;
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | // don't want any funny sign conversions happening
|
---|
| 785 | unsigned char *uoutput = (unsigned char *)output;
|
---|
| 786 | text_t::iterator textend = input->end();
|
---|
| 787 | len = 0;
|
---|
| 788 | while ((len < maxlen) && (texthere != textend))
|
---|
| 789 | {
|
---|
| 790 | if (*texthere < 256) *uoutput = (unsigned char)(*texthere);
|
---|
| 791 | else {
|
---|
| 792 | // put a space or a question mark depending on what
|
---|
| 793 | // the character is. Question marks tell the user that
|
---|
| 794 | // they are missing some information.
|
---|
| 795 | if (is_unicode_space (*texthere)) *uoutput = ' ';
|
---|
| 796 | else *uoutput = '?';
|
---|
| 797 | }
|
---|
| 798 | ++uoutput;
|
---|
| 799 | ++len;
|
---|
| 800 | ++texthere;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | if (texthere == textend) status = finished;
|
---|
| 804 | else status = unfinished;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | // will convert the 16-bit string to a 8-bit stream
|
---|
| 808 | // and place the result in a text_t. This method uses
|
---|
| 809 | // the above convert function.
|
---|
| 810 | text_t outconvertclass::convert (const text_t &t) {
|
---|
| 811 | text_t out;
|
---|
| 812 | unsigned char cbuf[256];
|
---|
| 813 | size_t cbuflen = 0;
|
---|
| 814 | status_t status = unfinished;
|
---|
[8727] | 815 |
|
---|
| 816 | out.clear();
|
---|
| 817 | if (out.capacity() < t.size() + 2)
|
---|
| 818 | out.reserve(t.size() + 2);
|
---|
[1076] | 819 | setinput ((text_t *)&t); // discard constant
|
---|
| 820 | while (status == unfinished) {
|
---|
| 821 | convert ((char *)cbuf, 256, cbuflen, status);
|
---|
| 822 | out.appendcarr ((char *)cbuf, cbuflen);
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 | out.setencoding (1); // other encoding
|
---|
| 826 |
|
---|
| 827 | return out;
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 |
|
---|
| 831 | void outconvertclass::setostream (ostream *theouts)
|
---|
| 832 | {
|
---|
| 833 | outs = theouts;
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 | ostream *outconvertclass::getostream ()
|
---|
| 837 | {
|
---|
| 838 | return outs;
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 |
|
---|
| 842 |
|
---|
| 843 |
|
---|
| 844 | // an instance of the default outconvertclass to do simple
|
---|
| 845 | // conversions
|
---|
| 846 | outconvertclass text_t2ascii;
|
---|
| 847 |
|
---|
| 848 |
|
---|
| 849 |
|
---|
| 850 | // stream operators for the output class
|
---|
| 851 |
|
---|
| 852 | outconvertclass &operator<< (ostream &theouts, outconvertclass &outconverter)
|
---|
| 853 | {
|
---|
| 854 | outconverter.setostream(&theouts);
|
---|
| 855 | return outconverter;
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 |
|
---|
| 859 | #define STREAMBUFSIZE 256
|
---|
| 860 | outconvertclass &operator<< (outconvertclass &outconverter, const text_t &t)
|
---|
| 861 | {
|
---|
| 862 | ostream *outstream = outconverter.getostream();
|
---|
| 863 |
|
---|
| 864 | if (outstream == NULL) return outconverter;
|
---|
| 865 |
|
---|
| 866 | char outbuf[STREAMBUFSIZE];
|
---|
| 867 | size_t len;
|
---|
| 868 | outconvertclass::status_t status = outconvertclass::unfinished;
|
---|
| 869 |
|
---|
| 870 | // assume that there is no data needing converting
|
---|
| 871 | // left in the converter
|
---|
| 872 | outconverter.setinput ((text_t *)(&t)); // note the const -> nonconst conversion
|
---|
| 873 |
|
---|
| 874 | while (status == outconvertclass::unfinished)
|
---|
| 875 | {
|
---|
| 876 | outconverter.convert (outbuf, STREAMBUFSIZE, len, status);
|
---|
| 877 | if (len > 0) outstream->write(outbuf, len);
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | return outconverter;
|
---|
| 881 | }
|
---|