]> git.tdb.fi Git - builder.git/blob - source/config.cpp
Replace per-file copyright notices with a single file
[builder.git] / source / config.cpp
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>
7 #include "builder.h"
8 #include "config.h"
9 #include "sourcepackage.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Config::Config(SourcePackage &p):
15         package(p),
16         freeze_mtime(false)
17 { }
18
19 void Config::add_option(const string &n, const string &v, const string &d)
20 {
21         options.insert(OptionMap::value_type(n, Option(n, v, d)));
22 }
23
24 const Config::Option &Config::get_option(const string &name) const
25 {
26         return get_item(options, name);
27 }
28
29 bool Config::is_option(const string &name) const
30 {
31         return options.count(name);
32 }
33
34 void Config::select_last_profile()
35 {
36         try
37         {
38                 IO::BufferedFile in((package.get_source()/".profile").str());
39                 string profile;
40                 in.getline(profile);
41                 set_option("profile", profile);
42         }
43         catch(const IO::file_not_found &)
44         { }
45
46         freeze_mtime = true;
47         package.get_builder().apply_profile_template(*this, get_option("profile").value);
48         freeze_mtime = false;
49
50         load();
51 }
52
53 void Config::select_profile(const string &profile)
54 {
55         set_option("profile", profile);
56
57         if(!package.get_builder().get_dry_run())
58         {
59                 IO::BufferedFile out((package.get_source()/".profile").str(), IO::M_WRITE);
60                 IO::print(out, "%s\n", profile);
61         }
62
63         freeze_mtime = true;
64         package.get_builder().apply_profile_template(*this, profile);
65         freeze_mtime = false;
66
67         load();
68 }
69
70 bool Config::update(const StringMap &opts)
71 {
72         bool changed = false;
73         for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
74         {
75                 if(set_option(i->first, i->second) && i->first!="profile")
76                         changed = true;
77         }
78
79         if(changed && !freeze_mtime)
80                 mtime = Time::now();
81
82         return changed;
83 }
84
85 void Config::finish()
86 {
87         for(OptionMap::iterator i=options.begin(); i!=options.end(); ++i)
88                 i->second.value = package.expand_string(i->second.value);
89 }
90
91 void Config::save() const
92 {
93         FS::Path fn = package.get_source()/".options";
94
95         OptionMap::const_iterator i = options.find("profile");
96         if(i!=options.end())
97                 fn = package.get_source()/(".options."+i->second.value);
98
99         IO::BufferedFile out(fn.str(), IO::M_WRITE);
100
101         for(i=options.begin(); i!=options.end(); ++i)
102                 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
103 }
104
105 bool Config::set_option(const string &opt, const string &val)
106 {
107         bool result = false;
108
109         OptionMap::iterator i = options.find(opt);
110         if(i!=options.end())
111         {
112                 if(i->second.value!=val)
113                         result = true;
114                 i->second.value = val;
115         }
116
117         return result;
118 }
119
120 void Config::load()
121 {
122         FS::Path fn = package.get_source()/(".options."+get_option("profile").value);
123
124         try
125         {
126                 IO::BufferedFile in(fn.str());
127
128                 mtime = FS::stat(fn).get_modify_time();
129
130                 DataFile::Parser parser(in, fn.str());
131                 Loader loader(*this);
132                 loader.load(parser);
133         }
134         catch(const IO::file_not_found &)
135         { }
136 }
137
138
139 Config::Option::Option(const string &n, const string &v, const string &d):
140         name(n),
141         defv(v),
142         descr(d),
143         value(v)
144 { }
145
146
147 Config::Loader::Loader(Config &c):
148         conf(c)
149 {
150         add("option", &Loader::option);
151 }
152
153 void Config::Loader::option(const string &n, const string &v)
154 {
155         conf.set_option(n, v);
156 }