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 bool Config::set_option(const string &opt, const string &val)
31 OptionMap::iterator i = options.find(opt);
34 if(i->second.value!=val)
39 i->second.value = val;
45 bool Config::is_option(const string &name) const
47 return options.count(name);
50 const Config::Option &Config::get_option(const string &name) const
52 return get_item(options, name);
57 FS::Path fn = package.get_source_directory()/".config";
58 FS::Stat stat = FS::stat(fn);
61 package.get_builder().get_logger().log("files", format("Reading %s", fn));
62 IO::BufferedFile in(fn.str());
64 mtime = stat.get_modify_time();
66 DataFile::Parser parser(in, fn.str());
72 void Config::save() const
77 FS::Path fn = package.get_source_directory()/".config";
79 package.get_builder().get_logger().log("files", format("Writing %s", fn));
80 IO::BufferedFile out(fn.str(), IO::M_WRITE);
82 for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
83 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
87 Config::Option::Option(const string &n, const string &v, const string &d):
95 Config::Loader::Loader(Config &c):
96 DataFile::ObjectLoader<Config>(c)
98 add("option", &Loader::option);
101 void Config::Loader::option(const string &n, const string &v)
103 if(obj.options.count(n))
104 obj.set_option(n, v);
106 obj.pending_options[n] = v;