45 #include "TheBESKeys.h" 48 #include "BESFSFile.h" 49 #include "BESInternalFatalError.h" 50 #include "BESSyntaxUserError.h" 52 #define BES_INCLUDE_KEY "BES.Include" 56 vector<string> TheBESKeys::KeyList;
63 if (_instance)
return _instance;
73 string try_ini =
"/usr/local/etc/bes/bes.conf";
74 if (access(try_ini.c_str(), R_OK) == 0) {
80 try_ini =
"/etc/bes/bes.conf";
81 if (access(try_ini.c_str(), R_OK) == 0) {
87 try_ini =
"/usr/etc/bes/bes.conf";
88 if (access(try_ini.c_str(), R_OK) == 0) {
112 TheBESKeys::TheBESKeys(
const string &keys_file_name) :
113 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(0), _own_keys(true)
115 _the_keys =
new map<string, vector<string> >;
119 TheBESKeys::TheBESKeys(
const string &keys_file_name, map<
string, vector<string> > *keys) :
120 _keys_file(0), _keys_file_name(keys_file_name), _the_keys(keys), _own_keys(false)
132 void TheBESKeys::initialize_keys()
134 _keys_file =
new ifstream(_keys_file_name.c_str());
136 if (!(*_keys_file)) {
138 getcwd(path,
sizeof(path));
139 string s = string(
"Cannot open BES configuration file '") + _keys_file_name +
"': ";
140 char *err = strerror(errno);
144 s +=
"Unknown error";
146 s += (string)
".\n" +
"The current working directory is " + path;
161 string s = (string)
"Undefined exception while trying to load keys from the BES configuration file '" 162 + _keys_file_name +
"'";
167 void TheBESKeys::clean()
174 if (_the_keys && _own_keys) {
186 bool TheBESKeys::LoadedKeys(
const string &key_file)
188 vector<string>::const_iterator i = TheBESKeys::KeyList.begin();
189 vector<string>::const_iterator e = TheBESKeys::KeyList.end();
190 for (; i != e; i++) {
191 if ((*i) == key_file) {
199 void TheBESKeys::load_keys()
201 string key, value, line;
202 while (!_keys_file->eof()) {
204 getline(*_keys_file, line);
205 if (break_pair(line.c_str(), key, value, addto)) {
206 if (key == BES_INCLUDE_KEY) {
211 load_include_files(value);
228 inline bool TheBESKeys::break_pair(
const char* b,
string& key,
string &value,
bool &addto)
232 if (b && (b[0] !=
'#') && (!only_blanks(b))) {
233 register size_t l = strlen(b);
237 for (
register size_t j = 0; j < l && !done; j++) {
242 if (pos != static_cast<int>(j - 1)) {
243 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name
244 +
" '+' character found in variable name" +
" or attempting '+=' with space" 245 +
" between the characters.\n";
251 else if (b[j] ==
'+') {
257 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name +
": " 258 +
" '=' character not found.\n";
263 key = s.substr(0, pos);
266 value = s.substr(pos + 2, s.size());
268 value = s.substr(pos + 1, s.size());
289 void TheBESKeys::load_include_files(
const string &files)
296 if (!files.empty() && files[0] ==
'/') {
297 newdir = allfiles.getDirName();
303 string currdir = currfile.getDirName();
305 string alldir = allfiles.getDirName();
307 if ((currdir ==
"./" || currdir ==
".") && (alldir ==
"./" || alldir ==
".")) {
311 if (alldir ==
"./" || alldir ==
".") {
315 newdir = currdir +
"/" + alldir;
322 BESFSDir fsd(newdir, allfiles.getFileName());
323 BESFSDir::fileIterator i = fsd.beginOfFileList();
324 BESFSDir::fileIterator e = fsd.endOfFileList();
325 for (; i != e; i++) {
326 load_include_file((*i).getFullPath());
336 void TheBESKeys::load_include_file(
const string &file)
341 if (!TheBESKeys::LoadedKeys(file)) {
342 TheBESKeys::KeyList.push_back(file);
347 bool TheBESKeys::only_blanks(
const char *line)
349 string my_line = line;
350 if (my_line.find_first_not_of(
" ") != string::npos)
374 map<string, vector<string> >::iterator i;
375 i = _the_keys->find(key);
376 if (i == _the_keys->end()) {
378 (*_the_keys)[key] = vals;
380 if (!addto) (*_the_keys)[key].clear();
382 (*_the_keys)[key].push_back(val);
402 break_pair(pair.c_str(), key, val, addto);
423 map<string, vector<string> >::iterator i;
424 i = _the_keys->find(s);
425 if (i != _the_keys->end()) {
427 if ((*i).second.size() > 1) {
428 string err = string(
"Multiple values for the key ") + s +
" found, should only be one.";
431 if ((*i).second.size() == 1) {
432 val = (*i).second[0];
454 map<string, vector<string> >::iterator i;
455 i = _the_keys->find(s);
456 if (i != _the_keys->end()) {
482 return (value ==
"true" || value ==
"yes"|| value ==
"on");
485 return default_value;
506 if (value[value.length() - 1] ==
'/') value.erase(value.length() - 1);
510 return default_value;
531 std::istringstream iss(value);
534 if (iss.eof() || iss.bad() || iss.fail())
535 return default_value;
540 return default_value;
552 strm << BESIndent::LMarg <<
"BESKeys::dump - (" << (
void *)
this <<
")" << endl;
554 strm << BESIndent::LMarg <<
"key file:" << _keys_file_name << endl;
555 if (_keys_file && *_keys_file) {
556 strm << BESIndent::LMarg <<
"key file is valid" << endl;
559 strm << BESIndent::LMarg <<
"key file is NOT valid" << endl;
561 if (_the_keys && _the_keys->size()) {
562 strm << BESIndent::LMarg <<
" keys:" << endl;
564 Keys_citer i = _the_keys->begin();
565 Keys_citer ie = _the_keys->end();
566 for (; i != ie; i++) {
567 strm << BESIndent::LMarg << (*i).first <<
": " ;
569 vector<string>::const_iterator v = (*i).second.begin();
570 vector<string>::const_iterator ve = (*i).second.end();
571 for (; v != ve; v++) {
577 BESIndent::UnIndent();
580 strm << BESIndent::LMarg <<
"keys: none" << endl;
582 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.
static string lowercase(const string &s)
std::string read_string_key(const std::string &key, const std::string &default_value)
Read a string-valued key from the bes.conf file.
int read_int_key(const std::string &key, int default_value)
Read an integer-valued key from the bes.conf file.
virtual std::string get_message()
get the error message for this exception
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
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 std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
bool read_bool_key(const std::string &key, bool default_value)
Read a boolean-valued key from the bes.conf file.
virtual std::string get_file()
get the file name where the exception was thrown
virtual void dump(std::ostream &strm) const
dumps information about this object
void set_key(const std::string &key, const std::string &val, bool addto=false)
allows the user to set key/value pairs from within the application.
static std::string ConfigFile
virtual int get_line()
get the line number where the exception was thrown