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>
10 #include "sourcepackage.h"
15 Config::Config(SourcePackage &p):
20 const Config::Option &Config::add_option(const Feature &f)
23 auto i = pending_options.find(opt.name);
24 if(i!=pending_options.end())
25 opt.value = i->second;
27 return options.insert({ opt.name, opt }).first->second;
30 bool Config::set_option(const string &opt, const string &val)
34 auto i = options.find(opt);
37 if(i->second.value!=val)
43 i->second.value = val;
49 bool Config::is_option(const string &name) const
51 return options.count(name);
54 const Config::Option &Config::get_option(const string &name) const
56 return get_item(options, name);
61 FS::Path fn = package.get_source_directory()/".config";
62 FS::Stat stat = FS::stat(fn);
65 package.get_builder().get_logger().log("files", format("Reading %s", fn));
66 IO::BufferedFile in(fn.str());
68 mtime = stat.get_modify_time();
70 DataFile::Parser parser(in, fn.str());
76 void Config::save() const
81 FS::Path fn = package.get_source_directory()/".config";
83 package.get_builder().get_logger().log("files", format("Writing %s", fn));
84 IO::BufferedFile out(fn.str(), IO::M_WRITE);
85 DataFile::Writer writer(out);
87 for(const auto &kvp: options)
88 writer.write((DataFile::Statement("option"), kvp.second.name, kvp.second.value));
94 Config::Option::Option(const Feature &f):
102 Config::Loader::Loader(Config &c):
103 DataFile::ObjectLoader<Config>(c)
105 add("option", &Loader::option);
108 void Config::Loader::option(const string &n, const string &v)
110 if(obj.options.count(n))
111 obj.set_option(n, v);
113 obj.pending_options[n] = v;