]> Dogcows Code - chaz/yoink/blobdiff - src/moof/settings.hh
the massive refactoring effort
[chaz/yoink] / src / moof / settings.hh
diff --git a/src/moof/settings.hh b/src/moof/settings.hh
new file mode 100644 (file)
index 0000000..4807eeb
--- /dev/null
@@ -0,0 +1,106 @@
+
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#ifndef _MOOF_SETTINGS_HH_
+#define _MOOF_SETTINGS_HH_
+
+/**
+ * \file settings.hh
+ * Load, store, save program settings.
+ */
+
+#include <string>
+#include <vector>
+
+#include <boost/algorithm/string.hpp>
+
+#include <moof/script.hh>
+
+
+namespace moof {
+
+
+class settings
+{
+public:
+
+       settings(int argc, char* argv[], const std::string& path);
+
+       ~settings();
+
+       void parse_args(int argc, char* argv[]);
+
+       void load_files(const std::string& path);
+       void load_files(const std::vector<std::string>& path);
+
+
+       /**
+        * Remove all settings.
+        */
+       void clear();
+
+       void save_as(const std::string& path);
+       void save() const;
+
+
+       /**
+        * Get a setting by name.
+        * \param key The name of the setting.
+        * \param value A reference to the variable to store the setting.
+        * \return True if the setting exists, false otherwise.
+        */
+       template <class T>
+       bool get(const std::string& key, T& value) const;
+
+
+private:
+
+       mutable script  script_;
+
+       std::string             mUserFile;
+};
+
+
+template <class T>
+bool settings::get(const std::string& key, T& value) const
+{
+       script::slot top = script_[-1];
+       script::slot globals = script_.globals();
+
+       std::vector<std::string> fields;
+       boost::split(fields, key, boost::is_any_of("."));
+
+       globals.push_copy();
+
+       std::vector<std::string>::iterator it;
+       for (it = fields.begin(); it != fields.end(); ++it)
+       {
+               if (top.is_table())
+               {
+                       top.push_field(*it);
+               }
+               else
+               {
+                       script_.clear_stack();
+                       return false;
+               }
+       }
+
+       bool got = top.get(value);
+       script_.clear_stack();
+       return got;
+}
+
+
+} // namepsace moof
+
+#endif // _MOOF_SETTINGS_HH_
+
This page took 0.02243 seconds and 4 git commands to generate.