#include "Environment.h" using namespace std; namespace CHCDemoEngine { bool Environment::GetFloatParam(std::string &str, float &val) const { map::const_iterator it = mParams.find(str); if (it == mParams.end()) // parameter not found return false; val = (float)atof((*it).second.c_str()); return true; } bool Environment::GetIntParam(std::string &str, int &val) const { map::const_iterator it = mParams.find(str); if (it == mParams.end()) // parameter not found return false; val = atoi((*it).second.c_str()); return true; } bool Environment::GetBoolParam(std::string &str, bool &val) const { map::const_iterator it = mParams.find(str); if (it == mParams.end()) // parameter not found return false; val = (bool)atoi((*it).second.c_str()); return true; } bool Environment::GetVectorParam(std::string &str, Vector3 &val) const { map::const_iterator it = mParams.find(str); static vector tokens; tokens.clear(); if (it == mParams.end()) // parameter not found return false; Tokenize((*it).second, tokens); val.x = (float)atof(tokens[1].c_str()); val.y = (float)atof(tokens[2].c_str()); val.z = (float)atof(tokens[3].c_str()); return true; } bool Environment::Read(const string &filename) { FILE *file; if ((file = fopen(filename.c_str(), "rt")) == NULL) return false; char str[256]; while (fgets(str, 256, file) != NULL) { static vector tokens; tokens.clear(); Tokenize(str, tokens, "="); // this line is a comment if (tokens[0][0] == '#') continue; // store parameter mParams[tokens[0]] = tokens[1]; } return true; } void Environment::Tokenize(const string& str, vector &tokens, const string &delim) const { // skip delimiters the string begins with string::size_type lastPos = str.find_first_not_of(delim, 0); // find next delimiter string::size_type pos = str.find_first_of(delim, lastPos); // find tokens until string has been completely parsed while ((string::npos != pos) || (string::npos != lastPos)) { // add new token tokens.push_back(str.substr(lastPos, pos - lastPos)); // skip delimiters, go to start of next token lastPos = str.find_first_not_of(delim, pos); // found next delimiter pos = str.find_first_of(delim, lastPos); } } }