-
Notifications
You must be signed in to change notification settings - Fork 11
/
configuration.cpp
104 lines (85 loc) · 3.17 KB
/
configuration.cpp
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
97
98
99
100
101
102
103
104
/* Copyright © 2001-2014, Hove and/or its affiliates. All rights reserved.
This file is part of Navitia,
the software to build cool stuff with public transport.
Hope you'll enjoy and contribute to this project,
powered by Hove (www.hove.com).
Help us simplify mobility and open public transport:
a non ending quest to the responsive locomotion way of traveling!
LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Stay tuned using
twitter @navitia
IRC #navitia on freenode
https://groups.google.com/d/forum/navitia
www.navitia.io
*/
#include "configuration.h"
#include <boost/foreach.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#ifdef WIN32
#include "windows.h"
HINSTANCE hinstance = NULL;
#endif
Configuration* Configuration::get() {
if (instance == nullptr) {
instance = new Configuration();
#ifdef WIN32
char buf[2048];
DWORD filePath = GetModuleFileName(::hinstance, buf, 2048);
std::string::size_type posSlash = std::string(buf).find_last_of("\\/");
std::string::size_type posDot = std::string(buf).find_last_of(".");
instance->strings["application"] = std::string(buf).substr(posSlash + 1, posDot - (posSlash + 1));
instance->strings["path"] = std::string(buf).substr(0, posSlash) + "\\";
#endif
}
return instance;
}
bool Configuration::is_instanciated() {
return instance != nullptr;
}
void Configuration::load_ini(const std::string& filename) {
boost::property_tree::ptree pt;
boost::property_tree::read_ini(filename, pt);
BOOST_FOREACH (auto section, pt) {
BOOST_FOREACH (auto key_val, pt.get_child(section.first)) {
std::string key = key_val.first;
std::string value = key_val.second.data();
instance->ini[section.first][key] = value;
}
}
}
bool Configuration::has_section(const std::string& section_name) {
return instance->ini.find(section_name) != instance->ini.end();
}
std::string Configuration::get_string(const std::string& key) {
mutex.lock_shared();
std::string ret = strings.at(key);
mutex.unlock_shared();
return ret;
}
int Configuration::get_int(const std::string& key) {
mutex.lock_shared();
int ret = ints[key];
mutex.unlock_shared();
return ret;
}
void Configuration::set_string(const std::string& key, const std::string& value) {
mutex.lock();
strings[key] = value;
mutex.unlock();
}
void Configuration::set_int(const std::string& key, int value) {
mutex.lock();
ints[key] = value;
mutex.unlock();
}
Configuration* Configuration::instance = nullptr;