]> git.tdb.fi Git - builder.git/blob - source/config.cpp
Variables in Build files weren't such a hot idea. KISS.
[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         changed(false)
17 { }
18
19 void Config::add_option(const string &n, const string &v, const string &d)
20 {
21         Option opt(n, v, d);
22         if(pending_options.count(n))
23                 opt.value = pending_options[n];
24         options.insert(OptionMap::value_type(n, opt));
25 }
26
27 const Config::Option &Config::get_option(const string &name) const
28 {
29         return get_item(options, name);
30 }
31
32 bool Config::is_option(const string &name) const
33 {
34         return options.count(name);
35 }
36
37 bool Config::update(const StringMap &opts)
38 {
39         bool changed_now = false;
40         for(StringMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
41         {
42                 if(set_option(i->first, i->second) && i->first!="profile")
43                         changed_now = true;
44         }
45
46         if(changed_now)
47         {
48                 mtime = Time::now();
49                 changed = true;
50         }
51
52         return changed_now;
53 }
54
55 void Config::save() const
56 {
57         if(!changed)
58                 return;
59
60         FS::Path fn = package.get_source()/".config";
61
62         package.get_builder().get_logger().log("files", format("Writing %s", fn));
63         IO::BufferedFile out(fn.str(), IO::M_WRITE);
64
65         for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
66                 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
67 }
68
69 bool Config::set_option(const string &opt, const string &val)
70 {
71         bool result = false;
72
73         OptionMap::iterator i = options.find(opt);
74         if(i!=options.end())
75         {
76                 if(i->second.value!=val)
77                 {
78                         result = true;
79                         changed = true;
80                 }
81                 i->second.value = val;
82         }
83
84         return result;
85 }
86
87 void Config::load()
88 {
89         FS::Path fn = package.get_source()/".config";
90
91         FS::Stat stat = FS::stat(fn);
92         if(stat)
93         {
94                 package.get_builder().get_logger().log("files", format("Reading %s", fn));
95                 IO::BufferedFile in(fn.str());
96
97                 mtime = stat.get_modify_time();
98
99                 DataFile::Parser parser(in, fn.str());
100                 Loader loader(*this);
101                 loader.load(parser);
102         }
103 }
104
105
106 Config::Option::Option(const string &n, const string &v, const string &d):
107         name(n),
108         defv(v),
109         descr(d),
110         value(v)
111 { }
112
113
114 Config::Loader::Loader(Config &c):
115         DataFile::ObjectLoader<Config>(c)
116 {
117         add("option", &Loader::option);
118 }
119
120 void Config::Loader::option(const string &n, const string &v)
121 {
122         if(obj.options.count(n))
123                 obj.set_option(n, v);
124         else
125                 obj.pending_options[n] = v;
126 }