Statistics
| Branch: | Revision:

root / src / common / enumstr.cc @ 81ad8b66

History | View | Annotate | Download (2.08 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  ENUMSTR.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <stdlib.h>
18
#include "enumstr.h"
19
20
USING_NAMESPACE
21
22
EnumStringIterator::EnumStringIterator(const char *s)
23
{
24
     // loop through string to check its syntax
25
     str = s;
26
     current = until = -1;
27
     err = false;
28
     while ((*this)++ != -1);
29
30
     // position on first value
31
     if (!err) {
32
         str = s;
33
         current = until = -1;
34
         (*this)++;
35
     }
36
}
37
38
static void skip_whitespace(const char *&str)
39
{
40
     while (*str==' ' || *str=='\t') str++;
41
}
42
43
static int get_number(const char *&str, int& number)
44
{
45
46
     while (*str==' ' || *str=='\t') str++;
47
     if (*str<'0' || *str>'9')
48
         return 0;
49
     else
50
     {
51
         number = atoi(str);
52
         while (*str>='0' && *str<='9') str++;
53
         return 1;
54
     }
55
}
56
57
int EnumStringIterator::operator++(int)
58
{
59
     if (err || !str)
60
        return -1;
61
62
     if (current<until)
63
        return ++current;
64
65
     skip_whitespace(str);
66
     if (*str=='\0')
67
        return current=-1;
68
69
     if (!get_number(str,current)) {err=true;return -1;}
70
     until = -1;
71
     if (*str=='\0')
72
         ;
73
     else if (*str==',')
74
         str++;
75
     else if (*str=='-' || (*str=='.' && *(str+1)=='.'))
76
     {
77
         str++;
78
         if (*str=='.') str++;
79
         skip_whitespace(str);
80
         if (!get_number(str,until)) {err=true;return -1;}
81
         skip_whitespace(str);
82
         if (*str=='\0')
83
            ;
84
         else if (*str==',')
85
            str++;
86
         else
87
            {err=true;return -1;}
88
     }
89
     else
90
         {err=true;return -1;}
91
     return current;
92
}