File: search_8cc-source.html

package info (click to toggle)
libpcre%2B%2B 0.9.5-6.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 2,032 kB
  • ctags: 194
  • sloc: sh: 6,870; cpp: 835; makefile: 41
file content (163 lines) | stat: -rw-r--r-- 10,729 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>search.cc Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3-rc3 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center>
<hr><h1>search.cc</h1><a href="search_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*</span>
00002 <span class="comment"> *</span>
00003 <span class="comment"> *  This file  is part of the PCRE++ Class Library.</span>
00004 <span class="comment"> *</span>
00005 <span class="comment"> *  By  accessing  this software,  PCRE++, you  are  duly informed</span>
00006 <span class="comment"> *  of and agree to be  bound  by the  conditions  described below</span>
00007 <span class="comment"> *  in this notice:</span>
00008 <span class="comment"> *</span>
00009 <span class="comment"> *  This software product,  PCRE++,  is developed by Thomas Linden</span>
00010 <span class="comment"> *  and copyrighted (C) 2002-2003 by Thomas Linden,with all rights </span>
00011 <span class="comment"> *  reserved.</span>
00012 <span class="comment"> *</span>
00013 <span class="comment"> *  There  is no charge for PCRE++ software.  You can redistribute</span>
00014 <span class="comment"> *  it and/or modify it under the terms of the GNU  Lesser General</span>
00015 <span class="comment"> *  Public License, which is incorporated by reference herein.</span>
00016 <span class="comment"> *</span>
00017 <span class="comment"> *  PCRE++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,</span>
00018 <span class="comment"> *  OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that</span>
00019 <span class="comment"> *  the use of it will not infringe on any third party's intellec-</span>
00020 <span class="comment"> *  tual property rights.</span>
00021 <span class="comment"> *</span>
00022 <span class="comment"> *  You should have received a copy of the GNU Lesser General Public</span>
00023 <span class="comment"> *  License along with PCRE++.  Copies can also be obtained from:</span>
00024 <span class="comment"> *</span>
00025 <span class="comment"> *    http://www.gnu.org/licenses/lgpl.txt</span>
00026 <span class="comment"> *</span>
00027 <span class="comment"> *  or by writing to:</span>
00028 <span class="comment"> *</span>
00029 <span class="comment"> *  Free Software Foundation, Inc.</span>
00030 <span class="comment"> *  59 Temple Place, Suite 330</span>
00031 <span class="comment"> *  Boston, MA 02111-1307</span>
00032 <span class="comment"> *  USA</span>
00033 <span class="comment"> *</span>
00034 <span class="comment"> *  Or contact:</span>
00035 <span class="comment"> *</span>
00036 <span class="comment"> *   "Thomas Linden" &lt;tom@daemon.de&gt;</span>
00037 <span class="comment"> *</span>
00038 <span class="comment"> *</span>
00039 <span class="comment"> */</span>
00040 
00041 
00042 <span class="preprocessor">#include "<a class="code" href="pcre++_8h.html">pcre++.h</a>"</span>
00043 
00044 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00045 <span class="keyword">using</span> <span class="keyword">namespace </span>pcrepp;
00046 
00047 <span class="comment">/*</span>
00048 <span class="comment"> * the search interface to pcre</span>
00049 <span class="comment"> */</span>
00050 
00051 
00052 <span class="comment">/*</span>
00053 <span class="comment"> * compile the expression</span>
00054 <span class="comment"> */</span>
00055 <span class="keywordtype">void</span> Pcre::Compile(<span class="keywordtype">int</span> flags) {
00056   p_pcre       = pcre_compile((<span class="keywordtype">char</span> *)_expression.c_str(), flags,
00057                               (<span class="keyword">const</span> <span class="keywordtype">char</span> **)(&amp;err_str), &amp;erroffset, tables);
00058 
00059   <span class="keywordflow">if</span>(p_pcre == NULL) {
00060     <span class="comment">/* umh, that's odd, the parser should not fail at all */</span>
00061     string Error = err_str;
00062     <span class="keywordflow">throw</span> exception(<span class="stringliteral">"pcre_compile(..) failed: "</span> + Error + <span class="stringliteral">" at: "</span> + _expression.substr(erroffset));
00063   }
00064 
00065   <span class="comment">/* calculate the number of substrings we are willing to catch */</span>
00066   <span class="keywordtype">int</span> where;
00067   <span class="keywordtype">int</span> info = pcre_fullinfo( p_pcre, p_pcre_extra, PCRE_INFO_CAPTURECOUNT, &amp;where);
00068   <span class="keywordflow">if</span>(info == 0) {
00069     sub_len = (where +2) * 3; <span class="comment">/* see "man pcre" for the exact formula */</span>
00070   }
00071   <span class="keywordflow">else</span> {
00072     <span class="keywordflow">throw</span> exception(info);
00073   }
00074   reset();
00075 }
00076 
00077 
00078 
00079 
00080 <span class="comment">/*</span>
00081 <span class="comment"> * API methods</span>
00082 <span class="comment"> */</span>
<a name="l00083"></a><a class="code" href="classpcrepp_1_1Pcre.html#a9">00083</a> <span class="keywordtype">bool</span> Pcre::search(<span class="keyword">const</span> string&amp; stuff, <span class="keywordtype">int</span> OffSet){
00084   <span class="keywordflow">return</span> dosearch(stuff, OffSet);
00085 }
00086 
<a name="l00087"></a><a class="code" href="classpcrepp_1_1Pcre.html#a8">00087</a> <span class="keywordtype">bool</span> Pcre::search(<span class="keyword">const</span> string&amp; stuff){
00088   <span class="keywordflow">return</span> dosearch(stuff, 0);
00089 }
00090 
00091 <span class="keywordtype">bool</span> Pcre::dosearch(<span class="keyword">const</span> string&amp; stuff, <span class="keywordtype">int</span> OffSet){
00092   reset();
00093   <span class="keywordflow">if</span> (sub_vec != NULL)
00094     <span class="keyword">delete</span>[] sub_vec;
00095 
00096   sub_vec = <span class="keyword">new</span> <span class="keywordtype">int</span>[sub_len];
00097   <span class="keywordtype">int</span> num = pcre_exec(p_pcre, p_pcre_extra, (<span class="keywordtype">char</span> *)stuff.c_str(),
00098                         (int)stuff.length(), OffSet, 0, (<span class="keywordtype">int</span> *)sub_vec, sub_len);
00099 
00100   <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">"Pcre::dosearch(): pcre_exec() returned: "</span> &lt;&lt; num &lt;&lt; endl;
00101 
00102   <span class="keywordflow">if</span>(num &lt; 0) {
00103     <span class="comment">/* no match at all */</span>
00104     <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">" - no match"</span> &lt;&lt; endl;
00105     <span class="keywordflow">return</span> <span class="keyword">false</span>;
00106   }
00107   <span class="keywordflow">else</span> <span class="keywordflow">if</span>(num == 0) {
00108     <span class="comment">/* vector too small, there were too many substrings in stuff */</span>
00109     <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">" - too many substrings"</span> &lt;&lt; endl;
00110     <span class="keywordflow">return</span> <span class="keyword">false</span>;
00111   }
00112   <span class="keywordflow">else</span> <span class="keywordflow">if</span>(num == 1) {
00113     <span class="comment">/* we had a match, but without substrings */</span>
00114     <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">" - match without substrings"</span> &lt;&lt; endl;
00115     did_match = <span class="keyword">true</span>;
00116     num_matches = 0;
00117     <span class="keywordflow">return</span> <span class="keyword">true</span>;
00118   }
00119   <span class="keywordflow">else</span> <span class="keywordflow">if</span>(num &gt; 1) {
00120     <span class="comment">/* we had matching substrings */</span>
00121     <span class="keywordflow">if</span> (resultset != NULL)
00122       <span class="keyword">delete</span> resultset;
00123     resultset = <span class="keyword">new</span> vector&lt;string&gt;;
00124     <span class="keyword">const</span> <span class="keywordtype">char</span> **stringlist;
00125     did_match = <span class="keyword">true</span>;
00126     num_matches = num - 1;
00127 
00128     <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">" - match with "</span> &lt;&lt; num_matches &lt;&lt; <span class="stringliteral">" substrings"</span> &lt;&lt; endl;
00129 
00130     <span class="keywordtype">int</span> res = pcre_get_substring_list((<span class="keywordtype">char</span> *)stuff.c_str(), sub_vec, num, &amp;stringlist);
00131     <span class="keywordflow">if</span>(res == 0) {
00132       <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">"Pcre::dosearch(): matched substrings: "</span> &lt;&lt; endl;
00133       <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=1; i&lt;num; i++) {
00134         <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; string(stringlist[i]) &lt;&lt; endl;
00135         resultset-&gt;push_back(stringlist[i]);
00136       }
00137       pcre_free_substring_list(stringlist);
00138     }
00139     <span class="keywordflow">else</span> {
00140       <span class="keywordflow">throw</span> exception(res);
00141     }
00142     <span class="keywordflow">return</span> <span class="keyword">true</span>;
00143   }
00144   <span class="keywordflow">else</span> {
00145     <span class="comment">/* some other uncommon error occured */</span>
00146     <a class="code" href="pcre++_8h.html#a0">__pcredebug</a> &lt;&lt; <span class="stringliteral">" - uncommon error"</span> &lt;&lt; endl;
00147     <span class="keywordflow">return</span> <span class="keyword">false</span>;
00148   }
00149 }
</pre></div><hr><address style="align: right;"><small>Generated on Wed Aug 25 01:38:04 2004 for PCRE++ by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.3-rc3 </small></address>
</body>
</html>