source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.cpp @ 2828

Revision 2828, 2.5 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "Environment.h"
2
3
4using namespace std;
5
6
7namespace CHCDemoEngine
8{
9
10
11bool 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
24bool 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
37bool 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
50bool 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        Tokenize((*it).second, tokens);
61
62        val.x = (float)atof(tokens[1].c_str());
63        val.y = (float)atof(tokens[2].c_str());
64        val.z = (float)atof(tokens[3].c_str());
65
66        return true;
67}
68
69
70bool Environment::Read(const string &filename)
71{
72        FILE *file;
73        if ((file = fopen(filename.c_str(), "rt")) == NULL)
74                return false;
75
76        char str[256];
77
78        while (fgets(str, 256, file) != NULL)
79        {
80                static vector<string> tokens;
81                tokens.clear();
82
83                Tokenize(str, tokens, "=");
84
85                // this line is a comment
86                if (tokens[0][0] == '#')
87                        continue;
88
89                // store parameter
90                mParams[tokens[0]] = tokens[1];
91        }
92
93        return true;
94}
95
96
97void Environment::Tokenize(const string& str,
98                                                   vector<string> &tokens,
99                                                   const string &delim) const
100{
101    // skip delimiters the string begins with
102    string::size_type lastPos = str.find_first_not_of(delim, 0);
103    // find next delimiter
104    string::size_type pos = str.find_first_of(delim, lastPos);
105
106        // find tokens until string has been completely parsed
107    while ((string::npos != pos) || (string::npos != lastPos))
108    {
109        // add new token
110        tokens.push_back(str.substr(lastPos, pos - lastPos));
111        // skip delimiters, go to start of next token
112        lastPos = str.find_first_not_of(delim, pos);
113        // found next delimiter
114        pos = str.find_first_of(delim, lastPos);
115    }
116}
117
118
119}
Note: See TracBrowser for help on using the repository browser.