]> Dogcows Code - chaz/yoink/blobdiff - src/settings.cc
big batch of progress
[chaz/yoink] / src / settings.cc
index 07ec0e50c1cebbc0d10f40e55f5e22e31b73376c..74ab384dd0bb1cd6e2a9a334bfc9ca5cb6bf1efd 100644 (file)
 *******************************************************************************/
 
 #include <sstream>
-#include <cstring>
+#include <cstdlib>             // getenv
+#include <cstring>             // strchr
+
+#include <boost/algorithm/string.hpp>
 
 #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<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: *************************************************/
+
This page took 0.019698 seconds and 4 git commands to generate.