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