ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Matcher.cpp
Revision: 115
Committed: 2004-03-16T22:53:38-08:00 (21 years, 3 months ago) by Douglas Thrift
File size: 1243 byte(s)
Log Message:
Hot!

File Contents

# User Rev Content
1 Douglas Thrift 113 // Bender
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Matcher.hpp"
8    
9 Douglas Thrift 115 Matcher::Matcher(const string& expression)
10     {
11     this->expression = NULL;
12    
13     (*this)(expression);
14     }
15    
16 Douglas Thrift 114 Matcher::~Matcher()
17     {
18 Douglas Thrift 115 if (this->expression != NULL)
19     {
20     pcre_free(this->expression);
21     }
22 Douglas Thrift 114 }
23    
24 Douglas Thrift 113 bool Matcher::match(const string& stuff)
25     {
26 Douglas Thrift 115 substrings.clear();
27    
28     if (this->expression != NULL)
29     {
30     int length;
31 Douglas Thrift 113
32 Douglas Thrift 115 pcre_fullinfo(expression, NULL, PCRE_INFO_CAPTURECOUNT, &length);
33    
34     int* substrings = new int[(++length *= 3)];
35     int count = pcre_exec(expression, NULL, stuff.c_str(), stuff.length(),
36     0, 0, substrings, length);
37    
38     if (count > 0)
39     {
40     char* substring = new char[stuff.length() + 1];
41    
42     for (int index = 0; index < count; index++)
43     {
44     pcre_copy_substring(stuff.c_str(), substrings, count, index,
45     substring, stuff.length() + 1);
46    
47     this->substrings.push_back(substring);
48     }
49    
50     delete [] substring;
51     delete [] substrings;
52     return true;
53     }
54    
55     delete [] substrings;
56     }
57    
58 Douglas Thrift 113 return false;
59     }
60 Douglas Thrift 114
61     Matcher& Matcher::operator()(const string& expression)
62     {
63 Douglas Thrift 115 substrings.clear();
64    
65     if (this->expression != NULL)
66     {
67     pcre_free(this->expression);
68     }
69 Douglas Thrift 114
70 Douglas Thrift 115 this->expression = pcre_compile(expression.c_str(), PCRE_MULTILINE, NULL,
71     NULL, NULL);
72    
73 Douglas Thrift 114 return *this;
74     }

Properties

Name Value
svn:eol-style native
svn:keywords Id