37 #include "TheBESKeys.h" 38 #include "BESInternalFatalError.h" 39 #include "BESSyntaxUserError.h" 50 #include "TheBESKeys.h" 53 #include "BESFSFile.h" 55 #define BES_INCLUDE_KEY "BES.Include" 57 std::vector<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) {
95 throw BESInternalFatalError(
"Unable to find a conf file or module version mismatch.", __FILE__, __LINE__);
114 TheBESKeys::TheBESKeys(
const string &keys_file_name) :
115 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(0), _own_keys(true)
117 _the_keys =
new map<string, vector<string> >;
121 TheBESKeys::TheBESKeys(
const string &keys_file_name, map<
string, vector<string> > *keys) :
122 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(keys), _own_keys(false)
134 void TheBESKeys::initialize_keys()
136 _keys_file =
new ifstream(_keys_file_name.c_str());
138 if (!(*_keys_file)) {
140 getcwd(path,
sizeof(path));
141 string s = string(
"BES: fatal, cannot open BES configuration file ") + _keys_file_name +
": ";
142 char *err = strerror(errno);
146 s +=
"Unknown error";
148 s += (string)
".\n" +
"The current working directory is " + path;
163 string s = (string)
"Undefined exception while trying to load keys from bes configuration file " 169 void TheBESKeys::clean()
176 if (_the_keys && _own_keys) {
188 bool TheBESKeys::LoadedKeys(
const string &key_file)
190 vector<string>::const_iterator i = TheBESKeys::KeyList.begin();
191 vector<string>::const_iterator e = TheBESKeys::KeyList.end();
192 for (; i != e; i++) {
193 if ((*i) == key_file) {
201 void TheBESKeys::load_keys()
203 string key, value, line;
204 while (!_keys_file->eof()) {
206 getline(*_keys_file, line);
207 if (break_pair(line.c_str(), key, value, addto)) {
208 if (key == BES_INCLUDE_KEY) {
213 load_include_files(value);
230 inline bool TheBESKeys::break_pair(
const char* b,
string& key,
string &value,
bool &addto)
234 if (b && (b[0] !=
'#') && (!only_blanks(b))) {
235 register size_t l = strlen(b);
239 for (
register size_t j = 0; j < l && !done; j++) {
244 if (pos != static_cast<int>(j - 1)) {
245 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name
246 +
" '+' character found in variable name" +
" or attempting '+=' with space" 247 +
" between the characters.\n";
253 else if (b[j] ==
'+') {
259 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name +
": " 260 +
" '=' character not found.\n";
265 key = s.substr(0, pos);
268 value = s.substr(pos + 2, s.size());
270 value = s.substr(pos + 1, s.size());
291 void TheBESKeys::load_include_files(
const string &files)
298 if (!files.empty() && files[0] ==
'/') {
299 newdir = allfiles.getDirName();
305 string currdir = currfile.getDirName();
307 string alldir = allfiles.getDirName();
309 if ((currdir ==
"./" || currdir ==
".") && (alldir ==
"./" || alldir ==
".")) {
313 if (alldir ==
"./" || alldir ==
".") {
317 newdir = currdir +
"/" + alldir;
324 BESFSDir fsd(newdir, allfiles.getFileName());
325 BESFSDir::fileIterator i = fsd.beginOfFileList();
326 BESFSDir::fileIterator e = fsd.endOfFileList();
327 for (; i != e; i++) {
328 load_include_file((*i).getFullPath());
338 void TheBESKeys::load_include_file(
const string &file)
343 if (!TheBESKeys::LoadedKeys(file)) {
344 TheBESKeys::KeyList.push_back(file);
349 bool TheBESKeys::only_blanks(
const char *line)
351 string my_line = line;
352 if (my_line.find_first_not_of(
" ") != string::npos)
376 map<string, vector<string> >::iterator i;
377 i = _the_keys->find(key);
378 if (i == _the_keys->end()) {
380 (*_the_keys)[key] = vals;
382 if (!addto) (*_the_keys)[key].clear();
384 (*_the_keys)[key].push_back(val);
404 break_pair(pair.c_str(), key, val, addto);
425 map<string, vector<string> >::iterator i;
426 i = _the_keys->find(s);
427 if (i != _the_keys->end()) {
429 if ((*i).second.size() > 1) {
430 string err = string(
"Multiple values for the key ") + s +
" found, should only be one.";
433 if ((*i).second.size() == 1) {
434 val = (*i).second[0];
456 map<string, vector<string> >::iterator i;
457 i = _the_keys->find(s);
458 if (i != _the_keys->end()) {
472 strm << BESIndent::LMarg <<
"BESKeys::dump - (" << (
void *)
this <<
")" << endl;
474 strm << BESIndent::LMarg <<
"key file:" << _keys_file_name << endl;
475 if (_keys_file && *_keys_file) {
476 strm << BESIndent::LMarg <<
"key file is valid" << endl;
479 strm << BESIndent::LMarg <<
"key file is NOT valid" << endl;
481 if (_the_keys && _the_keys->size()) {
482 strm << BESIndent::LMarg <<
" keys:" << endl;
484 Keys_citer i = _the_keys->begin();
485 Keys_citer ie = _the_keys->end();
486 for (; i != ie; i++) {
487 strm << BESIndent::LMarg << (*i).first <<
":" << endl;
489 vector<string>::const_iterator v = (*i).second.begin();
490 vector<string>::const_iterator ve = (*i).second.end();
491 for (; v != ve; v++) {
492 strm << (*v) << endl;
494 BESIndent::UnIndent();
496 BESIndent::UnIndent();
499 strm << BESIndent::LMarg <<
"keys: none" << endl;
501 BESIndent::UnIndent();
exception thrown if an internal error is found and is fatal to the BES
mapping of key/value pairs defining different behaviors of an application.
virtual std::string get_message()
get the error message for this exception
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
void set_key(const string &key, const string &val, bool addto=false)
allows the user to set key/value pairs from within the application.
static void removeLeadingAndTrailingBlanks(string &key)
error thrown if there is a user syntax error in the request or any other user error ...
Abstract exception class for the BES with basic string message.
static TheBESKeys * TheKeys()
virtual ~TheBESKeys()
cleans up the key/value pair mapping
void get_values(const string &s, vector< string > &vals, bool &found)
Retrieve the values of a given key, if set.
virtual std::string get_file()
get the file name where the exception was thrown
virtual int get_line()
get the line number where the exception was thrown
virtual void dump(ostream &strm) const
dumps information about this object