Nilorea Library
C utilities for networking, threading, graphics
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
14
59N_PCRE *npcre_new( char *str, int max_cap, int flags )
60{
61 N_PCRE *pcre = NULL ;
62 __n_assert( str, return NULL );
63
64 Malloc( pcre, N_PCRE, 1 );
65 __n_assert( pcre, return NULL );
66
67 pcre -> captured = 0 ;
68
69 if( max_cap <= 0 )
70 {
71 max_cap = 1 ;
72 }
73 pcre -> match_list = NULL ;
74
75 pcre -> regexp_str = strdup( str );
76 __n_assert( str, npcre_delete( &pcre ); return NULL );
77
78 pcre -> ovecount = max_cap ;
79 Malloc( pcre -> ovector, int, 3 * max_cap );
80 __n_assert( pcre -> ovector, npcre_delete( &pcre ); return NULL );
81
82 const char *error = NULL ;
83 int erroroffset = 0 ;
84
85 pcre -> regexp = pcre_compile( str, flags, &error, &erroroffset, NULL ) ;
86 if( !pcre -> regexp )
87 {
88 n_log( LOG_ERR, " pcre compilation of %s failed at offset %d : %s", str, erroroffset, error );
89 npcre_delete( &pcre );
90 return FALSE ;
91 }
92 /* no flags for study = no JIT compilation */
93 pcre -> extra = pcre_study( pcre -> regexp, 0, &error );
94
95 return pcre ;
96}/* npcre_new(...) */
97
98
99
107int npcre_delete( N_PCRE ** pcre )
108{
109 __n_assert( pcre&&(*pcre), return FALSE );
110
111 if( (*pcre) -> ovector )
112 {
113 Free( (*pcre) -> ovector );
114 }
115 if( (*pcre) -> regexp )
116 {
117 pcre_free( (*pcre) -> regexp );
118 (*pcre) -> regexp = NULL ;
119 }
120#ifdef __linux__
121 pcre_free_study( (*pcre) -> extra );
122 (*pcre) -> extra = NULL ;
123#else
124 pcre_free( (*pcre) -> extra );
125 (*pcre) -> extra = NULL ;
126#endif
127 if( (*pcre) -> captured > 0 )
128 {
129 pcre_free_substring_list( (*pcre) -> match_list );
130 }
131 FreeNoLog( (*pcre) -> regexp_str );
132 FreeNoLog( (*pcre) );
133 return TRUE ;
134} /* npcre_delete (...) */
135
136
137
138
145{
146 __n_assert( pcre, return FALSE );
147 __n_assert( pcre -> match_list, return FALSE );
148
149 if( pcre -> captured > 0 )
150 {
151 pcre -> captured = 0 ;
152 pcre_free_substring_list( pcre -> match_list );
153 }
154
155 return TRUE ;
156} /* npcre_clean_match */
157
158
165int npcre_match( char *str, N_PCRE *pcre )
166{
167 __n_assert( str, return FALSE );
168 __n_assert( pcre, return FALSE );
169 __n_assert( pcre -> regexp_str, return FALSE );
170 __n_assert( pcre -> regexp, return FALSE );
171
172 int rc, len = 0;
173 len = ( int )strlen( str ) ;
174
175 rc = pcre_exec( pcre -> regexp, pcre -> extra, str, len, 0, 0, pcre -> ovector, pcre -> ovecount );
176 if ( rc < 0 )
177 {
178 switch( rc )
179 {
180 case PCRE_ERROR_NOMATCH : /* n_log( LOG_DEBUG , "String did not match the pattern");*/ break;
181 case PCRE_ERROR_NULL : n_log( LOG_DEBUG , "Something was null"); break;
182 case PCRE_ERROR_BADOPTION : n_log( LOG_DEBUG , "A bad option was passed"); break;
183 case PCRE_ERROR_BADMAGIC : n_log( LOG_DEBUG , "Magic number bad (compiled re corrupt?)"); break;
184 case PCRE_ERROR_UNKNOWN_NODE : n_log( LOG_DEBUG , "Something kooky in the compiled regexp"); break;
185 case PCRE_ERROR_NOMEMORY : n_log( LOG_DEBUG , "Ran out of memory"); break;
186 default : n_log( LOG_DEBUG , "Unknown error"); break;
187
188 }
189 return FALSE ;
190 }
191 else if( rc == 0 )
192 {
193 rc = pcre -> ovecount ;
194 }
195
196 if( rc > 0 )
197 {
198 npcre_clean_match( pcre );
199 pcre -> captured = rc ;
200 pcre_get_substring_list( str, pcre -> ovector, rc, &pcre -> match_list );
201 }
202 return TRUE ;
203}/* npcre_match(...) */
204
205
206
#define FreeNoLog(__ptr)
Free Handler without log.
Definition: n_common.h:268
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition: n_common.h:183
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
#define Free(__ptr)
Free Handler to get errors.
Definition: n_common.h:256
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_ERR
error conditions
Definition: n_log.h:58
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:165
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
Definition: n_pcre.c:144
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Definition: n_pcre.c:107
N_PCRE structure.
Definition: n_pcre.h:29
Generic log system.
PCRE helpers for regex matching.