- Timestamp:
- Oct 26, 2003, 3:23:38 PM (21 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:
- bc08664
- Parents:
- 70b53ec
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
util.c
rc15bbfb re3d9c77 2 2 #include <stdlib.h> 3 3 #include <string.h> 4 #include <unistd.h>5 4 #include <unistd.h> 6 5 #include <ctype.h> … … 306 305 } 307 306 308 309 307 /* return the index of the last char before a change from the first one */ 310 308 int owl_util_find_trans(char *in, int len) … … 317 315 } 318 316 319 320 317 /* downcase the string 'foo' */ 321 318 void downstr(char *foo) … … 325 322 foo[i]=tolower(foo[i]); 326 323 } 327 }328 329 /* exactly like strstr but case insensitive */330 char *stristr(char *a, char *b)331 {332 char *x, *y, *ret;333 334 if ((x=owl_strdup(a))==NULL) return(NULL);335 if ((y=owl_strdup(b))==NULL) return(NULL);336 downstr(x);337 downstr(y);338 ret=strstr(x, y);339 if (ret==NULL) {340 owl_free(x);341 owl_free(y);342 return(NULL);343 }344 ret=ret-x+a;345 owl_free(x);346 owl_free(y);347 return(ret);348 324 } 349 325 … … 382 358 } 383 359 384 /* return 1 if a string is only whitespace, otherwise 0 */385 int only_whitespace(char *s)386 {387 int i;388 for (i=0; s[i]; i++) {389 if (!isspace((int) s[i])) return(0);390 }391 return(1);392 }393 394 360 /* hooks for doing memory allocation et. al. in owl */ 395 361 … … 413 379 return(realloc(ptr, size)); 414 380 } 415 416 381 417 382 /* allocates memory and returns the string or null. … … 443 408 } 444 409 445 /* Strip a local realm fron the zephyr user name.446 * The caller must free the return447 */448 char *short_zuser(char *in)449 {450 char *out, *ptr;451 452 out=owl_strdup(in);453 ptr=strchr(out, '@');454 if (ptr) {455 if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {456 *ptr='\0';457 }458 }459 return(out);460 }461 462 /* Append a local realm to the zephyr user name if necessary.463 * The caller must free the return.464 */465 char *long_zuser(char *in)466 {467 char *ptr;468 469 if (NULL != (ptr=strchr(in, '@'))) {470 return owl_strdup(in);471 } else {472 return owl_sprintf("%s@%s", in, owl_zephyr_get_realm());473 }474 }475 476 477 /* strip out the instance from a zsender's principal. Preserves the478 * realm if present. daemon.webzephyr is a special case. The479 * caller must free the return480 */481 char *owl_util_smartstripped_user(char *in)482 {483 char *ptr, *realm, *out;484 485 out=owl_strdup(in);486 487 /* bail immeaditly if we don't have to do any work */488 ptr=strchr(in, '.');489 if (!strchr(in, '/') && !ptr) {490 /* no '/' and no '.' */491 return(out);492 }493 if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {494 /* There's a '.' but it's in the realm */495 return(out);496 }497 if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {498 return(out);499 }500 501 /* remove the realm from ptr, but hold on to it */502 realm=strchr(out, '@');503 if (realm) realm[0]='\0';504 505 /* strip */506 ptr=strchr(out, '.');507 if (!ptr) ptr=strchr(out, '/');508 ptr[0]='\0';509 510 /* reattach the realm if we had one */511 if (realm) {512 strcat(out, "@");513 strcat(out, realm+1);514 }515 516 return(out);517 }518 519 char *owl_getquoting(char *line)520 {521 if (line[0]=='\0') return("'");522 if (strchr(line, '\'')) return("\"");523 if (strchr(line, '"')) return("'");524 if (strchr(line, ' ')) return("'");525 return("");526 }527 528 529 530 /* Return a string with any occurances of 'from' replaced with 'to'.531 * Does not currently handle backslash quoting, but may in the future.532 * Caller must free returned string.533 */534 char *owl_util_substitute(char *in, char *from, char *to)535 {536 537 char *out;538 int outlen, tolen, fromlen, inpos=0, outpos=0;539 540 if (!*from) return owl_strdup(in);541 542 outlen = strlen(in)+1;543 tolen = strlen(to);544 fromlen = strlen(from);545 out = malloc(outlen);546 547 while (in[inpos]) {548 if (!strncmp(in+inpos, from, fromlen)) {549 outlen += tolen;550 out = owl_realloc(out, outlen);551 strcpy(out+outpos, to);552 inpos += fromlen;553 outpos += tolen;554 } else {555 out[outpos] = in[inpos];556 inpos++; outpos++;557 }558 }559 out[outpos] = '\0';560 return(out);561 }562 563 /* replace all instances of character a in buff with the character564 * b. buff must be null terminated.565 */566 void owl_util_tr(char *buff, char a, char b)567 {568 int i;569 570 owl_function_debugmsg("In: %s", buff);571 for (i=0; buff[i]!='\0'; i++) {572 if (buff[i]==a) buff[i]=b;573 }574 owl_function_debugmsg("Out: %s", buff);575 }576 577 578 410 /* Return the owl color associated with the named color */ 579 411 int owl_util_string_to_color(char *color) … … 805 637 806 638 FAIL_UNLESS("owl_util_substitute 1", 807 !strcmp("foo", owl_ util_substitute("foo", "", "Y")));808 FAIL_UNLESS("owl_ util_substitute 2",809 !strcmp("fYZYZ", owl_ util_substitute("foo", "o", "YZ")));810 FAIL_UNLESS("owl_ util_substitute 3",811 !strcmp("foo", owl_ util_substitute("fYZYZ", "YZ", "o")));812 FAIL_UNLESS("owl_ util_substitute 4",813 !strcmp("/u/foo/meep", owl_ util_substitute("~/meep", "~", "/u/foo")));639 !strcmp("foo", owl_text_substitute("foo", "", "Y"))); 640 FAIL_UNLESS("owl_text_substitute 2", 641 !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ"))); 642 FAIL_UNLESS("owl_text_substitute 3", 643 !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o"))); 644 FAIL_UNLESS("owl_text_substitute 4", 645 !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo"))); 814 646 815 647 FAIL_UNLESS("skiptokens 1",
Note: See TracChangeset
for help on using the changeset viewer.