]> Dogcows Code - chaz/yoink/blob - src/Moof/Settings.hh
reformatting
[chaz/yoink] / src / Moof / Settings.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_SETTINGS_HH_
13 #define _MOOF_SETTINGS_HH_
14
15 /**
16 * @file Settings.hh
17 * Load, store, save program settings.
18 */
19
20 #include <string>
21 #include <vector>
22
23 #include <boost/algorithm/string.hpp>
24
25 #include <Moof/Log.hh>
26 #include <Moof/Script.hh>
27
28
29 namespace Mf {
30
31
32 class Settings
33 {
34 public:
35
36 Settings()
37 {
38 mScript.importBaseLibrary();
39 importLogFunctions(mScript);
40 }
41
42 ~Settings();
43
44 void parseArgs(int argc, char* argv[]);
45
46 void loadFromFiles(const std::string& path);
47 void loadFromFiles(const std::vector<std::string>& path);
48
49 void clear(); // remove all settings
50
51 void saveAs(const std::string& path);
52 void save() const;
53
54 template <typename T>
55 bool get(const std::string& key, T& value) const;
56
57 private:
58
59 mutable Script mScript;
60
61 std::string mUserFile;
62 };
63
64
65 template <typename T>
66 bool Settings::get(const std::string& key, T& value) const
67 {
68 Script::Slot top = mScript[-1];
69 Script::Slot globals = mScript.getGlobalTable();
70
71 std::vector<std::string> fields;
72 boost::split(fields, key, boost::is_any_of("."));
73
74 globals.pushCopy();
75
76 std::vector<std::string>::iterator it;
77 for (it = fields.begin(); it != fields.end(); ++it)
78 {
79 if (top.isTable())
80 {
81 top.pushField(*it);
82 }
83 else
84 {
85 mScript.clear();
86 return false;
87 }
88 }
89
90 bool got = top.get(value);
91 mScript.clear();
92 return got;
93 }
94
95
96 extern Settings settings;
97
98
99 } // namepsace Mf
100
101 #endif // _MOOF_SETTINGS_HH_
102
This page took 0.035796 seconds and 4 git commands to generate.