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