Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_pcre.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
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 <https://www.gnu.org/licenses/>.
17 */
18
27#ifndef __NILOREA_PCRE_HELPERS__
28#define __NILOREA_PCRE_HELPERS__
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/* PCRE2 uses a different API than legacy PCRE (pcre.h).
35 * We use the 8-bit library (char / UTF-8 style strings). */
36#ifndef PCRE2_CODE_UNIT_WIDTH
37#define PCRE2_CODE_UNIT_WIDTH 8
38#endif
39#include <pcre2.h>
40
41#include <nilorea/n_common.h>
42
43/* -------------------------------------------------------------------------
44 * Compatibility aliases
45 * -------------------------------------------------------------------------
46 * The legacy PCRE (pcre.h) used PCRE_* option names. PCRE2 uses PCRE2_*.
47 * If your existing code still passes PCRE_* flags to npcre_new(), these
48 * aliases let it keep compiling.
49 */
50#ifndef PCRE_CASELESS
51#define PCRE_CASELESS PCRE2_CASELESS
52#endif
53#ifndef PCRE_MULTILINE
54#define PCRE_MULTILINE PCRE2_MULTILINE
55#endif
56#ifndef PCRE_DOTALL
57#define PCRE_DOTALL PCRE2_DOTALL
58#endif
59#ifndef PCRE_EXTENDED
60#define PCRE_EXTENDED PCRE2_EXTENDED
61#endif
62#ifndef PCRE_ANCHORED
63#define PCRE_ANCHORED PCRE2_ANCHORED
64#endif
65#ifndef PCRE_UTF8
66#define PCRE_UTF8 PCRE2_UTF
67#endif
68#ifndef PCRE_UCP
69#define PCRE_UCP PCRE2_UCP
70#endif
71
72/* pcre2_substring_list_free() had a const PCRE2_UCHAR** argument up to 10.41,
73 * then it was changed to PCRE2_UCHAR** in 10.42 to fix the mismatch with
74 * pcre2_substring_list(). Use void* as intermediate to satisfy both. */
75#ifdef PCRE2_LIST_FREE_CONST
76#define PCRE2_LIST_FREE_CAST(x) ((const PCRE2_UCHAR8**)(void*)(x))
77#else
78#define PCRE2_LIST_FREE_CAST(x) ((PCRE2_UCHAR8**)(void*)(x))
79#endif
80
82#define npcre_print_error(__LEVEL__, __rc__) \
83 do { \
84 _npcre_print_error(__LEVEL__, __FILE__, __func__, __LINE__, __rc__); \
85 } while (0)
86
93typedef struct N_PCRE {
97 pcre2_code* regexp;
99 pcre2_match_data* match_data;
104 PCRE2_UCHAR8** match_list;
108 pthread_rwlock_t rwlock;
109} N_PCRE;
110
112N_PCRE* npcre_new(char* str, int flags);
114int npcre_delete(N_PCRE** pcre);
116int npcre_clean_match(N_PCRE* pcre);
118int npcre_match(char* str, N_PCRE* pcre);
120int npcre_match_capture(char* str, N_PCRE* pcre);
122int _npcre_print_error(int LOG_LEVEL, const char* file, const char* func, int line, int rc);
123
128#ifdef __cplusplus
129}
130#endif
131/* #ifndef __NILOREA_PCRE_HELPERS__ */
132#endif
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
Definition n_pcre.h:104
pcre2_code * regexp
regexp
Definition n_pcre.h:97
pcre2_match_data * match_data
match data storage (ovector, etc.)
Definition n_pcre.h:99
char * regexp_str
original regexp string
Definition n_pcre.h:95
pthread_rwlock_t rwlock
rwlock protecting match_data, match_list, and captured
Definition n_pcre.h:108
int captured
flag for match_list cleaning
Definition n_pcre.h:106
N_PCRE * npcre_new(char *str, int flags)
pcre helper, compile and optimize regexp
Definition n_pcre.c:80
int _npcre_print_error(int LOG_LEVEL, const char *file, const char *func, int line, int rc)
print error helper
Definition n_pcre.c:187
int npcre_match_capture(char *str, N_PCRE *pcre)
thread-safe pcre match with captures stored in pcre->match_list
Definition n_pcre.c:261
int npcre_match(char *str, N_PCRE *pcre)
thread-safe pcre match, only returns TRUE/FALSE, no captures stored
Definition n_pcre.c:226
int npcre_clean_match(N_PCRE *pcre)
clean match results from a previous npcre_match_capture
Definition n_pcre.c:164
int npcre_delete(N_PCRE **pcre)
pcre helper, delete/free a compiled regexp
Definition n_pcre.c:134
N_PCRE structure.
Definition n_pcre.h:93
Common headers and low-level functions & define.
static int LOG_LEVEL
static global maximum wanted log level value
Definition n_log.c:77