]> git.tdb.fi Git - builder.git/blob - source/config.cpp
Replace the overly generic configuration profiles with something more purposeful
[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         IO::BufferedFile out(fn.str(), IO::M_WRITE);
66
67         for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
68                 IO::print(out, "option \"%s\" \"%s\";\n", i->second.name, i->second.value);
69 }
70
71 bool Config::set_option(const string &opt, const string &val)
72 {
73         bool result = false;
74
75         OptionMap::iterator i = options.find(opt);
76         if(i!=options.end())
77         {
78                 if(i->second.value!=val)
79                         result = true;
80                 i->second.value = val;
81         }
82
83         return result;
84 }
85
86 void Config::load()
87 {
88         FS::Path fn = package.get_source()/".config";
89
90         FS::Stat stat = FS::stat(fn);
91         if(stat)
92         {
93                 IO::BufferedFile in(fn.str());
94
95                 mtime = stat.get_modify_time();
96
97                 DataFile::Parser parser(in, fn.str());
98                 Loader loader(*this);
99                 loader.load(parser);
100         }
101 }
102
103
104 Config::Option::Option(const string &n, const string &v, const string &d):
105         name(n),
106         defv(v),
107         descr(d),
108         value(v)
109 { }
110
111
112 Config::Loader::Loader(Config &c):
113         DataFile::ObjectLoader<Config>(c)
114 {
115         add("option", &Loader::option);
116 }
117
118 void Config::Loader::option(const string &n, const string &v)
119 {
120         obj.set_option(n, v);
121 }