Archived
1
0
Fork 0
This repository has been archived on 2026-05-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
dlight/filter.c
2011-09-21 17:19:09 +02:00

94 lines
No EOL
2.1 KiB
C

/* filter.c
*
* Copyright (C) 2011 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <assert.h>
#include <pcre.h>
#include <stdio.h>
#include <string.h>
#include "filter.h"
static inline pcre* compile(const char *pattern) {
const char *error;
int eoffset;
pcre *regex;
regex = pcre_compile(pattern, 0, &error, &eoffset, NULL);
if (!regex) {
fprintf(stderr, "Error compiling expression\n");
return NULL;
}
return regex;
}
static inline int match(pcre *pcre, const char *subject) {
int ovector[1];
return pcre_exec(pcre, NULL, subject, strlen(subject), 0, 0,
ovector, sizeof(ovector));
}
int filter_check_syntax(const char *pattern) {
const char *error;
int eoffset;
pcre *regex;
regex = pcre_compile(pattern, 0, &error, &eoffset, NULL);
if (!regex) {
fprintf(stderr, "filter: error in expression '%s': %s\n",
pattern, error);
return 0;
}
return 1;
}
int filter_match(const char *pattern, const char *subject) {
pcre *regex;
int rc;
if (!pattern || !subject)
return 0;
regex = compile(pattern);
if (!regex)
return 0;
rc = match(regex, subject);
pcre_free(regex);
return rc > 0;
}
int filter_match_list(char **patterns, unsigned n, const char *subject) {
int i;
for(i=0; i < n; i++) {
/* return true at the first matching pattern */
if (filter_match(patterns[i], subject))
return 1;
}
return 0;
}