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)
21 options.insert(OptionMap::value_type(n, Option(n, v, d)));
24 const Config::Option &Config::get_option(const string &name) const
26 return get_item(options, name);
29 bool Config::is_option(const string &name) const
31 return options.count(name);
34 bool Config::update(const StringMap &opts)
36 bool changed_now = false;
37 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
39 if(set_option(i->first, i->second) && i->first!="profile")
54 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
55 i->second.value = package.expand_string(i->second.value);
58 void Config::save() const
63 FS::Path fn = package.get_source()/".config";
65 IO::BufferedFile out(fn.str(), IO::M_WRITE);
67 for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
68 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
71 bool Config::set_option(const string &opt, const string &val)
75 OptionMap::iterator i = options.find(opt);
78 if(i->second.value!=val)
80 i->second.value = val;
88 FS::Path fn = package.get_source()/".config";
90 FS::Stat stat = FS::stat(fn);
93 IO::BufferedFile in(fn.str());
95 mtime = stat.get_modify_time();
97 DataFile::Parser parser(in, fn.str());
104 Config::Option::Option(const string &n, const string &v, const string &d):
112 Config::Loader::Loader(Config &c):
113 DataFile::ObjectLoader<Config>(c)
115 add("option", &Loader::option);
118 void Config::Loader::option(const string &n, const string &v)
120 obj.set_option(n, v);