* Load, store, save program settings.
*/
-#include <map>
#include <string>
+#include <vector>
-#include <Moof/Serializable.hh>
+#include <boost/algorithm/string.hpp>
+
+#include <Moof/Log.hh>
+#include <Moof/Script.hh>
namespace Mf {
class Settings
{
public:
- Settings() {}
- Settings(int argc, char* argv[]);
+
+ Settings()
+ {
+ mScript.importBaseLibrary();
+ importLogFunctions(mScript);
+ }
+
+ ~Settings();
// 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);
+
+ void clear(); // remove all settings
+
+ void saveAs(const std::string& filePath);
+ void save() const;
template <typename T>
- bool get(const std::string& key, T& value);
- template <typename T>
- bool getNumber(const std::string& key, T& value);
+ bool get(const std::string& key, T& value) const;
private:
- Serializable::Map map_;
+
+ mutable Script mScript;
+
+ std::string mUserFile;
};
template <typename T>
-bool Settings::get(const std::string& key, T& value)
+bool Settings::get(const std::string& key, T& value) const
{
- Serializable::Map::const_iterator it = map_.find(key);
+ Script::Slot top = mScript[-1];
+ Script::Slot globals = mScript.getGlobalTable();
- if (it != map_.end())
- {
- SerializableP 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)
-{
- Serializable::Map::const_iterator it = map_.find(key);
+ globals.pushCopy();
- if (it != map_.end())
+ std::vector<std::string>::iterator it;
+ for (it = fields.begin(); it != fields.end(); ++it)
{
- SerializableP obj = (*it).second;
- return obj->getNumber(value);
- }
- else
- {
- return false;
+ if (top.isTable())
+ {
+ top.pushField(*it);
+ }
+ else
+ {
+ mScript.clear();
+ return false;
+ }
}
+
+ bool got = top.get(value);
+ mScript.clear();
+ return got;
}