- Timestamp:
- Jul 9, 2002, 12:04:35 AM (23 years ago)
- Branches:
- master, barnowl_perlaim, debian, owl, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- 2c8a07c
- Parents:
- 10b866d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
util.c
r10b866d re1c4636 148 148 149 149 /* skips n tokens and returns where that would be. 150 * TODO: handle quotes . */150 * TODO: handle quotes more sanely. */ 151 151 char *skiptokens(char *buff, int n) { 152 152 int inquotes=0; … … 154 154 while (*buff == ' ') buff++; 155 155 while (*buff && (inquotes || *buff != ' ')) { 156 if (*buff == '"' ) inquotes=!inquotes;156 if (*buff == '"' || *buff == '\'') inquotes=!inquotes; 157 157 buff++; 158 158 } … … 386 386 } 387 387 388 /* Caller must free returned string. 389 * Returns a string with any occurances of 'from' replaced with 'to'. 390 * Does not currently handle backslash quoting, but may in the future. 391 */ 392 char *owl_util_substitute(char *in, char *from, char *to) { 393 char *out; 394 int outlen, tolen, fromlen, inpos=0, outpos=0; 395 396 if (!*from) return owl_strdup(in); 397 398 outlen = strlen(in)+1; 399 tolen = strlen(to); 400 fromlen = strlen(from); 401 out = malloc(outlen); 402 403 while (in[inpos]) { 404 if (!strncmp(in+inpos, from, fromlen)) { 405 outlen += tolen; 406 out = owl_realloc(out, outlen); 407 strcpy(out+outpos, to); 408 inpos += fromlen; 409 outpos += tolen; 410 } else { 411 out[outpos] = in[inpos]; 412 inpos++; outpos++; 413 } 414 } 415 out[outpos] = '\0'; 416 return(out); 417 } 418 388 419 389 420 int owl_util_string_to_color(char *color) { … … 422 453 return("Unknown color"); 423 454 } 455 456 457 /**************************************************************************/ 458 /************************* REGRESSION TESTS *******************************/ 459 /**************************************************************************/ 460 461 #ifdef OWL_INCLUDE_REG_TESTS 462 463 #define FAIL_UNLESS(desc,pred) printf("\t%-4s: %s\n", (pred)?"ok":(numfailed++,"FAIL"), desc) 464 465 int owl_util_regtest(void) { 466 int numfailed=0; 467 468 printf("BEGIN testing owl_util\n"); 469 470 FAIL_UNLESS("owl_util_substitute 1", 471 !strcmp("foo", owl_util_substitute("foo", "", "Y"))); 472 FAIL_UNLESS("owl_util_substitute 2", 473 !strcmp("fYZYZ", owl_util_substitute("foo", "o", "YZ"))); 474 FAIL_UNLESS("owl_util_substitute 3", 475 !strcmp("foo", owl_util_substitute("fYZYZ", "YZ", "o"))); 476 FAIL_UNLESS("owl_util_substitute 4", 477 !strcmp("/u/foo/meep", owl_util_substitute("~/meep", "~", "/u/foo"))); 478 479 FAIL_UNLESS("skiptokens 1", 480 !strcmp("bar quux", skiptokens("foo bar quux", 1))); 481 FAIL_UNLESS("skiptokens 2", 482 !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); 483 484 if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); 485 printf("END testing owl_util (%d failures)\n", numfailed); 486 return(numfailed); 487 } 488 489 #endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracChangeset
for help on using the changeset viewer.