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