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()
38 IO::BufferedFile in((package.get_source()/".profile").str());
41 set_option("profile", profile);
43 catch(const IO::file_not_found &)
47 package.get_builder().apply_profile_template(*this, get_option("profile").value);
53 void Config::select_profile(const string &profile)
55 set_option("profile", profile);
57 if(!package.get_builder().get_dry_run())
59 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
60 IO::print(out, "%s\n", profile);
64 package.get_builder().apply_profile_template(*this, profile);
70 bool Config::update(const StringMap &opts)
73 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
75 if(set_option(i->first, i->second) && i->first!="profile")
79 if(changed && !freeze_mtime)
87 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
88 i->second.value = package.expand_string(i->second.value);
91 void Config::save() const
93 FS::Path fn = package.get_source()/".options";
95 OptionMap::const_iterator i = options.find("profile");
97 fn = package.get_source()/(".options."+i->second.value);
99 IO::BufferedFile out(fn.str(), IO::M_WRITE);
101 for(i=options.begin(); i!=options.end(); ++i)
102 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
105 bool Config::set_option(const string &opt, const string &val)
109 OptionMap::iterator i = options.find(opt);
112 if(i->second.value!=val)
114 i->second.value = val;
122 FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
126 IO::BufferedFile in(fn.str());
128 mtime = FS::stat(fn).get_modify_time();
130 DataFile::Parser parser(in, fn.str());
131 Loader loader(*this);
134 catch(const IO::file_not_found &)
139 Config::Option::Option(const string &n, const string &v, const string &d):
147 Config::Loader::Loader(Config &c):
150 add("option", &Loader::option);
153 void Config::Loader::option(const string &n, const string &v)
155 conf.set_option(n, v);