1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
// $Id: Dstr.cc,v 1.3 2002/12/19 18:40:45 flaterco Exp $
// Dstr: Dave's String class.
// This source is shared among several of my projects and is public domain.
// Operations are added as needed and there is not necessarily a canonical
// version.
#include "common.hh"
#include "collation.hh"
Dstr::Dstr () {
theBuffer = NULL;
}
Dstr::Dstr (const char *val) {
if (val) {
theBuffer = strdup (val);
used = strlen (val);
max = used + 1;
} else
theBuffer = NULL;
}
Dstr::Dstr (char val) {
char t[2];
t[0] = val;
t[1] = '\0';
theBuffer = strdup (t);
used = 1;
max = 2;
}
Dstr::Dstr (const Dstr &val) {
if (!(val.isNull())) {
theBuffer = val.asdupchar();
used = val.length();
max = used + 1;
} else
theBuffer = NULL;
}
Dstr::~Dstr () {
if (theBuffer)
free (theBuffer);
}
// This sped up the indexing step on my PC from 14 seconds to 12 seconds,
// so it's worth keeping, but the primary time waster is all the effort
// required to extract coordinates and build the StationRefs.
Dstr&
Dstr::getline (FILE *fp) {
char buf[82], *ret;
if (!(ret = fgets (buf, 82, fp)))
(*this) = (char *)NULL;
else {
(*this) = "";
while (ret) {
(*this) += buf;
if (used > 0)
if (theBuffer[used-1] == '\n') {
(*this) -= used-1;
break;
}
ret = fgets (buf, 82, fp);
}
}
return (*this);
}
void Dstr::getline (Dstr &line_out) {
line_out = (char *)NULL;
while (((*this).length() > 0) && (*this)[0] != '\n') {
line_out += (*this)[0];
(*this) /= 1;
}
if ((*this)[0] == '\n')
(*this) /= 1;
}
// Scan a string like fscanf (fp, "%s")
Dstr&
Dstr::scan (FILE *fp) {
int c;
(*this) = (char *)NULL;
// Skip whitespace
do {
c = getc(fp);
if (c == EOF)
return (*this);
} while (isspace (c));
// Get the string
(*this) = (char)c;
while (1) {
c = getc(fp);
if (c == EOF)
return (*this);
if (isspace (c))
return (*this);
(*this) += (char)c;
}
}
Dstr &
Dstr::pad (unsigned to_length) {
while (length() < to_length)
(*this) += " ";
return (*this);
}
Dstr &Dstr::trim () {
while (isspace ((*this)[0]))
(*this) /= 1;
while (isspace ((*this)[this->length()-1]))
(*this) -= (this->length()-1);
return (*this);
}
Dstr&
Dstr::operator-= (unsigned at_index) {
if (theBuffer) {
if (at_index < used) {
theBuffer[at_index] = '\0';
used = at_index;
}
}
return (*this);
}
int
Dstr::strchr (char val) const {
if (!theBuffer)
return -1;
char *c = ::strchr (theBuffer, val);
if (!c)
return -1;
return (c - theBuffer);
}
int
Dstr::strrchr (char val) const {
if (!theBuffer)
return -1;
char *c = ::strrchr (theBuffer, val);
if (!c)
return -1;
return (c - theBuffer);
}
int
Dstr::strstr (const Dstr &val) const {
if (!theBuffer)
return -1;
if (!val.theBuffer)
return -1;
char *c = ::strstr (theBuffer, val.theBuffer);
if (!c)
return -1;
return (c - theBuffer);
}
int Dstr::strcasestr (const Dstr &val) const {
if (!theBuffer)
return -1;
if (!val.theBuffer)
return -1;
if (val.length() == 0)
return 0;
int i;
for (i=0; i<(int)used-(int)(val.length())+1; i++)
if (!slackcmp (theBuffer+i, val.aschar()))
return i;
return -1;
}
int
Dstr::strstr (const char *val) const {
Dstr temp (val);
return strstr (temp);
}
Dstr &
Dstr::operator= (const char *val) {
if (theBuffer)
free (theBuffer);
if (val) {
theBuffer = strdup (val);
used = strlen (val);
max = used + 1;
} else
theBuffer = NULL;
return (*this);
}
Dstr&
Dstr::operator= (char val) {
char t[2];
t[0] = val;
t[1] = '\0';
(*this) = t;
return (*this);
}
Dstr &
Dstr::operator= (const Dstr &val) {
(*this) = val.theBuffer;
return (*this);
}
Dstr&
Dstr::operator+= (const char *val) {
if (val) {
if (!theBuffer)
(*this) = val;
else {
unsigned l;
if ((l = strlen (val))) {
while (l + used >= max) { // Leave room for terminator
// Expand
max *= 2;
assert (theBuffer = (char *) realloc (theBuffer, max*sizeof(char)));
}
strcpy (theBuffer+used, val);
used += l;
}
}
}
return (*this);
}
Dstr&
Dstr::operator+= (char val) {
char t[2];
t[0] = val;
t[1] = '\0';
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator+= (const Dstr &val) {
(*this) += val.theBuffer;
return (*this);
}
Dstr&
Dstr::operator+= (int val) {
char t[80];
sprintf (t, "%d", val);
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator+= (unsigned int val) {
char t[80];
sprintf (t, "%u", val);
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator+= (long int val) {
char t[80];
sprintf (t, "%ld", val);
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator+= (long unsigned int val) {
char t[80];
sprintf (t, "%lu", val);
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator+= (long long int val) {
char t[80];
sprintf (t, "%lld", val);
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator+= (double val) {
char t[80];
sprintf (t, "%f", val);
(*this) += t;
return (*this);
}
Dstr&
Dstr::operator*= (const char *val) {
Dstr temp (*this);
(*this) = val;
(*this) += temp;
return (*this);
}
Dstr&
Dstr::operator*= (char val) {
Dstr temp (*this);
(*this) = val;
(*this) += temp;
return (*this);
}
Dstr&
Dstr::operator*= (const Dstr &val) {
Dstr temp (*this);
(*this) = val;
(*this) += temp;
return (*this);
}
Dstr&
Dstr::operator/= (unsigned at_index) {
if (theBuffer) {
Dstr temp ((*this).ascharfrom(at_index));
(*this) = temp;
}
return (*this);
}
Dstr&
Dstr::operator/= (Dstr &val) {
val = (char *)NULL;
if (theBuffer) {
// Eat whitespace
while (used > 0 && isspace ((*this)[0]))
(*this) /= 1;
// Anything left?
if (used == 0) {
// Nothing left.
(*this) = (char *)NULL;
} else {
if ((*this)[0] == '"') {
// Delimited argument
val += (*this)[0];
(*this) /= 1;
while (used > 0 && (*this)[0] != '"') {
val += (*this)[0];
(*this) /= 1;
}
// Grab the matching quote, if any.
if (used > 0) {
val += (*this)[0];
(*this) /= 1;
}
} else {
// Undelimited argument
while (used > 0 && (!(isspace ((*this)[0])))) {
val += (*this)[0];
(*this) /= 1;
}
}
}
}
return (*this);
}
char
Dstr::operator[] (unsigned at_index) const {
if (!theBuffer)
return '\0';
if (at_index >= used)
return '\0';
return theBuffer[at_index];
}
unsigned
Dstr::repchar (char X, char Y) {
unsigned i, repcount=0;
for (i=0; i<length(); i++)
if (theBuffer[i] == X) {
theBuffer[i] = Y;
repcount++;
}
return repcount;
}
unsigned
Dstr::length () const {
if (!theBuffer)
return 0;
return used;
}
int
Dstr::isNull () const {
if (theBuffer)
return 0;
return 1;
}
double
Dstr::asdouble() const {
double t;
if (sscanf (aschar(), "%lf", &t) != 1)
fprintf (stderr, "Warning: illegal conversion of Dstr to double\n");
return t;
}
char *
Dstr::aschar () const {
if (theBuffer)
return theBuffer;
return "";
}
char *
Dstr::asdupchar() const {
return strdup (aschar());
}
char *
Dstr::ascharfrom(unsigned from_index) const {
if (!theBuffer)
return "";
if (from_index >= used)
return "";
return theBuffer + from_index;
}
char *
Dstr::asrawchar () const {
return theBuffer;
}
int operator== (const Dstr &val1, const char *val2) {
if ((!val2) && (val1.isNull()))
return 1;
if ((!val2) || (val1.isNull()))
return 0;
return (!(strcmp (val1.aschar(), val2)));
}
int operator== (const char *val1, const Dstr &val2) {
return (val2 == val1);
}
int operator== (const Dstr &val1, const Dstr &val2) {
return (val1 == val2.asrawchar());
}
int operator!= (const char *val1, const Dstr &val2) {
return (!(val1 == val2));
}
int operator!= (const Dstr &val1, const char *val2) {
return (!(val1 == val2));
}
int operator!= (const Dstr &val1, const Dstr &val2) {
return (!(val1 == val2));
}
Dstr &
Dstr::lowercase() {
unsigned i;
for (i=0; i<length(); i++)
theBuffer[i] = tolower(theBuffer[i]);
return (*this);
}
int dstrcasecmp (const Dstr &val1, const Dstr &val2) {
unsigned n = min (val1.length(), val2.length());
unsigned i;
int c, ii;
for (i=0; i<n; i++) {
ii = (int)(val1[i]);
if (ii < 0)
ii += 256;
c = collation[ii];
ii = (int)(val2[i]);
if (ii < 0)
ii += 256;
c -= collation[ii];
if (c)
return c;
}
c = (int)(val1.length()) - (int)(val2.length());
return c;
}
/* Slackful strcmp; 0 = match. It's case-insensitive and accepts a
prefix instead of the entire string. The second argument is the
one that can be shorter. */
int
slackcmp (char *a, char *b)
{
int c, n, ii;
unsigned i;
n = strlen (b);
if ((int)(strlen (a)) < n)
return 1;
for (i=0;i<n;i++) {
ii = (int)(a[i]);
if (ii < 0)
ii += 256;
c = collation[ii];
ii = (int)(b[i]);
if (ii < 0)
ii += 256;
c -= collation[ii];
if (c)
return c;
}
return 0;
}
int operator%= (const Dstr &a, const Dstr &b) {
return !(slackcmp (a.aschar(), b.aschar()));
}
int dstrcasecmp (const Dstr &val1, char *val2) {
Dstr temp (val2);
return dstrcasecmp (val1, temp);
}
|