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