home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / gperf / iterator.cc < prev    next >
C/C++ Source or Header  |  1993-07-30  |  3KB  |  88 lines

  1. /* Provides an Iterator for keyword characters.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  4.  
  5. This file is part of GNU GPERF.
  6.  
  7. GNU GPERF is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU GPERF is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU GPERF; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stream.h>
  22. #include <std.h>
  23. #include <ctype.h>
  24. #include "iterator.h"
  25. #include "trace.h"
  26.  
  27. /* Constructor for Iterator. */
  28.  
  29. Iterator::Iterator (char *s, int lo, int hi, int word_end, int bad_val, int key_end)
  30. {
  31.   T (Trace t ("Iterator::Iterator");)
  32.   end         = key_end;
  33.   error_value = bad_val;
  34.   end_word    = word_end;
  35.   str         = s;
  36.   hi_bound    = hi;
  37.   lo_bound    = lo;
  38. }
  39.  
  40. /* Provide an Iterator, returning the ``next'' value from 
  41.    the list of valid values given in the constructor. */
  42.  
  43. int 
  44. Iterator::operator() (void) 
  45.   T (Trace t ("Iterator::operator()");)
  46. /* Variables to record the Iterator's status when handling ranges, e.g., 3-12. */
  47.  
  48.   static int size;              
  49.   static int curr_value;           
  50.   static int upper_bound;
  51.  
  52.   if (size) 
  53.     { 
  54.       if (++curr_value >= upper_bound) 
  55.         size = 0;    
  56.       return curr_value; 
  57.     }
  58.   else 
  59.     {
  60.       while (*str) 
  61.         switch (*str)
  62.           {
  63.           default: return error_value;
  64.           case ',': str++; break;
  65.           case '$': str++; return end_word;
  66.           case '1'..'9':
  67.             for (curr_value = 0; isdigit (*str); str++) 
  68.               curr_value = curr_value * 10 + *str - '0';
  69.  
  70.             if (*str == '-') 
  71.               {
  72.  
  73.                 for (size = 1, upper_bound = 0; 
  74.                      isdigit (*++str); 
  75.                      upper_bound = upper_bound * 10 + *str - '0');
  76.  
  77.                 if (upper_bound <= curr_value || upper_bound > hi_bound) 
  78.                   return error_value;
  79.               }
  80.             return curr_value >= lo_bound && curr_value <= hi_bound 
  81.               ? curr_value : error_value;
  82.           }
  83.  
  84.       return end;
  85.     }
  86. }
  87.