00001
00002
00003 #include "sys/types.h"
00004 #include "sys/stat.h"
00005 #include "dirent.h"
00006 #include "stdio.h"
00007
00008 #include "BESCatalogUtils.h"
00009 #include "TheBESKeys.h"
00010 #include "BESException.h"
00011 #include "GNURegex.h"
00012
00013 map<string, BESCatalogUtils *> BESCatalogUtils::_instances ;
00014
00015 BESCatalogUtils::
00016 BESCatalogUtils( const string &n )
00017 {
00018 string key = "BES.Catalog." + n + ".RootDirectory" ;
00019 bool found = false ;
00020 _root_dir = TheBESKeys::TheKeys()->get_key( key, found ) ;
00021 if( !found || _root_dir == "" )
00022 {
00023 string s = key + " not defined in key file" ;
00024 throw BESException( s, __FILE__, __LINE__ ) ;
00025 }
00026 DIR *dip = opendir( _root_dir.c_str() ) ;
00027 if( dip == NULL )
00028 {
00029 string serr = "BESCatalogDirectory - root directory "
00030 + _root_dir + " does not exist" ;
00031 throw BESException( serr, __FILE__, __LINE__ ) ;
00032 }
00033 closedir( dip ) ;
00034
00035 key = (string)"BES.Catalog." + n + ".Exclude" ;
00036 string e_str = TheBESKeys::TheKeys()->get_key( key, found ) ;
00037 if( found && e_str != "" && e_str != ";" )
00038 {
00039 build_list( _exclude, e_str ) ;
00040 }
00041
00042 key = (string)"BES.Catalog." + n + ".Include" ;
00043 string i_str = TheBESKeys::TheKeys()->get_key( key, found ) ;
00044 if( found && i_str != "" && i_str != ";" )
00045 {
00046 build_list( _include, i_str ) ;
00047 }
00048
00049 key = "BES.Catalog." + n + ".TypeMatch" ;
00050 string curr_str = TheBESKeys::TheKeys()->get_key( key, found ) ;
00051 if( curr_str == "" )
00052 {
00053 string s = key + " not defined in key file" ;
00054 throw BESException( s, __FILE__, __LINE__ ) ;
00055 }
00056
00057 string::size_type str_begin = 0 ;
00058 string::size_type str_end = curr_str.length() ;
00059 string::size_type semi = 0 ;
00060 bool done = false ;
00061 while( done == false )
00062 {
00063 semi = curr_str.find( ";", str_begin ) ;
00064 if( semi == string::npos )
00065 {
00066 string s = (string)"Catalog type match malformed, no semicolon, "
00067 "looking for type:regexp;[type:regexp;]" ;
00068 throw BESException( s, __FILE__, __LINE__ ) ;
00069 }
00070 else
00071 {
00072 string a_pair = curr_str.substr( str_begin, semi-str_begin ) ;
00073 str_begin = semi+1 ;
00074 if( semi == str_end-1 )
00075 {
00076 done = true ;
00077 }
00078
00079 string::size_type col = a_pair.find( ":" ) ;
00080 if( col == string::npos )
00081 {
00082 string s = (string)"Catalog type match malformed, no colon, "
00083 + "looking for type:regexp;[type:regexp;]" ;
00084 throw BESException( s, __FILE__, __LINE__ ) ;
00085 }
00086 else
00087 {
00088 type_reg newval ;
00089 newval.type = a_pair.substr( 0, col ) ;
00090 newval.reg = a_pair.substr( col+1, a_pair.length()-col ) ;
00091 _match_list.push_back( newval ) ;
00092 }
00093 }
00094 }
00095 }
00096
00097 void
00098 BESCatalogUtils::build_list( list<string> &theList, const string &listStr )
00099 {
00100 string::size_type str_begin = 0 ;
00101 string::size_type str_end = listStr.length() ;
00102 string::size_type semi = 0 ;
00103 bool done = false ;
00104 while( done == false )
00105 {
00106 semi = listStr.find( ";", str_begin ) ;
00107 if( semi == string::npos )
00108 {
00109 string s = (string)"Catalog type match malformed, no semicolon, "
00110 "looking for type:regexp;[type:regexp;]" ;
00111 throw BESException( s, __FILE__, __LINE__ ) ;
00112 }
00113 else
00114 {
00115 string a_member = listStr.substr( str_begin, semi-str_begin ) ;
00116 str_begin = semi+1 ;
00117 if( semi == str_end-1 )
00118 {
00119 done = true ;
00120 }
00121 if( a_member != "" ) theList.push_back( a_member ) ;
00122 }
00123 }
00124 }
00125
00126 bool
00127 BESCatalogUtils::include( const string &inQuestion ) const
00128 {
00129 bool toInclude = false ;
00130
00131
00132
00133
00134 if( _include.size() == 0 )
00135 {
00136 toInclude = true ;
00137 }
00138 else
00139 {
00140 list<string>::const_iterator i_iter = _include.begin() ;
00141 list<string>::const_iterator i_end = _include.end() ;
00142 for( ; i_iter != i_end; i_iter++ )
00143 {
00144 string reg = *i_iter ;
00145 Regex reg_expr( reg.c_str() ) ;
00146 if( reg_expr.match( inQuestion.c_str(), inQuestion.length() ) != -1)
00147 {
00148 toInclude = true ;
00149 }
00150 }
00151 }
00152
00153 if( toInclude == true )
00154 {
00155 if( exclude( inQuestion ) )
00156 {
00157 toInclude = false ;
00158 }
00159 }
00160
00161 return toInclude ;
00162 }
00163
00164 bool
00165 BESCatalogUtils::exclude( const string &inQuestion ) const
00166 {
00167 list<string>::const_iterator e_iter = _exclude.begin() ;
00168 list<string>::const_iterator e_end = _exclude.end() ;
00169 for( ; e_iter != e_end; e_iter++ )
00170 {
00171 string reg = *e_iter ;
00172 Regex reg_expr( reg.c_str() ) ;
00173 if( reg_expr.match( inQuestion.c_str(), inQuestion.length() ) != -1)
00174 {
00175 return true ;
00176 }
00177 }
00178 return false ;
00179 }
00180
00181 BESCatalogUtils::match_citer
00182 BESCatalogUtils::match_list_begin() const
00183 {
00184 return _match_list.begin() ;
00185 }
00186
00187 BESCatalogUtils::match_citer
00188 BESCatalogUtils::match_list_end() const
00189 {
00190 return _match_list.end() ;
00191 }
00192
00193 void
00194 BESCatalogUtils::dump( ostream &strm ) const
00195 {
00196 strm << BESIndent::LMarg << "BESCatalogUtils::dump - ("
00197 << (void *)this << ")" << endl ;
00198 BESIndent::Indent() ;
00199
00200 strm << BESIndent::LMarg << "root directory: " << _root_dir << endl ;
00201
00202 if( _include.size() )
00203 {
00204 strm << BESIndent::LMarg << "include list:" << endl ;
00205 BESIndent::Indent() ;
00206 list<string>::const_iterator i_iter = _include.begin() ;
00207 list<string>::const_iterator i_end = _include.end() ;
00208 for( ; i_iter != i_end; i_iter++ )
00209 {
00210 strm << BESIndent::LMarg << *i_iter << endl ;
00211 }
00212 BESIndent::UnIndent() ;
00213 }
00214 else
00215 {
00216 strm << BESIndent::LMarg << "include list: empty" << endl ;
00217 }
00218
00219 if( _exclude.size() )
00220 {
00221 strm << BESIndent::LMarg << "exclude list:" << endl ;
00222 BESIndent::Indent() ;
00223 list<string>::const_iterator e_iter = _exclude.begin() ;
00224 list<string>::const_iterator e_end = _exclude.end() ;
00225 for( ; e_iter != e_end; e_iter++ )
00226 {
00227 strm << BESIndent::LMarg << *e_iter << endl ;
00228 }
00229 BESIndent::UnIndent() ;
00230 }
00231 else
00232 {
00233 strm << BESIndent::LMarg << "exclude list: empty" << endl ;
00234 }
00235
00236 if( _match_list.size() )
00237 {
00238 strm << BESIndent::LMarg << "type matches:" << endl ;
00239 BESIndent::Indent() ;
00240 BESCatalogUtils::match_citer i = _match_list.begin() ;
00241 BESCatalogUtils::match_citer ie = _match_list.end() ;
00242 for( ; i != ie; i++ )
00243 {
00244 type_reg match = (*i) ;
00245 strm << BESIndent::LMarg << match.type << " : "
00246 << match.reg << endl ;
00247 }
00248 BESIndent::UnIndent() ;
00249 }
00250 else
00251 {
00252 strm << BESIndent::LMarg << " type matches: empty" << endl ;
00253 }
00254
00255 BESIndent::UnIndent() ;
00256 }
00257
00258 const BESCatalogUtils *
00259 BESCatalogUtils::Utils( const string &cat_name )
00260 {
00261 BESCatalogUtils *utils = BESCatalogUtils::_instances[cat_name] ;
00262 if( !utils )
00263 {
00264 utils = new BESCatalogUtils( cat_name );
00265 BESCatalogUtils::_instances[cat_name] = utils ;
00266 }
00267 return utils ;
00268 }
00269