WvStreams
wvregex.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2004 Net Integration Technologies, Inc.
4  *
5  * Implementation of regular expression support though libc
6  */
7 #include "wvregex.h"
8 
10 
11 const int WvRegex::default_cflags = WvRegex::EXTENDED;
12 const int WvRegex::default_eflags = 0;
13 
14 void WvRegex::seterr(int errcode)
15 {
16  int error_desc_len = ::regerror(errcode, &preg, NULL, 0);
17  if (error_desc_len > 0)
18  {
19  WvString error_desc;
20  error_desc.setsize(error_desc_len);
21  ::regerror(errcode, &preg, error_desc.edit(), error_desc_len);
22  WvErrorBase::seterr_both(errcode, error_desc);
23  }
24  else WvErrorBase::seterr(errcode);
25 }
26 
27 bool WvRegex::set(WvStringParm regex, int cflags)
28 {
29  if (have_preg) ::regfree(&preg);
30 
31  int errcode = ::regcomp(&preg, regex, cflags);
32  if (errcode)
33  {
34  seterr(errcode);
35  have_preg = false;
36  }
37  else have_preg = true;
38 
39  return have_preg;
40 }
41 
42 WvRegex::~WvRegex()
43 {
44  if (have_preg) ::regfree(&preg);
45 }
46 
47 bool WvRegex::match(WvStringParm string, int eflags,
48  size_t nmatch, regmatch_t pmatch[]) const
49 {
50  if (!have_preg) return false;
51 
52  return ::regexec(&preg, string, nmatch, pmatch, eflags) == 0;
53 }
WvString::edit
char * edit()
make the string editable, and return a non-const (char*)
Definition: wvstring.h:397
WvRegex::set
bool set(WvStringParm regex, int cflags=default_cflags)
Replace the current regex to match with a new one.
Definition: wvregex.cc:27
WvErrorBase::seterr
virtual void seterr(int _errnum)
Set the errnum variable – we have an error.
Definition: wverror.cc:144
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvRegex::__wvre_null_reg
static WvString __wvre_null_reg
Internal use only.
Definition: wvregex.h:142