Changeset e3d9c77 for util.c


Ignore:
Timestamp:
Oct 26, 2003, 3:23:38 PM (17 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
bc08664
Parents:
70b53ec
Message:
Write owl_text_quote
Moved some functions between util.c, text.c and zephyr.c
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    rc15bbfb re3d9c77  
    22#include <stdlib.h>
    33#include <string.h>
    4 #include <unistd.h>
    54#include <unistd.h>
    65#include <ctype.h>
     
    306305}
    307306
    308 
    309307/* return the index of the last char before a change from the first one */
    310308int owl_util_find_trans(char *in, int len)
     
    317315}
    318316
    319 
    320317/* downcase the string 'foo' */
    321318void downstr(char *foo)
     
    325322    foo[i]=tolower(foo[i]);
    326323  }
    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);
    348324}
    349325
     
    382358}
    383359
    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 
    394360/* hooks for doing memory allocation et. al. in owl */
    395361
     
    413379  return(realloc(ptr, size));
    414380}
    415 
    416381
    417382/* allocates memory and returns the string or null.
     
    443408}
    444409
    445 /* Strip a local realm fron the zephyr user name.
    446  * The caller must free the return
    447  */
    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 the
    478  * realm if present.  daemon.webzephyr is a special case.  The
    479  * caller must free the return
    480  */
    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 character
    564  * 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 
    578410/* Return the owl color associated with the named color */
    579411int owl_util_string_to_color(char *color)
     
    805637
    806638  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")));
    814646
    815647  FAIL_UNLESS("skiptokens 1",
Note: See TracChangeset for help on using the changeset viewer.