]> Dogcows Code - chaz/yoink/blob - src/Moof/Settings.cc
no more useless singleton class
[chaz/yoink] / src / Moof / Settings.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <sstream>
30 #include <cstdlib> // getenv
31 #include <cstring> // strchr
32
33 #include <boost/algorithm/string.hpp>
34
35 #include "Settings.hh"
36
37
38 namespace Mf {
39
40
41 Settings::Settings(int argc, char* argv[])
42 {
43 parseArgs(argc, argv);
44 }
45
46
47 Settings& Settings::getInstance()
48 {
49 static Settings settings;
50 return settings;
51 }
52
53
54 void Settings::parseArgs(int argc, char* argv[])
55 {
56 for (int i = 1; i < argc; ++i)
57 {
58 char* where = strchr(argv[i], (int)'=');
59
60 if (where)
61 {
62 std::string key(argv[i], (size_t)(where - argv[i]));
63 std::string stringValue(where + 1);
64
65 std::stringstream stream;
66 stream << stringValue << std::endl;
67
68 Deserializer deserializer(stream);
69
70 try
71 {
72 SerializablePtr value(deserializer.deserialize());
73 map_[key] = value;
74 }
75 catch (std::exception e)
76 {
77 // it doesn't deserialize to anything we know, so just store it
78 // as a string
79 map_[key] = SerializablePtr(new SerializableString(stringValue));
80 }
81 }
82 }
83 }
84
85
86 void Settings::loadFromFile(const std::string& filePath, bool precedence)
87 {
88 std::vector<std::string> paths;
89 boost::split(paths, filePath, boost::is_any_of(":"));
90
91 loadFromFiles(paths, precedence);
92 }
93
94 void Settings::loadFromFiles(const std::vector<std::string>& filePaths,
95 bool precedence)
96 {
97 std::vector<std::string>::const_iterator it;
98
99 char* home = getenv("HOME");
100
101 for (it = filePaths.begin(); it != filePaths.end(); ++it)
102 {
103 std::string path = *it;
104
105 if (home)
106 {
107 boost::replace_first(path, "$HOME", home);
108 }
109
110 try
111 {
112 Deserializer deserializer(*it, true);
113
114 SerializablePtr obj = deserializer.deserialize();
115 std::map<std::string,SerializablePtr> map;
116
117 if (obj && obj->get(map))
118 {
119 if (!precedence)
120 {
121 map_.insert(map.begin(), map.end());
122 }
123 else
124 {
125 map.insert(map_.begin(), map_.end());
126 map_ = map;
127 }
128 }
129 }
130 catch (Deserializer::Exception e)
131 {
132 std::cerr << "Cannot load settings from " << *it <<
133 " because an exception was thrown: " << e.what() << std::endl;
134 }
135 }
136 }
137
138
139 } // namepsace Mf
140
141 /** vim: set ts=4 sw=4 tw=80: *************************************************/
142
This page took 0.037227 seconds and 4 git commands to generate.