]> git.tdb.fi Git - builder.git/blob - source/config.cpp
48864a9fac64f8fb33d7f4143acac3cb1a27c5ba
[builder.git] / source / config.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/datafile/writer.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/file.h>
6 #include <msp/io/print.h>
7 #include <msp/time/utils.h>
8 #include "builder.h"
9 #include "config.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Config::Config(SourcePackage &p):
16         package(p)
17 { }
18
19 const Config::Option &Config::add_option(const Feature &f)
20 {
21         Option opt(f);
22         auto i = pending_options.find(opt.name);
23         if(i!=pending_options.end())
24                 opt.value = i->second;
25
26         return options.insert({ 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         auto 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", "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", "Writing %s", fn);
83         IO::BufferedFile out(fn.str(), IO::M_WRITE);
84         DataFile::Writer writer(out);
85
86         for(const auto &kvp: options)
87                 writer.write((DataFile::Statement("option"), kvp.second.name, kvp.second.value));
88
89         changed = false;
90 }
91
92
93 Config::Option::Option(const Feature &f):
94         Feature(f),
95         value(default_value)
96 {
97         name = "with_"+name;
98 }
99
100
101 Config::Loader::Loader(Config &c):
102         DataFile::ObjectLoader<Config>(c)
103 {
104         add("option", &Loader::option);
105 }
106
107 void Config::Loader::option(const string &n, const string &v)
108 {
109         if(obj.options.count(n))
110                 obj.set_option(n, v);
111         else
112                 obj.pending_options[n] = v;
113 }