Changeset e1c4636 for util.c


Ignore:
Timestamp:
Jul 9, 2002, 12:04:35 AM (19 years ago)
Author:
Erik Nygren <nygren@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:
2c8a07c
Parents:
10b866d
Message:
* Added owl::send_zwrite(command, message) to the perl glue
         to allow for the direct sending of multi-line messages.
         For example:  owl::send_zwrite("-c foo -i bar", "hello");
* Reverted attempted fix for the pagedown problem
         which just made things worse.
* Changed owl_fmtext_print_plain to return an alloc'd string to
         avoid buffer overrun risks.
* Added owl::ztext_stylestrip("...") function to perlglue
          which returns the ztext with formatting stripped out.
* Added colorztext variable which can be used to disable @color()
          strings arriving in messages after it is set.
          (Currently, changing its value won't reformat messages).
* Outgoing zephyr logging now obeys the logpath variable.
* The '~' character in logpath and classlogpath now gets
          replaced with the user's home directory.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r10b866d re1c4636  
    148148
    149149/* skips n tokens and returns where that would be.
    150  * TODO: handle quotes. */
     150 * TODO: handle quotes more sanely. */
    151151char *skiptokens(char *buff, int n) {
    152152  int inquotes=0;
     
    154154      while (*buff == ' ') buff++;
    155155      while (*buff && (inquotes || *buff != ' ')) {
    156         if (*buff == '"') inquotes=!inquotes;
     156        if (*buff == '"' || *buff == '\'') inquotes=!inquotes;
    157157        buff++;
    158158      }
     
    386386}
    387387
     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 */
     392char *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
    388419
    389420int owl_util_string_to_color(char *color) {
     
    422453  return("Unknown color");
    423454}
     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
     465int 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.