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

Diff of /code/trunk/get.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 53 by nigel, Sat Feb 24 21:39:42 2007 UTC revision 75 by nigel, Sat Feb 24 21:40:37 2007 UTC
# Line 9  the file Tech.Notes for some information Line 9  the file Tech.Notes for some information
9    
10  Written by: Philip Hazel <ph10@cam.ac.uk>  Written by: Philip Hazel <ph10@cam.ac.uk>
11    
12             Copyright (c) 1997-2001 University of Cambridge             Copyright (c) 1997-2003 University of Cambridge
13    
14  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
15  Permission is granted to anyone to use this software for any purpose on any  Redistribution and use in source and binary forms, with or without
16  computer system, and to redistribute it freely, subject to the following  modification, are permitted provided that the following conditions are met:
17  restrictions:  
18        * Redistributions of source code must retain the above copyright notice,
19  1. This software is distributed in the hope that it will be useful,        this list of conditions and the following disclaimer.
20     but WITHOUT ANY WARRANTY; without even the implied warranty of  
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.      * Redistributions in binary form must reproduce the above copyright
22          notice, this list of conditions and the following disclaimer in the
23  2. The origin of this software must not be misrepresented, either by        documentation and/or other materials provided with the distribution.
24     explicit claim or by omission.  
25        * Neither the name of the University of Cambridge nor the names of its
26  3. Altered versions must be plainly marked as such, and must not be        contributors may be used to endorse or promote products derived from
27     misrepresented as being the original software.        this software without specific prior written permission.
28    
29  4. If PCRE is embedded in any software that is released under the GNU  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30     General Purpose Licence (GPL), then the terms of that licence shall  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31     supersede any condition above with which it is incompatible.  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39    POSSIBILITY OF SUCH DAMAGE.
40  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
41  */  */
42    
43  /* This module contains some convenience functions for extracting substrings  /* This module contains some convenience functions for extracting substrings
44  from the subject string after a regex match has succeeded. The original idea  from the subject string after a regex match has succeeded. The original idea
45  for these functions came from Scott Wimer <scottw@cgibuilder.com>. */  for these functions came from Scott Wimer. */
46    
47    
48  /* Include the internals header, which itself includes Standard C headers plus  /* Include the internals header, which itself includes Standard C headers plus
# Line 43  the external pcre header. */ Line 51  the external pcre header. */
51  #include "internal.h"  #include "internal.h"
52    
53    
54    /*************************************************
55    *           Find number for named string         *
56    *************************************************/
57    
58    /* This function is used by the two extraction functions below, as well
59    as being generally available.
60    
61    Arguments:
62      code        the compiled regex
63      stringname  the name whose number is required
64    
65    Returns:      the number of the named parentheses, or a negative number
66                    (PCRE_ERROR_NOSUBSTRING) if not found
67    */
68    
69    int
70    pcre_get_stringnumber(const pcre *code, const char *stringname)
71    {
72    int rc;
73    int entrysize;
74    int top, bot;
75    uschar *nametable;
76    
77    if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
78      return rc;
79    if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
80    
81    if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
82      return rc;
83    if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
84      return rc;
85    
86    bot = 0;
87    while (top > bot)
88      {
89      int mid = (top + bot) / 2;
90      uschar *entry = nametable + entrysize*mid;
91      int c = strcmp(stringname, (char *)(entry + 2));
92      if (c == 0) return (entry[0] << 8) + entry[1];
93      if (c > 0) bot = mid + 1; else top = mid;
94      }
95    
96    return PCRE_ERROR_NOSUBSTRING;
97    }
98    
99    
100    
101  /*************************************************  /*************************************************
102  *      Copy captured string to given buffer      *  *      Copy captured string to given buffer      *
# Line 89  return yield; Line 143  return yield;
143    
144    
145  /*************************************************  /*************************************************
146    *   Copy named captured string to given buffer   *
147    *************************************************/
148    
149    /* This function copies a single captured substring into a given buffer,
150    identifying it by name.
151    
152    Arguments:
153      code           the compiled regex
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      stringname     the name of the required substring
161      buffer         where to put the substring
162      size           the size of the buffer
163    
164    Returns:         if successful:
165                       the length of the copied string, not including the zero
166                       that is put on the end; can be zero
167                     if not successful:
168                       PCRE_ERROR_NOMEMORY (-6) buffer too small
169                       PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
170    */
171    
172    int
173    pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,
174      int stringcount, const char *stringname, char *buffer, int size)
175    {
176    int n = pcre_get_stringnumber(code, stringname);
177    if (n <= 0) return n;
178    return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
179    }
180    
181    
182    
183    /*************************************************
184  *      Copy all captured strings to new store    *  *      Copy all captured strings to new store    *
185  *************************************************/  *************************************************/
186    
# Line 207  return yield; Line 299  return yield;
299    
300    
301    
302    /*************************************************
303    *   Copy named captured string to new store      *
304    *************************************************/
305    
306    /* This function copies a single captured substring, identified by name, into
307    new store.
308    
309    Arguments:
310      code           the compiled regex
311      subject        the subject string that was matched
312      ovector        pointer to the offsets table
313      stringcount    the number of substrings that were captured
314                       (i.e. the yield of the pcre_exec call, unless
315                       that was zero, in which case it should be 1/3
316                       of the offset table size)
317      stringname     the name of the required substring
318      stringptr      where to put the pointer
319    
320    Returns:         if successful:
321                       the length of the copied string, not including the zero
322                       that is put on the end; can be zero
323                     if not successful:
324                       PCRE_ERROR_NOMEMORY (-6) couldn't get memory
325                       PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
326    */
327    
328    int
329    pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,
330      int stringcount, const char *stringname, const char **stringptr)
331    {
332    int n = pcre_get_stringnumber(code, stringname);
333    if (n <= 0) return n;
334    return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
335    }
336    
337    
338    
339    
340  /*************************************************  /*************************************************
341  *       Free store obtained by get_substring     *  *       Free store obtained by get_substring     *
342  *************************************************/  *************************************************/

Legend:
Removed from v.53  
changed lines
  Added in v.75

  ViewVC Help
Powered by ViewVC 1.1.5