home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / ftp / BFTP.312 / parsedlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-29  |  6.6 KB  |  239 lines

  1. /***********************************************************
  2.         Copyright IBM Corporation 1988
  3.  
  4.                       All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of IBM not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ******************************************************************/
  22. /*
  23. $Header: parsedlib.c,v 1.2 88/09/14 23:01:03 ghoti Exp $
  24. $Source: /afs/andrew.cmu.edu/itc/src/andrew/ams/libs/ms/RCS/parsedlib.c,v $
  25. */
  26. /*
  27.  * parsedlib.c - A small library of routines used by parsedate.
  28.  *
  29.  * This file is made up of 3 files from libcmu:  fold.c nxtarg.c skipto.c.
  30.  * The only change is that the actual text of fold.h has been
  31.  * inserted (it was included in fold.c), because parsedate.y does
  32.  * not access fold.h.
  33.  *
  34.  */
  35.  
  36.  
  37.  
  38.  
  39. /* beginning of fold.c */
  40.  
  41. /*  fold  --  perform case folding
  42.  *
  43.  *  Usage:  p = fold (out,in,whichway);
  44.  *        p = foldup (out,in);
  45.  *        p = folddown (out,in);
  46.  *    char *p,*in,*out;
  47.  *    enum {FOLDUP, FOLDDOWN} whichway;
  48.  *
  49.  *  Fold performs case-folding, moving string "in" to
  50.  *  "out" and folding one case to another en route.
  51.  *  Folding may be upper-to-lower case (folddown) or
  52.  *  lower-to-upper case.
  53.  *  Foldup folds to upper case; folddown folds to lower case.
  54.  *  The same string may be specified as both "in" and "out".
  55.  *  The address of "out" is returned for convenience.
  56.  *
  57.  */
  58.  
  59. /* beginning of fold.h */
  60. /*  macros for fold() routine */
  61.  
  62. typedef enum {
  63.     FOLDUP, FOLDDOWN} 
  64. FOLDMODE;
  65.  
  66. char *fold(),*foldup(),*folddown();
  67.  
  68. /* end of fold.h */
  69.  
  70. /* continuation of fold.c */
  71.  
  72. char *fold (out,in,whichway)
  73. char *in,*out;
  74. FOLDMODE whichway;
  75. {
  76.     register char *i,*o;
  77.     register char lower;
  78.     char upper;
  79.     int delta;
  80.  
  81.     switch (whichway)
  82.     {
  83.     case FOLDUP:
  84.         lower = 'a';        /* lower bound of range to change */
  85.         upper = 'z';        /* upper bound of range */
  86.         delta = 'A' - 'a';    /* amount of change */
  87.         break;
  88.     case FOLDDOWN:
  89.         lower = 'A';
  90.         upper = 'Z';
  91.         delta = 'a' - 'A';
  92.     }
  93.  
  94.     i = in;
  95.     o = out;
  96.     do {
  97.         if (*i >= lower && *i <= upper)        *o++ = *i++ + delta;
  98.         else                    *o++ = *i++;
  99.     } 
  100.     while (*i);
  101.     *o = '\0';
  102.     return (out);
  103. }
  104.  
  105. char *foldup (out,in)
  106. char *in,*out;
  107. {
  108.     return (fold(out,in,FOLDUP));
  109. }
  110.  
  111. char *folddown (out,in)
  112. char *in,*out;
  113. {
  114.     return (fold(out,in,FOLDDOWN));
  115. }
  116.  
  117. /* end of fold.c */
  118.  
  119.  
  120.  
  121. /* beginning of nxtarg.c */
  122.  
  123. /*
  124.  *  nxtarg -- strip off arguments from a string
  125.  *
  126.  *  Usage:  p = nxtarg (&q,brk);
  127.  *    char *p,*q,*brk;
  128.  *    extern char _argbreak;
  129.  *
  130.  *    q is pointer to next argument in string
  131.  *    after call, p points to string containing argument,
  132.  *    q points to remainder of string
  133.  *
  134.  *  Leading blanks and tabs are skipped; the argument ends at the
  135.  *  first occurence of one of the characters in the string "brk".
  136.  *  When such a character is found, it is put into the external
  137.  *  variable "_argbreak", and replaced by a null character; if the
  138.  *  arg string ends before that, then the null character is
  139.  *  placed into _argbreak;
  140.  *  If "brk" is 0, then " " is substituted.
  141.  *
  142.  *  HISTORY
  143.  * 01-Jul-83  Steven Shafer (sas) at Carnegie-Mellon University
  144.  *    Bug fix: added check for "back >= front" in loop to chop trailing
  145.  *    white space.
  146.  *
  147.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  148.  *    Rewritten for VAX.  By popular demand, a table of break characters
  149.  *    has been added (implemented as a string passed into nxtarg).
  150.  *
  151.  *  Originally    from klg (Ken Greer); IUS/SUS UNIX.
  152.  */
  153.  
  154. char _argbreak;
  155. char *skipto();
  156.  
  157. char *nxtarg (q,brk)
  158. char **q,*brk;
  159. {
  160.     register char *front,*back;
  161.     front = *q;            /* start of string */
  162.     /* leading blanks and tabs */
  163.     while (*front && (*front == ' ' || *front == '\t')) front++;
  164.     /* find break character at end */
  165.     if (brk == 0)  brk = " ";
  166.     back = skipto (front,brk);
  167.     _argbreak = *back;
  168.     *q = (*back ? back+1 : back);    /* next arg start loc */
  169.     /* elim trailing blanks and tabs */
  170.     back -= 1;
  171.     while ((back >= front) && (*back == ' ' || *back == '\t')) back--;
  172.     back++;
  173.     if (*back)  *back = '\0';
  174.     return (front);
  175. }
  176.  
  177. /* end of nxtarg.c */
  178.  
  179.  
  180.  
  181. /* beginning of skipto.c */
  182.  
  183. /************************************************************************
  184.  *  skipover and skipto -- skip over characters in string
  185.  *
  186.  *  Usage:    p = skipto (string,charset);
  187.  *        p = skipover (string,charset);
  188.  *
  189.  *  char *p,*charset,*string;
  190.  *
  191.  *  Skipto returns a pointer to the first character in string which
  192.  *  is in the string charset; it "skips until" a character in charset.
  193.  *  Skipover returns a pointer to the first character in string which
  194.  *  is not in the string charset; it "skips over" characters in charset.
  195.  ************************************************************************
  196.  * HISTORY
  197.  * 26-Jun-81  David Smith (drs) at Carnegie-Mellon University
  198.  *    Skipover, skipto rewritten to avoid inner loop at expense of space.
  199.  *
  200.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  201.  *    Skipover, skipto adapted for VAX from skip() and skipx() on the PDP-11
  202.  *    (from Ken Greer).  The names are more mnemonic.
  203.  *
  204.  *    Sindex adapted for VAX from indexs() on the PDP-11 (thanx to Ralph
  205.  *    Guggenheim).  The name has changed to be more like the index()
  206.  *    and rindex() functions from Bell Labs; the return value (pointer
  207.  *    rather than integer) has changed partly for the same reason,
  208.  *    and partly due to popular usage of this function.
  209.  */
  210.  
  211. static unsigned char tab[256] = {
  212.     0};
  213.  
  214. char *skipto (string,charset)
  215. unsigned char *string, *charset;
  216. {
  217.     register unsigned char *setp,*strp;
  218.  
  219.     tab[0] = 1;        /* Stop on a null, too. */
  220.     for (setp=charset;  *setp;  setp++) tab[*setp]=1;
  221.     for (strp=string;  tab[*strp]==0;  strp++)  ;
  222.     for (setp=charset;  *setp;  setp++) tab[*setp]=0;
  223.     return ((char *)strp);
  224. }
  225.  
  226. char *skipover (string,charset)
  227. unsigned char *string, *charset;
  228. {
  229.     register unsigned char *setp,*strp;
  230.  
  231.     tab[0] = 0;        /* Do not skip over nulls. */
  232.     for (setp=charset;  *setp;  setp++) tab[*setp]=1;
  233.     for (strp=string;  tab[*strp];  strp++)  ;
  234.     for (setp=charset;  *setp;  setp++) tab[*setp]=0;
  235.     return ((char *)strp);
  236. }
  237.  
  238. /* end of skipto.c */
  239.