-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpconfig.hpp
76 lines (64 loc) · 1.62 KB
/
pconfig.hpp
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
#ifndef _PCONFIG_HPP_
#define _PCONFIG_HPP_
#include <string>
#include <fstream>
#include <sstream>
#include <memory>
#include <cerrno>
#include <exception>
struct PConfigEntry {
double rx;
double ry;
double vx;
double vy;
double radius;
int mass;
int r;
int g;
int b;
};
class PConfigError : public std::runtime_error {
private:
int line;
int col;
public:
PConfigError(int line, int col, const std::string msg) :
std::runtime_error(msg), line(line), col(col) {};
virtual ~PConfigError() {};
int getLine() const {
return line;
}
int getColumn() const {
return col;
}
};
// Quick&dirty config parser.
class PConfig {
private:
int colNum;
int lineNum;
std::ifstream ifile;
template <class T>
T readValue(std::istringstream &ss, const std::string &name,
T floor, T ceil) {
T val;
ss >> val;
if (ss.fail())
throw PConfigError(lineNum, colNum, name + " was expected next, but " +
"got nothing");
if (val < floor)
throw PConfigError(lineNum, colNum, name + " can not be less than " +
std::to_string(floor));
if (val > ceil)
throw PConfigError(lineNum, colNum, name + " can not be greater than " +
std::to_string(ceil));
colNum++;
return val;
}
public:
PConfig(const char *cfg_file);
virtual ~PConfig();
std::unique_ptr<PConfigEntry> nextEntry();
static const char *formatString();
};
#endif /* _PCONFIG_HPP_ */