]> Dogcows Code - chaz/yoink/blob - src/settings.cc
beginnings of scene rendering
[chaz/yoink] / src / 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 dc {
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 in(stream);
62
63 try
64 {
65 serializable_ptr value(in.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] = serializable_ptr(new wrapped_string(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 deserializer in(*it, true);
104
105 std::cout << "Looking for a config file at " << path << std::endl;
106 try
107 {
108 serializable_ptr obj = in.deserialize();
109 std::map<std::string,serializable_ptr> dict;
110 if (obj && obj->get(dict))
111 {
112 if (!precedence)
113 {
114 map.insert(dict.begin(), dict.end());
115 }
116 else
117 {
118 dict.insert(map.begin(), map.end());
119 map = dict;
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 dc
133
134 /** vim: set ts=4 sw=4 tw=80: *************************************************/
135
This page took 0.041448 seconds and 4 git commands to generate.