get.c

Go to the documentation of this file.
00001 
00002 /*  $Id: get.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /*************************************************
00005 *      Perl-Compatible Regular Expressions       *
00006 *************************************************/
00007 
00008 /*
00009 This is a library of functions to support regular expressions whose syntax
00010 and semantics are as close as possible to those of the Perl 5 language. See
00011 the file Tech.Notes for some information on the internals.
00012 
00013 Written by: Philip Hazel <ph10@cam.ac.uk>
00014 
00015            Copyright (c) 1997-2003 University of Cambridge
00016 
00017 -----------------------------------------------------------------------------
00018 Permission is granted to anyone to use this software for any purpose on any
00019 computer system, and to redistribute it freely, subject to the following
00020 restrictions:
00021 
00022 1. This software is distributed in the hope that it will be useful,
00023    but WITHOUT ANY WARRANTY; without even the implied warranty of
00024    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00025 
00026 2. The origin of this software must not be misrepresented, either by
00027    explicit claim or by omission.
00028 
00029 3. Altered versions must be plainly marked as such, and must not be
00030    misrepresented as being the original software.
00031 
00032 4. If PCRE is embedded in any software that is released under the GNU
00033    General Purpose Licence (GPL), then the terms of that licence shall
00034    supersede any condition above with which it is incompatible.
00035 -----------------------------------------------------------------------------
00036 */
00037 
00038 /* This module contains some convenience functions for extracting substrings
00039 from the subject string after a regex match has succeeded. The original idea
00040 for these functions came from Scott Wimer <scottw@cgibuilder.com>. */
00041 
00042 
00043 /* Include the internals header, which itself includes Standard C headers plus
00044 the external pcre header. */
00045 
00046 #include "pcre_internal.h"
00047 
00048 
00049 /*************************************************
00050 *           Find number for named string         *
00051 *************************************************/
00052 
00053 /* This function is used by the two extraction functions below, as well
00054 as being generally available.
00055 
00056 Arguments:
00057   code        the compiled regex
00058   stringname  the name whose number is required
00059 
00060 Returns:      the number of the named parentheses, or a negative number
00061                 (PCRE_ERROR_NOSUBSTRING) if not found
00062 */
00063 
00064 int
00065 pcre_get_stringnumber(const pcre *code, const char *stringname)
00066 {
00067 int rc;
00068 int entrysize;
00069 int top, bot;
00070 uschar *nametable;
00071 
00072 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
00073   return rc;
00074 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
00075 
00076 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
00077   return rc;
00078 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
00079   return rc;
00080 
00081 bot = 0;
00082 while (top > bot)
00083   {
00084   int mid = (top + bot) / 2;
00085   uschar *entry = nametable + entrysize*mid;
00086   int c = strcmp(stringname, (char *)(entry + 2));
00087   if (c == 0) return (entry[0] << 8) + entry[1];
00088   if (c > 0) bot = mid + 1; else top = mid;
00089   }
00090 
00091 return PCRE_ERROR_NOSUBSTRING;
00092 }
00093 
00094 
00095 
00096 /*************************************************
00097 *      Copy captured string to given buffer      *
00098 *************************************************/
00099 
00100 /* This function copies a single captured substring into a given buffer.
00101 Note that we use memcpy() rather than strncpy() in case there are binary zeros
00102 in the string.
00103 
00104 Arguments:
00105   subject        the subject string that was matched
00106   ovector        pointer to the offsets table
00107   stringcount    the number of substrings that were captured
00108                    (i.e. the yield of the pcre_exec call, unless
00109                    that was zero, in which case it should be 1/3
00110                    of the offset table size)
00111   stringnumber   the number of the required substring
00112   buffer         where to put the substring
00113   size           the size of the buffer
00114 
00115 Returns:         if successful:
00116                    the length of the copied string, not including the zero
00117                    that is put on the end; can be zero
00118                  if not successful:
00119                    PCRE_ERROR_NOMEMORY (-6) buffer too small
00120                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
00121 */
00122 
00123 int
00124 pcre_copy_substring(const char *subject, int *ovector, int stringcount,
00125   int stringnumber, char *buffer, int size)
00126 {
00127 int yield;
00128 if (stringnumber < 0 || stringnumber >= stringcount)
00129   return PCRE_ERROR_NOSUBSTRING;
00130 stringnumber *= 2;
00131 yield = ovector[stringnumber+1] - ovector[stringnumber];
00132 if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
00133 memcpy(buffer, subject + ovector[stringnumber], yield);
00134 buffer[yield] = 0;
00135 return yield;
00136 }
00137 
00138 
00139 
00140 /*************************************************
00141 *   Copy named captured string to given buffer   *
00142 *************************************************/
00143 
00144 /* This function copies a single captured substring into a given buffer,
00145 identifying it by name.
00146 
00147 Arguments:
00148   code           the compiled regex
00149   subject        the subject string that was matched
00150   ovector        pointer to the offsets table
00151   stringcount    the number of substrings that were captured
00152                    (i.e. the yield of the pcre_exec call, unless
00153                    that was zero, in which case it should be 1/3
00154                    of the offset table size)
00155   stringname     the name of the required substring
00156   buffer         where to put the substring
00157   size           the size of the buffer
00158 
00159 Returns:         if successful:
00160                    the length of the copied string, not including the zero
00161                    that is put on the end; can be zero
00162                  if not successful:
00163                    PCRE_ERROR_NOMEMORY (-6) buffer too small
00164                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
00165 */
00166 
00167 int
00168 pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,
00169   int stringcount, const char *stringname, char *buffer, int size)
00170 {
00171 int n = pcre_get_stringnumber(code, stringname);
00172 if (n <= 0) return n;
00173 return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
00174 }
00175 
00176 
00177 
00178 /*************************************************
00179 *      Copy all captured strings to new store    *
00180 *************************************************/
00181 
00182 /* This function gets one chunk of store and builds a list of pointers and all
00183 of the captured substrings in it. A NULL pointer is put on the end of the list.
00184 
00185 Arguments:
00186   subject        the subject string that was matched
00187   ovector        pointer to the offsets table
00188   stringcount    the number of substrings that were captured
00189                    (i.e. the yield of the pcre_exec call, unless
00190                    that was zero, in which case it should be 1/3
00191                    of the offset table size)
00192   listptr        set to point to the list of pointers
00193 
00194 Returns:         if successful: 0
00195                  if not successful:
00196                    PCRE_ERROR_NOMEMORY (-6) failed to get store
00197 */
00198 
00199 int
00200 pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
00201   const char ***listptr)
00202 {
00203 int i;
00204 int size = sizeof(char *);
00205 int double_count = stringcount * 2;
00206 char **stringlist;
00207 char *p;
00208 
00209 for (i = 0; i < double_count; i += 2)
00210   size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;
00211 
00212 stringlist = (char **)(pcre_malloc)(size);
00213 if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
00214 
00215 *listptr = (const char **)stringlist;
00216 p = (char *)(stringlist + stringcount + 1);
00217 
00218 for (i = 0; i < double_count; i += 2)
00219   {
00220   int len = ovector[i+1] - ovector[i];
00221   memcpy(p, subject + ovector[i], len);
00222   *stringlist++ = p;
00223   p += len;
00224   *p++ = 0;
00225   }
00226 
00227 *stringlist = NULL;
00228 return 0;
00229 }
00230 
00231 
00232 
00233 /*************************************************
00234 *   Free store obtained by get_substring_list    *
00235 *************************************************/
00236 
00237 /* This function exists for the benefit of people calling PCRE from non-C
00238 programs that can call its functions, but not free() or (pcre_free)() directly.
00239 
00240 Argument:   the result of a previous pcre_get_substring_list()
00241 Returns:    nothing
00242 */
00243 
00244 void
00245 pcre_free_substring_list(const char **pointer)
00246 {
00247 (pcre_free)((void *)pointer);
00248 }
00249 
00250 
00251 
00252 /*************************************************
00253 *      Copy captured string to new store         *
00254 *************************************************/
00255 
00256 /* This function copies a single captured substring into a piece of new
00257 store
00258 
00259 Arguments:
00260   subject        the subject string that was matched
00261   ovector        pointer to the offsets table
00262   stringcount    the number of substrings that were captured
00263                    (i.e. the yield of the pcre_exec call, unless
00264                    that was zero, in which case it should be 1/3
00265                    of the offset table size)
00266   stringnumber   the number of the required substring
00267   stringptr      where to put a pointer to the substring
00268 
00269 Returns:         if successful:
00270                    the length of the string, not including the zero that
00271                    is put on the end; can be zero
00272                  if not successful:
00273                    PCRE_ERROR_NOMEMORY (-6) failed to get store
00274                    PCRE_ERROR_NOSUBSTRING (-7) substring not present
00275 */
00276 
00277 int
00278 pcre_get_substring(const char *subject, int *ovector, int stringcount,
00279   int stringnumber, const char **stringptr)
00280 {
00281 int yield;
00282 char *substring;
00283 if (stringnumber < 0 || stringnumber >= stringcount)
00284   return PCRE_ERROR_NOSUBSTRING;
00285 stringnumber *= 2;
00286 yield = ovector[stringnumber+1] - ovector[stringnumber];
00287 substring = (char *)(pcre_malloc)(yield + 1);
00288 if (substring == NULL) return PCRE_ERROR_NOMEMORY;
00289 memcpy(substring, subject + ovector[stringnumber], yield);
00290 substring[yield] = 0;
00291 *stringptr = substring;
00292 return yield;
00293 }
00294 
00295 
00296 
00297 /*************************************************
00298 *   Copy named captured string to new store      *
00299 *************************************************/
00300 
00301 /* This function copies a single captured substring, identified by name, into
00302 new store.
00303 
00304 Arguments:
00305   code           the compiled regex
00306   subject        the subject string that was matched
00307   ovector        pointer to the offsets table
00308   stringcount    the number of substrings that were captured
00309                    (i.e. the yield of the pcre_exec call, unless
00310                    that was zero, in which case it should be 1/3
00311                    of the offset table size)
00312   stringname     the name of the required substring
00313   stringptr      where to put the pointer
00314 
00315 Returns:         if successful:
00316                    the length of the copied string, not including the zero
00317                    that is put on the end; can be zero
00318                  if not successful:
00319                    PCRE_ERROR_NOMEMORY (-6) couldn't get memory
00320                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
00321 */
00322 
00323 int
00324 pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,
00325   int stringcount, const char *stringname, const char **stringptr)
00326 {
00327 int n = pcre_get_stringnumber(code, stringname);
00328 if (n <= 0) return n;
00329 return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
00330 }
00331 
00332 
00333 
00334 
00335 /*************************************************
00336 *       Free store obtained by get_substring     *
00337 *************************************************/
00338 
00339 /* This function exists for the benefit of people calling PCRE from non-C
00340 programs that can call its functions, but not free() or (pcre_free)() directly.
00341 
00342 Argument:   the result of a previous pcre_get_substring()
00343 Returns:    nothing
00344 */
00345 
00346 void
00347 pcre_free_substring(const char *pointer)
00348 {
00349 (pcre_free)((void *)pointer);
00350 }
00351 
00352 /* End of get.c */

Generated on Wed May 31 18:19:47 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6