1 |
/*************************************************
|
2 |
* Perl-Compatible Regular Expressions *
|
3 |
*************************************************/
|
4 |
|
5 |
/*
|
6 |
This is a library of functions to support regular expressions whose syntax
|
7 |
and semantics are as close as possible to those of the Perl 5 language. See
|
8 |
the file Tech.Notes for some information on the internals.
|
9 |
|
10 |
Written by: Philip Hazel <ph10@cam.ac.uk>
|
11 |
|
12 |
Copyright (c) 1997-2001 University of Cambridge
|
13 |
|
14 |
-----------------------------------------------------------------------------
|
15 |
Permission is granted to anyone to use this software for any purpose on any
|
16 |
computer system, and to redistribute it freely, subject to the following
|
17 |
restrictions:
|
18 |
|
19 |
1. This software is distributed in the hope that it will be useful,
|
20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
22 |
|
23 |
2. The origin of this software must not be misrepresented, either by
|
24 |
explicit claim or by omission.
|
25 |
|
26 |
3. Altered versions must be plainly marked as such, and must not be
|
27 |
misrepresented as being the original software.
|
28 |
|
29 |
4. If PCRE is embedded in any software that is released under the GNU
|
30 |
General Purpose Licence (GPL), then the terms of that licence shall
|
31 |
supersede any condition above with which it is incompatible.
|
32 |
-----------------------------------------------------------------------------
|
33 |
*/
|
34 |
|
35 |
/* This module contains some convenience functions for extracting substrings
|
36 |
from the subject string after a regex match has succeeded. The original idea
|
37 |
for these functions came from Scott Wimer <scottw@cgibuilder.com>. */
|
38 |
|
39 |
|
40 |
/* Include the internals header, which itself includes Standard C headers plus
|
41 |
the external pcre header. */
|
42 |
|
43 |
#include "internal.h"
|
44 |
|
45 |
|
46 |
|
47 |
/*************************************************
|
48 |
* Copy captured string to given buffer *
|
49 |
*************************************************/
|
50 |
|
51 |
/* This function copies a single captured substring into a given buffer.
|
52 |
Note that we use memcpy() rather than strncpy() in case there are binary zeros
|
53 |
in the string.
|
54 |
|
55 |
Arguments:
|
56 |
subject the subject string that was matched
|
57 |
ovector pointer to the offsets table
|
58 |
stringcount the number of substrings that were captured
|
59 |
(i.e. the yield of the pcre_exec call, unless
|
60 |
that was zero, in which case it should be 1/3
|
61 |
of the offset table size)
|
62 |
stringnumber the number of the required substring
|
63 |
buffer where to put the substring
|
64 |
size the size of the buffer
|
65 |
|
66 |
Returns: if successful:
|
67 |
the length of the copied string, not including the zero
|
68 |
that is put on the end; can be zero
|
69 |
if not successful:
|
70 |
PCRE_ERROR_NOMEMORY (-6) buffer too small
|
71 |
PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
|
72 |
*/
|
73 |
|
74 |
int
|
75 |
pcre_copy_substring(const char *subject, int *ovector, int stringcount,
|
76 |
int stringnumber, char *buffer, int size)
|
77 |
{
|
78 |
int yield;
|
79 |
if (stringnumber < 0 || stringnumber >= stringcount)
|
80 |
return PCRE_ERROR_NOSUBSTRING;
|
81 |
stringnumber *= 2;
|
82 |
yield = ovector[stringnumber+1] - ovector[stringnumber];
|
83 |
if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
|
84 |
memcpy(buffer, subject + ovector[stringnumber], yield);
|
85 |
buffer[yield] = 0;
|
86 |
return yield;
|
87 |
}
|
88 |
|
89 |
|
90 |
|
91 |
/*************************************************
|
92 |
* Copy all captured strings to new store *
|
93 |
*************************************************/
|
94 |
|
95 |
/* This function gets one chunk of store and builds a list of pointers and all
|
96 |
of the captured substrings in it. A NULL pointer is put on the end of the list.
|
97 |
|
98 |
Arguments:
|
99 |
subject the subject string that was matched
|
100 |
ovector pointer to the offsets table
|
101 |
stringcount the number of substrings that were captured
|
102 |
(i.e. the yield of the pcre_exec call, unless
|
103 |
that was zero, in which case it should be 1/3
|
104 |
of the offset table size)
|
105 |
listptr set to point to the list of pointers
|
106 |
|
107 |
Returns: if successful: 0
|
108 |
if not successful:
|
109 |
PCRE_ERROR_NOMEMORY (-6) failed to get store
|
110 |
*/
|
111 |
|
112 |
int
|
113 |
pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
|
114 |
const char ***listptr)
|
115 |
{
|
116 |
int i;
|
117 |
int size = sizeof(char *);
|
118 |
int double_count = stringcount * 2;
|
119 |
char **stringlist;
|
120 |
char *p;
|
121 |
|
122 |
for (i = 0; i < double_count; i += 2)
|
123 |
size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;
|
124 |
|
125 |
stringlist = (char **)(pcre_malloc)(size);
|
126 |
if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
|
127 |
|
128 |
*listptr = (const char **)stringlist;
|
129 |
p = (char *)(stringlist + stringcount + 1);
|
130 |
|
131 |
for (i = 0; i < double_count; i += 2)
|
132 |
{
|
133 |
int len = ovector[i+1] - ovector[i];
|
134 |
memcpy(p, subject + ovector[i], len);
|
135 |
*stringlist++ = p;
|
136 |
p += len;
|
137 |
*p++ = 0;
|
138 |
}
|
139 |
|
140 |
*stringlist = NULL;
|
141 |
return 0;
|
142 |
}
|
143 |
|
144 |
|
145 |
|
146 |
/*************************************************
|
147 |
* Free store obtained by get_substring_list *
|
148 |
*************************************************/
|
149 |
|
150 |
/* This function exists for the benefit of people calling PCRE from non-C
|
151 |
programs that can call its functions, but not free() or (pcre_free)() directly.
|
152 |
|
153 |
Argument: the result of a previous pcre_get_substring_list()
|
154 |
Returns: nothing
|
155 |
*/
|
156 |
|
157 |
void
|
158 |
pcre_free_substring_list(const char **pointer)
|
159 |
{
|
160 |
(pcre_free)((void *)pointer);
|
161 |
}
|
162 |
|
163 |
|
164 |
|
165 |
/*************************************************
|
166 |
* Copy captured string to new store *
|
167 |
*************************************************/
|
168 |
|
169 |
/* This function copies a single captured substring into a piece of new
|
170 |
store
|
171 |
|
172 |
Arguments:
|
173 |
subject the subject string that was matched
|
174 |
ovector pointer to the offsets table
|
175 |
stringcount the number of substrings that were captured
|
176 |
(i.e. the yield of the pcre_exec call, unless
|
177 |
that was zero, in which case it should be 1/3
|
178 |
of the offset table size)
|
179 |
stringnumber the number of the required substring
|
180 |
stringptr where to put a pointer to the substring
|
181 |
|
182 |
Returns: if successful:
|
183 |
the length of the string, not including the zero that
|
184 |
is put on the end; can be zero
|
185 |
if not successful:
|
186 |
PCRE_ERROR_NOMEMORY (-6) failed to get store
|
187 |
PCRE_ERROR_NOSUBSTRING (-7) substring not present
|
188 |
*/
|
189 |
|
190 |
int
|
191 |
pcre_get_substring(const char *subject, int *ovector, int stringcount,
|
192 |
int stringnumber, const char **stringptr)
|
193 |
{
|
194 |
int yield;
|
195 |
char *substring;
|
196 |
if (stringnumber < 0 || stringnumber >= stringcount)
|
197 |
return PCRE_ERROR_NOSUBSTRING;
|
198 |
stringnumber *= 2;
|
199 |
yield = ovector[stringnumber+1] - ovector[stringnumber];
|
200 |
substring = (char *)(pcre_malloc)(yield + 1);
|
201 |
if (substring == NULL) return PCRE_ERROR_NOMEMORY;
|
202 |
memcpy(substring, subject + ovector[stringnumber], yield);
|
203 |
substring[yield] = 0;
|
204 |
*stringptr = substring;
|
205 |
return yield;
|
206 |
}
|
207 |
|
208 |
|
209 |
|
210 |
/*************************************************
|
211 |
* Free store obtained by get_substring *
|
212 |
*************************************************/
|
213 |
|
214 |
/* This function exists for the benefit of people calling PCRE from non-C
|
215 |
programs that can call its functions, but not free() or (pcre_free)() directly.
|
216 |
|
217 |
Argument: the result of a previous pcre_get_substring()
|
218 |
Returns: nothing
|
219 |
*/
|
220 |
|
221 |
void
|
222 |
pcre_free_substring(const char *pointer)
|
223 |
{
|
224 |
(pcre_free)((void *)pointer);
|
225 |
}
|
226 |
|
227 |
/* End of get.c */
|