X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fconfig.cpp;h=5b43fe3e68e01aef7521f56610e2b98b134ce289;hb=398983749391592988daca0c9b62b3d38f86e4e6;hp=61f7322e742ead72279f2d89a33f1012a4c8f497;hpb=cbb3c4c6aab7b04f7bd2178fb8f12846d532a472;p=builder.git diff --git a/source/config.cpp b/source/config.cpp index 61f7322..5b43fe3 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -1,12 +1,21 @@ -#include -#include -#include +#include +#include +#include +#include +#include #include +#include "builder.h" #include "config.h" +#include "sourcepackage.h" using namespace std; using namespace Msp; +Config::Config(SourcePackage &p): + package(p), + changed(false) +{ } + void Config::add_option(const string &n, const string &v, const string &d) { options.insert(OptionMap::value_type(n, Option(n, v, d))); @@ -14,11 +23,7 @@ void Config::add_option(const string &n, const string &v, const string &d) const Config::Option &Config::get_option(const string &name) const { - OptionMap::const_iterator i=options.find(name); - if(i==options.end()) - throw Exception("Tried to access nonexistent option "+name); - - return i->second; + return get_item(options, name); } bool Config::is_option(const string &name) const @@ -26,49 +31,78 @@ bool Config::is_option(const string &name) const return options.count(name); } -bool Config::process(const RawOptionMap &opts) +bool Config::update(const StringMap &opts) { - bool changed=false; - for(RawOptionMap::const_iterator i=opts.begin(); i!=opts.end(); ++i) + bool changed_now = false; + for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i) { - OptionMap::iterator j=options.find(i->first); - if(j!=options.end()) - { - if(i->second!=j->second.value) - changed=true; - j->second.value=i->second; - } + if(set_option(i->first, i->second) && i->first!="profile") + changed_now = true; } - if(changed) - mtime=Time::now(); + if(changed_now) + { + mtime = Time::now(); + changed = true; + } - return changed; + return changed_now; } -void Config::load(const Path::Path &fn) +void Config::finish() { - ifstream in(fn.str().c_str()); - if(!in) return; + for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i) + i->second.value = package.expand_string(i->second.value); +} - struct stat st; - Path::stat(fn, st); - mtime=Time::TimeStamp::from_unixtime(st.st_mtime); +void Config::save() const +{ + if(!changed) + return; - Parser::Parser parser(in, fn.str()); - Loader loader(*this); - loader.load(parser); + FS::Path fn = package.get_source()/".config"; + + package.get_builder().get_logger().log("files", format("Writing %s", fn)); + IO::BufferedFile out(fn.str(), IO::M_WRITE); + + for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i) + IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value); } -void Config::save(const Path::Path &fn) const +bool Config::set_option(const string &opt, const string &val) { - 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"; + bool result = false; + + OptionMap::iterator i = options.find(opt); + if(i!=options.end()) + { + if(i->second.value!=val) + result = true; + i->second.value = val; + } + + return result; +} + +void Config::load() +{ + FS::Path fn = package.get_source()/".config"; + + FS::Stat stat = FS::stat(fn); + if(stat) + { + package.get_builder().get_logger().log("files", format("Reading %s", fn)); + IO::BufferedFile in(fn.str()); + + mtime = stat.get_modify_time(); + + DataFile::Parser parser(in, fn.str()); + Loader loader(*this); + loader.load(parser); + } } + Config::Option::Option(const string &n, const string &v, const string &d): name(n), defv(v), @@ -76,15 +110,14 @@ Config::Option::Option(const string &n, const string &v, const string &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; + obj.set_option(n, v); }