1 | #include "Environment.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | using namespace std;
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace CHCDemoEngine
|
---|
8 | {
|
---|
9 |
|
---|
10 |
|
---|
11 | bool Environment::GetFloatParam(std::string &str, float &val) const
|
---|
12 | {
|
---|
13 | map<string, string>::const_iterator it = mParams.find(str);
|
---|
14 |
|
---|
15 | if (it == mParams.end()) // parameter not found
|
---|
16 | return false;
|
---|
17 |
|
---|
18 | val = (float)atof((*it).second.c_str());
|
---|
19 |
|
---|
20 | return true;
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | bool Environment::GetIntParam(std::string &str, int &val) const
|
---|
25 | {
|
---|
26 | map<string, string>::const_iterator it = mParams.find(str);
|
---|
27 |
|
---|
28 | if (it == mParams.end()) // parameter not found
|
---|
29 | return false;
|
---|
30 |
|
---|
31 | val = atoi((*it).second.c_str());
|
---|
32 |
|
---|
33 | return true;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | bool Environment::GetBoolParam(std::string &str, bool &val) const
|
---|
38 | {
|
---|
39 | map<string, string>::const_iterator it = mParams.find(str);
|
---|
40 |
|
---|
41 | if (it == mParams.end()) // parameter not found
|
---|
42 | return false;
|
---|
43 |
|
---|
44 | val = (bool)atoi((*it).second.c_str());
|
---|
45 |
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | bool Environment::GetVectorParam(std::string &str, Vector3 &val) const
|
---|
51 | {
|
---|
52 | map<string, string>::const_iterator it = mParams.find(str);
|
---|
53 |
|
---|
54 | static vector<string> tokens;
|
---|
55 | tokens.clear();
|
---|
56 |
|
---|
57 | if (it == mParams.end()) // parameter not found
|
---|
58 | return false;
|
---|
59 |
|
---|
60 | // parse vector values
|
---|
61 | Tokenize((*it).second, tokens);
|
---|
62 |
|
---|
63 | val.x = (float)atof(tokens[0].c_str());
|
---|
64 | val.y = (float)atof(tokens[1].c_str());
|
---|
65 | val.z = (float)atof(tokens[2].c_str());
|
---|
66 |
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | bool Environment::Read(const string &filename)
|
---|
72 | {
|
---|
73 | FILE *file;
|
---|
74 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
75 | return false;
|
---|
76 |
|
---|
77 | char str[256];
|
---|
78 |
|
---|
79 | while (fgets(str, 256, file) != NULL)
|
---|
80 | {
|
---|
81 | // this line is empty or a comment
|
---|
82 | if ((strlen(str) <= 1) || (str[0] == '#'))
|
---|
83 | continue;
|
---|
84 |
|
---|
85 | static vector<string> tokens;
|
---|
86 | tokens.clear();
|
---|
87 |
|
---|
88 | Tokenize(str, tokens, "=");
|
---|
89 |
|
---|
90 | if (tokens.size() == 1)
|
---|
91 | cerr << "parsing failed at: " << tokens[0] << endl;
|
---|
92 | // store parameter
|
---|
93 | mParams[tokens[0]] = tokens[1];
|
---|
94 | }
|
---|
95 |
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void Environment::Tokenize(const string& str,
|
---|
101 | vector<string> &tokens,
|
---|
102 | const string &delim) const
|
---|
103 | {
|
---|
104 | // skip delimiters the string begins with
|
---|
105 | string::size_type lastPos = str.find_first_not_of(delim, 0);
|
---|
106 | // find next delimiter
|
---|
107 | string::size_type pos = str.find_first_of(delim, lastPos);
|
---|
108 |
|
---|
109 | // find tokens until string has been completely parsed
|
---|
110 | while ((string::npos != pos) || (string::npos != lastPos))
|
---|
111 | {
|
---|
112 | // add new token
|
---|
113 | tokens.push_back(str.substr(lastPos, pos - lastPos));
|
---|
114 | // skip delimiters, go to start of next token
|
---|
115 | lastPos = str.find_first_not_of(delim, pos);
|
---|
116 | // found next delimiter
|
---|
117 | pos = str.find_first_of(delim, lastPos);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | }
|
---|