00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #include <sys/types.h>
00036 #include "regex.h"
00037 }
00038 #endif
00039
00040 #include <stdio.h>
00041 #include <unistd.h>
00042 #include <iostream>
00043
00044 using std::endl ;
00045 using std::cout ;
00046
00047 #include "BESKeys.h"
00048 #include "BESKeysException.h"
00049
00066 BESKeys::BESKeys( const string &keys_file_name )
00067 : _keys_file( 0 ),
00068 _keys_file_name( keys_file_name ),
00069 _the_keys( 0 )
00070 {
00071 _keys_file = new ifstream( _keys_file_name.c_str() ) ;
00072 if( !(*_keys_file) )
00073 {
00074 char path[500] ;
00075 getcwd( path, sizeof( path ) ) ;
00076 string s = string("BES: fatal, can not open initialization file ")
00077 + _keys_file_name + "\n"
00078 + "The current working directory is " + path + "\n" ;
00079 throw BESKeysException( s, __FILE__, __LINE__ ) ;
00080 }
00081
00082 _the_keys = new map<string,string>;
00083 try
00084 {
00085 load_keys();
00086 }
00087 catch(BESKeysException &ex)
00088 {
00089 clean();
00090 throw;
00091 }
00092 catch(...)
00093 {
00094 clean() ;
00095 string s = (string)"Undefined exception while trying to load keys "
00096 + "from bes configuration file " + _keys_file_name ;
00097 throw BESKeysException( s, __FILE__, __LINE__ ) ;
00098 }
00099 }
00100
00103 BESKeys::~BESKeys()
00104 {
00105 clean() ;
00106 }
00107
00108 void
00109 BESKeys::clean()
00110 {
00111 if( _keys_file )
00112 {
00113 _keys_file->close() ;
00114 delete _keys_file ;
00115 }
00116 if( _the_keys )
00117 {
00118 delete _the_keys ;
00119 }
00120 }
00121
00122 inline void
00123 BESKeys::load_keys()
00124 {
00125 char buffer[255];
00126 string key,value;
00127 while(!(*_keys_file).eof())
00128 {
00129 if((*_keys_file).getline(buffer,255))
00130 {
00131 if( break_pair( buffer, key, value ) )
00132 {
00133 (*_the_keys)[key]=value;
00134 }
00135 }
00136 }
00137 }
00138
00139 inline bool
00140 BESKeys::break_pair(const char* b, string& key, string &value)
00141 {
00142 if((b[0]!='#') && (!only_blanks(b)))
00143 {
00144 register size_t l=strlen(b);
00145 if(l>1)
00146 {
00147 register int how_many_equals=0;
00148 int pos=0;
00149 for (register size_t j=0;j<l;j++)
00150 {
00151 if(b[j] == '=')
00152 {
00153 how_many_equals++;
00154 pos=j;
00155 }
00156 }
00157
00158 if(how_many_equals!=1)
00159 {
00160 char howmany[256] ;
00161 sprintf( howmany, "%d", how_many_equals ) ;
00162 string s = string( "BES: invalid entry " ) + b
00163 + "; there are " + howmany
00164 + " = characters.\n";
00165 throw BESKeysException( s, __FILE__, __LINE__ );
00166 }
00167 else
00168 {
00169 string s=b;
00170 key=s.substr(0,pos);
00171 removeLeadingAndTrailingBlanks( key ) ;
00172 value=s.substr(pos+1,s.size());
00173 removeLeadingAndTrailingBlanks( value ) ;
00174
00175 return true;
00176 }
00177 }
00178
00179 return false;
00180 }
00181
00182 return false;
00183 }
00184
00185 void
00186 BESKeys::removeLeadingAndTrailingBlanks( string &key )
00187 {
00188 if( !key.empty() )
00189 {
00190 string::size_type first = key.find_first_not_of( " " ) ;
00191 string::size_type last = key.find_last_not_of( " " ) ;
00192 if( first == string::npos ) first = 0 ;
00193 if( last == string::npos ) last = key.length() ;
00194 string::size_type num = last - first + 1 ;
00195 key = key.substr( first, num ) ;
00196 }
00197 }
00198
00199 bool
00200 BESKeys::only_blanks(const char *line)
00201 {
00202 int val;
00203 regex_t rx;
00204 string expr = "[^[:space:]]" ;
00205 val = regcomp( &rx, expr.c_str(), REG_ICASE ) ;
00206
00207 if( val != 0 )
00208 {
00209 string s = (string)"Regular expression " + expr
00210 + " did not compile correctly" ;
00211 throw BESKeysException( s, __FILE__, __LINE__ ) ;
00212 }
00213 val = regexec( &rx, line, 0, 0, REG_NOTBOL ) ;
00214 if( val == 0 )
00215 {
00216 regfree( &rx ) ;
00217 return false ;
00218 }
00219 else
00220 {
00221 if( val == REG_NOMATCH )
00222 {
00223 regfree( &rx ) ;
00224 return true ;
00225 }
00226 else if( val == REG_ESPACE )
00227 {
00228 string s = "Execution of regular expression out of space" ;
00229 throw BESKeysException( s, __FILE__, __LINE__ ) ;
00230 }
00231 else
00232 {
00233 string s = "Execution of regular expression has unknown problem" ;
00234 throw BESKeysException( s, __FILE__, __LINE__ ) ;
00235 }
00236 }
00237 }
00238
00253 string
00254 BESKeys::set_key( const string &key, const string &val )
00255 {
00256 map< string, string >::iterator i ;
00257 i = _the_keys->find( key ) ;
00258 if( i == _the_keys->end() )
00259 {
00260 (*_the_keys)[key] = val ;
00261 return val ;
00262 }
00263 else
00264 {
00265 (*i).second = val ;
00266 return val ;
00267 }
00268 return "" ;
00269 }
00270
00283 string
00284 BESKeys::set_key( const string &pair )
00285 {
00286 string key ;
00287 string val ;
00288 break_pair( pair.c_str(), key, val ) ;
00289 return set_key( key, val ) ;
00290 }
00291
00303 string
00304 BESKeys::get_key( const string& s, bool &found )
00305 {
00306 map<string,string>::iterator i;
00307 i=_the_keys->find(s);
00308 if(i!=_the_keys->end())
00309 {
00310 found = true ;
00311 return (*i).second;
00312 }
00313 else
00314 {
00315 found = false ;
00316 return "";
00317 }
00318 }
00319
00329 void
00330 BESKeys::show_keys()
00331 {
00332 map<string,string>::iterator i ;
00333 for( i= _the_keys->begin(); i != _the_keys->end(); ++i )
00334 {
00335 cout << "key: \"" << (*i).first
00336 << "\", value: \"" << (*i).second << "\""
00337 << endl ;
00338 }
00339 }
00340
00347 void
00348 BESKeys::dump( ostream &strm ) const
00349 {
00350 strm << BESIndent::LMarg << "BESKeys::dump - ("
00351 << (void *)this << ")" << endl ;
00352 BESIndent::Indent() ;
00353 strm << BESIndent::LMarg << "key file:" << _keys_file_name << endl ;
00354 if( _keys_file && *_keys_file )
00355 {
00356 strm << BESIndent::LMarg << "key file is valid" << endl ;
00357 }
00358 else
00359 {
00360 strm << BESIndent::LMarg << "key file is NOT valid" << endl ;
00361 }
00362 if( _the_keys && _the_keys->size() )
00363 {
00364 strm << BESIndent::LMarg << " keys:" << endl ;
00365 BESIndent::Indent() ;
00366 Keys_citer i = _the_keys->begin() ;
00367 Keys_citer ie = _the_keys->end() ;
00368 for( ; i != ie; i++ )
00369 {
00370 strm << BESIndent::LMarg << (*i).first << ": "
00371 << (*i).second << endl ;
00372 }
00373 BESIndent::UnIndent() ;
00374 }
00375 else
00376 {
00377 strm << BESIndent::LMarg << "keys: none" << endl ;
00378 }
00379 BESIndent::UnIndent() ;
00380 }
00381