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