home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / STPCVT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  5.1 KB  |  180 lines

  1. /**
  2. *
  3. * Name        STPCVT -- String conversion.
  4. *
  5. * Synopsis    presult = stpcvt (psource,conv);
  6. *
  7. *        char *presult      Pointer to the converted string
  8. *        char *psource      Pointer to the source string
  9. *        int conv      Conversion code
  10. *
  11. * Description    STPCVT converts the string psource according to the
  12. *        instructions specified in conv and returns a pointer to
  13. *        the altered string.  The string psource is itself
  14. *        altered.  The possible conversion codes are:
  15. *
  16. *        RWHITE      (1) - Discard all white space.
  17. *        RLWHITE   (2) - Discard all leading white space.
  18. *        RTWHITE   (4) - Discard all trailing white space.
  19. *        REDUCE      (8) - Reduce all white space to one space.
  20. *        NOQUOTE  (16) - Characters enclosed in single or double
  21. *                quotes are not to be altered.
  22. *        TOUP     (32) - Convert lower case characters to upper case.
  23. *        TOLOW     (64) - Convert upper case characters to lower case.
  24. *        RCONTROL(128) - Discard all control characters.
  25. *
  26. *        The conversion codes can be combined by adding them
  27. *        together.  Except for 16, the codes are executed in the
  28. *        order shown.  For example, 33 discards all white space
  29. *        and converts all lower case characters to upper case
  30. *        characters.  The code 49 does the same, but no
  31. *        conversion takes place within quoted substrings.
  32. *
  33. *        Notice that white space and control characters are
  34. *        defined to be ASCII characters (code less than 128) as
  35. *        reported by isascii().    Therefore, the special
  36. *        characters having ASCII codes greater than 127 are not
  37. *        altered.
  38. *
  39. *        Trailing white space will not be protected if unbalanced
  40. *        quoted substrings are present.
  41. *
  42. * Returns    presult     Pointer to the converted string.
  43. *        psource     Pointer to the altered source string.
  44. *                It is the same as presult.
  45. *
  46. * Version    6.00 (C)Copyright Blaise Computing Inc.  1983, 1987, 1989
  47. *
  48. **/
  49.  
  50. #include <ctype.h>        /* Definition of char manipulation    */
  51.                 /* macros.                */
  52.  
  53. #include <bstrings.h>
  54.  
  55. #define TRUE       1
  56. #define FALSE       0
  57. #define BLANK     ' '
  58.  
  59. char *stpcvt(psource,conv)
  60. char *psource;
  61. int   conv;
  62. {
  63.     char *pfrom        = psource;    /* Next character to get    */
  64.                     /* from source.         */
  65.  
  66.     register char *pto = psource;    /* Next position to fill in */
  67.                     /* target.            */
  68.     register char c;
  69.     char quote_char = '\0';
  70.  
  71.     /* Convenient flags (not to be changed):                */
  72.  
  73.     int rlwhite      = conv & RLWHITE;
  74.     int rwhite         = conv & RWHITE;
  75.     int reduce         = (!rwhite) && (conv & REDUCE);
  76.     int ckquotes     = conv & NOQUOTE;
  77.     int to_up         = conv & TOUP;
  78.     int to_low         = conv & TOLOW;
  79.     int rcontrol     = conv & RCONTROL;
  80.  
  81.     /* Flags indicating current state as we process the string        */
  82.     /* (these change as we encounter various characters):        */
  83.  
  84.     int in_white     = FALSE;    /* Not in a white field yet.        */
  85.     int hit_nonwhite = FALSE;    /* No nonwhite chars encountered.   */
  86.     int quote_on     = FALSE;    /* Not in a quote yet.            */
  87.  
  88.     /* This main loop handles everything but trailing white space.  */
  89.     /* Each pass handles one character c from psource, writing zero */
  90.     /* or one character back to psource.  We can process psource in */
  91.     /* place this way because we're never adding additional chars.  */
  92.  
  93.     while ((c = *pfrom++) != '\0')
  94.     {
  95.     if (quote_on)
  96.     {                /* We're inside a quoted    */
  97.         *pto++ = c;         /* substring.            */
  98.         if (c == quote_char)
  99.         quote_on = FALSE;    /* Found end of the quote.  */
  100.     }
  101.     else if (ckquotes && ((c == '"') || (c == '\'')))
  102.     {                /* Begin new quote.        */
  103.         *pto++ = c;
  104.         in_white     = FALSE;
  105.         hit_nonwhite = TRUE;
  106.         quote_on     = TRUE;
  107.         quote_char     = c;
  108.     }
  109.     else if (isspace(c) && isascii(c))
  110.     {
  111.         if (rwhite)
  112.         ;    /* Do nothing with character c.         */
  113.  
  114.         else if (rlwhite && !hit_nonwhite)
  115.         ;    /* Leading white space to omit-- do nothing */
  116.             /* with character c.                */
  117.  
  118.         else if (reduce)
  119.         {
  120.         if (in_white)
  121.             ;    /* We're within white block-- do nothing    */
  122.             /* with character c.                */
  123.  
  124.         else
  125.         {    /* Begin block of white space.            */
  126.             *pto++ = BLANK;
  127.             in_white = TRUE;
  128.         }
  129.         }
  130.  
  131.         else if (rcontrol && iscntrl (c))
  132.         ;    /* Remove control characters which are        */
  133.             /* defined as whitespace.            */
  134.  
  135.         else
  136.             /* Just copy normally .             */
  137.         *pto++ = c;
  138.     }
  139.     else if (iscntrl(c) && isascii(c))
  140.     {
  141.         in_white     = FALSE;
  142.         hit_nonwhite = TRUE;
  143.  
  144.         if (rcontrol)
  145.         ;    /* Don't copy the ctrl char.                */
  146.  
  147.         else
  148.             /* Just copy normally.                */
  149.         *pto++ = c;
  150.     }
  151.     else
  152.     {                /* Simple nonwhite or        */
  153.         in_white     = FALSE;    /* control character.        */
  154.         hit_nonwhite = TRUE;
  155.  
  156.         if (isascii(c))
  157.         {
  158.         if (to_up)
  159.             c = toupper(c);
  160.  
  161.         if (to_low)
  162.             c = tolower(c);
  163.         }
  164.  
  165.         *pto++ = c; /* Just copy normally.                */
  166.     }
  167.     }
  168.     *pto = '\0';
  169.  
  170.     /* Now handle trailing white space...                */
  171.  
  172.     if (conv & RTWHITE)
  173.     for (c = *--pto;
  174.          isspace(c) && isascii(c) && (pto >= psource);
  175.          c = *--pto)
  176.         *pto = '\0';
  177.  
  178.     return (psource);
  179. }
  180.