]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Settings.hh
port to NetBSD
[chaz/yoink] / src / Moof / Settings.hh
index 9c29007613f90d6b35a7c8500208581dae05a5a5..42e7d3a7a5f26e159237a3b37682efb2a4a9f015 100644 (file)
  * Load, store, save program settings.
  */
 
-#include <map>
 #include <string>
+#include <vector>
 
-#include <Moof/Singleton.hh>
-#include <Moof/Serializable.hh>
+#include <boost/algorithm/string.hpp>
+
+#include <Moof/Log.hh>
+#include <Moof/Script.hh>
 
 
 namespace Mf {
 
 
-class Settings : public Singleton<Settings>
+class Settings
 {
 public:
-       Settings() {}
-       Settings(int argc, char* argv[]);
+
+       Settings() :
+               mGlobals(mScript.getGlobalTable()),
+               mTop(mScript[-1])
+       {
+               importLogScript(mScript);
+       }
+
+       // get global instance
+       static Settings& getInstance();
 
        void parseArgs(int argc, char* argv[]);
 
-       void loadFromFile(const std::string& filePath, bool precedence = false);
-       void loadFromFiles(const std::vector<std::string>& filePaths,
-                       bool precedence = false);
+       void loadFromFile(const std::string& filePath);
+       void loadFromFiles(const std::vector<std::string>& filePaths);
 
        template <typename T>
        bool get(const std::string& key, T& value);
-       template <typename T>
-       bool getNumber(const std::string& key, T& value);
 
 private:
-       std::map<std::string,SerializablePtr> map_;
+
+       Script                  mScript;
+       Script::Value   mGlobals, mTop;
 };
 
 
 template <typename T>
 bool Settings::get(const std::string& key, T& value)
 {
-       std::map<std::string,SerializablePtr>::const_iterator it = map_.find(key);
-
-       if (it != map_.end())
-       {
-               SerializablePtr obj = (*it).second;
-               return obj->get(value);
-       }
-       else
-       {
-               return false;
-       }
-}
+       std::vector<std::string> fields;
+       boost::split(fields, key, boost::is_any_of("."));
 
-template <typename T>
-bool Settings::getNumber(const std::string& key, T& value)
-{
-       std::map<std::string,SerializablePtr>::const_iterator it = map_.find(key);
+       mGlobals.pushCopy();
 
-       if (it != map_.end())
-       {
-               SerializablePtr obj = (*it).second;
-               return obj->getNumber(value);
-       }
-       else
+       std::vector<std::string>::iterator it;
+       for (it = fields.begin(); it != fields.end(); ++it)
        {
-               return false;
+               if (mTop.isTable())
+               {
+                       mTop.pushField(*it);
+               }
+               else
+               {
+                       mScript.clear();
+                       return false;
+               }
        }
+
+       bool got = mTop.get(value);
+       mScript.clear();
+       return got;
 }
 
 
This page took 0.020416 seconds and 4 git commands to generate.