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 const Config::Option &Config::add_option(const Feature &f)
18 auto i = pending_options.find(opt.name);
19 if(i!=pending_options.end())
20 opt.value = i->second;
22 return options.insert({ opt.name, opt }).first->second;
25 bool Config::set_option(const string &opt, const string &val)
29 auto i = options.find(opt);
32 if(i->second.value!=val)
38 i->second.value = val;
44 bool Config::is_option(const string &name) const
46 return options.count(name);
49 const Config::Option &Config::get_option(const string &name) const
51 return get_item(options, name);
56 FS::Path fn = package.get_source_directory()/".config";
57 FS::Stat stat = FS::stat(fn);
60 package.get_builder().get_logger().log("files", "Reading %s", fn);
61 IO::BufferedFile in(fn.str());
63 mtime = stat.get_modify_time();
65 DataFile::Parser parser(in, fn.str());
71 void Config::save() const
76 FS::Path fn = package.get_source_directory()/".config";
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);
82 for(const auto &kvp: options)
83 writer.write((DataFile::Statement("option"), kvp.second.name, kvp.second.value));
89 Config::Option::Option(const Feature &f):
97 Config::Loader::Loader(Config &c):
98 DataFile::ObjectLoader<Config>(c)
100 add("option", &Loader::option);
103 void Config::Loader::option(const string &n, const string &v)
105 if(obj.options.count(n))
106 obj.set_option(n, v);
108 obj.pending_options[n] = v;