forked from aalto-speech/AaltoASR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleConfig.cc
236 lines (210 loc) · 6.03 KB
/
ModuleConfig.cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <assert.h>
#include <vector>
#include "str.hh"
#include "ModuleConfig.hh"
bool
ModuleConfig::exists(const std::string &name) const
{
return m_value_map.find(name) != m_value_map.end();
}
void
ModuleConfig::set(const std::string &name, int value)
{
assert(name.find_first_of(" \t\n") == std::string::npos);
insert(name, str::fmt(64, "%d", value));
}
void
ModuleConfig::set(const std::string &name, float value)
{
assert(name.find_first_of(" \t\n") == std::string::npos);
insert(name, str::fmt(64, "%g", value));
}
void
ModuleConfig::set(const std::string &name, const std::string &str)
{
assert(name.find_first_of(" \t\n") == std::string::npos);
assert(str.find_first_of("\n") == std::string::npos);
insert(name, str);
}
void
ModuleConfig::set(const std::string &name, const std::vector<int> &vec)
{
assert(name.find_first_of(" \t\n") == std::string::npos);
std::string value_str;
for (int i = 0; i < (int)vec.size(); i++) {
if (i != 0)
value_str.append(" ");
value_str.append(str::fmt(64, "%d", vec[i]));
}
insert(name, value_str);
}
void
ModuleConfig::set(const std::string &name, const std::vector<float> &vec)
{
assert(name.find_first_of(" \t\n") == std::string::npos);
std::string value_str;
for (int i = 0; i < (int)vec.size(); i++) {
if (i != 0)
value_str.append(" ");
value_str.append(str::fmt(64, "%g", vec[i]));
}
insert(name, value_str);
}
void
ModuleConfig::set(const std::string &name, const std::vector<std::string> &vec)
{
assert(name.find_first_of(" \t\n") == std::string::npos);
std::string value_str;
for (int i = 0; i < (int)vec.size(); i++) {
assert(vec[i].find_first_of(" \t\n") == std::string::npos);
if (i != 0)
value_str.append(" ");
value_str.append(vec[i]);
}
insert(name, value_str);
}
bool
ModuleConfig::get(const std::string &name, int &value) const
{
ValueMap::const_iterator it = m_value_map.find(name);
if (it == m_value_map.end())
return false;
bool ok = true;
value = str::str2long(&m_values.at(it->second), &ok);
if (!ok)
throw std::string("invalid integer value: ") + m_values.at(it->second);
return true;
}
bool
ModuleConfig::get(const std::string &name, float &value) const
{
ValueMap::const_iterator it = m_value_map.find(name);
if (it == m_value_map.end())
return false;
bool ok = true;
value = str::str2float(&m_values.at(it->second), &ok);
if (!ok)
throw std::string("invalid float value: ") + m_values.at(it->second);
return true;
}
bool
ModuleConfig::get(const std::string &name, std::string &str) const
{
ValueMap::const_iterator it = m_value_map.find(name);
if (it == m_value_map.end())
return false;
str = m_values.at(it->second);
return true;
}
bool
ModuleConfig::get(const std::string &name, std::vector<int> &vec) const
{
ValueMap::const_iterator it = m_value_map.find(name);
if (it == m_value_map.end())
return false;
std::vector<std::string> fields;
str::split(&m_values.at(it->second), " \t", true, &fields);
vec.resize(fields.size());
bool ok = true;
for (int i = 0; i < (int)fields.size(); i++) {
vec[i] = str::str2long(&fields[i], &ok);
if (!ok)
throw std::string("invalid value '") + fields[i] +
std::string("'in integer vector: ") + m_values.at(it->second);
}
return true;
}
bool
ModuleConfig::get(const std::string &name, std::vector<float> &vec) const
{
ValueMap::const_iterator it = m_value_map.find(name);
if (it == m_value_map.end())
return false;
std::vector<std::string> fields;
str::split(&m_values.at(it->second), " \t", true, &fields);
vec.resize(fields.size());
bool ok = true;
for (int i = 0; i < (int)fields.size(); i++) {
vec[i] = str::str2float(&fields[i], &ok);
if (!ok)
throw std::string("invalid value '") + fields[i] +
std::string("' in float vector: ") + m_values.at(it->second);
}
return true;
}
bool
ModuleConfig::get(const std::string &name, std::vector<std::string> &vec) const
{
ValueMap::const_iterator it = m_value_map.find(name);
if (it == m_value_map.end())
return false;
str::split(&m_values.at(it->second), " \t", true, &vec);
return true;
}
void
ModuleConfig::read(FILE *file)
{
m_num_lines_read = 0;
bool first_line = true;
std::string line;
std::vector<std::string> fields;
while (1) {
if (!str::read_line(&line, file, true))
throw std::string("unexpected end of module config file");
m_num_lines_read++;
str::clean(&line, " \t");
if (line.empty())
continue;
if (first_line) {
if (line != "{")
throw std::string("'{' expected in module config file: ") + line;
first_line = false;
continue;
}
if (line == "}")
break;
str::split(&line, " \t", true, &fields, 2);
assert(fields.size() >= 1 && fields.size() <= 2);
if (fields.size() == 1)
throw std::string("value missing for option: ") + line;
if (m_value_map.find(fields[0]) != m_value_map.end())
throw std::string("value redefined: ") + line;
insert(fields[0], fields[1]);
}
assert(m_value_map.size() == m_names.size());
assert(m_value_map.size() == m_values.size());
}
void
ModuleConfig::write(FILE *file, int indent) const
{
assert(indent >= 0);
assert(m_value_map.size() == m_names.size());
assert(m_value_map.size() == m_values.size());
std::string module_indent_str(indent, ' ');
std::string indent_str(indent+2, ' ');
fputs(module_indent_str.c_str(), file);
fputs("{\n", file);
for (int i = 0; i < (int)m_values.size(); i++) {
fputs(indent_str.c_str(), file);
fputs(m_names[i].c_str(), file);
fputc(' ', file);
fputs(m_values[i].c_str(), file);
fputc('\n', file);
}
fputs(module_indent_str.c_str(), file);
fputs("}\n", file);
}
void // private
ModuleConfig::insert(const std::string &name, const std::string &value)
{
std::pair<ValueMap::iterator, bool> ret =
m_value_map.insert(ValueMap::value_type(name, (int)m_values.size()));
if (ret.second) {
m_names.push_back(name);
m_values.push_back(value);
}
else {
int index = ret.first->second;
m_values[index] = value;
}
}