home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STPCVT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  5.1 KB  |  167 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. It
  44. *                  is the same as presult.
  45. *
  46. * Version    3.0 (C)Copyright Blaise Computing Inc.    1983,1984,1985,1986
  47. *
  48. **/
  49.  
  50. #include <ctype.h>          /* Definition of character manipulation */
  51.                   /* macros                   */
  52.  
  53. #include <bstring.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 char to get from source */
  64.     register char *pto = psource;   /* Next position to fill in target */
  65.     register char c;
  66.     char quote_char = '\0';
  67.  
  68.     /* Convenient flags (not to be changed):                   */
  69.  
  70.     int rlwhite      = conv & RLWHITE;
  71.     int rwhite         = conv & RWHITE;
  72.     int reduce         = (!rwhite) && (conv & REDUCE);
  73.     int ckquotes     = conv & NOQUOTE;
  74.     int to_up         = conv & TOUP;
  75.     int to_low         = conv & TOLOW;
  76.     int rcontrol     = conv & RCONTROL;
  77.  
  78.     /* Flags indicating current state as we process the string           */
  79.     /* (these change as we encounter various characters):           */
  80.  
  81.     int in_white     = FALSE;           /* Not in a white field yet     */
  82.     int hit_nonwhite = FALSE;           /* No nonwhite chars encountered*/
  83.     int quote_on     = FALSE;           /* Not in a quote yet           */
  84.  
  85.     /* This main loop handles everything but trailing white space.     */
  86.     /* Each pass handles one character c from psource, writing zero or */
  87.     /* one character back to psource.  We can process psource in place */
  88.     /* this way because we're never adding additional characters.      */
  89.  
  90.     while ((c = *pfrom++) != '\0')
  91.     {
  92.     if (quote_on)
  93.     {                   /* We're inside quoted substring*/
  94.         *pto++ = c;
  95.         if (c == quote_char)
  96.         quote_on = FALSE;      /* Found end of the quote       */
  97.     }
  98.     else if (ckquotes && ((c == '"') || (c == '\'')))
  99.     {                   /* Begin new quote           */
  100.         *pto++ = c;
  101.         in_white     = FALSE;
  102.         hit_nonwhite = TRUE;
  103.         quote_on     = TRUE;
  104.         quote_char     = c;
  105.     }
  106.     else if (isspace(c) && isascii(c))
  107.     {
  108.         if (rwhite)
  109.         {
  110.                        /* Do nothing with character c  */
  111.         }
  112.         else if (rlwhite && !hit_nonwhite)
  113.         {                   /* Leading white space to omit  */
  114.                        /* Do nothing with character c  */
  115.         }
  116.         else if (reduce)
  117.         {
  118.         if (in_white)
  119.         {               /* We're within white block     */
  120.                        /* Do nothing with character c  */
  121.         }
  122.         else
  123.         {               /* Begin block of white space   */
  124.             *pto++ = BLANK;
  125.             in_white = TRUE;
  126.         }
  127.         }
  128.         else
  129.         *pto++ = c;           /* Just copy normally           */
  130.     }
  131.     else if (iscntrl(c) && isascii(c))
  132.     {
  133.         in_white     = FALSE;
  134.         hit_nonwhite = TRUE;
  135.         if (rcontrol)
  136.         {
  137.                        /* Don't copy the ctrl char     */
  138.         }
  139.         else
  140.         *pto++ = c;           /* Just copy normally           */
  141.     }
  142.     else
  143.     {                   /* Simple nonwhite or control   */
  144.         in_white     = FALSE;      /* character               */
  145.         hit_nonwhite = TRUE;
  146.         if (isascii(c))
  147.         {
  148.         if (to_up)
  149.             c = toupper(c);
  150.         if (to_low)
  151.             c = tolower(c);
  152.         }
  153.         *pto++ = c;            /* Just copy normally           */
  154.     }
  155.     }
  156.     *pto = '\0';
  157.  
  158.     /* Now handle trailing white space                       */
  159.  
  160.     if (conv & RTWHITE)
  161.     for (c = *--pto; isspace(c) && isascii(c) && (pto >= psource);
  162.                                   c = *--pto)
  163.         *pto = '\0';
  164.  
  165.     return(psource);
  166. }
  167.