PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/beta3/harbour/source/hbpcre/pcreget.c

#
C | 352 lines | 109 code | 55 blank | 188 comment | 25 complexity | 78516945081dba934c0042e93d0283ef MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2005 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains some convenience functions for extracting substrings
  33. from the subject string after a regex match has succeeded. The original idea
  34. for these functions came from Scott Wimer. */
  35. #include "pcreinal.h"
  36. /*************************************************
  37. * Find number for named string *
  38. *************************************************/
  39. /* This function is used by the two extraction functions below, as well
  40. as being generally available.
  41. Arguments:
  42. code the compiled regex
  43. stringname the name whose number is required
  44. Returns: the number of the named parentheses, or a negative number
  45. (PCRE_ERROR_NOSUBSTRING) if not found
  46. */
  47. int
  48. pcre_get_stringnumber(const pcre *code, const char *stringname)
  49. {
  50. int rc;
  51. int entrysize;
  52. int top, bot;
  53. uschar *nametable;
  54. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  55. return rc;
  56. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  57. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  58. return rc;
  59. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  60. return rc;
  61. bot = 0;
  62. while (top > bot)
  63. {
  64. int mid = (top + bot) / 2;
  65. uschar *entry = nametable + entrysize*mid;
  66. int c = strcmp(stringname, (char *)(entry + 2));
  67. if (c == 0) return (entry[0] << 8) + entry[1];
  68. if (c > 0) bot = mid + 1; else top = mid;
  69. }
  70. return PCRE_ERROR_NOSUBSTRING;
  71. }
  72. /*************************************************
  73. * Copy captured string to given buffer *
  74. *************************************************/
  75. /* This function copies a single captured substring into a given buffer.
  76. Note that we use memcpy() rather than strncpy() in case there are binary zeros
  77. in the string.
  78. Arguments:
  79. subject the subject string that was matched
  80. ovector pointer to the offsets table
  81. stringcount the number of substrings that were captured
  82. (i.e. the yield of the pcre_exec call, unless
  83. that was zero, in which case it should be 1/3
  84. of the offset table size)
  85. stringnumber the number of the required substring
  86. buffer where to put the substring
  87. size the size of the buffer
  88. Returns: if successful:
  89. the length of the copied string, not including the zero
  90. that is put on the end; can be zero
  91. if not successful:
  92. PCRE_ERROR_NOMEMORY (-6) buffer too small
  93. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  94. */
  95. int
  96. pcre_copy_substring(const char *subject, int *ovector, int stringcount,
  97. int stringnumber, char *buffer, int size)
  98. {
  99. int yield;
  100. if (stringnumber < 0 || stringnumber >= stringcount)
  101. return PCRE_ERROR_NOSUBSTRING;
  102. stringnumber *= 2;
  103. yield = ovector[stringnumber+1] - ovector[stringnumber];
  104. if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
  105. memcpy(buffer, subject + ovector[stringnumber], yield);
  106. buffer[yield] = 0;
  107. return yield;
  108. }
  109. /*************************************************
  110. * Copy named captured string to given buffer *
  111. *************************************************/
  112. /* This function copies a single captured substring into a given buffer,
  113. identifying it by name.
  114. Arguments:
  115. code the compiled regex
  116. subject the subject string that was matched
  117. ovector pointer to the offsets table
  118. stringcount the number of substrings that were captured
  119. (i.e. the yield of the pcre_exec call, unless
  120. that was zero, in which case it should be 1/3
  121. of the offset table size)
  122. stringname the name of the required substring
  123. buffer where to put the substring
  124. size the size of the buffer
  125. Returns: if successful:
  126. the length of the copied string, not including the zero
  127. that is put on the end; can be zero
  128. if not successful:
  129. PCRE_ERROR_NOMEMORY (-6) buffer too small
  130. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  131. */
  132. int
  133. pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,
  134. int stringcount, const char *stringname, char *buffer, int size)
  135. {
  136. int n = pcre_get_stringnumber(code, stringname);
  137. if (n <= 0) return n;
  138. return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
  139. }
  140. /*************************************************
  141. * Copy all captured strings to new store *
  142. *************************************************/
  143. /* This function gets one chunk of store and builds a list of pointers and all
  144. of the captured substrings in it. A NULL pointer is put on the end of the list.
  145. Arguments:
  146. subject the subject string that was matched
  147. ovector pointer to the offsets table
  148. stringcount the number of substrings that were captured
  149. (i.e. the yield of the pcre_exec call, unless
  150. that was zero, in which case it should be 1/3
  151. of the offset table size)
  152. listptr set to point to the list of pointers
  153. Returns: if successful: 0
  154. if not successful:
  155. PCRE_ERROR_NOMEMORY (-6) failed to get store
  156. */
  157. int
  158. pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
  159. const char ***listptr)
  160. {
  161. int i;
  162. int size = sizeof(char *);
  163. int double_count = stringcount * 2;
  164. char **stringlist;
  165. char *p;
  166. for (i = 0; i < double_count; i += 2)
  167. size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;
  168. stringlist = (char **)(pcre_malloc)(size);
  169. if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
  170. *listptr = (const char **)stringlist;
  171. p = (char *)(stringlist + stringcount + 1);
  172. for (i = 0; i < double_count; i += 2)
  173. {
  174. int len = ovector[i+1] - ovector[i];
  175. memcpy(p, subject + ovector[i], len);
  176. *stringlist++ = p;
  177. p += len;
  178. *p++ = 0;
  179. }
  180. *stringlist = NULL;
  181. return 0;
  182. }
  183. /*************************************************
  184. * Free store obtained by get_substring_list *
  185. *************************************************/
  186. /* This function exists for the benefit of people calling PCRE from non-C
  187. programs that can call its functions, but not free() or (pcre_free)() directly.
  188. Argument: the result of a previous pcre_get_substring_list()
  189. Returns: nothing
  190. */
  191. void
  192. pcre_free_substring_list(const char **pointer)
  193. {
  194. (pcre_free)((void *)pointer);
  195. }
  196. /*************************************************
  197. * Copy captured string to new store *
  198. *************************************************/
  199. /* This function copies a single captured substring into a piece of new
  200. store
  201. Arguments:
  202. subject the subject string that was matched
  203. ovector pointer to the offsets table
  204. stringcount the number of substrings that were captured
  205. (i.e. the yield of the pcre_exec call, unless
  206. that was zero, in which case it should be 1/3
  207. of the offset table size)
  208. stringnumber the number of the required substring
  209. stringptr where to put a pointer to the substring
  210. Returns: if successful:
  211. the length of the string, not including the zero that
  212. is put on the end; can be zero
  213. if not successful:
  214. PCRE_ERROR_NOMEMORY (-6) failed to get store
  215. PCRE_ERROR_NOSUBSTRING (-7) substring not present
  216. */
  217. int
  218. pcre_get_substring(const char *subject, int *ovector, int stringcount,
  219. int stringnumber, const char **stringptr)
  220. {
  221. int yield;
  222. char *substring;
  223. if (stringnumber < 0 || stringnumber >= stringcount)
  224. return PCRE_ERROR_NOSUBSTRING;
  225. stringnumber *= 2;
  226. yield = ovector[stringnumber+1] - ovector[stringnumber];
  227. substring = (char *)(pcre_malloc)(yield + 1);
  228. if (substring == NULL) return PCRE_ERROR_NOMEMORY;
  229. memcpy(substring, subject + ovector[stringnumber], yield);
  230. substring[yield] = 0;
  231. *stringptr = substring;
  232. return yield;
  233. }
  234. /*************************************************
  235. * Copy named captured string to new store *
  236. *************************************************/
  237. /* This function copies a single captured substring, identified by name, into
  238. new store.
  239. Arguments:
  240. code the compiled regex
  241. subject the subject string that was matched
  242. ovector pointer to the offsets table
  243. stringcount the number of substrings that were captured
  244. (i.e. the yield of the pcre_exec call, unless
  245. that was zero, in which case it should be 1/3
  246. of the offset table size)
  247. stringname the name of the required substring
  248. stringptr where to put the pointer
  249. Returns: if successful:
  250. the length of the copied string, not including the zero
  251. that is put on the end; can be zero
  252. if not successful:
  253. PCRE_ERROR_NOMEMORY (-6) couldn't get memory
  254. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  255. */
  256. int
  257. pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,
  258. int stringcount, const char *stringname, const char **stringptr)
  259. {
  260. int n = pcre_get_stringnumber(code, stringname);
  261. if (n <= 0) return n;
  262. return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
  263. }
  264. /*************************************************
  265. * Free store obtained by get_substring *
  266. *************************************************/
  267. /* This function exists for the benefit of people calling PCRE from non-C
  268. programs that can call its functions, but not free() or (pcre_free)() directly.
  269. Argument: the result of a previous pcre_get_substring()
  270. Returns: nothing
  271. */
  272. void
  273. pcre_free_substring(const char *pointer)
  274. {
  275. (pcre_free)((void *)pointer);
  276. }
  277. /* End of pcreget.c */