42#include "BESInternalFatalError.h"
47#define BES_INCLUDE_KEY "BES.Include"
55 const std::string &keys_file_name,
56 set<string> &loaded_kvp_files,
57 std::unordered_map<std::string, std::vector<std::string> > &keystore);
59 bool only_blanks(
const char *line) {
60 string my_line = line;
61 if (my_line.find_first_not_of(
' ') != string::npos)
76 bool break_pair(
const char *b,
string &key,
string &value,
bool &addto) {
79 if (b && (b[0] !=
'#') && (!only_blanks(b))) {
84 for (
size_t j = 0; j < l && !done; j++) {
89 if (pos !=
static_cast<int>(j - 1)) {
90 string s = string(
"BES: Invalid entry ") + b +
91 " in configuration file "
92 +
" '+' character found in variable name" +
" or attempting '+=' with space"
93 +
" between the characters.\n";
94 throw BESInternalFatalError(s, __FILE__, __LINE__);
98 }
else if (b[j] ==
'+') {
104 string s = string(
"BES: Invalid entry ") + b +
" in configuration file, '=' character not found.\n";
105 throw BESInternalFatalError(s, __FILE__, __LINE__);
109 key = s.substr(0, pos);
112 value = s.substr(pos + 2, s.size());
114 value = s.substr(pos + 1, s.size());
130 void load_include_file(
132 set<string> &loaded_kvp_files,
133 std::unordered_map<std::string, std::vector<std::string> > &keystore
138 auto it = loaded_kvp_files.find(file);
140 if (it == loaded_kvp_files.end()) {
142 loaded_kvp_files.insert(file);
143 load_keys(file, loaded_kvp_files, keystore);
159 void load_include_files(
160 const string ¤t_keys_file_name,
161 const string &file_expr,
162 set<string> &loaded_kvp_files,
163 std::unordered_map<std::string, std::vector<std::string> > &keystore
166 BESFSFile allfiles(file_expr);
170 if (!file_expr.empty() && file_expr[0] ==
'/') {
171 newdir = allfiles.getDirName();
175 BESFSFile currfile(current_keys_file_name);
176 string currdir = currfile.getDirName();
178 string alldir = allfiles.getDirName();
180 if ((currdir ==
"./" || currdir ==
".") && (alldir ==
"./" || alldir ==
".")) {
183 if (alldir ==
"./" || alldir ==
".") {
186 newdir = currdir +
"/" + alldir;
193 BESFSDir fsd(newdir, allfiles.getFileName());
194 BESFSDir::fileIterator i = fsd.beginOfFileList();
195 BESFSDir::fileIterator e = fsd.endOfFileList();
196 for (; i != e; i++) {
197 string include_file = (*i).getFullPath();
198 load_include_file(include_file, loaded_kvp_files, keystore);
206 std::unordered_map<std::string, std::vector<std::string> > &keystore) {
208 auto i = keystore.find(key);
209 if (i == keystore.end()) {
211 keystore[key] = vals;
213 if (!addto) keystore[key].clear();
215 keystore[key].push_back(val);
220 const string ¤t_keys_file_name,
221 std::ifstream &keys_file,
222 set<string> &loaded_kvp_files,
223 std::unordered_map<std::string, std::vector<std::string> > &keystore ) {
225 string key, value, line;
226 while (!keys_file.eof()) {
228 getline(keys_file, line);
229 if (break_pair(line.c_str(), key, value, addto)) {
230 if (key == BES_INCLUDE_KEY) {
234 set_key(key, value,
true, keystore);
236 load_include_files(current_keys_file_name, value, loaded_kvp_files, keystore );
238 set_key(key, value, addto, keystore);
245 const std::string &keys_file_name,
246 set<string> &loaded_kvp_files,
247 std::unordered_map<std::string, std::vector<std::string> > &keystore
249 std::ifstream keys_file(keys_file_name.c_str());
253 getcwd(path,
sizeof(path));
254 string s = string(
"Cannot open configuration file '") + keys_file_name +
"': ";
255 const char *err = strerror(errno);
259 s +=
"Unknown error";
261 s += (string)
".\n" +
"The current working directory is " + path;
262 throw BESInternalFatalError(s, __FILE__, __LINE__);
266 loaded_kvp_files.insert(keys_file_name);
267 load_keys(keys_file_name, keys_file, loaded_kvp_files, keystore);
269 catch (
const BESError &e) {
272 catch (
const std::exception &e) {
273 string s = (string)
"Caught exception load keys from the BES configuration file '"
274 + keys_file_name +
"' message:" + e.what();
275 throw BESInternalFatalError(s, __FILE__, __LINE__);
280 const std::string &keys_file_name,
281 std::unordered_map<std::string, std::vector<std::string> > &keystore
283 set<string> loaded_kvp_files;
285 load_keys(keys_file_name, loaded_kvp_files, keystore);
unsigned int get_line() const
get the line number where the exception was thrown
std::string get_file() const
get the file name where the exception was thrown
std::string get_message() const
get the error message for this exception
static void removeLeadingAndTrailingBlanks(std::string &key)