3 This file is part of builder
4 Copyright © 2006-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/except.h>
9 #include <msp/fs/stat.h>
10 #include <msp/fs/utils.h>
11 #include <msp/io/except.h>
12 #include <msp/io/file.h>
13 #include <msp/io/print.h>
14 #include <msp/time/utils.h>
17 #include "sourcepackage.h"
22 Config::Config(SourcePackage &p):
27 void Config::add_option(const string &n, const string &v, const string &d)
29 options.insert(OptionMap::value_type(n, Option(n, v, d)));
32 const Config::Option &Config::get_option(const string &name) const
34 OptionMap::const_iterator i = options.find(name);
36 throw KeyError("Unknown option", name);
41 bool Config::is_option(const string &name) const
43 return options.count(name);
46 void Config::select_last_profile()
50 IO::BufferedFile in((package.get_source()/".profile").str());
53 set_option("profile", profile);
55 catch(const IO::FileNotFound &)
59 package.get_builder().apply_profile_template(*this, get_option("profile").value);
65 void Config::select_profile(const string &profile)
67 set_option("profile", profile);
69 if(!package.get_builder().get_dry_run())
71 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
72 IO::print(out, "%s\n", profile);
76 package.get_builder().apply_profile_template(*this, profile);
82 bool Config::update(const StringMap &opts)
85 for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
87 if(set_option(i->first, i->second) && i->first!="profile")
91 if(changed && !freeze_mtime)
99 for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
100 i->second.value = package.expand_string(i->second.value);
103 void Config::save() const
105 FS::Path fn = package.get_source()/".options";
107 OptionMap::const_iterator i = options.find("profile");
109 fn = package.get_source()/(".options."+i->second.value);
111 IO::BufferedFile out(fn.str(), IO::M_WRITE);
113 for(i=options.begin(); i!=options.end(); ++i)
114 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
117 bool Config::set_option(const string &opt, const string &val)
121 OptionMap::iterator i = options.find(opt);
124 if(i->second.value!=val)
126 i->second.value = val;
134 FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
138 IO::BufferedFile in(fn.str());
140 mtime = Time::TimeStamp::from_unixtime(FS::stat(fn).st_mtime);
142 DataFile::Parser parser(in, fn.str());
143 Loader loader(*this);
146 catch(const IO::FileNotFound &)
151 Config::Option::Option(const string &n, const string &v, const string &d):
159 Config::Loader::Loader(Config &c):
162 add("option", &Loader::option);
165 void Config::Loader::option(const string &n, const string &v)
167 conf.set_option(n, v);