45 #include "TheBESKeys.h"
46 #include "kvp_utils.h"
49 #include "BESFSFile.h"
50 #include "BESInternalFatalError.h"
51 #include "BESSyntaxUserError.h"
53 #define BES_INCLUDE_KEY "BES.Include"
57 set<string> TheBESKeys::KeyList;
64 if (_instance)
return _instance;
74 string try_ini =
"/usr/local/etc/bes/bes.conf";
75 if (access(try_ini.c_str(), R_OK) == 0) {
81 try_ini =
"/etc/bes/bes.conf";
82 if (access(try_ini.c_str(), R_OK) == 0) {
88 try_ini =
"/usr/etc/bes/bes.conf";
89 if (access(try_ini.c_str(), R_OK) == 0) {
113 TheBESKeys::TheBESKeys(
const string &keys_file_name) :
114 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(0), _own_keys(true)
116 _the_keys =
new map<string, vector<string> >;
120 TheBESKeys::TheBESKeys(
const string &keys_file_name, map<
string, vector<string> > *keys) :
121 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(keys), _own_keys(false)
133 void TheBESKeys::initialize_keys()
135 kvp::load_keys(KeyList, _keys_file_name, *_the_keys);
138 _keys_file =
new ifstream(_keys_file_name.c_str());
140 if (!(*_keys_file)) {
142 getcwd(path,
sizeof(path));
143 string s = string(
"Cannot open BES configuration file '") + _keys_file_name +
"': ";
144 char *err = strerror(errno);
148 s +=
"Unknown error";
150 s += (string)
".\n" +
"The current working directory is " + path;
165 string s = (string)
"Undefined exception while trying to load keys from the BES configuration file '"
166 + _keys_file_name +
"'";
173 void TheBESKeys::clean()
180 if (_the_keys && _own_keys) {
192 bool TheBESKeys::LoadedKeys(
const string &key_file)
195 vector<string>::const_iterator i = TheBESKeys::KeyList.begin();
196 vector<string>::const_iterator e = TheBESKeys::KeyList.end();
197 for (; i != e; i++) {
198 if ((*i) == key_file) {
203 set<string>::iterator it = KeyList.find(key_file);
205 return it!=KeyList.end();
226 map<string, vector<string> >::iterator i;
227 i = _the_keys->find(key);
228 if (i == _the_keys->end()) {
230 (*_the_keys)[key] = vals;
232 if (!addto) (*_the_keys)[key].clear();
234 (*_the_keys)[key].push_back(val);
254 kvp::break_pair(pair.c_str(), key, val, addto);
275 map<string, vector<string> >::iterator i;
276 i = _the_keys->find(s);
277 if (i != _the_keys->end()) {
279 if ((*i).second.size() > 1) {
280 string err = string(
"Multiple values for the key ") + s +
" found, should only be one.";
283 if ((*i).second.size() == 1) {
284 val = (*i).second[0];
306 map<string, vector<string> >::iterator i;
307 i = _the_keys->find(s);
308 if (i != _the_keys->end()) {
334 return (value ==
"true" || value ==
"yes"|| value ==
"on");
337 return default_value;
358 if (value[value.length() - 1] ==
'/') value.erase(value.length() - 1);
362 return default_value;
383 std::istringstream iss(value);
386 if (iss.eof() || iss.bad() || iss.fail())
387 return default_value;
392 return default_value;
404 strm << BESIndent::LMarg <<
"BESKeys::dump - (" << (
void *)
this <<
")" << endl;
406 strm << BESIndent::LMarg <<
"key file:" << _keys_file_name << endl;
407 if (_keys_file && *_keys_file) {
408 strm << BESIndent::LMarg <<
"key file is valid" << endl;
411 strm << BESIndent::LMarg <<
"key file is NOT valid" << endl;
413 if (_the_keys && _the_keys->size()) {
414 strm << BESIndent::LMarg <<
" keys:" << endl;
416 Keys_citer i = _the_keys->begin();
417 Keys_citer ie = _the_keys->end();
418 for (; i != ie; i++) {
419 strm << BESIndent::LMarg << (*i).first <<
": " ;
421 vector<string>::const_iterator v = (*i).second.begin();
422 vector<string>::const_iterator ve = (*i).second.end();
423 for (; v != ve; v++) {
429 BESIndent::UnIndent();
432 strm << BESIndent::LMarg <<
"keys: none" << endl;
434 BESIndent::UnIndent();