ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/LimitTool/Chameleon.h
Revision: 1.1.1.1 (vendor branch)
Committed: Fri Jun 17 13:09:26 2011 UTC (13 years, 10 months ago) by tschum
Content type: text/plain
Branch: tschum, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
Evolved standalone RA7 limit calculation tool

File Contents

# Content
1 #ifndef CHAMELEON_H
2 #define CHAMELEON_H
3
4
5 //---chameleon class
6
7 #include <sstream>
8 #include <string>
9 #include <map>
10 #include <fstream>
11 #include <string>
12 #include <iostream>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <math.h>
16
17 class Chameleon {
18
19 public:
20 Chameleon() {}
21 Chameleon(std::string const& value) {
22 value_=value;
23 }
24
25
26 Chameleon(const char* c) {
27 value_=c;
28 }
29
30 Chameleon(double d) {
31 std::stringstream s;
32 s<<d;
33 value_=s.str();
34 }
35
36 Chameleon(Chameleon const& other) {
37 value_=other.value_;
38 }
39
40 Chameleon& operator=(Chameleon const& other) {
41 value_=other.value_;
42 return *this;
43 }
44
45 Chameleon& operator=(double i) {
46 std::stringstream s;
47 s << i;
48 value_ = s.str();
49 return *this;
50 }
51
52 Chameleon& operator=(std::string const& s) {
53 value_=s;
54 return *this;
55 }
56
57 operator std::string() const {
58 return value_;
59 }
60
61 operator double() const {
62 return atof(value_.c_str());
63 }
64
65
66
67 private:
68 std::string value_;
69 };
70
71
72
73 //---trim function
74 std::string trim(std::string const& source, char const* delims = " \t\r\n") {
75 std::string result(source);
76 std::string::size_type index = result.find_last_not_of(delims);
77 if(index != std::string::npos)
78 result.erase(++index);
79
80 index = result.find_first_not_of(delims);
81 if(index != std::string::npos)
82 result.erase(0, index);
83 else
84 result.erase();
85 return result;
86 }
87
88 //---trim function
89
90 //---class
91 class ConfigFile {
92 std::map<std::string,Chameleon> content_;
93
94 public:
95 ConfigFile(std::string const& configFile);
96
97 Chameleon const& Value(std::string const& section, std::string const& entry) const;
98 Chameleon const& Value(std::string const& section, std::string const& entry, double value);
99 Chameleon const& Value(std::string const& section, std::string const& entry, std::string const& value);
100 };
101
102
103 ConfigFile::ConfigFile(std::string const& configFile) {
104 std::ifstream file(configFile.c_str());
105
106 std::string line;
107 std::string name;
108 std::string value;
109 std::string inSection;
110 int posEqual;
111 while (std::getline(file,line)) {
112
113 if (! line.length()) continue;
114
115 if (line[0] == '#') continue;
116 if (line[0] == ';') continue;
117
118 if (line[0] == '[') {
119 inSection=trim(line.substr(1,line.find(']')-1));
120 continue;
121 }
122
123 posEqual=line.find('=');
124 name = trim(line.substr(0,posEqual));
125 value = trim(line.substr(posEqual+1));
126
127 content_[inSection+'/'+name]=Chameleon(value);
128 }
129 }
130
131 Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry) const {
132
133 std::map<std::string,Chameleon>::const_iterator ci = content_.find(section + '/' + entry);
134
135 if (ci == content_.end()) throw "does not exist";
136
137 return ci->second;
138 }
139
140 Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry, double value) {
141 try {
142 return Value(section, entry);
143 } catch(const char *) {
144 return content_.insert(std::make_pair(section+'/'+entry, Chameleon(value))).first->second;
145 }
146 }
147
148 Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry, std::string const& value) {
149 try {
150 return Value(section, entry);
151 } catch(const char *) {
152 return content_.insert(std::make_pair(section+'/'+entry, Chameleon(value))).first->second;
153 }
154 }
155
156
157 #endif