Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70f5a42

Browse files
copy from adp-gmbh.ch
1 parent 7a8535d commit 70f5a42

File tree

7 files changed

+252
-0
lines changed

7 files changed

+252
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

‎Chameleon.cpp‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Chameleon.cpp
3+
4+
Copyright (C) 2002-2004 René Nyffenegger
5+
6+
This source code is provided 'as-is', without any express or implied
7+
warranty. In no event will the author be held liable for any damages
8+
arising from the use of this software.
9+
10+
Permission is granted to anyone to use this software for any purpose,
11+
including commercial applications, and to alter it and redistribute it
12+
freely, subject to the following restrictions:
13+
14+
1. The origin of this source code must not be misrepresented; you must not
15+
claim that you wrote the original source code. If you use this source code
16+
in a product, an acknowledgment in the product documentation would be
17+
appreciated but is not required.
18+
19+
2. Altered source versions must be plainly marked as such, and must not be
20+
misrepresented as being the original source code.
21+
22+
3. This notice may not be removed or altered from any source distribution.
23+
24+
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
25+
*/
26+
27+
#include <string>
28+
#include <sstream>
29+
30+
#include "Chameleon.h"
31+
32+
Chameleon::Chameleon(std::string const& value) {
33+
value_=value;
34+
}
35+
36+
#include <iostream>
37+
38+
Chameleon::Chameleon(const char* c) {
39+
value_=c;
40+
}
41+
42+
Chameleon::Chameleon(double d) {
43+
std::stringstream s;
44+
s<<d;
45+
value_=s.str();
46+
}
47+
48+
Chameleon::Chameleon(Chameleon const& other) {
49+
value_=other.value_;
50+
}
51+
52+
Chameleon& Chameleon::operator=(Chameleon const& other) {
53+
value_=other.value_;
54+
return *this;
55+
}
56+
57+
Chameleon& Chameleon::operator=(double i) {
58+
std::stringstream s;
59+
s << i;
60+
value_ = s.str();
61+
return *this;
62+
}
63+
64+
Chameleon& Chameleon::operator=(std::string const& s) {
65+
value_=s;
66+
return *this;
67+
}
68+
69+
Chameleon::operator std::string() const {
70+
return value_;
71+
}
72+
73+
Chameleon::operator double() const {
74+
return atof(value_.c_str());
75+
}

‎Chameleon.h‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Chameleon.h
3+
4+
Copyright (C) 2002-2004 René Nyffenegger
5+
6+
This source code is provided 'as-is', without any express or implied
7+
warranty. In no event will the author be held liable for any damages
8+
arising from the use of this software.
9+
10+
Permission is granted to anyone to use this software for any purpose,
11+
including commercial applications, and to alter it and redistribute it
12+
freely, subject to the following restrictions:
13+
14+
1. The origin of this source code must not be misrepresented; you must not
15+
claim that you wrote the original source code. If you use this source code
16+
in a product, an acknowledgment in the product documentation would be
17+
appreciated but is not required.
18+
19+
2. Altered source versions must be plainly marked as such, and must not be
20+
misrepresented as being the original source code.
21+
22+
3. This notice may not be removed or altered from any source distribution.
23+
24+
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
25+
*/
26+
27+
#ifndef CHAMELEON_H__
28+
#define CHAMELEON_H__
29+
30+
#include <string>
31+
32+
class Chameleon {
33+
public:
34+
Chameleon() {};
35+
explicit Chameleon(const std::string&);
36+
explicit Chameleon(double);
37+
explicit Chameleon(const char*);
38+
39+
Chameleon(const Chameleon&);
40+
Chameleon& operator=(Chameleon const&);
41+
42+
Chameleon& operator=(double);
43+
Chameleon& operator=(std::string const&);
44+
45+
public:
46+
operator std::string() const;
47+
operator double () const;
48+
private:
49+
std::string value_;
50+
};
51+
52+
#endif

‎ConfigFile.cpp‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "ConfigFile.h"
2+
3+
#include <fstream>
4+
5+
std::string trim(std::string const& source, char const* delims = " \t\r\n") {
6+
std::string result(source);
7+
std::string::size_type index = result.find_last_not_of(delims);
8+
if(index != std::string::npos)
9+
result.erase(++index);
10+
11+
index = result.find_first_not_of(delims);
12+
if(index != std::string::npos)
13+
result.erase(0, index);
14+
else
15+
result.erase();
16+
return result;
17+
}
18+
19+
ConfigFile::ConfigFile(std::string const& configFile) {
20+
std::ifstream file(configFile.c_str());
21+
22+
std::string line;
23+
std::string name;
24+
std::string value;
25+
std::string inSection;
26+
int posEqual;
27+
while (std::getline(file,line)) {
28+
29+
if (! line.length()) continue;
30+
31+
if (line[0] == '#') continue;
32+
if (line[0] == ';') continue;
33+
34+
if (line[0] == '[') {
35+
inSection=trim(line.substr(1,line.find(']')-1));
36+
continue;
37+
}
38+
39+
posEqual=line.find('=');
40+
name = trim(line.substr(0,posEqual));
41+
value = trim(line.substr(posEqual+1));
42+
43+
content_[inSection+'/'+name]=Chameleon(value);
44+
}
45+
}
46+
47+
Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry) const {
48+
49+
std::map<std::string,Chameleon>::const_iterator ci = content_.find(section + '/' + entry);
50+
51+
if (ci == content_.end()) throw "does not exist";
52+
53+
return ci->second;
54+
}
55+
56+
Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry, double value) {
57+
try {
58+
return Value(section, entry);
59+
} catch(const char *) {
60+
return content_.insert(std::make_pair(section+'/'+entry, Chameleon(value))).first->second;
61+
}
62+
}
63+
64+
Chameleon const& ConfigFile::Value(std::string const& section, std::string const& entry, std::string const& value) {
65+
try {
66+
return Value(section, entry);
67+
} catch(const char *) {
68+
return content_.insert(std::make_pair(section+'/'+entry, Chameleon(value))).first->second;
69+
}
70+
}

‎ConfigFile.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __CONFIG_FILE_H__
2+
#define __CONFIG_FILE_H__
3+
4+
#include <string>
5+
#include <map>
6+
7+
#include "Chameleon.h"
8+
9+
class ConfigFile {
10+
std::map<std::string,Chameleon> content_;
11+
12+
public:
13+
ConfigFile(std::string const& configFile);
14+
15+
Chameleon const& Value(std::string const& section, std::string const& entry) const;
16+
17+
Chameleon const& Value(std::string const& section, std::string const& entry, double value);
18+
Chameleon const& Value(std::string const& section, std::string const& entry, std::string const& value);
19+
};
20+
21+
#endif

‎test/config.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[section_1]
2+
foo = bar
3+
water= h2o
4+
5+
[section_2]
6+
foo = foo
7+
water= wet
8+
four = 4.2

‎test/read-config.txt.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// g++ ../ConfigFile.cpp ../Chameleon.cpp read-config.txt.cpp -o read-config.txt
3+
//
4+
5+
#include "../ConfigFile.h"
6+
7+
#include <iostream>
8+
9+
int main() {
10+
ConfigFile cf("config.txt");
11+
12+
std::string foo;
13+
std::string water;
14+
double four;
15+
16+
foo = cf.Value("section_1","foo" );
17+
water = cf.Value("section_2","water");
18+
four = cf.Value("section_2","four" );
19+
20+
std::cout << foo << std::endl;
21+
std::cout << water << std::endl;
22+
std::cout << four << std::endl;
23+
24+
return 0;
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /