X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fsettings.cc;h=74ab384dd0bb1cd6e2a9a334bfc9ca5cb6bf1efd;hp=07ec0e50c1cebbc0d10f40e55f5e22e31b73376c;hb=7d15b919681bb9ec0088b4b27c6abf62d6dfb9b1;hpb=0fffd0097d7b496454413e57b398c903ecc252e4 diff --git a/src/settings.cc b/src/settings.cc index 07ec0e5..74ab384 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -27,7 +27,10 @@ *******************************************************************************/ #include -#include +#include // getenv +#include // strchr + +#include #include "settings.hh" @@ -64,37 +67,69 @@ void settings::parseArgs(int argc, char* argv[]) } catch (std::exception e) { + // it doesn't deserialize to anything we know, so just store it + // as a string map[key] = serializable_ptr(new wrapped_string(stringValue)); } } } } -void settings::loadFromFile(std::string filePath) + +void settings::loadFromFile(const std::string& filePath, bool precedence) { - deserializer in(filePath, true); + std::vector paths; + boost::split(paths, filePath, boost::is_any_of(":")); - try + loadFromFiles(paths, precedence); +} + +void settings::loadFromFiles(const std::vector& filePaths, + bool precedence) +{ + std::vector::const_iterator it; + + char* home = getenv("HOME"); + + for (it = filePaths.begin(); it != filePaths.end(); it++) { - serializable_ptr obj = in.deserialize(); - std::map dict; - if (obj && obj->get(dict)) + std::string path = *it; + + if (home) { - map.insert(dict.begin(), dict.end()); + boost::replace_first(path, "$HOME", home); } - else + + deserializer in(*it, true); + + std::cout << "Looking for a config file at " << path << std::endl; + try { - std::cerr << "The settings file " << filePath << - " does not contain any valid settings." << std::endl; + serializable_ptr obj = in.deserialize(); + std::map dict; + if (obj && obj->get(dict)) + { + if (!precedence) + { + map.insert(dict.begin(), dict.end()); + } + else + { + dict.insert(map.begin(), map.end()); + map = dict; + } + } + } + catch (deserializer::exception e) + { + std::cerr << "Cannot load settings from " << *it << + " because an exception was thrown: " << e.what() << std::endl; } - } - catch (deserializer::exception e) - { - std::cerr << "Cannot load settings from " << filePath << - " because an exception was thrown: " << e.what() << std::endl; } } } // namepsace dc +/** vim: set ts=4 sw=4 tw=80: *************************************************/ +