blob: 127f2b78654c192897bd9aab0fa534adcd018206 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*
* Config.cpp
*
* Copyright (C) ST-Ericsson SA 2011
* Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
* Ola Borgelin <ola.borgelin@stericsson.com>,
* Karin Hedlund <karin.hedlund@stericsson.com>,
* Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
* License terms: 3-clause BSD license
*
*/
/*
* @addtogroup Config
* @{
*/
#include "Config.h"
#include "Utilities.h"
#include <fstream>
#include <algorithm>
#include <cctype>
using namespace std;
Config::Config(): logger_("Config")
{
}
Config::Config(const string& path): logger_("Config")
{
load(path);
}
bool Config::load(const string& path)
{
ifstream file;
file.open(path.c_str());
if (file.is_open()) {
string line;
string command;
string key;
string value;
while (getline(file, line)) {
Utilities::trim(line);
if (line.length() == 0 || line[0] != '!')
continue;
line.erase(0, 1);
command = getToken(line);
if (command == "set") {
key = getToken(line);
values_[key] = line;
}
}
return true;
} else {
logger_.log(Logger::ERR, "Failed to open configuration file \"%s\"", path.c_str());
return false;
}
}
const char* Config::getValue(const string& key) const
{
map<string, string>::const_iterator i = values_.find(key);
if (i != values_.end()) {
return i->second.c_str();
} else {
return "";
}
}
std::string Config::getToken(std::string& line)
{
size_t pos = line.find_first_of(" ");
string token = line.substr(0, pos);
line.erase(0, pos + 1);
Utilities::trim(token);
return token;
}
bool Config::valueExists(const string& key) const
{
string value = getValue(key);
return value.size() != 0;
}
/* @} */
|