home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / unpost / part07 / sets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-18  |  2.9 KB  |  103 lines

  1. /******************************************************************************
  2. * Module    :   Bit Mapped Set Functions.
  3. *
  4. * Author    :   John W. M. Stevens.
  5. ******************************************************************************/
  6.  
  7. #include    "sets.h"
  8.  
  9. #define     NUL     '\0'
  10. #define     EOL     '\n'
  11.  
  12. /*-----------------------------------------------------------------------------
  13. | Routine   :   InSet() --- Test to see if a character is a member of a set.
  14. |
  15. | Inputs    :   CSet    - Character set to test against.
  16. |               c       - Character to test.
  17. |
  18. | Returns   :   Zero for not in the set, non-zero for in set.
  19. -----------------------------------------------------------------------------*/
  20.  
  21. int InSet(SET   CSet,
  22.           char  c)
  23. {
  24.     return( (int) (CSet[c / INT_SIZE] & (1 << (c % INT_SIZE))) );
  25. }
  26.  
  27. /*-----------------------------------------------------------------------------
  28. | Routine   :   CrtSet() --- Create a character set.
  29. |
  30. | Inputs    :   Str     - String containing set elements.
  31. | Outputs   :   CSet    - Initialized character set.
  32. -----------------------------------------------------------------------------*/
  33.  
  34. void    CrtSet(char **Str,
  35.                SET  CSet)
  36. {
  37.     register    int     i;
  38.     register    int     c;
  39.     auto        int     NegateFlag;
  40.     auto        char    LastCh;
  41.  
  42.     /*  If the first character of the set is a '^', this is a negated
  43.     *   set.
  44.     */
  45.     if (**Str == '^')
  46.     {
  47.         NegateFlag = 1;
  48.         (*Str)++;
  49.     }
  50.     else
  51.         NegateFlag = 0;
  52.  
  53.     /*  Clear the set first.    */
  54.     for (i = 0; i < SET_SIZE; i++)
  55.         CSet[i] = 0;
  56.  
  57.     /*  Get characters of set.  */
  58.     for (LastCh = '\0'; **Str && **Str != ']'; (*Str)++)
  59.     {
  60.         /*  Check for escape character. */
  61.         if (**Str == '\\')
  62.             (*Str)++;
  63.         else if (**Str == '-')
  64.         {
  65.             /*  Check to make sure that this is a range separator
  66.             *   character.
  67.             */
  68.             if ( LastCh )
  69.             {
  70.                 /*  Get termination character.  */
  71.                 if ((*Str)[1] && (*Str)[1] != ']')
  72.                 {
  73.                     /*  Skip escape character.  */
  74.                     if ((c = *++(*Str)) == '\\')
  75.                         c = *++(*Str);
  76.  
  77.                     /*  Fill in range.  */
  78.                     for (i = LastCh + 1; i < c; i++)
  79.                         CSet[i / INT_SIZE] |= 1 << (i % INT_SIZE);
  80.                     LastCh = '\0';
  81.                 }
  82.             }
  83.         }
  84.  
  85.         /*  Add character to set.   */
  86.         CSet[**Str / INT_SIZE] |= 1 << (**Str % INT_SIZE);
  87.         LastCh = **Str;
  88.     }
  89.     if (**Str == ']')
  90.         (*Str)++;
  91.  
  92.     /*  Negate set if necesary. */
  93.     if ( NegateFlag )
  94.     {
  95.         for (i = 0; i < SET_SIZE; i++)
  96.             CSet[i] = ~CSet[i];
  97.     }
  98.  
  99.     /*  Do not EVER match a '\0' or '\n'.   */
  100.     CSet[0] &= ~1;
  101.     CSet[EOL / INT_SIZE] &= ~(1 << (EOL % INT_SIZE));
  102. }
  103.