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