38 #include <sys/types.h> 53 #include "BESFSFile.h" 54 #include "BESInternalFatalError.h" 55 #include "BESSyntaxUserError.h" 57 #define BES_INCLUDE_KEY "BES.Include" 59 vector<string> BESKeys::KeyList;
77 BESKeys::BESKeys(
const string &keys_file_name) :
78 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(0), _own_keys(true)
80 _the_keys =
new map<string, vector<string> > ;
84 BESKeys::BESKeys(
const string &keys_file_name, map<
string, vector<string> > *keys) :
85 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(keys), _own_keys(
false)
97 void BESKeys::initialize_keys()
99 _keys_file =
new ifstream(_keys_file_name.c_str());
104 getcwd(path,
sizeof(path));
105 string s = string(
"BES: fatal, cannot open BES configuration file ") + _keys_file_name +
": ";
106 char *err = strerror(myerrno);
110 s +=
"Unknown error";
112 s += (string)
".\n" +
"The current working directory is " + path +
"\n";
130 string s = (string)
"Undefined exception while trying to load keys " +
"from bes configuration file " + _keys_file_name;
135 void BESKeys::clean()
143 if (_the_keys && _own_keys)
156 bool BESKeys::LoadedKeys(
const string &key_file)
158 vector<string>::const_iterator i = BESKeys::KeyList.begin();
159 vector<string>::const_iterator e = BESKeys::KeyList.end();
162 if ((*i) == key_file)
170 void BESKeys::load_keys()
172 #if 0 // Replaced this use of character buffer code with the string based version below. ndp 09/09/2015 175 while (!(*_keys_file).eof())
177 if ((*_keys_file).getline(buffer, 255))
180 if (break_pair(buffer, key, value, addto))
182 if (key == BES_INCLUDE_KEY)
190 load_include_files(value);
202 string key, value, line;
203 while(!_keys_file->eof() )
206 getline( *_keys_file, line );
207 if (break_pair(line.c_str(), key, value, addto))
209 if (key == BES_INCLUDE_KEY)
215 load_include_files(value);
235 inline bool BESKeys::break_pair(
const char* b,
string& key,
string &value,
bool &addto)
239 if (b && (b[0] !=
'#') && (!only_blanks(b)))
241 register size_t l = strlen(b);
246 for (
register size_t j = 0; j < l && !done; j++)
254 if (pos != static_cast<int> (j - 1))
256 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name
257 +
" '+' character found in variable name" +
" or attempting '+=' with space" +
" between the characters.\n";
263 else if (b[j] ==
'+')
271 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name +
": " +
" '=' character not found.\n";
276 key = s.substr(0, pos);
279 value = s.substr(pos + 2, s.size());
281 value = s.substr(pos + 1, s.size());
302 void BESKeys::load_include_files(
const string &files)
309 if (!files.empty() && files[0] ==
'/')
311 newdir = allfiles.getDirName();
318 string currdir = currfile.getDirName();
320 string alldir = allfiles.getDirName();
322 if ((currdir ==
"./" || currdir ==
".") && (alldir ==
"./" || alldir ==
"."))
328 if (alldir ==
"./" || alldir ==
".")
334 newdir = currdir +
"/" + alldir;
341 BESFSDir fsd(newdir, allfiles.getFileName());
342 BESFSDir::fileIterator i = fsd.beginOfFileList();
343 BESFSDir::fileIterator e = fsd.endOfFileList();
346 load_include_file((*i).getFullPath());
356 void BESKeys::load_include_file(
const string &file)
361 if (!BESKeys::LoadedKeys(file))
363 BESKeys::KeyList.push_back(file);
368 bool BESKeys::only_blanks(
const char *line)
370 string my_line = line;
371 if (my_line.find_first_not_of(
" ") != string::npos)
379 string expr =
"[^[:space:]]";
380 val = regcomp(&rx, expr.c_str(), REG_ICASE);
384 string s = (string)
"Regular expression " + expr +
" did not compile correctly " +
" in configuration file " + _keys_file_name;
387 val = regexec(&rx, line, 0, 0, REG_NOTBOL);
395 if (val == REG_NOMATCH)
400 else if (val == REG_ESPACE)
402 string s = (string)
"Execution of regular expression out of space" +
" in configuration file " + _keys_file_name;
407 string s = (string)
"Execution of regular expression has unknown " +
" problem in configuration file " + _keys_file_name;
432 map<string, vector<string> >::iterator i;
433 i = _the_keys->find(key);
434 if (i == _the_keys->end())
437 (*_the_keys)[key] = vals;
440 (*_the_keys)[key].clear();
443 (*_the_keys)[key].push_back(val);
463 break_pair(pair.c_str(), key, val, addto);
484 map<string, vector<string> >::iterator i;
485 i = _the_keys->find(s);
486 if (i != _the_keys->end())
489 if ((*i).second.size() > 1)
491 string err = string(
"Multiple values for the key ") + s +
" found, should only be one.";
494 if ((*i).second.size() == 1)
496 val = (*i).second[0];
519 map<string, vector<string> >::iterator i;
520 i = _the_keys->find(s);
521 if (i != _the_keys->end())
536 strm << BESIndent::LMarg <<
"BESKeys::dump - (" << (
void *)
this <<
")" << endl;
538 strm << BESIndent::LMarg <<
"key file:" << _keys_file_name << endl;
539 if (_keys_file && *_keys_file)
541 strm << BESIndent::LMarg <<
"key file is valid" << endl;
545 strm << BESIndent::LMarg <<
"key file is NOT valid" << endl;
547 if (_the_keys && _the_keys->size())
549 strm << BESIndent::LMarg <<
" keys:" << endl;
551 Keys_citer i = _the_keys->begin();
552 Keys_citer ie = _the_keys->end();
555 strm << BESIndent::LMarg << (*i).first <<
":" << endl;
557 vector<string>::const_iterator v = (*i).second.begin();
558 vector<string>::const_iterator ve = (*i).second.end();
561 strm << (*v) << endl;
563 BESIndent::UnIndent();
565 BESIndent::UnIndent();
569 strm << BESIndent::LMarg <<
"keys: none" << endl;
571 BESIndent::UnIndent();
virtual void dump(ostream &strm) const
dumps information about this object
exception thrown if an internal error is found and is fatal to the BES
virtual ~BESKeys()
cleans up the key/value pair mapping
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