match.cc
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 #include "colselect.h"
00026
00027 namespace suplib {
00028
00044 bool
00045 match(
00046 const string& str,
00047 const string& pattern
00048 ) throw ( Exception ) {
00049
00050 pcre* re;
00051 pcre_extra* pe = NULL;
00052 const char* error;
00053 int options = 0;
00054 int erroffset;
00055 int rc;
00056 int* ovector = NULL;
00057 int ovecsize = 0;
00058
00059 if ( NULL == (re = pcre_compile( pattern.c_str( ),
00060 options,
00061 &error,
00062 &erroffset,
00063 NULL )) )
00064 throw( Exception( error ) );
00065
00066 if ( 0 > (rc = pcre_exec( re,
00067 pe,
00068 str.c_str( ),
00069 str.size( ),
00070 0,
00071 options,
00072 ovector,
00073 ovecsize
00074 )) ) {
00075 switch(rc) {
00076 case PCRE_ERROR_NOMATCH:
00077 break;
00078
00079 case PCRE_ERROR_NULL:
00080 throw( Exception( "NULL pattern or subject string." ) );
00081
00082 case PCRE_ERROR_BADOPTION:
00083 throw( Exception( "Bad option." ) );
00084
00085 case PCRE_ERROR_BADMAGIC:
00086 throw( Exception( "Bad magic number(possible junk regex pointer?)" ) );
00087
00088 case PCRE_ERROR_UNKNOWN_NODE:
00089 throw( Exception( "Was your compiled pattern overwritten?" ) );
00090
00091 case PCRE_ERROR_NOMEMORY:
00092 throw( Exception( "Out of memory." ) );
00093
00094 default:
00095 throw( Exception( "Unforeseen matching error." ) ) ;
00096
00097 }
00098
00099 return false;
00100
00101 }
00102
00103 return true;
00104
00105 }
00106
00107 }