/[pcre]/code/trunk/get.c
ViewVC logotype

Contents of /code/trunk/get.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 29 - (hide annotations) (download)
Sat Feb 24 21:38:53 2007 UTC (7 years, 9 months ago) by nigel
File MIME type: text/plain
File size: 6466 byte(s)
Load pcre-2.03 into code/trunk.

1 nigel 29 /*************************************************
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-1999 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     * Copy captured string to new store *
148     *************************************************/
149    
150     /* This function copies a single captured substring into a piece of new
151     store
152    
153     Arguments:
154     subject the subject string that was matched
155     ovector pointer to the offsets table
156     stringcount the number of substrings that were captured
157     (i.e. the yield of the pcre_exec call, unless
158     that was zero, in which case it should be 1/3
159     of the offset table size)
160     stringnumber the number of the required substring
161     stringptr where to put a pointer to the substring
162    
163     Returns: if successful:
164     the length of the string, not including the zero that
165     is put on the end; can be zero
166     if not successful:
167     PCRE_ERROR_NOMEMORY (-6) failed to get store
168     PCRE_ERROR_NOSUBSTRING (-7) substring not present
169     */
170    
171     int
172     pcre_get_substring(const char *subject, int *ovector, int stringcount,
173     int stringnumber, const char **stringptr)
174     {
175     int yield;
176     char *substring;
177     if (stringnumber < 0 || stringnumber >= stringcount)
178     return PCRE_ERROR_NOSUBSTRING;
179     stringnumber *= 2;
180     yield = ovector[stringnumber+1] - ovector[stringnumber];
181     substring = (char *)(pcre_malloc)(yield + 1);
182     if (substring == NULL) return PCRE_ERROR_NOMEMORY;
183     memcpy(substring, subject + ovector[stringnumber], yield);
184     substring[yield] = 0;
185     *stringptr = substring;
186     return yield;
187     }
188    
189     /* End of get.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12