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 Selects the last profile used. If the profile cache file is not present, the
45 default profile is assumed.
47 void Config::select_last_profile()
49 ifstream in((source/".profile.cache").str().c_str());
54 set_option("profile", profile);
57 set_option("profile", "default");
63 Selects the given profile. The profile cache file is updated as well.
65 void Config::select_profile(const string &profile)
67 set_option("profile", profile);
69 ofstream out((source/".profile.cache").str().c_str());
77 Processes options from the given raw option map. Nonexistent options are
78 ignored. If any options were changed, the mtime of the configuration is updated
81 @param opts A map to process options from
83 @return Whether any option values were changed
85 bool Config::update(const RawOptionMap &opts)
88 for(RawOptionMap::const_iterator i=opts.begin(); i!=opts.end(); ++i)
90 if(set_option(i->first, i->second) && i->first!="profile")
100 void Config::save() const
102 Path::Path fn=source/".options.cache";
104 OptionMap::const_iterator i=options.find("profile");
106 fn=source/(".options."+i->second.value+".cache");
108 ofstream out(fn.str().c_str());
111 for(i=options.begin(); i!=options.end(); ++i)
112 out<<"option \""<<i->second.name<<"\" \""<<i->second.value<<"\";\n";
115 bool Config::set_option(const string &opt, const string &val)
119 OptionMap::iterator i=options.find(opt);
122 if(i->second.value!=val)
132 Path::Path fn=source/".options.cache";
134 OptionMap::iterator i=options.find("profile");
136 fn=source/(".options."+i->second.value+".cache");
138 ifstream in(fn.str().c_str());
143 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
145 Parser::Parser parser(in, fn.str());
146 Loader loader(*this);
150 Config::Option::Option(const string &n, const string &v, const string &d):
157 Config::Loader::Loader(Config &c):
160 add("option", &Loader::option);
163 void Config::Loader::option(const string &n, const string &v)
165 conf.set_option(n, v);