Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / gperf / src / iterator.cc
1 /* Provides an Iterator for keyword characters.
2    Copyright (C) 1989-1998, 2000 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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA.  */
20
21 #include "iterator.h"
22
23 #include <ctype.h>
24 #include "trace.h"
25
26 /* Constructor for Iterator. */
27
28 Iterator::Iterator (const char *s, int lo, int hi, int word_end, int bad_val, int key_end)
29 {
30   T (Trace t ("Iterator::Iterator");)
31   end         = key_end;
32   error_value = bad_val;
33   end_word    = word_end;
34   str         = s;
35   hi_bound    = hi;
36   lo_bound    = lo;
37 }
38
39 /* Provide an Iterator, returning the ``next'' value from
40    the list of valid values given in the constructor. */
41
42 int
43 Iterator::operator() (void)
44 {
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 '0': case '1': case '2': case '3': case '4':
67           case '5': case '6': case '7': case '8': case '9':
68             for (curr_value = 0; isdigit ((unsigned char)(*str)); str++)
69               curr_value = curr_value * 10 + (*str - '0');
70
71             if (*str == '-')
72               {
73
74                 for (size = 1, upper_bound = 0;
75                      isdigit ((unsigned char)(*++str));
76                      upper_bound = upper_bound * 10 + (*str - '0'));
77
78                 if (upper_bound <= curr_value || upper_bound > hi_bound)
79                   return error_value;
80               }
81             return curr_value >= lo_bound && curr_value <= hi_bound
82               ? curr_value : error_value;
83           }
84
85       return end;
86     }
87 }