00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2011 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * RCS INFORMATION: 00011 * 00012 * $RCSfile: JRegex.C,v $ 00013 * $Author: johns $ $Locker: $ $State: Exp $ 00014 * $Revision: 1.12 $ $Date: 2010/12/16 04:08:20 $ 00015 * 00016 *************************************************************************** 00017 * DESCRIPTION: 00018 * Interface for performing regular expression pattern matching, 00019 * encapsulating the PCRE regular expression package. 00020 ***************************************************************************/ 00021 00022 #include "JRegex.h" 00023 #include "Inform.h" 00024 00025 JRegex::JRegex(const char *pattern, int) { 00026 if (pattern == NULL) { 00027 msgErr << "NULL pattern passed to JRegex!" << sendmsg; 00028 } 00029 else { 00030 const char *errptr; 00031 int erroffset; 00032 rpat = vmdpcre_compile(pattern, // the regex pattern string 00033 0, // options 00034 &errptr, // points to error message, if any 00035 &erroffset, // offset into line where error was found 00036 NULL); // Table pointer; NULL for use default 00037 if (rpat == NULL) { 00038 msgWarn << "JRegex: Error in pcre_compile, " << errptr << sendmsg; 00039 msgWarn << "Error in regex pattern begins with " << pattern+erroffset 00040 << sendmsg; 00041 } 00042 } 00043 } 00044 00045 JRegex::~JRegex() { 00046 vmdpcre_free(rpat); 00047 } 00048 00049 int JRegex::match(const char *str, int len) const { 00050 if (rpat==NULL) { 00051 // msgWarn << "JRegex::match: bad regex pattern, no match" << sendmsg; 00052 return -1; 00053 } 00054 int retval; 00055 retval=vmdpcre_exec(rpat, // my regex pattern 00056 NULL, // No extra study wisdom 00057 str, // subject of the search 00058 len, // strlen of str 00059 0, // offset at which to start finding substrings 00060 0, // options 00061 NULL, // return vector for location of substrings 00062 0); // size of return vector 00063 return retval; 00064 } 00065 00066 int JRegex::search(const char *str, int len, int &length, int start) { 00067 if (rpat==NULL) { 00068 // msgWarn << "JRegex::search: bad regex pattern, no match" << sendmsg; 00069 return -1; 00070 } 00071 int ovec[6], retval; 00072 retval=vmdpcre_exec(rpat, // my regex pattern 00073 NULL, // No extra study wisdom 00074 str, // subject of the search 00075 len, // strlen of str 00076 start, // offset at which to start finding substrings 00077 0, // options 00078 ovec, // return vector for location of substrings 00079 6); // size of return vector 00080 if (retval < 0) return retval; 00081 length = ovec[1]-ovec[0]; 00082 return ovec[0]; 00083 } 00084
1.2.14 written by Dimitri van Heesch,
© 1997-2002