home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / extra / ustdio / uscanset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  4.2 KB  |  184 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File uscanset.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   12/03/98    stephen        Creation.
  18. *   03/13/99    stephen     Modified for new C API.
  19. *******************************************************************************
  20. */
  21.  
  22. #include "uscanset.h"
  23.  
  24.  
  25. bool_t
  26. u_scanf_scanset_add(u_scanf_scanset     *scanset,
  27.             UChar         c)
  28. {
  29.   if(scanset->single_count == U_SCANF_MAX_SCANSET_SIZE - 1)
  30.     return FALSE;
  31.  
  32.   scanset->singles[ scanset->single_count ] = c;
  33.   (scanset->single_count)++;
  34.  
  35.   return TRUE;
  36. }
  37.  
  38. bool_t
  39. u_scanf_scanset_addrange(u_scanf_scanset     *scanset,
  40.              UChar             start, 
  41.              UChar             end)
  42. {
  43.   if(scanset->pair_count == U_SCANF_MAX_SCANSET_SIZE - 1)
  44.     return FALSE;
  45.   
  46.   scanset->pairs[ scanset->pair_count ].start     = start;
  47.   scanset->pairs[ scanset->pair_count ].end     = end;
  48.  
  49.   (scanset->pair_count)++;
  50.  
  51.   return TRUE;
  52. }
  53.  
  54. bool_t
  55. u_scanf_scanset_init(u_scanf_scanset     *scanset,
  56.              const UChar    *s,
  57.              int32_t        *len)
  58. {
  59.   UChar        c;
  60.   const UChar     *limit;
  61.   int32_t     count;
  62.   bool_t    result;
  63.  
  64.  
  65.   /* set up parameters */
  66.   limit = s + *len;
  67.   count = 0;
  68.  
  69.   /* initialize to defaults */
  70.   scanset->single_count = 0;
  71.   scanset->pair_count     = 0;
  72.   scanset->is_inclusive = TRUE;
  73.  
  74.   /* check to see if this is an inclusive or exclusive scanset */
  75.   if(*s == 0x005E) { /* '^' */
  76.     scanset->is_inclusive = FALSE;
  77.  
  78.     /* increment s and count */
  79.     ++s;
  80.     ++count;
  81.   }
  82.  
  83.   /* if ']' is the first character, add it */
  84.   else if(*s == 0x005D) {
  85.     result = u_scanf_scanset_add(scanset, *s++);
  86.  
  87.     /* increment count */
  88.     ++count;
  89.   }
  90.  
  91.   /* if the first character is '^' and the second is ']', add ']' */
  92.   if( ! scanset->is_inclusive && *s == 0x005D) {
  93.     result = u_scanf_scanset_add(scanset, *s++);
  94.  
  95.     /* increment count */
  96.     ++count;
  97.   }
  98.  
  99.   /* add characters until a ']' is seen, adding ranges as necessary */
  100.   while(s < limit) {
  101.     
  102.     /* grab the current character */
  103.     c = *s++;
  104.  
  105.     /* if it's a ']', we're done */
  106.     if(c == 0x005D)
  107.       break;
  108.  
  109.     /* check if this is a range */
  110.     if(*s == 0x002D && *(s+1) != 0x005D) {
  111.       result = u_scanf_scanset_addrange(scanset, c, *(s+1));
  112.       
  113.       /* increment count and s */
  114.       s        += 2;
  115.       count     += 2;
  116.     }
  117.  
  118.     /* otherwise, just add the character */
  119.     else {
  120.       result = u_scanf_scanset_add(scanset, c);
  121.     }
  122.  
  123.       /* increment count */
  124.       ++count;
  125.   }
  126.  
  127.   /* update length to reflect # of characters consumed */
  128.   *len = count;
  129.   return result;
  130. }
  131.  
  132. bool_t
  133. u_scanf_scanset_in(u_scanf_scanset     *scanset,
  134.            UChar         c)
  135. {
  136.   int i;
  137.  
  138.   /* if this is an inclusive scanset, make sure c is in it */
  139.   if(scanset->is_inclusive) {
  140.  
  141.     /* check the single chars first*/
  142.     for(i = 0; i < scanset->single_count; ++i) {
  143.       if(c == scanset->singles[i])
  144.     return TRUE;
  145.     }
  146.     
  147.     /* check the pairs */
  148.     for(i = 0; i < scanset->pair_count; ++i) {
  149.       if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end)
  150.     return TRUE;
  151.     }
  152.    
  153.     /* didn't find it, so c isn't in set */
  154.     return FALSE;
  155.   }
  156.  
  157.   /* otherwise, make sure c isn't in it */
  158.   else {
  159.  
  160.     /* check the single chars first*/
  161.     for(i = 0; i < scanset->single_count; ++i) {
  162.       if(c == scanset->singles[i])
  163.     return FALSE;
  164.     }
  165.     
  166.     /* check the pairs */
  167.     for(i = 0; i < scanset->pair_count; ++i) {
  168.       if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end)
  169.     return FALSE;
  170.     }
  171.     
  172.     /* didn't find it, so c is in set */
  173.     return TRUE;
  174.   }
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.