forked from BlackIkeEagle/muttvcardsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
164 lines (138 loc) · 5.64 KB
/
settings.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/***************************************************************************
* Copyright (C) 2013 by Torsten Flammiger *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "settings.h"
#include "stringutils.h"
// CTOR creates settings hash
Settings::Settings() {
valid = false;
changed = false;
std::string line;
std::string filename (getConfigFile());
std::ifstream f (filename.c_str());
if(f.is_open()) {
int numSettings = 0; // we have 3
std::string section;
bool enterSection = false; // only valid for the first section
while(getline(f, line)) {
if(StringUtils::startsWith(line, "[") && StringUtils::endsWith(line, "]")) {
std::string s1 = line.substr(1);
std::string s2 = s1.substr(0, s1.length()-1);
section = s2;
std::map< std::string, std::string > _line;
cfg[section] = _line;
enterSection = true; // at least one section was found
} else {
if(enterSection == false) continue;
if(line.size() > 0) {
std::vector<std::string> tokens = StringUtils::split(line, "=");
if(tokens.size() >= 2) {
std::string v;
if(tokens.size() > 2) {
// combine 1 to end
for(int a = 1; a < tokens.size(); ++a) {
if(a > 1) {
v += "=";
}
v += tokens.at(a);
}
} else {
v = tokens.at(1);
}
std::map< std::string, std::string > _map = cfg[section];
_map.insert(std::pair<std::string, std::string>(tokens.at(0), v));
cfg[section] = _map;
numSettings++;
}
}
}
}
f.close();
if( numSettings > 0 && (numSettings % 3) == 0)
valid = true;
}
}
Settings::~Settings() {
if(changed) {
std::ofstream o;
o.open(getConfigFile().c_str(), ios::out | ios::trunc);
if(o.is_open()) {
CfgMap::iterator end = cfg.end();
for (CfgMap::const_iterator it = cfg.begin(); it != end; ++it) {
std::string section(it->first);
if(section.size() != 0) {
o << "[" << section << "]" << endl;
std::map <std::string, std::string> m = it->second;
for(std::map <std::string, std::string>::iterator _it = m.begin(); _it != m.end(); _it++) {
o << _it->first << "=" << _it->second << endl;
}
o << endl;
}
}
o.close();
} else {
cerr << "Can't update/write config file '" << getConfigFile() << "'" << endl;
}
}
cfg.clear();
}
bool Settings::isValid() {
return valid;
}
std::vector<std::string> Settings::getSections() {
std::vector<std::string> result;
CfgMap::iterator end = cfg.end();
for (CfgMap::const_iterator it = cfg.begin(); it != end; ++it) {
result.push_back(it->first);
}
return result;
}
void Settings::setSection(const string §ion) {
std::map <std::string, std::string> m;
cfg[section] = m;
}
void Settings::setProperty(const std::string §ion, std::string key, std::string &value) {
std::map <std::string, std::string> m = cfg[section];
m[key] = value;
cfg[section] = m;
changed = true;
}
std::string Settings::getProperty(const std::string& section, const string &key) {
std::string res;
std::map <std::string, std::string> m(cfg[section]);
std::map <std::string, std::string>::const_iterator it = m.begin();
it = m.find(key);
if(it != m.end())
res = it->second;
return res;
}
const std::string Settings::getConfigDir() {
std::string s = CONFIG_DIR;
return s;
}
const std::string Settings::getConfigFile() {
std::string s = FileUtils::getHomeDir();
s.append("/").append(CONFIG_DIR).append("/").append(CONFIG_FILE);
return s;
}
const std::string Settings::getCacheFile() {
std::string s = FileUtils::getHomeDir();
s.append("/").append(CONFIG_DIR).append("/cache.sqlite3");
return s;
}