1 //
2 // HtRegex.cc
3 //
4 // HtRegex: A simple C++ wrapper class for the system regex routines.
5 //
6 // Part of the ht://Dig package   <http://www.htdig.org/>
7 // Copyright (c) 1999-2004 The ht://Dig Group
8 // For copyright details, see the file COPYING in your distribution
9 // or the GNU Library General Public License (LGPL) version 2 or later
10 // <http://www.gnu.org/copyleft/lgpl.html>
11 //
12 // $Id: HtRegex.cc,v 1.13 2004/05/28 13:15:21 lha Exp $
13 //
14 
15 #ifdef HAVE_CONFIG_H
16 #include "htconfig.h"
17 #endif /* HAVE_CONFIG_H */
18 
19 #include "HtRegex.h"
20 #include <locale.h>
21 
22 
HtRegex()23 HtRegex::HtRegex() : compiled(0) { }
24 
HtRegex(const char * str,int case_sensitive)25 HtRegex::HtRegex(const char *str, int case_sensitive) : compiled(0)
26 {
27         set(str, case_sensitive);
28 }
29 
~HtRegex()30 HtRegex::~HtRegex()
31 {
32 	if (compiled != 0) regfree(&re);
33 	compiled = 0;
34 }
35 
lastError()36 const String &HtRegex::lastError()
37 {
38 	return lastErrorMessage;
39 }
40 
41 int
set(const char * str,int case_sensitive)42 HtRegex::set(const char * str, int case_sensitive)
43 {
44   if (compiled != 0) regfree(&re);
45 
46   int err;
47   compiled = 0;
48   if (str == NULL) return 0;
49   if (strlen(str) <= 0) return 0;
50   if (err = regcomp(&re, str, case_sensitive ? REG_EXTENDED : (REG_EXTENDED|REG_ICASE)), err == 0)
51     {
52       compiled = 1;
53     }
54   else
55     {
56       size_t len = regerror(err, &re, 0, 0);
57       char *buf = new char[len];
58       regerror(err, &re, buf, len);
59       lastErrorMessage = buf;
60       delete buf;
61     }
62   return compiled;
63 }
64 
65 int
setEscaped(StringList & list,int case_sensitive)66 HtRegex::setEscaped(StringList &list, int case_sensitive)
67 {
68     String *str;
69     String transformedLimits;
70     list.Start_Get();
71     while ((str = (String *) list.Get_Next()))
72       {
73 	if (str->indexOf('[') == 0 && str->lastIndexOf(']') == str->length()-1)
74 	  {
75 	    transformedLimits << str->sub(1,str->length()-2).get();
76 	  }
77 	else 	// Backquote any regex special characters
78 	  {
79 	    for (int pos = 0; pos < str->length(); pos++)
80 	      {
81 		if (strchr("^.[$()|*+?{\\", str->Nth(pos)))
82 		  transformedLimits << '\\';
83 		transformedLimits << str->Nth(pos);
84 	      }
85 	  }
86 	transformedLimits << "|";
87       }
88     transformedLimits.chop(1);
89 
90     return set(transformedLimits, case_sensitive);
91 }
92 
93 int
match(const char * str,int nullpattern,int nullstr)94 HtRegex::match(const char * str, int nullpattern, int nullstr)
95 {
96 	int	rval;
97 
98 	if (compiled == 0) return(nullpattern);
99 	if (str == NULL) return(nullstr);
100 	if (strlen(str) <= 0) return(nullstr);
101 	rval = regexec(&re, str, (size_t) 0, NULL, 0);
102 	if (rval == 0) return(1);
103 	else return(0);
104 }
105 
106