Changeset e3d9c77 for text.c


Ignore:
Timestamp:
Oct 26, 2003, 3:23:38 PM (18 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
  • text.c

    r8d24696 re3d9c77  
    115115}
    116116
    117 
    118117int owl_text_num_lines(char *in)
    119118{
     
    190189  }
    191190
    192   out2=owl_util_substitute(out, "&lt;", "<");
    193   owl_free(out);
    194   out=owl_util_substitute(out2, "&gt;", ">");
     191  out2=owl_text_substitute(out, "&lt;", "<");
     192  owl_free(out);
     193  out=owl_text_substitute(out2, "&gt;", ">");
    195194  owl_free(out2);
    196   out2=owl_util_substitute(out, "&amp;", "&");
    197   owl_free(out);
    198   out=owl_util_substitute(out2, "&quot;", "\"");
     195  out2=owl_text_substitute(out, "&amp;", "&");
     196  owl_free(out);
     197  out=owl_text_substitute(out2, "&quot;", "\"");
    199198  owl_free(out2);
    200   out2=owl_util_substitute(out, "&nbsp;", " ");
    201   owl_free(out);
    202   out=owl_util_substitute(out2, "&ensp;", "  ");
     199  out2=owl_text_substitute(out, "&nbsp;", " ");
     200  owl_free(out);
     201  out=owl_text_substitute(out2, "&ensp;", "  ");
    203202  owl_free(out2);
    204   out2=owl_util_substitute(out, "&emsp;", "   ");
    205   owl_free(out);
    206   out=owl_util_substitute(out2, "&endash;", "--");
     203  out2=owl_text_substitute(out, "&emsp;", "   ");
     204  owl_free(out);
     205  out=owl_text_substitute(out2, "&endash;", "--");
    207206  owl_free(out2);
    208   out2=owl_util_substitute(out, "&emdash;", "---");
     207  out2=owl_text_substitute(out, "&emdash;", "---");
    209208  owl_free(out);
    210209
     
    257256  return(out);
    258257}
     258
     259/* exactly like strstr but case insensitive */
     260char *stristr(char *a, char *b)
     261{
     262  char *x, *y, *ret;
     263
     264  if ((x=owl_strdup(a))==NULL) return(NULL);
     265  if ((y=owl_strdup(b))==NULL) return(NULL);
     266  downstr(x);
     267  downstr(y);
     268  ret=strstr(x, y);
     269  if (ret==NULL) {
     270    owl_free(x);
     271    owl_free(y);
     272    return(NULL);
     273  }
     274  ret=ret-x+a;
     275  owl_free(x);
     276  owl_free(y);
     277  return(ret);
     278}
     279
     280/* return 1 if a string is only whitespace, otherwise 0 */
     281int only_whitespace(char *s)
     282{
     283  int i;
     284  for (i=0; s[i]; i++) {
     285    if (!isspace((int) s[i])) return(0);
     286  }
     287  return(1);
     288}
     289
     290char *owl_getquoting(char *line)
     291{
     292  if (line[0]=='\0') return("'");
     293  if (strchr(line, '\'')) return("\"");
     294  if (strchr(line, '"')) return("'");
     295  if (strchr(line, ' ')) return("'");
     296  return("");
     297}
     298
     299/* Return a string with any occurances of 'from' replaced with 'to'.
     300 * Does not currently handle backslash quoting, but may in the future.
     301 * Caller must free returned string.
     302 */
     303char *owl_text_substitute(char *in, char *from, char *to)
     304{
     305 
     306  char *out;
     307  int   outlen, tolen, fromlen, inpos=0, outpos=0;
     308
     309  if (!*from) return owl_strdup(in);
     310
     311  outlen = strlen(in)+1;
     312  tolen  = strlen(to);
     313  fromlen  = strlen(from);
     314  out = malloc(outlen);
     315
     316  while (in[inpos]) {
     317    if (!strncmp(in+inpos, from, fromlen)) {
     318      outlen += tolen;
     319      out = owl_realloc(out, outlen);
     320      strcpy(out+outpos, to);
     321      inpos += fromlen;
     322      outpos += tolen;
     323    } else {
     324      out[outpos] = in[inpos];
     325      inpos++; outpos++;
     326    }
     327  }
     328  out[outpos] = '\0';
     329  return(out);
     330}
     331
     332/* replace all instances of character a in buff with the character
     333 * b.  buff must be null terminated.
     334 */
     335void owl_text_tr(char *buff, char a, char b)
     336{
     337  int i;
     338
     339  owl_function_debugmsg("In: %s", buff);
     340  for (i=0; buff[i]!='\0'; i++) {
     341    if (buff[i]==a) buff[i]=b;
     342  }
     343  owl_function_debugmsg("Out: %s", buff);
     344}
     345
     346/* Return a string which is like 'in' except that every instance of
     347 * any character in 'toquote' found in 'in' is preceeded by the string
     348 * 'quotestr'.  For example, owl_text_quote(in, "+*.", "\") would
     349 * place a backslash before every '+', '*' or '.' in 'in'.  It is
     350 * permissable for a character in 'quotestr' to be in 'toquote'.
     351 * On success returns the string, on error returns NULL.
     352 */
     353char *owl_text_quote(char *in, char *toquote, char *quotestr)
     354{
     355  int i, x, r, place;
     356  int in_len, toquote_len, quotestr_len;
     357  char *out;
     358
     359  in_len=strlen(in);
     360  toquote_len=strlen(toquote);
     361  quotestr_len=strlen(quotestr);
     362  out=owl_malloc((in_len*quotestr_len)+30);
     363  place=0;
     364  for (i=0; i<in_len; i++) {
     365
     366    /* check if it's a character that needs quoting */
     367    for (x=0; x<toquote_len; x++) {
     368      if (in[i]==toquote[x]) {
     369        /* quote it */
     370        for (r=0; r<quotestr_len; r++) {
     371          out[place+r]=quotestr[r];
     372        }
     373        place+=quotestr_len;
     374        break;
     375      }
     376    }
     377
     378    /* either way, we now copy over the character */
     379    out[place]=in[i];
     380    place++;
     381  }
     382  out[place]='\0';
     383  return(out);
     384}
Note: See TracChangeset for help on using the changeset viewer.