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):
20 void Config::add_option(const string &n, const string &v, const string &d)
22 options.insert(OptionMap::value_type(n, Option(n, v, d)));
25 const Config::Option &Config::get_option(const string &name) const
27 return get_item(options, name);
30 bool Config::is_option(const string &name) const
32 return options.count(name);
35 void Config::select_last_profile()
37 FS::Path profile_cache_fn = package.get_source()/".profile";
38 if(FS::exists(profile_cache_fn))
40 IO::BufferedFile in(profile_cache_fn.str());
43 set_option("profile", profile);
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);
58 package.get_builder().apply_profile_template(*this, profile);
64 bool Config::update(const StringMap &opts)
66 bool changed_now = false;
67 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
69 if(set_option(i->first, i->second) && i->first!="profile")
73 if(changed_now && !freeze_mtime)
84 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
85 i->second.value = package.expand_string(i->second.value);
88 void Config::save() const
93 FS::Path fn = package.get_source()/".options";
95 OptionMap::const_iterator i = options.find("profile");
98 fn = package.get_source()/(".options."+i->second.value);
100 IO::BufferedFile profile_out((package.get_source()/".profile").str(), IO::M_WRITE);
101 IO::print(profile_out, "%s\n", i->second.value);
104 IO::BufferedFile out(fn.str(), IO::M_WRITE);
106 for(i=options.begin(); i!=options.end(); ++i)
107 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
110 bool Config::set_option(const string &opt, const string &val)
114 OptionMap::iterator i = options.find(opt);
117 if(i->second.value!=val)
119 i->second.value = val;
127 FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
129 FS::Stat stat = FS::stat(fn);
132 IO::BufferedFile in(fn.str());
134 mtime = stat.get_modify_time();
136 DataFile::Parser parser(in, fn.str());
137 Loader loader(*this);
143 Config::Option::Option(const string &n, const string &v, const string &d):
151 Config::Loader::Loader(Config &c):
152 DataFile::ObjectLoader<Config>(c)
154 add("option", &Loader::option);
157 void Config::Loader::option(const string &n, const string &v)
159 obj.set_option(n, v);