cgv
Loading...
Searching...
No Matches
scan_enum.cxx
1#include "scan_enum.h"
2#include "tokenizer.h"
3#include "scan.h"
4#include <algorithm>
5
6namespace cgv {
7 namespace utils {
8
10void parse_enum_declarations(const std::string& enum_declarations, std::vector<token>& enum_names, std::vector<int>& enum_values)
11{
12 std::vector<token> toks;
13 tokenizer(enum_declarations).set_ws(";,").bite_all(toks);
14 int val = 0;
15 for (unsigned int i=0; i<toks.size(); ++i) {
16 std::vector<token> defs;
17 tokenizer(toks[i]).set_ws("=").bite_all(defs);
18 if (defs.size() < 1)
19 continue;
20 int new_val;
21 if (defs.size() >= 2 && is_integer(defs[1].begin, defs[1].end, new_val))
22 val = new_val;
23 while (defs[0].size() > 0 && *defs[0].begin == ' ')
24 ++defs[0].begin;
25 while (defs[0].size() > 0 && defs[0].end[-1] == ' ')
26 --defs[0].end;
27 enum_names.push_back(defs[0]);
28 enum_values.push_back(val);
29 ++val;
30 }
31
32}
33
35unsigned find_enum_index(int value, const std::vector<int>& enum_values)
36{
37 std::vector<int>::const_iterator i = std::find(enum_values.begin(), enum_values.end(), value);
38 if (i == enum_values.end())
39 return -1;
40 return (int)(i-enum_values.begin());
41}
42
44unsigned find_enum_index(const std::string& name, const std::vector<token>& enum_names)
45{
46 for (unsigned i=0; i<enum_names.size(); ++i) {
47 if (enum_names[i] == name)
48 return i;
49 }
50 return -1;
51}
53std::string find_enum_name(const std::string& enum_declarations, int value)
54{
55 std::vector<token> enum_names;
56 std::vector<int> enum_values;
57
58 parse_enum_declarations(enum_declarations, enum_names, enum_values);
59 return to_string(enum_names[find_enum_index(value, enum_values)]);
60}
61
62 }
63}
the tokenizer allows to split text into tokens in a convenient way.
Definition tokenizer.h:68
tokenizer & set_ws(const std::string &ws)
set the list of white spaces, that separate tokens and are skipped
Definition tokenizer.cxx:38
void parse_enum_declarations(const std::string &enum_declarations, std::vector< token > &enum_names, std::vector< int > &enum_values)
parse an enum declaration string into names and values
Definition scan_enum.cxx:10
std::string find_enum_name(const std::string &enum_declarations, int value)
lookup name of value in enum declaration
Definition scan_enum.cxx:53
unsigned find_enum_index(int value, const std::vector< int > &enum_values)
convert value to index
Definition scan_enum.cxx:35
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool is_integer(const char *begin, const char *end, int &value)
check if the text range (begin,end( defines an integer value. If yes, store the value in the passed r...
Definition scan.cxx:367
the cgv namespace
Definition print.h:11
Helper functions to process strings.
Helper functions to process enum declarations from strings.