[2825] | 1 | #include "Environment.h"
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | using namespace std;
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | namespace CHCDemoEngine
|
---|
| 8 | {
|
---|
| 9 |
|
---|
[2828] | 10 |
|
---|
| 11 | bool Environment::GetFloatParam(std::string &str, float &val) const
|
---|
[2825] | 12 | {
|
---|
[2828] | 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 |
|
---|
[2825] | 20 | return true;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[2826] | 23 |
|
---|
[2828] | 24 | bool Environment::GetIntParam(std::string &str, int &val) const
|
---|
[2826] | 25 | {
|
---|
[2828] | 26 | map<string, string>::const_iterator it = mParams.find(str);
|
---|
[2826] | 27 |
|
---|
[2828] | 28 | if (it == mParams.end()) // parameter not found
|
---|
| 29 | return false;
|
---|
| 30 |
|
---|
| 31 | val = atoi((*it).second.c_str());
|
---|
[2826] | 32 |
|
---|
[2828] | 33 | return true;
|
---|
| 34 | }
|
---|
[2826] | 35 |
|
---|
| 36 |
|
---|
[2828] | 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;
|
---|
[2825] | 47 | }
|
---|
[2826] | 48 |
|
---|
| 49 |
|
---|
[2828] | 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 |
|
---|
[2829] | 60 | // parse vector values
|
---|
[2828] | 61 | Tokenize((*it).second, tokens);
|
---|
| 62 |
|
---|
[2829] | 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());
|
---|
[2828] | 66 |
|
---|
| 67 | return true;
|
---|
[2826] | 68 | }
|
---|
[2828] | 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 | {
|
---|
[2829] | 81 | // this line is empty or a comment
|
---|
| 82 | if ((strlen(str) <= 1) || (str[0] == '#'))
|
---|
| 83 | continue;
|
---|
| 84 |
|
---|
[2828] | 85 | static vector<string> tokens;
|
---|
| 86 | tokens.clear();
|
---|
| 87 |
|
---|
| 88 | Tokenize(str, tokens, "=");
|
---|
| 89 |
|
---|
[2829] | 90 | if (tokens.size() == 1)
|
---|
| 91 | cerr << "parsing failed at: " << tokens[0] << endl;
|
---|
[2828] | 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 | }
|
---|