#include "iterator.h"
#include <ctype.h>
#include "trace.h"
Iterator::Iterator (const char *s, int lo, int hi, int word_end, int bad_val, int key_end)
{
T (Trace t ("Iterator::Iterator");)
end = key_end;
error_value = bad_val;
end_word = word_end;
str = s;
hi_bound = hi;
lo_bound = lo;
}
int
Iterator::operator() (void)
{
T (Trace t ("Iterator::operator()");)
static int size;
static int curr_value;
static int upper_bound;
if (size)
{
if (++curr_value >= upper_bound)
size = 0;
return curr_value;
}
else
{
while (*str)
switch (*str)
{
default: return error_value;
case ',': str++; break;
case '$': str++; return end_word;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
for (curr_value = 0; isdigit ((unsigned char)(*str)); str++)
curr_value = curr_value * 10 + (*str - '0');
if (*str == '-')
{
for (size = 1, upper_bound = 0;
isdigit ((unsigned char)(*++str));
upper_bound = upper_bound * 10 + (*str - '0'));
if (upper_bound <= curr_value || upper_bound > hi_bound)
return error_value;
}
return curr_value >= lo_bound && curr_value <= hi_bound
? curr_value : error_value;
}
return end;
}
}