Debian Codegraph @

olad/plugin_api/Preferences.cpp

raw
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * Preferences.cpp
17 * This class stores preferences in files
18 * Copyright (C) 2005 Simon Newton
19 */
20
21 #define __STDC_LIMIT_MACROS // for UINT8_MAX & friends
22
23 #if HAVE_CONFIG_H
24 #include <config.h>
25 #endif // HAVE_CONFIG_H
26
27 #include "olad/Preferences.h"
28
29 #include <dirent.h>
30 #include <errno.h>
31 #ifdef _WIN32
32 // On MinGW, pthread.h pulls in Windows.h, which in turn pollutes the global
33 // namespace. We define VC_EXTRALEAN and WIN32_LEAN_AND_MEAN to reduce this.
34 #define VC_EXTRALEAN
35 #define WIN32_LEAN_AND_MEAN
36 #endif // _WIN32
37 #include <pthread.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43
44 #include <fstream>
45 #include <list>
46 #include <map>
47 #include <memory>
48 #include <string>
49 #include <utility>
50 #include <vector>
51
52 #include "ola/Logging.h"
53 #include "ola/StringUtils.h"
54 #include "ola/file/Util.h"
55 #include "ola/network/IPV4Address.h"
56 #include "ola/stl/STLUtils.h"
57 #include "ola/thread/Thread.h"
58
59 namespace ola {
60
61 using ola::thread::Mutex;
62 using ola::thread::ConditionVariable;
63 using std::ifstream;
64 using std::ofstream;
65 using std::map;
66 using std::pair;
67 using std::string;
68 using std::vector;
69
70 namespace {
71 void SavePreferencesToFile(
72 const string *filename_ptr,
73 const FilePreferenceSaverThread::PreferencesMap *pref_map_ptr) {
74 std::auto_ptr<const string> filename(filename_ptr);
75 std::auto_ptr<const FilePreferenceSaverThread::PreferencesMap> pref_map(
76 pref_map_ptr);
77
78 FilePreferenceSaverThread::PreferencesMap::const_iterator iter;
79 ofstream pref_file(filename->data());
80
81 if (!pref_file.is_open()) {
82 OLA_WARN << "Could not open " << *filename_ptr << ": " << strerror(errno);
83 return;
84 }
85
86 for (iter = pref_map->begin(); iter != pref_map->end(); ++iter) {
87 pref_file << iter->first << " = " << iter->second << std::endl;
88 }
89 pref_file.flush();
90 pref_file.close();
91 }
92 } // namespace
93
94 const char BoolValidator::ENABLED[] = "true";
95 const char BoolValidator::DISABLED[] = "false";
96
97 const char FileBackedPreferences::OLA_CONFIG_PREFIX[] = "ola-";
98 const char FileBackedPreferences::OLA_CONFIG_SUFFIX[] = ".conf";
99
100 // Validators
101 //-----------------------------------------------------------------------------
102
103 bool StringValidator::IsValid(const string &value) const {
104 return m_empty_ok || !value.empty();
105 }
106
107
108 bool BoolValidator::IsValid(const string &value) const {
109 return (value == ENABLED || value == DISABLED);
110 }
111
112
113 bool UIntValidator::IsValid(const string &value) const {
114 unsigned int output;
115 if (!StringToInt(value, &output)) {
116 return false;
117 }
118
119 return (output >= m_gt && output <= m_lt);
120 }
121
122
123 bool IntValidator::IsValid(const string &value) const {
124 int output;
125 if (!StringToInt(value, &output)) {
126 return false;
127 }
128
129 return (output >= m_gt && output <= m_lt);
130 }
131
132
133 template <>
134 bool SetValidator<string>::IsValid(const string &value) const {
135 return STLContains(m_values, value);
136 }
137
138
139 template <>
140 bool SetValidator<unsigned int>::IsValid(const string &value) const {
141 unsigned int output;
142 // It's an integer based set validator, so if we can't parse it to an
143 // integer, it can't possibly match an integer and be valid
144 if (!StringToInt(value, &output)) {
145 return false;
146 }
147
148 return STLContains(m_values, output);
149 }
150
151
152 template <>
153 bool SetValidator<int>::IsValid(const string &value) const {
154 int output;
155 // It's an integer based set validator, so if we can't parse it to an
156 // integer, it can't possibly match an integer and be valid
157 if (!StringToInt(value, &output)) {
158 return false;
159 }
160
161 return STLContains(m_values, output);
162 }
163
164
165 bool IPv4Validator::IsValid(const string &value) const {
166 if (value.empty()) {
167 return m_empty_ok;
168 }
169
170 vector<string> tokens;
171 StringSplit(value, &tokens, ".");
172 if (tokens.size() != ola::network::IPV4Address::LENGTH) {
173 return false;
174 }
175
176 for (unsigned int i = 0 ; i < 4; i++) {
177 unsigned int octet;
178 if (!StringToInt(tokens[i], &octet)) {
179 return false;
180 }
181 if (octet > UINT8_MAX) {
182 return false;
183 }
184 }
185 return true;
186 }
187
188
189 // Prefs Factory
190 //-----------------------------------------------------------------------------
191
192 PreferencesFactory::~PreferencesFactory() {
193 map<string, Preferences*>::const_iterator iter;
194 for (iter = m_preferences_map.begin(); iter != m_preferences_map.end();
195 ++iter) {
196 delete iter->second;
197 }
198 m_preferences_map.clear();
199 }
200
201
202 Preferences *PreferencesFactory::NewPreference(const string &name) {
203 map<string, Preferences*>::iterator iter = m_preferences_map.find(name);
204 if (iter == m_preferences_map.end()) {
205 Preferences *pref = Create(name);
206 m_preferences_map.insert(make_pair(name, pref));
207 return pref;
208 } else {
209 return iter->second;
210 }
211 }
212
213
214 // Memory Preferences
215 //-----------------------------------------------------------------------------
216
217 MemoryPreferences::~MemoryPreferences() {
218 m_pref_map.clear();
219 }
220
221
222 void MemoryPreferences::Clear() {
223 m_pref_map.clear();
224 }
225
226
227 void MemoryPreferences::SetValue(const string &key,
228 const string &value) {
229 m_pref_map.erase(key);
230 m_pref_map.insert(make_pair(key, value));
231 }
232
233
234 void MemoryPreferences::SetValue(const string &key, unsigned int value) {
235 SetValue(key, IntToString(value));
236 }
237
238
239 void MemoryPreferences::SetValue(const string &key, int value) {
240 SetValue(key, IntToString(value));
241 }
242
243
244 void MemoryPreferences::SetMultipleValue(const string &key,
245 const string &value) {
246 m_pref_map.insert(make_pair(key, value));
247 }
248
249
250 void MemoryPreferences::SetMultipleValue(const string &key,
251 unsigned int value) {
252 SetMultipleValue(key, IntToString(value));
253 }
254
255
256 void MemoryPreferences::SetMultipleValue(const string &key, int value) {
257 SetMultipleValue(key, IntToString(value));
258 }
259
260
261 bool MemoryPreferences::SetDefaultValue(const string &key,
262 const Validator &validator,
263 const string &value) {
264 PreferencesMap::const_iterator iter;
265 iter = m_pref_map.find(key);
266
267 if (iter == m_pref_map.end() || !validator.IsValid(iter->second)) {
268 SetValue(key, value);
269 return true;
270 }
271 return false;
272 }
273
274
275 bool MemoryPreferences::SetDefaultValue(const string &key,
276 const Validator &validator,
277 const char value[]) {
278 return SetDefaultValue(key, validator, string(value));
279 }
280
281 bool MemoryPreferences::SetDefaultValue(const string &key,
282 const Validator &validator,
283 unsigned int value) {
284 return SetDefaultValue(key, validator, IntToString(value));
285 }
286
287
288 bool MemoryPreferences::SetDefaultValue(const string &key,
289 const Validator &validator,
290 int value) {
291 return SetDefaultValue(key, validator, IntToString(value));
292 }
293
294
295 bool MemoryPreferences::SetDefaultValue(const string &key,
296 const Validator &validator,
297 const bool value) {
298 return SetDefaultValue(
299 key,
300 validator,
301 value ? BoolValidator::ENABLED : BoolValidator::DISABLED);
302 }
303
304
305 string MemoryPreferences::GetValue(const string &key) const {
306 PreferencesMap::const_iterator iter;
307 iter = m_pref_map.find(key);
308
309 if (iter != m_pref_map.end())
310 return iter->second;
311 return "";
312 }
313
314
315 vector<string> MemoryPreferences::GetMultipleValue(const string &key) const {
316 vector<string> values;
317 PreferencesMap::const_iterator iter;
318
319 for (iter = m_pref_map.find(key);
320 iter != m_pref_map.end() && iter->first == key; ++iter) {
321 values.push_back(iter->second);
322 }
323 return values;
324 }
325
326
327 bool MemoryPreferences::HasKey(const string &key) const {
328 return STLContains(m_pref_map, key);
329 }
330
331
332 void MemoryPreferences::RemoveValue(const string &key) {
333 m_pref_map.erase(key);
334 }
335
336
337 bool MemoryPreferences::GetValueAsBool(const string &key) const {
338 PreferencesMap::const_iterator iter;
339 iter = m_pref_map.find(key);
340
341 if (iter != m_pref_map.end())
342 return iter->second == BoolValidator::ENABLED;
343 return false;
344 }
345
346
347 void MemoryPreferences::SetValueAsBool(const string &key, bool value) {
348 m_pref_map.erase(key);
349 m_pref_map.insert(make_pair(key, (value ? BoolValidator::ENABLED :
350 BoolValidator::DISABLED)));
351 }
352
353
354
355
356 // FilePreferenceSaverThread
357 //-----------------------------------------------------------------------------
358
359 FilePreferenceSaverThread::FilePreferenceSaverThread()
360 : Thread(Thread::Options("pref-saver")) {
361 // set a long poll interval so we don't spin
362 m_ss.SetDefaultInterval(TimeInterval(60, 0));
363 }
364
365 void FilePreferenceSaverThread::SavePreferences(
366 const string &file_name,
367 const PreferencesMap &preferences) {
368 const string *file_name_ptr = new string(file_name);
369 const PreferencesMap *save_map = new PreferencesMap(preferences);
370 SingleUseCallback0<void> *cb =
371 NewSingleCallback(SavePreferencesToFile, file_name_ptr, save_map);
372 m_ss.Execute(cb);
373 }
374
375
376 void *FilePreferenceSaverThread::Run() {
377 m_ss.Run();
378 return NULL;
379 }
380
381
382 bool FilePreferenceSaverThread::Join(void *ptr) {
383 m_ss.Terminate();
384 return Thread::Join(ptr);
385 }
386
387
388 void FilePreferenceSaverThread::Syncronize() {
389 Mutex syncronize_mutex;
390 ConditionVariable condition_var;
391 syncronize_mutex.Lock();
392 m_ss.Execute(NewSingleCallback(
393 this,
394 &FilePreferenceSaverThread::CompleteSyncronization,
395 &condition_var,
396 &syncronize_mutex));
397 condition_var.Wait(&syncronize_mutex);
398 }
399
400
401 void FilePreferenceSaverThread::CompleteSyncronization(
402 ConditionVariable *condition,
403 Mutex *mutex) {
404 // calling lock here forces us to block until Wait() is called on the
405 // condition_var.
406 mutex->Lock();
407 mutex->Unlock();
408 condition->Signal();
409 }
410
411
412 // FileBackedPreferences
413 //-----------------------------------------------------------------------------
414
415 bool FileBackedPreferences::Load() {
416 return LoadFromFile(FileName());
417 }
418
419
420 bool FileBackedPreferences::Save() const {
421 m_saver_thread->SavePreferences(FileName(), m_pref_map);
422 return true;
423 }
424
425
426 const string FileBackedPreferences::FileName() const {
427 return (m_directory + ola::file::PATH_SEPARATOR + OLA_CONFIG_PREFIX +
428 m_preference_name + OLA_CONFIG_SUFFIX);
429 }
430
431
432 bool FileBackedPreferences::LoadFromFile(const string &filename) {
433 ifstream pref_file(filename.data());
434
435 if (!pref_file.is_open()) {
436 OLA_INFO << "Missing " << filename << ": " << strerror(errno) <<
437 " - this isn't an error, we'll just use the defaults";
438 return false;
439 }
440
441 m_pref_map.clear();
442 string line;
443 while (getline(pref_file, line)) {
444 StringTrim(&line);
445
446 if (line.empty() || line.at(0) == '#') {
447 continue;
448 }
449
450 vector<string> tokens;
451 StringSplit(line, &tokens, "=");
452
453 if (tokens.size() != 2) {
454 OLA_INFO << "Skipping line: " << line;
455 continue;
456 }
457
458 string key = tokens[0];
459 string value = tokens[1];
460 StringTrim(&key);
461 StringTrim(&value);
462 m_pref_map.insert(make_pair(key, value));
463 }
464 pref_file.close();
465 return true;
466 }
467 } // namespace ola