*******************************************************************************/
#include <sstream>
-#include <cstring>
+#include <cstdlib> // getenv
+#include <cstring> // strchr
+
+#include <boost/algorithm/string.hpp>
#include "settings.hh"
}
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<std::string> paths;
+ boost::split(paths, filePath, boost::is_any_of(":"));
- try
+ loadFromFiles(paths, precedence);
+}
+
+void settings::loadFromFiles(const std::vector<std::string>& filePaths,
+ bool precedence)
+{
+ std::vector<std::string>::const_iterator it;
+
+ char* home = getenv("HOME");
+
+ for (it = filePaths.begin(); it != filePaths.end(); it++)
{
- serializable_ptr obj = in.deserialize();
- std::map<std::string,serializable_ptr> 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<std::string,serializable_ptr> 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: *************************************************/
+