Go to the documentation of this file.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 #include <iostream>
00033 #include <string>
00034 #include <map>
00035
00036 using std::cout ;
00037 using std::endl ;
00038 using std::string ;
00039 using std::multimap ;
00040 using std::pair ;
00041
00042 #include "BESRegex.h"
00043 #include "BESError.h"
00044
00045 multimap<string,string> expressions ;
00046
00047 bool break_includes( const string &s, string &err ) ;
00048 bool break_types( const string &s, string &err ) ;
00049
00050 void
00051 usage( const string &prog )
00052 {
00053 cout << "Usage: " << prog << " include|exclude|type <regular_expression> <string_to_match>" << endl ;
00054 cout << " samples:" << endl ;
00055 cout << " besregtest include \"123456;\" 01234567 matches 6 of 8 characters" << endl ;
00056 cout << " besregtest include \"^123456$;\" 01234567 does not match" << endl ;
00057 cout << " besregtest include \"^123456$;\" 123456 matches all 6 of 6 characters" << endl ;
00058 cout << " besregtest include \".*\\.nc$;\" fnoc1.nc matches" << endl ;
00059 cout << " besregtest include \".*\\.nc$;\" fnoc1.ncd does not matche" << endl ;
00060 cout << " besregtest type \"nc:.*\\.nc$;nc:.*\\.nc\\.gz$;ff:.*\\.dat$;ff:.*\\.dat\\.gz$;\" fnoc1.nc matches type nc" << endl ;
00061 }
00062
00063 int
00064 main( int argc, char **argv )
00065 {
00066 if( argc != 4 )
00067 {
00068 usage( argv[0] ) ;
00069 return 1 ;
00070 }
00071
00072 string what = argv[1] ;
00073 if( what != "include" && what != "exclude" && what != "type" )
00074 {
00075 cout << "please specify either an Include or TypeMatch expression "
00076 << "by using include or type respectively as first parameter" << endl ;
00077 usage( argv[0] ) ;
00078 return 1 ;
00079 }
00080
00081 string err ;
00082 bool status = true ;
00083 if( what == "include" || what == "exclude" )
00084 status = break_includes( argv[2], err ) ;
00085 else
00086 status = break_types( argv[2], err ) ;
00087
00088 if( !status )
00089 {
00090 cout << err << endl ;
00091 usage( argv[0] ) ;
00092 return 1 ;
00093 }
00094
00095 string inQuestion( argv[3] ) ;
00096
00097 multimap<string,string>::const_iterator i = expressions.begin() ;
00098 multimap<string,string>::const_iterator ie = expressions.end() ;
00099
00100 for( ; i != ie; i++ )
00101 {
00102 string reg = (*i).second ;
00103 try
00104 {
00105 BESRegex reg_expr( reg.c_str() ) ;
00106 int result = reg_expr.match( inQuestion.c_str(), inQuestion.length() ) ;
00107 if( result != -1)
00108 {
00109 if( result == inQuestion.length() )
00110 {
00111 cout << "expression \"" << reg << "\" matches exactly" ;
00112 }
00113 else
00114 {
00115 cout << "expression \"" << reg << "\" matches " << result
00116 << " characters out of " << inQuestion.length()
00117 << " characters" ;
00118 }
00119 if( what == "type" )
00120 cout << ", type = " << (*i).first ;
00121 cout << endl ;
00122 }
00123 else
00124 {
00125 cout << "expression \"" << reg << "\" does not match" ;
00126 if( what == "type" )
00127 cout << " for type " << (*i).first ;
00128 cout << endl ;
00129 }
00130 }
00131 catch( BESError &e )
00132 {
00133 string serr = (string)"malformed regular expression \""
00134 + reg + "\": " + e.get_message() ;
00135 cout << serr << endl ;
00136 }
00137 }
00138
00139 return 0 ;
00140 }
00141
00142 bool
00143 break_includes( const string &listStr, string &err )
00144 {
00145 string::size_type str_begin = 0 ;
00146 string::size_type str_end = listStr.length() ;
00147 string::size_type semi = 0 ;
00148 bool done = false ;
00149 while( done == false )
00150 {
00151 semi = listStr.find( ";", str_begin ) ;
00152 if( semi == string::npos )
00153 {
00154 err = (string)"regular expression malformed, no semicolon" ;
00155 return false ;
00156 }
00157 else
00158 {
00159 string a_member = listStr.substr( str_begin, semi-str_begin ) ;
00160 str_begin = semi+1 ;
00161 if( semi == str_end-1 )
00162 {
00163 done = true ;
00164 }
00165 if( a_member != "" )
00166 {
00167 expressions.insert( pair<string,string>( "", a_member ) );
00168 }
00169 }
00170 }
00171
00172 return true ;
00173 }
00174
00175 bool
00176 break_types( const string &listStr, string &err )
00177 {
00178 string::size_type str_begin = 0 ;
00179 string::size_type str_end = listStr.length() ;
00180 string::size_type semi = 0 ;
00181 bool done = false ;
00182 while( done == false )
00183 {
00184 semi = listStr.find( ";", str_begin ) ;
00185 if( semi == string::npos )
00186 {
00187 err = (string)"type match malformed, no semicolon, "
00188 + "looking for type:regexp;[type:regexp;]" ;
00189 return false ;
00190 }
00191 else
00192 {
00193 string a_pair = listStr.substr( str_begin, semi-str_begin ) ;
00194 str_begin = semi+1 ;
00195 if( semi == str_end-1 )
00196 {
00197 done = true ;
00198 }
00199
00200 string::size_type col = a_pair.find( ":" ) ;
00201 if( col == string::npos )
00202 {
00203 err = (string)"Catalog type match malformed, no colon, "
00204 + "looking for type:regexp;[type:regexp;]" ;
00205 return false ;
00206 }
00207 else
00208 {
00209 string a_type = a_pair.substr( 0, col ) ;
00210 string a_reg = a_pair.substr( col+1, a_pair.length()-col ) ;
00211 expressions.insert( pair<string,string>( a_type, a_reg ) ) ;
00212 }
00213 }
00214 }
00215
00216 return true ;
00217 }
00218