-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.c
110 lines (91 loc) · 2.04 KB
/
filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "filter.h"
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILTER 300
static char* filters[MAX_FILTER];
static size_t count;
static void add_filter(const char* const name)
{
if (!name) {
logLine("%s: NULL pointer\n", __func__);
return;
}
if (strlen(name) > 0) {
if (count < MAX_FILTER) {
if (filters[count] == NULL) {
filters[count] = strdup(name);
logLine("Filter[%u] '%s' added", count, filters[count]);
count++;
}
}
}
}
static void strip(char* const buffer)
{
if (!buffer) {
logLine("%s: NULL pointer\n", __func__);
return;
}
size_t i;
for (i = 0; i < strlen(buffer); i++) {
switch (buffer[i]) {
case ' ':
case '\n':
case '#':
case ';':
buffer[i] = 0;
break;
}
}
}
BOOL load_filters(const char* const fileName)
{
if (!fileName) {
// No problem, we don't filter, then
return TRUE;
}
FILE* file = fopen(fileName, "r");
if (!file) {
printf("Failed to open filter file '%s'\n", fileName);
return FALSE;
}
char buffer[64];
while (!feof(file)) {
if (fgets(buffer, sizeof(buffer), file) != NULL) {
strip(buffer);
add_filter(buffer);
}
}
fclose(file);
return TRUE;
}
BOOL match(const char* const name)
{
if (!name) {
logLine("%s: NULL pointer\n", __func__);
return FALSE;
}
if (count == 0) {
// No filters in use
return TRUE;
}
size_t i;
for (i = 0; i < count; i++) {
if (filters[i] && (strcmp(name, filters[i]) == 0)) {
//logLine("Match '%s' vs '%s'", name, filters[i]);
return TRUE;
}
}
return FALSE;
}
void free_filters(void)
{
size_t i;
for (i = 0; i < count; i++) {
free(filters[i]);
filters[i] = NULL;
}
count = 0;
}