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 void Config::select_last_profile()
36 FS::Path profile_cache_fn = package.get_source()/".profile";
37 if(FS::exists(profile_cache_fn))
39 IO::BufferedFile in(profile_cache_fn.str());
42 set_option("profile", profile);
46 package.get_builder().apply_profile_template(*this, get_option("profile").value);
52 void Config::select_profile(const string &profile)
54 set_option("profile", profile);
56 if(!package.get_builder().get_dry_run())
58 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
59 IO::print(out, "%s\n", profile);
63 package.get_builder().apply_profile_template(*this, profile);
69 bool Config::update(const StringMap &opts)
72 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
74 if(set_option(i->first, i->second) && i->first!="profile")
78 if(changed && !freeze_mtime)
86 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
87 i->second.value = package.expand_string(i->second.value);
90 void Config::save() const
92 FS::Path fn = package.get_source()/".options";
94 OptionMap::const_iterator i = options.find("profile");
96 fn = package.get_source()/(".options."+i->second.value);
98 IO::BufferedFile out(fn.str(), IO::M_WRITE);
100 for(i=options.begin(); i!=options.end(); ++i)
101 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
104 bool Config::set_option(const string &opt, const string &val)
108 OptionMap::iterator i = options.find(opt);
111 if(i->second.value!=val)
113 i->second.value = val;
121 FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
123 FS::Stat stat = FS::stat(fn);
126 IO::BufferedFile in(fn.str());
128 mtime = stat.get_modify_time();
130 DataFile::Parser parser(in, fn.str());
131 Loader loader(*this);
137 Config::Option::Option(const string &n, const string &v, const string &d):
145 Config::Loader::Loader(Config &c):
148 add("option", &Loader::option);
151 void Config::Loader::option(const string &n, const string &v)
153 conf.set_option(n, v);