X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fconfig.cpp;h=67f932fbafe9fa29fc06d0a0637027badb42e8a4;hb=1ec533a7777be4dce9c8b6bbb1cbc32d38098ae4;hp=99e5981503eb35ab9780711a092cea30a3701776;hpb=74266a6e650f019063cdcd1c9a7bd26d8f99041b;p=builder.git diff --git a/source/config.cpp b/source/config.cpp index 99e5981..67f932f 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -1,116 +1,112 @@ -#include -#include -#include +#include +#include +#include +#include +#include #include +#include "builder.h" #include "config.h" +#include "sourcepackage.h" using namespace std; using namespace Msp; -/** -Adds a configuration option. +Config::Config(SourcePackage &p): + package(p), + changed(false) +{ } -@param n Option name -@param v Default value -@param d Description -*/ -void Config::add_option(const string &n, const string &v, const string &d) +const Config::Option &Config::add_option(const Feature &f) { - options.insert(OptionMap::value_type(n, Option(n, v, d))); + Option opt(f); + auto i = pending_options.find(opt.name); + if(i!=pending_options.end()) + opt.value = i->second; + + return options.insert({ opt.name, opt }).first->second; } -/** -Gets the given option from the configuration. If the option doesn't exist, -an Exception is thrown. -*/ -const Config::Option &Config::get_option(const string &name) const +bool Config::set_option(const string &opt, const string &val) { - OptionMap::const_iterator i=options.find(name); - if(i==options.end()) - throw Exception("Tried to access nonexistent option "+name); + bool result = false; + + auto i = options.find(opt); + if(i!=options.end()) + { + if(i->second.value!=val) + { + result = true; + changed = true; + mtime = Time::now(); + } + i->second.value = val; + } - return i->second; + return result; } -/** -Checks whether an option with the given name exists. -*/ bool Config::is_option(const string &name) const { return options.count(name); } -/** -Processes options from the given raw option map. Nonexistent options are -ignored. If any options were changed, the mtime of the configuration is updated -to the current time. - -@param opts A map to process options from +const Config::Option &Config::get_option(const string &name) const +{ + return get_item(options, name); +} -@return Whether any option values were changed -*/ -bool Config::process(const RawOptionMap &opts) +void Config::load() { - bool changed=false; - for(RawOptionMap::const_iterator i=opts.begin(); i!=opts.end(); ++i) + FS::Path fn = package.get_source_directory()/".config"; + FS::Stat stat = FS::stat(fn); + if(stat) { - OptionMap::iterator j=options.find(i->first); - if(j!=options.end()) - { - if(i->second!=j->second.value) - changed=true; - j->second.value=i->second; - } - } + package.get_builder().get_logger().log("files", format("Reading %s", fn)); + IO::BufferedFile in(fn.str()); - if(changed) - mtime=Time::now(); + mtime = stat.get_modify_time(); - return changed; + DataFile::Parser parser(in, fn.str()); + Loader loader(*this); + loader.load(parser); + } } -/** -Loads configuration from a file, if it exists. -*/ -void Config::load(const Path::Path &fn) +void Config::save() const { - ifstream in(fn.str().c_str()); - if(!in) return; + if(!changed) + return; + + FS::Path fn = package.get_source_directory()/".config"; + + package.get_builder().get_logger().log("files", format("Writing %s", fn)); + IO::BufferedFile out(fn.str(), IO::M_WRITE); - struct stat st; - Path::stat(fn, st); - mtime=Time::TimeStamp::from_unixtime(st.st_mtime); + for(const auto &kvp: options) + IO::print(out, "option \"%s\" \"%s\";\n", kvp.second.name, kvp.second.value); - Parser::Parser parser(in, fn.str()); - Loader loader(*this); - loader.load(parser); + changed = false; } -void Config::save(const Path::Path &fn) const + +Config::Option::Option(const Feature &f): + Feature(f), + value(default_value) { - ofstream out(fn.str().c_str()); - if(!out) return; - - for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i) - out<<"option \""<second.name<<"\" \""<second.value<<"\";\n"; + name = "with_"+name; } -Config::Option::Option(const string &n, const string &v, const string &d): - name(n), - defv(v), - descr(d), - value(v) -{ } Config::Loader::Loader(Config &c): - conf(c) + DataFile::ObjectLoader(c) { add("option", &Loader::option); } void Config::Loader::option(const string &n, const string &v) { - OptionMap::iterator i=conf.options.find(n); - if(i!=conf.options.end()) - i->second.value=v; + if(obj.options.count(n)) + obj.set_option(n, v); + else + obj.pending_options[n] = v; }