3 This file is part of builder
4 Copyright © 2006-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/maputils.h>
9 #include <msp/fs/stat.h>
10 #include <msp/fs/utils.h>
11 #include <msp/io/file.h>
12 #include <msp/io/print.h>
13 #include <msp/time/utils.h>
16 #include "sourcepackage.h"
21 Config::Config(SourcePackage &p):
26 void Config::add_option(const string &n, const string &v, const string &d)
28 options.insert(OptionMap::value_type(n, Option(n, v, d)));
31 const Config::Option &Config::get_option(const string &name) const
33 return get_item(options, name);
36 bool Config::is_option(const string &name) const
38 return options.count(name);
41 void Config::select_last_profile()
45 IO::BufferedFile in((package.get_source()/".profile").str());
48 set_option("profile", profile);
50 catch(const IO::file_not_found &)
54 package.get_builder().apply_profile_template(*this, get_option("profile").value);
60 void Config::select_profile(const string &profile)
62 set_option("profile", profile);
64 if(!package.get_builder().get_dry_run())
66 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
67 IO::print(out, "%s\n", profile);
71 package.get_builder().apply_profile_template(*this, profile);
77 bool Config::update(const StringMap &opts)
80 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
82 if(set_option(i->first, i->second) && i->first!="profile")
86 if(changed && !freeze_mtime)
94 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
95 i->second.value = package.expand_string(i->second.value);
98 void Config::save() const
100 FS::Path fn = package.get_source()/".options";
102 OptionMap::const_iterator i = options.find("profile");
104 fn = package.get_source()/(".options."+i->second.value);
106 IO::BufferedFile out(fn.str(), IO::M_WRITE);
108 for(i=options.begin(); i!=options.end(); ++i)
109 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
112 bool Config::set_option(const string &opt, const string &val)
116 OptionMap::iterator i = options.find(opt);
119 if(i->second.value!=val)
121 i->second.value = val;
129 FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
133 IO::BufferedFile in(fn.str());
135 mtime = FS::stat(fn).get_modify_time();
137 DataFile::Parser parser(in, fn.str());
138 Loader loader(*this);
141 catch(const IO::file_not_found &)
146 Config::Option::Option(const string &n, const string &v, const string &d):
154 Config::Loader::Loader(Config &c):
157 add("option", &Loader::option);
160 void Config::Loader::option(const string &n, const string &v)
162 conf.set_option(n, v);