-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job.cc
189 lines (161 loc) · 2.86 KB
/
Job.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
#include "Job.hh"
#include "support.hh"
class ValueGetter
{
public:
ValueGetter(): _value() {}
JobIStream& get(JobIStream& is);
const std::string& value() const { return _value; }
private:
std::string _value;
JobIStream& getQuoted(char quote, JobIStream& is, bool keepEscape=false);
JobIStream& getBlock(JobIStream& is);
void warn(const std::string& msg)
{
std::cerr << msg << "\n";
}
};
JobIStream& ValueGetter::get(JobIStream& is)
{
char c;
is >> c;
if (is.eof())
throw UnexpectedEOF();
if (c == '"' || c == '\'')
return getQuoted(c, is);
if (c == '{')
return getBlock(is);
is.unget();
is >> _value;
return is;
}
JobIStream& ValueGetter::getQuoted(char quote, JobIStream& is, bool keepEscape)
{
int c;
bool escaped = false;
_value.clear();
while (true)
{
c = is.get();
if (is.eof() || (c == quote && !escaped) || c == '\n')
break;
if (keepEscape)
{
_value += c;
}
else if (escaped)
{
switch (c)
{
case '\\': _value += '\\'; break;
case 'a' : _value += '\a'; break;
case 'b' : _value += '\b'; break;
case 'f' : _value += '\f'; break;
case 'n' : _value += '\n'; break;
case 'r' : _value += '\r'; break;
case 't' : _value += '\t'; break;
case 'v' : _value += '\v'; break;
default : _value += c;
}
escaped = false;
}
else if (c == '\\')
{
escaped = true;
}
else
{
_value += c;
}
}
if (c != quote)
warn("Unterminated string value");
return is;
}
JobIStream& ValueGetter::getBlock(JobIStream& is)
{
bool open = true;
_value.clear();
while (open)
{
int c = is.get();
if (is.eof())
{
is.getLine();
c = is.get();
if (is.eof())
throw UnexpectedEOF();
}
switch (c)
{
case '\'':
case '"':
{
ValueGetter vg;
vg.getQuoted(c, is, true);
_value += char(c) + vg.value() + char(c);
break;
}
case '{':
{
ValueGetter vg;
vg.getBlock(is);
_value += '{' + vg.value() + '}';
break;
}
case '}':
open = false;
break;
default:
_value += c;
}
}
return is;
}
JobIStream& operator>>(JobIStream& is, ValueGetter& vg)
{
return vg.get(is);
}
JobIStream& Job::scan(JobIStream& is)
{
while (true)
{
is >> getline;
if (is.eof())
break;
std::string key;
is >> key;
ValueGetter vg;
is >> vg;
_params[lower(key)] = vg.value();
}
return is;
}
const std::string Job::get(const std::string& key) const
{
ParamMap::const_iterator it = _params.find(lower(key));
if (it == _params.end())
throw InvalidIndex(key);
return it->second;
}
const std::string Job::get(const std::string& key,
const std::string& defval) const
{
try
{
return get(key);
}
catch (const InvalidIndex&)
{
return defval;
}
}
template <>
const Geometry Job::get<Geometry>(const std::string& key) const
{
std::istringstream is(get(key));
JobIStream jis(is);
Geometry geom;
jis >> geom;
return geom;
}