home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / util / select.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.1 KB  |  111 lines

  1. /*
  2.  *    select -- functions to create a selection table and
  3.  *    to determine whether an item is an entry in the table
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <local\std.h>
  10.  
  11. #define NRANGES    10
  12. #define NDIGITS    5
  13.  
  14. struct slist_st {
  15.     long int s_min;
  16.     long int s_max;
  17. } Slist[NRANGES + 1];
  18.  
  19. long Highest = 0;
  20.  
  21. /*
  22.  *    mkslist -- create the selection lookup table
  23.  */
  24.  
  25. int
  26. mkslist(list)
  27. char *list;
  28. {
  29.     int i;
  30.     char *listp, *s;
  31.     long tmp;
  32.  
  33.     static long save_range();
  34.  
  35.     /* fill in table of selected items */
  36.     if (*list == '\0') {
  37.         /* if no list, select all */
  38.         Slist[0].s_min = 0;
  39.         Slist[0].s_max = Highest = BIGGEST;
  40.         Slist[1].s_min = -1;
  41.     }
  42.     else {
  43.         listp = list;
  44.         for (i = 0; i < NRANGES; ++i) {
  45.             if ((s = strtok(listp, ", \t")) == NULL)
  46.                 break;
  47.             if ((tmp = save_range(i, s)) > Highest)
  48.                 Highest = tmp;
  49.             listp = NULL;
  50.         }
  51.         Slist[i].s_min = -1;
  52.     }
  53.     return (0);
  54. } /* end mkslist() */
  55.  
  56. /*
  57.  *    selected -- return non-zero value if the number
  58.  *    argument is a member of the selection list
  59.  */
  60.  
  61. int
  62. selected(n)
  63. unsigned int n;
  64. {
  65.     int i;
  66.  
  67.     /* look for converted number in selection list */ 
  68.     for (i = 0; Slist[i].s_min != -1; ++i)
  69.         if (n >= Slist[i].s_min && n <= Slist[i].s_max)
  70.             return (1);
  71.     return (0);
  72. } /* end selected() */
  73.  
  74. /*
  75.  *    save_range -- convert a string number spec to a
  76.  *    numeric range in the selection table and return
  77.  *    the highest number in the range
  78.  */
  79.  
  80. static long
  81. save_range(n, s)
  82. int n;
  83. char *s;
  84. {
  85.     int radix = 10;
  86.     char *cp, num[NDIGITS + 1];
  87.  
  88.     /* get the first (and possibly only) number */
  89.     cp = num;
  90.     while (*s != '\0' && *s != '-')
  91.         *cp++ = *s++;
  92.     *cp = '\0';
  93.     Slist[n].s_min = atol(num);
  94.     if (*s == '\0')
  95.         /* pretty narrow range, huh? */
  96.         return (Slist[n].s_max = Slist[n].s_min);
  97.  
  98.     /* get the second number */
  99.     if (*++s == '\0')
  100.         /* unspecified top end of range */
  101.         Slist[n].s_max = BIGGEST;
  102.     else {
  103.         cp = num;
  104.         while (*s != '\0' && *s != '-')
  105.             *cp++ = *s++;
  106.         *cp = '\0';
  107.         Slist[n].s_max = atol(num);
  108.     }
  109.     return (Slist[n].s_max);
  110. } /* end save_range() */
  111.