]>
Dogcows Code - chaz/yoink/blob - src/Moof/Settings.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
30 #include <cstdlib> // getenv
31 #include <cstring> // strchr
33 #include <boost/algorithm/string.hpp>
36 #include "Settings.hh"
42 Settings::Settings(int argc
, char* argv
[])
44 parseArgs(argc
, argv
);
48 Settings
& Settings::getInstance()
50 static Settings settings
;
55 void Settings::parseArgs(int argc
, char* argv
[])
57 for (int i
= 1; i
< argc
; ++i
)
59 char* where
= strchr(argv
[i
], (int)'=');
63 std::string
key(argv
[i
], (size_t)(where
- argv
[i
]));
64 std::string
stringValue(where
+ 1);
66 std::stringstream stream
;
67 stream
<< stringValue
<< std::endl
;
69 Deserializer
deserializer(stream
);
73 SerializableP
value(deserializer
.deserialize());
76 catch (std::exception e
)
78 // it doesn't deserialize to anything we know, so just store it
80 map_
[key
] = SerializableP(new SerializableBasic
<Serializable::String
>(stringValue
));
87 void Settings::loadFromFile(const std::string
& filePath
, bool precedence
)
89 std::vector
<std::string
> paths
;
90 boost::split(paths
, filePath
, boost::is_any_of(":"));
92 loadFromFiles(paths
, precedence
);
95 void Settings::loadFromFiles(const std::vector
<std::string
>& filePaths
,
98 std::vector
<std::string
>::const_iterator it
;
100 char* home
= getenv("HOME");
102 for (it
= filePaths
.begin(); it
!= filePaths
.end(); ++it
)
104 std::string path
= *it
;
108 boost::replace_all(path
, "$HOME", home
);
113 Deserializer
deserializer(*it
, true);
115 SerializableP obj
= deserializer
.deserialize();
116 Serializable::Map map
;
118 if (obj
&& obj
->get(map
))
122 map_
.insert(map
.begin(), map
.end());
126 map
.insert(map_
.begin(), map_
.end());
131 catch (Deserializer::Exception e
)
133 logWarning("cannot load settings from %s because an exception was"
134 "thrown: %s", (*it
).c_str(), e
.what());
142 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.042959 seconds and 4 git commands to generate.