WvStreams
wvregex.cc
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2004 Net Integration Technologies, Inc.
00004  * 
00005  * Implementation of regular expression support though libc
00006  */
00007 #include "wvregex.h"
00008 
00009 WvString WvRegex::__wvre_null_reg;
00010 
00011 const int WvRegex::default_cflags = WvRegex::EXTENDED;
00012 const int WvRegex::default_eflags = 0;
00013     
00014 void WvRegex::seterr(int errcode)
00015 {
00016     int error_desc_len = ::regerror(errcode, &preg, NULL, 0);
00017     if (error_desc_len > 0)
00018     {
00019         WvString error_desc;
00020         error_desc.setsize(error_desc_len);
00021         ::regerror(errcode, &preg, error_desc.edit(), error_desc_len);
00022         WvErrorBase::seterr_both(errcode, error_desc);
00023     }
00024     else WvErrorBase::seterr(errcode);
00025 }
00026 
00027 bool WvRegex::set(WvStringParm regex, int cflags)
00028 {
00029     if (have_preg) ::regfree(&preg);
00030 
00031     int errcode = ::regcomp(&preg, regex, cflags);
00032     if (errcode)
00033     {
00034         seterr(errcode);
00035         have_preg = false;
00036     }
00037     else have_preg = true;
00038     
00039     return have_preg;
00040 }
00041 
00042 WvRegex::~WvRegex()
00043 {
00044     if (have_preg) ::regfree(&preg);
00045 }
00046 
00047 bool WvRegex::match(WvStringParm string, int eflags,
00048         size_t nmatch, regmatch_t pmatch[]) const
00049 {
00050     if (!have_preg) return false;
00051 
00052     return ::regexec(&preg, string, nmatch, pmatch, eflags) == 0;
00053 }