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