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