]> Dogcows Code - chaz/yoink/blob - src/Moof/Settings.cc
initial port to win32
[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 "Log.hh"
36 #include "Settings.hh"
37
38
39 namespace Mf {
40
41
42 Settings::Settings(int argc, char* argv[])
43 {
44 parseArgs(argc, argv);
45 }
46
47
48 Settings& Settings::getInstance()
49 {
50 static Settings settings;
51 return settings;
52 }
53
54
55 void Settings::parseArgs(int argc, char* argv[])
56 {
57 for (int i = 1; i < argc; ++i)
58 {
59 char* where = strchr(argv[i], (int)'=');
60
61 if (where)
62 {
63 std::string key(argv[i], (size_t)(where - argv[i]));
64 std::string stringValue(where + 1);
65
66 std::stringstream stream;
67 stream << stringValue << std::endl;
68
69 Deserializer deserializer(stream);
70
71 try
72 {
73 SerializableP value(deserializer.deserialize());
74 map_[key] = value;
75 }
76 catch (std::exception e)
77 {
78 // it doesn't deserialize to anything we know, so just store it
79 // as a string
80 map_[key] = SerializableP(new SerializableBasic<Serializable::String>(stringValue));
81 }
82 }
83 }
84 }
85
86
87 void Settings::loadFromFile(const std::string& filePath, bool precedence)
88 {
89 std::vector<std::string> paths;
90 boost::split(paths, filePath, boost::is_any_of(":"));
91
92 loadFromFiles(paths, precedence);
93 }
94
95 void Settings::loadFromFiles(const std::vector<std::string>& filePaths,
96 bool precedence)
97 {
98 std::vector<std::string>::const_iterator it;
99
100 char* home = getenv("HOME");
101
102 for (it = filePaths.begin(); it != filePaths.end(); ++it)
103 {
104 std::string path = *it;
105
106 if (home)
107 {
108 boost::replace_all(path, "$HOME", home);
109 }
110
111 try
112 {
113 Deserializer deserializer(*it, true);
114
115 SerializableP obj = deserializer.deserialize();
116 Serializable::Map map;
117
118 if (obj && obj->get(map))
119 {
120 if (!precedence)
121 {
122 map_.insert(map.begin(), map.end());
123 }
124 else
125 {
126 map.insert(map_.begin(), map_.end());
127 map_ = map;
128 }
129 }
130 }
131 catch (Deserializer::Exception e)
132 {
133 logWarning("cannot load settings from %s because an exception was"
134 "thrown: %s", (*it).c_str(), e.what());
135 }
136 }
137 }
138
139
140 } // namepsace Mf
141
142 /** vim: set ts=4 sw=4 tw=80: *************************************************/
143
This page took 0.034559 seconds and 4 git commands to generate.