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