-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
165 lines (143 loc) · 3.61 KB
/
Config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "Config.h"
#include <algorithm>
#include <cctype>
#include <iostream>
#include <fstream>
std::map<std::string, double> Config::m_settings;
Config::Config()
{
}
void Config::Dump()
{
std::cout << "DUMP" << std::endl;
for (
std::map<std::string,double>::const_iterator itr = m_settings.begin();
itr != m_settings.end();
++itr)
{
std::cout << itr->first << ": " << itr->second << std::endl;
}
std::cout << "END DUMP" << std::endl;
}
bool Config::LoadFromFile(std::string filename)
{
return Parse(filename);
}
double Config::GetSetting(std::string name, double reasonable_default)
{
double retval = reasonable_default;
name = ConvertToLower(name);
if (HasKey(name)) {
std::map<std::string,double>::const_iterator itr = m_settings.find(name);
retval = (*itr).second;
}
return retval;
}
void Config::SetSetting(std::string name, double value)
{
name = ConvertToLower(name);
if (HasKey(name)) {
std::map<std::string,double>::const_iterator itr = m_settings.find(name);
// We can't get erase to work right now, so we'll use the first
// occurance
//m_settings.erase(itr);
m_settings[name] = value;
} else
{
m_settings[name] = value;
}
}
std::string Config::ConvertToLower(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), tolower);
return str;
}
bool Config::HasKey(std::string name)
{
name = ConvertToLower(name);
return (m_settings.find(name) != m_settings.end());
}
inline bool Config::ValidKeyChar(char ch)
{
return (isalpha(ch) || isdigit(ch) || (ch == '_'));
}
bool Config::Parse(std::string filename)
{
std::ifstream infile;
infile.open(filename.c_str(), std::ios_base::in);
if (!infile.is_open()) {
std::cerr << "Could not open file!" << std::endl;
return false;
}
std::string line = "";
int line_number = 0;
while (!infile.eof()) {
++line_number;
std::getline(infile,line);
std::string name, value_str;
int state = 0;
bool done = false;
bool invalid = false;
//
// Process each line to eliminate comments.
for (int i = 0; i < line.length() && !done; ++i)
{
switch(state) {
case 0: // Ignoring initial whitespace,
// looking for either comment
// or valid key name
//std::cout<<"Entering state 0 ......"<<std::endl;
if (isspace(line[i])) {
continue;
} else if (line[i] == '#') {
state = 3;
} else {
name += line[i];
state = 1;
}
break;
case 1:
// Currently reading key name,
// looking for equals sign
//std::cout<<"Entering state 1 ......"<<std::endl;
if (isspace(line[i])) {
// Ignore for now
} else if (line[i] == '=') {
state = 2;
} else if (! ValidKeyChar(line[i])) {
state = 3;
invalid = true;
} else {
name += line[i];
}
break;
case 2:
// Just read equals sign, reading
// value string
//std::cout<<"Entering state 2 ......"<<std::endl;
if (isspace(line[i])) {
// Ignore!
} else if (line[i] == '#') {
state = 3;
} else {
value_str += line[i];
}
break;
case 3:
//std::cout<<"Entering state 3 ......"<<std::endl;
done = true;
continue; // Ignore anything after the
break;
}
}
if (!invalid) {
name = ConvertToLower(name);
double val = ::atof(value_str.c_str());
//std::cout<<"Name: "<<name<<std::endl<<"Value: "<<val<<std::endl;
SetSetting(name, val);
} else {
std::cerr << "Invalid string on line " << line_number << ": " << line << std::endl;
}
}
return true;
}