2 #include <msp/core/error.h>
3 #include <msp/path/utils.h>
4 #include <msp/time/utils.h>
12 Config::Config(Package &p):
18 Adds a configuration option.
21 @param v Default value
24 void Config::add_option(const string &n, const string &v, const string &d)
26 options.insert(OptionMap::value_type(n, Option(n, v, d)));
30 Gets the given option from the configuration. If the option doesn't exist,
31 an Exception is thrown.
33 const Config::Option &Config::get_option(const string &name) const
35 OptionMap::const_iterator i=options.find(name);
37 throw Exception("Tried to access nonexistent option "+name);
43 Checks whether an option with the given name exists.
45 bool Config::is_option(const string &name) const
47 return options.count(name);
51 Selects the last profile used. If the profile cache file is not present, the
52 default profile is assumed.
54 void Config::select_last_profile()
56 ifstream in((package.get_source()/".profile.cache").str().c_str());
61 set_option("profile", profile);
65 package.get_builder().apply_profile_template(*this, get_option("profile").value);
72 Selects the given profile. The profile cache file is updated as well, unless
75 void Config::select_profile(const string &profile)
77 set_option("profile", profile);
79 if(!package.get_builder().get_dry_run())
81 ofstream out((package.get_source()/".profile.cache").str().c_str());
87 package.get_builder().apply_profile_template(*this, profile);
94 Processes options from the given raw option map. Nonexistent options are
95 ignored. If any options were changed, the mtime of the configuration is updated
98 @param opts A map to process options from
100 @return Whether any option values were changed
102 bool Config::update(const StringMap &opts)
105 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
107 if(set_option(i->first, i->second) && i->first!="profile")
111 if(changed && !freeze_mtime)
118 Expands any variable references in options.
120 void Config::finish()
122 for(unsigned n=0; n<20; ++n)
125 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
127 Option &opt=i->second;
129 while((dollar=opt.value.find('$', dollar))!=string::npos)
133 if(opt.value[dollar+1]=='{')
135 end=opt.value.find('}', dollar+2);
136 if(end==string::npos)
137 throw Exception("Unterminated variable reference");
138 var=opt.value.substr(dollar+2, end-dollar-2);
143 for(end=dollar+1; (isalnum(opt.value[end]) && opt.value[end]!='_'); ++end);
144 var=opt.value.substr(dollar+1, end-dollar-1);
149 value=get_option(var).value;
151 value=getenv(var.c_str());
153 opt.value.replace(dollar, end-dollar, value);
155 dollar+=value.size();
165 void Config::save() const
167 Path::Path fn=package.get_source()/".options.cache";
169 OptionMap::const_iterator i=options.find("profile");
171 fn=package.get_source()/(".options."+i->second.value+".cache");
173 ofstream out(fn.str().c_str());
176 for(i=options.begin(); i!=options.end(); ++i)
177 out<<"option \""<<i->second.name<<"\" \""<<i->second.value<<"\";\n";
180 bool Config::set_option(const string &opt, const string &val)
184 OptionMap::iterator i=options.find(opt);
187 if(i->second.value!=val)
197 Path::Path fn=package.get_source()/".options.cache";
199 OptionMap::iterator i=options.find("profile");
201 fn=package.get_source()/(".options."+i->second.value+".cache");
203 ifstream in(fn.str().c_str());
208 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
210 Parser::Parser parser(in, fn.str());
211 Loader loader(*this);
216 Config::Option::Option(const string &n, const string &v, const string &d):
224 Config::Loader::Loader(Config &c):
227 add("option", &Loader::option);
230 void Config::Loader::option(const string &n, const string &v)
232 conf.set_option(n, v);