39 #include <sys/types.h> 55 #include "BESFSFile.h" 56 #include "BESInternalFatalError.h" 57 #include "BESSyntaxUserError.h" 59 #define BES_INCLUDE_KEY "BES.Include" 61 vector<string> BESKeys::KeyList;
79 BESKeys::BESKeys(
const string &keys_file_name) :
80 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(0), _own_keys(true)
82 _the_keys =
new map<string, vector<string> > ;
86 BESKeys::BESKeys(
const string &keys_file_name, map<
string, vector<string> > *keys) :
87 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(keys), _own_keys(
false)
99 void BESKeys::initialize_keys()
101 _keys_file =
new ifstream(_keys_file_name.c_str());
106 getcwd(path,
sizeof(path));
107 string s = string(
"BES: fatal, cannot open BES configuration file ") + _keys_file_name +
": ";
108 char *err = strerror(errno);
112 s +=
"Unknown error";
114 s += (string)
".\n" +
"The current working directory is " + path +
"\n";
132 string s = (string)
"Undefined exception while trying to load keys " +
"from bes configuration file " + _keys_file_name;
137 void BESKeys::clean()
145 if (_the_keys && _own_keys)
158 bool BESKeys::LoadedKeys(
const string &key_file)
160 vector<string>::const_iterator i = BESKeys::KeyList.begin();
161 vector<string>::const_iterator e = BESKeys::KeyList.end();
164 if ((*i) == key_file)
172 void BESKeys::load_keys()
174 #if 0 // Replaced this use of character buffer code with the string based version below. ndp 09/09/2015 177 while (!(*_keys_file).eof())
179 if ((*_keys_file).getline(buffer, 255))
182 if (break_pair(buffer, key, value, addto))
184 if (key == BES_INCLUDE_KEY)
192 load_include_files(value);
204 string key, value, line;
205 while(!_keys_file->eof() )
208 getline( *_keys_file, line );
209 if (break_pair(line.c_str(), key, value, addto))
211 if (key == BES_INCLUDE_KEY)
217 load_include_files(value);
237 inline bool BESKeys::break_pair(
const char* b,
string& key,
string &value,
bool &addto)
241 if (b && (b[0] !=
'#') && (!only_blanks(b)))
243 register size_t l = strlen(b);
248 for (
register size_t j = 0; j < l && !done; j++)
256 if (pos != static_cast<int> (j - 1))
258 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name
259 +
" '+' character found in variable name" +
" or attempting '+=' with space" +
" between the characters.\n";
265 else if (b[j] ==
'+')
273 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name +
": " +
" '=' character not found.\n";
278 key = s.substr(0, pos);
281 value = s.substr(pos + 2, s.size());
283 value = s.substr(pos + 1, s.size());
304 void BESKeys::load_include_files(
const string &files)
311 if (!files.empty() && files[0] ==
'/')
313 newdir = allfiles.getDirName();
320 string currdir = currfile.getDirName();
322 string alldir = allfiles.getDirName();
324 if ((currdir ==
"./" || currdir ==
".") && (alldir ==
"./" || alldir ==
"."))
330 if (alldir ==
"./" || alldir ==
".")
336 newdir = currdir +
"/" + alldir;
343 BESFSDir fsd(newdir, allfiles.getFileName());
344 BESFSDir::fileIterator i = fsd.beginOfFileList();
345 BESFSDir::fileIterator e = fsd.endOfFileList();
348 load_include_file((*i).getFullPath());
358 void BESKeys::load_include_file(
const string &file)
363 if (!BESKeys::LoadedKeys(file))
365 BESKeys::KeyList.push_back(file);
370 bool BESKeys::only_blanks(
const char *line)
372 string my_line = line;
373 if (my_line.find_first_not_of(
" ") != string::npos)
381 string expr =
"[^[:space:]]";
382 val = regcomp(&rx, expr.c_str(), REG_ICASE);
386 string s = (string)
"Regular expression " + expr +
" did not compile correctly " +
" in configuration file " + _keys_file_name;
389 val = regexec(&rx, line, 0, 0, REG_NOTBOL);
397 if (val == REG_NOMATCH)
402 else if (val == REG_ESPACE)
404 string s = (string)
"Execution of regular expression out of space" +
" in configuration file " + _keys_file_name;
409 string s = (string)
"Execution of regular expression has unknown " +
" problem in configuration file " + _keys_file_name;
434 map<string, vector<string> >::iterator i;
435 i = _the_keys->find(key);
436 if (i == _the_keys->end())
439 (*_the_keys)[key] = vals;
442 (*_the_keys)[key].clear();
445 (*_the_keys)[key].push_back(val);
465 break_pair(pair.c_str(), key, val, addto);
486 map<string, vector<string> >::iterator i;
487 i = _the_keys->find(s);
488 if (i != _the_keys->end())
491 if ((*i).second.size() > 1)
493 string err = string(
"Multiple values for the key ") + s +
" found, should only be one.";
496 if ((*i).second.size() == 1)
498 val = (*i).second[0];
521 map<string, vector<string> >::iterator i;
522 i = _the_keys->find(s);
523 if (i != _the_keys->end())
538 strm << BESIndent::LMarg <<
"BESKeys::dump - (" << (
void *)
this <<
")" << endl;
540 strm << BESIndent::LMarg <<
"key file:" << _keys_file_name << endl;
541 if (_keys_file && *_keys_file)
543 strm << BESIndent::LMarg <<
"key file is valid" << endl;
547 strm << BESIndent::LMarg <<
"key file is NOT valid" << endl;
549 if (_the_keys && _the_keys->size())
551 strm << BESIndent::LMarg <<
" keys:" << endl;
553 Keys_citer i = _the_keys->begin();
554 Keys_citer ie = _the_keys->end();
557 strm << BESIndent::LMarg << (*i).first <<
":" << endl;
559 vector<string>::const_iterator v = (*i).second.begin();
560 vector<string>::const_iterator ve = (*i).second.end();
563 strm << (*v) << endl;
565 BESIndent::UnIndent();
567 BESIndent::UnIndent();
571 strm << BESIndent::LMarg <<
"keys: none" << endl;
573 BESIndent::UnIndent();
exception thrown if an internal error is found and is fatal to the BES
virtual ~BESKeys()
cleans up the key/value pair mapping
virtual void dump(ostream &strm) const
dumps information about this object
virtual std::string get_message()
get the error message for this exception
static void removeLeadingAndTrailingBlanks(string &key)
error thrown if there is a user syntax error in the request or any other user error ...
void set_key(const string &key, const string &val, bool addto=false)
allows the user to set key/value pairs from within the application.
mapping of key/value pairs defining different behaviors of an application.
Abstract exception class for the BES with basic string message.
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
virtual std::string get_file()
get the file name where the exception was thrown
void get_values(const string &s, vector< string > &vals, bool &found)
Retrieve the values of a given key, if set.
virtual int get_line()
get the line number where the exception was thrown