1 #include <msp/core/maputils.h>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/io/file.h>
5 #include <msp/io/print.h>
6 #include <msp/time/utils.h>
9 #include "sourcepackage.h"
14 Config::Config(SourcePackage &p):
19 void Config::add_option(const string &n, const string &v, const string &d)
22 if(pending_options.count(n))
23 opt.value = pending_options[n];
24 options.insert(OptionMap::value_type(n, opt));
27 const Config::Option &Config::get_option(const string &name) const
29 return get_item(options, name);
32 bool Config::is_option(const string &name) const
34 return options.count(name);
37 bool Config::update(const StringMap &opts)
39 bool changed_now = false;
40 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
42 if(set_option(i->first, i->second) && i->first!="profile")
57 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
58 i->second.value = package.expand_string(i->second.value);
61 void Config::save() const
66 FS::Path fn = package.get_source()/".config";
68 package.get_builder().get_logger().log("files", format("Writing %s", fn));
69 IO::BufferedFile out(fn.str(), IO::M_WRITE);
71 for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
72 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
75 bool Config::set_option(const string &opt, const string &val)
79 OptionMap::iterator i = options.find(opt);
82 if(i->second.value!=val)
87 i->second.value = val;
95 FS::Path fn = package.get_source()/".config";
97 FS::Stat stat = FS::stat(fn);
100 package.get_builder().get_logger().log("files", format("Reading %s", fn));
101 IO::BufferedFile in(fn.str());
103 mtime = stat.get_modify_time();
105 DataFile::Parser parser(in, fn.str());
106 Loader loader(*this);
112 Config::Option::Option(const string &n, const string &v, const string &d):
120 Config::Loader::Loader(Config &c):
121 DataFile::ObjectLoader<Config>(c)
123 add("option", &Loader::option);
126 void Config::Loader::option(const string &n, const string &v)
128 if(obj.options.count(n))
129 obj.set_option(n, v);
131 obj.pending_options[n] = v;