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