38 #include <sys/types.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()
142 if (_the_keys && _own_keys)
155 bool BESKeys::LoadedKeys(
const string &key_file)
157 vector<string>::const_iterator i = BESKeys::KeyList.begin();
158 vector<string>::const_iterator e = BESKeys::KeyList.end();
161 if ((*i) == key_file)
169 void BESKeys::load_keys()
173 while (!(*_keys_file).eof())
175 if ((*_keys_file).getline(buffer, 255))
178 if (break_pair(buffer, key, value, addto))
188 load_include_files(value);
207 inline bool BESKeys::break_pair(
const char* b,
string& key,
string &value,
bool &addto)
211 if (b && (b[0] !=
'#') && (!only_blanks(b)))
213 register size_t l = strlen(b);
218 for (
register size_t j = 0; j < l && !done; j++)
226 if (pos != static_cast<int> (j - 1))
228 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name
229 +
" '+' character found in variable name" +
" or attempting '+=' with space" +
" between the characters.\n";
235 else if (b[j] ==
'+')
243 string s = string(
"BES: Invalid entry ") + b +
" in configuration file " + _keys_file_name +
": " +
" '=' character not found.\n";
248 key = s.substr(0, pos);
251 value = s.substr(pos + 2, s.size());
253 value = s.substr(pos + 1, s.size());
274 void BESKeys::load_include_files(
const string &files)
281 if (!files.empty() && files[0] ==
'/')
283 newdir = allfiles.getDirName();
290 string currdir = currfile.getDirName();
292 string alldir = allfiles.getDirName();
294 if ((currdir ==
"./" || currdir ==
".") && (alldir ==
"./" || alldir ==
"."))
300 if (alldir ==
"./" || alldir ==
".")
306 newdir = currdir +
"/" + alldir;
313 BESFSDir fsd(newdir, allfiles.getFileName());
318 load_include_file((*i).getFullPath());
328 void BESKeys::load_include_file(
const string &file)
333 if (!BESKeys::LoadedKeys(file))
335 BESKeys::KeyList.push_back(file);
340 bool BESKeys::only_blanks(
const char *line)
342 string my_line = line;
343 if (my_line.find_first_not_of(
" ") != string::npos)
351 string expr =
"[^[:space:]]";
352 val = regcomp(&rx, expr.c_str(), REG_ICASE);
356 string s = (string)
"Regular expression " + expr +
" did not compile correctly " +
" in configuration file " + _keys_file_name;
359 val = regexec(&rx, line, 0, 0, REG_NOTBOL);
367 if (val == REG_NOMATCH)
372 else if (val == REG_ESPACE)
374 string s = (string)
"Execution of regular expression out of space" +
" in configuration file " + _keys_file_name;
379 string s = (string)
"Execution of regular expression has unknown " +
" problem in configuration file " + _keys_file_name;
404 map<string, vector<string> >::iterator i;
405 i = _the_keys->find(key);
406 if (i == _the_keys->end())
409 (*_the_keys)[key] = vals;
412 (*_the_keys)[key].clear();
415 (*_the_keys)[key].push_back(val);
435 break_pair(pair.c_str(), key, val, addto);
456 map<string, vector<string> >::iterator i;
457 i = _the_keys->find(s);
458 if (i != _the_keys->end())
461 if ((*i).second.size() > 1)
463 string err = string(
"Multiple values for the key ") + s +
" found, should only be one.";
466 if ((*i).second.size() == 1)
468 val = (*i).second[0];
491 map<string, vector<string> >::iterator i;
492 i = _the_keys->find(s);
493 if (i != _the_keys->end())
508 strm <<
BESIndent::LMarg <<
"BESKeys::dump - (" << (
void *)
this <<
")" << endl;
511 if (_keys_file && *_keys_file)
519 if (_the_keys && _the_keys->size())
529 vector<string>::const_iterator v = (*i).second.begin();
530 vector<string>::const_iterator ve = (*i).second.end();
533 strm << (*v) << endl;