Commit | Line | Data |
---|---|---|
a2e9d147 MW |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2017 Mark Wooding | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | /** @file lib/regexp.c | |
19 | * @brief Regular expressions | |
20 | */ | |
21 | #include "common.h" | |
22 | ||
23 | #include "regexp.h" | |
24 | #include "mem.h" | |
25 | ||
26 | void regexp_setup(void) | |
27 | { | |
28 | pcre_malloc = xmalloc; | |
29 | pcre_free = xfree; | |
30 | } | |
31 | ||
32 | regexp *regexp_compile(const char *pat, unsigned f, | |
33 | char *errbuf, size_t errlen, size_t *erroff_out) | |
34 | { | |
35 | char *p; | |
36 | const char *e; | |
37 | int erroff; | |
38 | regexp *re; | |
39 | size_t i; | |
40 | ||
41 | re = pcre_compile(pat, f, &e, &erroff, 0); | |
42 | if(!re) { | |
43 | *erroff_out = erroff; | |
44 | for(p = errbuf, i = errlen - 1; i && *e; i--) *p++ = *e++; | |
45 | *p = 0; | |
46 | } | |
47 | return re; | |
48 | } | |
49 | ||
50 | int regexp_match(const regexp *re, const char *s, size_t n, unsigned f, | |
51 | size_t *ov, size_t on) | |
52 | { | |
53 | int rc; | |
54 | int *myov; | |
55 | size_t i; | |
56 | ||
57 | myov = xmalloc(on*sizeof(*myov)); | |
58 | rc = pcre_exec(re, 0, s, n, 0, f, myov, on); | |
59 | for(i = 0; i < on; i++) ov[i] = myov[i]; | |
60 | xfree(myov); | |
61 | return rc; | |
62 | } | |
63 | ||
64 | void regexp_free(regexp *re) | |
65 | { pcre_free(re); } | |
66 | ||
67 | /* | |
68 | Local Variables: | |
69 | c-basic-offset:2 | |
70 | comment-column:40 | |
71 | End: | |
72 | */ |