37 #include <sys/types.h> 56 #define BES_INCLUDE_KEY "BES.Include" 58 vector<string> BESKeys::KeyList ;
76 BESKeys::BESKeys(
const string &keys_file_name )
78 _keys_file_name( keys_file_name ),
82 _the_keys =
new map<string,vector<string> >;
86 BESKeys::BESKeys(
const string &
keys_file_name, map<
string,vector<string> > *keys)
103 BESKeys::initialize_keys( )
105 _keys_file =
new ifstream( _keys_file_name.c_str() ) ;
106 int myerrno = errno ;
110 getcwd( path,
sizeof( path ) ) ;
111 string s = string(
"BES: fatal, cannot open BES configuration file ")
112 + _keys_file_name +
": " ;
113 char *err = strerror( myerrno ) ;
117 s +=
"Unknown error" ;
119 s += (string)
".\n" +
"The current working directory is " + path +
"\n" ;
138 string s = (string)
"Undefined exception while trying to load keys " 139 +
"from bes configuration file " + _keys_file_name ;
149 _keys_file->close() ;
152 if( _the_keys && _own_keys )
166 BESKeys::LoadedKeys(
const string &key_file )
168 vector<string>::const_iterator i = BESKeys::KeyList.begin() ;
169 vector<string>::const_iterator e = BESKeys::KeyList.end() ;
172 if( (*i) == key_file )
185 while(!(*_keys_file).eof())
187 if((*_keys_file).getline(buffer,255))
190 if( break_pair( buffer, key, value, addto ) )
194 load_include_files( value ) ;
214 BESKeys::break_pair(
const char* b,
string& key,
string &value,
bool &addto )
218 if( b && ( b[0] !=
'#' ) && ( !only_blanks( b ) ) )
220 register size_t l = strlen( b ) ;
225 for(
register size_t j = 0; j < l && !done ; j++ )
229 if( !addto ) pos = j ;
232 if( pos != static_cast<int>(j-1) )
234 string s = string(
"BES: Invalid entry ") + b
235 +
" in configuration file " 237 +
" '+' character found in variable name" 238 +
" or attempting '+=' with space" 239 +
" between the characters.\n" ;
245 else if( b[j] ==
'+' )
253 string s = string(
"BES: Invalid entry ") + b
254 +
" in configuration file " 255 + _keys_file_name +
": " 256 +
" '=' character not found.\n" ;
261 key = s.substr( 0, pos ) ;
263 if( addto ) value = s.substr( pos+2, s.size() ) ;
264 else value = s.substr( pos+1, s.size() ) ;
286 BESKeys::load_include_files(
const string &files )
293 if( !files.empty() && files[0] ==
'/' )
306 if( ( currdir ==
"./" || currdir ==
"." )
307 && ( alldir ==
"./" || alldir ==
"." ) )
313 if( alldir ==
"./" || alldir ==
"." )
319 newdir = currdir +
"/" + alldir ;
331 load_include_file( (*i).getFullPath() ) ;
342 BESKeys::load_include_file(
const string &file )
347 if( !BESKeys::LoadedKeys( file ) )
349 BESKeys::KeyList.push_back( file ) ;
350 BESKeys tmp( file, _the_keys ) ;
355 BESKeys::only_blanks(
const char *line)
359 string expr =
"[^[:space:]]" ;
360 val = regcomp( &rx, expr.c_str(), REG_ICASE ) ;
364 string s = (string)
"Regular expression " + expr
365 +
" did not compile correctly " 366 +
" in configuration file " + _keys_file_name ;
369 val = regexec( &rx, line, 0, 0, REG_NOTBOL ) ;
377 if( val == REG_NOMATCH )
382 else if( val == REG_ESPACE )
384 string s = (string)
"Execution of regular expression out of space" 385 +
" in configuration file " + _keys_file_name ;
390 string s = (string)
"Execution of regular expression has unknown " 391 +
" problem in configuration file " + _keys_file_name ;
416 map<string,vector<string> >::iterator i ;
417 i = _the_keys->find( key ) ;
418 if( i == _the_keys->end() )
420 vector<string> vals ;
421 (*_the_keys)[key] = vals ;
423 if( !addto ) (*_the_keys)[key].clear() ;
426 (*_the_keys)[key].push_back( val ) ;
447 break_pair( pair.c_str(), key, val, addto ) ;
469 map<string,vector<string> >::iterator i ;
470 i = _the_keys->find( s ) ;
471 if( i != _the_keys->end() )
474 if( (*i).second.size() > 1 )
476 string err = string(
"Multiple values for the key ") + s
477 +
" found, should only be one." ;
480 if( (*i).second.size() == 1 )
482 val = (*i).second[0] ;
506 map<string,vector<string> >::iterator i ;
507 i = _the_keys->find( s ) ;
508 if( i != _the_keys->end() )
525 << (
void *)
this <<
")" << endl ;
528 if( _keys_file && *_keys_file )
536 if( _the_keys && _the_keys->size() )
542 for( ; i != ie; i++ )
546 vector<string>::const_iterator v = (*i).second.begin() ;
547 vector<string>::const_iterator ve = (*i).second.end() ;
548 for( ; v != ve; v++ )
550 strm << (*v) << endl ;
exception thrown if an internal error is found and is fatal to the BES
~BESKeys()
cleans up the key/value pair mapping
virtual void dump(ostream &strm) const
dumps information about this object
static void removeLeadingAndTrailingBlanks(string &key)
remove leading and trailing blanks from a string
error thrown if there is a user syntax error in the request or any other user error ...
virtual string get_file()
get the file name where the exception was thrown
virtual string get_message()
get the error message for this exception
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.
static ostream & LMarg(ostream &strm)
virtual string getDirName()
list< BESFSFile >::iterator fileIterator
virtual string getFileName()
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
void get_values(const string &s, vector< string > &vals, bool &found)
Retrieve the values of a given key, if set.
virtual BESFSDir::fileIterator beginOfFileList()
map< string, vector< string > >::const_iterator Keys_citer
virtual int get_line()
get the line number where the exception was thrown