3 #include <msp/path/utils.h>
4 #include <msp/time/utils.h>
11 Adds a configuration option.
14 @param v Default value
17 void Config::add_option(const string &n, const string &v, const string &d)
19 options.insert(OptionMap::value_type(n, Option(n, v, d)));
23 Gets the given option from the configuration. If the option doesn't exist,
24 an Exception is thrown.
26 const Config::Option &Config::get_option(const string &name) const
28 OptionMap::const_iterator i=options.find(name);
30 throw Exception("Tried to access nonexistent option "+name);
36 Checks whether an option with the given name exists.
38 bool Config::is_option(const string &name) const
40 return options.count(name);
44 Processes options from the given raw option map. Nonexistent options are
45 ignored. If any options were changed, the mtime of the configuration is updated
48 @param opts A map to process options from
50 @return Whether any option values were changed
52 bool Config::process(const RawOptionMap &opts)
55 for(RawOptionMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
57 OptionMap::iterator j=options.find(i->first);
60 if(i->second!=j->second.value)
62 j->second.value=i->second;
73 Loads configuration from a file, if it exists.
75 void Config::load(const Path::Path &fn)
77 ifstream in(fn.str().c_str());
82 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
84 Parser::Parser parser(in, fn.str());
89 void Config::save(const Path::Path &fn) const
91 ofstream out(fn.str().c_str());
94 for(OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
95 out<<"option \""<<i->second.name<<"\" \""<<i->second.value<<"\";\n";
98 Config::Option::Option(const string &n, const string &v, const string &d):
105 Config::Loader::Loader(Config &c):
108 add("option", &Loader::option);
111 void Config::Loader::option(const string &n, const string &v)
113 OptionMap::iterator i=conf.options.find(n);
114 if(i!=conf.options.end())