Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_pcre.c
Go to the documentation of this file.
1
8#include "nilorea/n_pcre.h"
9#include "nilorea/n_log.h"
10#include "strings.h"
11
12#include "string.h"
13
58N_PCRE* npcre_new(char* str, int max_cap, int flags) {
59 N_PCRE* pcre = NULL;
60 __n_assert(str, return NULL);
61
62 Malloc(pcre, N_PCRE, 1);
63 __n_assert(pcre, return NULL);
64
65 pcre->captured = 0;
66
67 if (max_cap == 0) {
68 max_cap = 1;
69 }
70 pcre->match_list = NULL;
71
72 pcre->regexp_str = strdup(str);
73 __n_assert(str, npcre_delete(&pcre); return NULL);
74
75 pcre->ovecount = max_cap;
76 Malloc(pcre->ovector, int, (size_t)(3 * max_cap));
77 __n_assert(pcre->ovector, npcre_delete(&pcre); return NULL);
78
79 const char* error = NULL;
80 int erroroffset = 0;
81
82 pcre->regexp = pcre_compile(str, flags, &error, &erroroffset, NULL);
83 if (!pcre->regexp) {
84 n_log(LOG_ERR, " pcre compilation of %s failed at offset %d : %s", str, erroroffset, error);
85 npcre_delete(&pcre);
86 return FALSE;
87 }
88 /* no flags for study = no JIT compilation */
89 pcre->extra = pcre_study(pcre->regexp, 0, &error);
90
91 return pcre;
92} /* npcre_new(...) */
93
101int npcre_delete(N_PCRE** pcre) {
102 __n_assert(pcre && (*pcre), return FALSE);
103
104 if ((*pcre)->ovector) {
105 Free((*pcre)->ovector);
106 }
107 if ((*pcre)->regexp) {
108 pcre_free((*pcre)->regexp);
109 (*pcre)->regexp = NULL;
110 }
111#ifdef __linux__
112 pcre_free_study((*pcre)->extra);
113 (*pcre)->extra = NULL;
114#else
115 pcre_free((*pcre)->extra);
116 (*pcre)->extra = NULL;
117#endif
118 if ((*pcre)->captured > 0) {
119 pcre_free_substring_list((*pcre)->match_list);
120 }
121 FreeNoLog((*pcre)->regexp_str);
122 FreeNoLog((*pcre));
123 return TRUE;
124} /* npcre_delete (...) */
125
132 __n_assert(pcre, return FALSE);
133 __n_assert(pcre->match_list, return FALSE);
134
135 if (pcre->captured > 0) {
136 pcre->captured = 0;
137 pcre_free_substring_list(pcre->match_list);
138 }
139
140 return TRUE;
141} /* npcre_clean_match */
142
149int npcre_match(char* str, N_PCRE* pcre) {
150 __n_assert(str, return FALSE);
151 __n_assert(pcre, return FALSE);
152 __n_assert(pcre->regexp_str, return FALSE);
153 __n_assert(pcre->regexp, return FALSE);
154
155 int rc, len = 0;
156 len = (int)strlen(str);
157
158 rc = pcre_exec(pcre->regexp, pcre->extra, str, len, 0, 0, pcre->ovector, pcre->ovecount);
159 if (rc < 0) {
160 switch (rc) {
161 case PCRE_ERROR_NOMATCH: /* n_log( LOG_DEBUG , "String did not match the pattern");*/
162 break;
163 case PCRE_ERROR_NULL:
164 n_log(LOG_DEBUG, "Something was null");
165 break;
166 case PCRE_ERROR_BADOPTION:
167 n_log(LOG_DEBUG, "A bad option was passed");
168 break;
169 case PCRE_ERROR_BADMAGIC:
170 n_log(LOG_DEBUG, "Magic number bad (compiled re corrupt?)");
171 break;
172 case PCRE_ERROR_UNKNOWN_NODE:
173 n_log(LOG_DEBUG, "Something kooky in the compiled regexp");
174 break;
175 case PCRE_ERROR_NOMEMORY:
176 n_log(LOG_DEBUG, "Ran out of memory");
177 break;
178 default:
179 n_log(LOG_DEBUG, "Unknown error");
180 break;
181 }
182 return FALSE;
183 } else if (rc == 0) {
184 rc = pcre->ovecount;
185 }
186
187 if (rc > 0) {
188 npcre_clean_match(pcre);
189 pcre->captured = rc;
190 pcre_get_substring_list(str, pcre->ovector, rc, &pcre->match_list);
191 }
192 return TRUE;
193} /* npcre_match(...) */
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:251
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
#define LOG_ERR
error conditions
Definition n_log.h:56
char * regexp_str
original regexp string
Definition n_pcre.h:29
int captured
flag for match_list cleaning
Definition n_pcre.h:42
pcre * regexp
regexp
Definition n_pcre.h:31
const char ** match_list
populated match list if nPcreCapMatch is called
Definition n_pcre.h:40
int * ovector
list of indexes
Definition n_pcre.h:38
int ovecount
configured maximum number of matched occurence
Definition n_pcre.h:36
pcre_extra * extra
optimization if any
Definition n_pcre.h:33
N_PCRE * npcre_new(char *str, int max_cap, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
Definition n_pcre.c:58
int npcre_match(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures up to max_cap.
Definition n_pcre.c:149
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
Definition n_pcre.c:131
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Definition n_pcre.c:101
N_PCRE structure.
Definition n_pcre.h:27
Generic log system.
PCRE helpers for regex matching.