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::GetStringParam(std::string &str, string &val) const
|
---|
72 | {
|
---|
73 | map<string, string>::const_iterator it = mParams.find(str);
|
---|
74 |
|
---|
75 | if (it == mParams.end()) // parameter not found
|
---|
76 | return false;
|
---|
77 |
|
---|
78 | // just assign value as it is - the line break character
|
---|
79 | val = (*it).second;
|
---|
80 | val.erase(val.end() - 1);
|
---|
81 |
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | bool Environment::Read(const string &filename)
|
---|
87 | {
|
---|
88 | FILE *file;
|
---|
89 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
90 | return false;
|
---|
91 |
|
---|
92 | char str[256];
|
---|
93 |
|
---|
94 | while (fgets(str, 256, file) != NULL)
|
---|
95 | {
|
---|
96 | // this line is empty or a comment
|
---|
97 | if ((strlen(str) <= 1) || (str[0] == '#'))
|
---|
98 | continue;
|
---|
99 |
|
---|
100 | static vector<string> tokens;
|
---|
101 | tokens.clear();
|
---|
102 |
|
---|
103 | Tokenize(str, tokens, "=");
|
---|
104 |
|
---|
105 | if (tokens.size() == 1)
|
---|
106 | cerr << "parsing failed at: " << tokens[0] << endl;
|
---|
107 | // store parameter
|
---|
108 | mParams[tokens[0]] = tokens[1];
|
---|
109 | }
|
---|
110 |
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | void Environment::Tokenize(const string& str,
|
---|
116 | vector<string> &tokens,
|
---|
117 | const string &delim) const
|
---|
118 | {
|
---|
119 | // skip delimiters the string begins with
|
---|
120 | string::size_type lastPos = str.find_first_not_of(delim, 0);
|
---|
121 | // find next delimiter
|
---|
122 | string::size_type pos = str.find_first_of(delim, lastPos);
|
---|
123 |
|
---|
124 | // find tokens until string has been completely parsed
|
---|
125 | while ((string::npos != pos) || (string::npos != lastPos))
|
---|
126 | {
|
---|
127 | // add new token
|
---|
128 | tokens.push_back(str.substr(lastPos, pos - lastPos));
|
---|
129 | // skip delimiters, go to start of next token
|
---|
130 | lastPos = str.find_first_not_of(delim, pos);
|
---|
131 | // found next delimiter
|
---|
132 | pos = str.find_first_of(delim, lastPos);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | }
|
---|