X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fmoof%2Fsettings.hh;fp=src%2Fmoof%2Fsettings.hh;h=4807eeb16d8745ddba02c0b2762172586cc7230f;hb=831f04d4bc19a390415ac0bbac4331c7a65509bc;hp=0000000000000000000000000000000000000000;hpb=299af4f2047e767e5d79501c26444473bda64c64;p=chaz%2Fyoink diff --git a/src/moof/settings.hh b/src/moof/settings.hh new file mode 100644 index 0000000..4807eeb --- /dev/null +++ b/src/moof/settings.hh @@ -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 +#include + +#include + +#include + + +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& 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 + bool get(const std::string& key, T& value) const; + + +private: + + mutable script script_; + + std::string mUserFile; +}; + + +template +bool settings::get(const std::string& key, T& value) const +{ + script::slot top = script_[-1]; + script::slot globals = script_.globals(); + + std::vector fields; + boost::split(fields, key, boost::is_any_of(".")); + + globals.push_copy(); + + std::vector::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_ +