Skip to content

Commit

Permalink
Require complete string match
Browse files Browse the repository at this point in the history
  • Loading branch information
capehill committed Apr 23, 2019
1 parent c67ed6c commit 8901c5d
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ 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) {
size_t i;
for (i = 0; i < MAX_FILTER; i++) {
if (filters[i] == NULL) {
filters[i] = strdup(name);
logLine("Filter[%u] '%s' added", count, filters[i]);
if (count < MAX_FILTER) {
if (filters[count] == NULL) {
filters[count] = strdup(name);
logLine("Filter[%u] '%s' added", count, filters[count]);
count++;
break;
}
}
}
}

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]) {
Expand All @@ -42,36 +50,45 @@ static void strip(char* const buffer)

BOOL load_filters(const char* const fileName)
{
if (fileName) {
FILE* file = fopen(fileName, "r");

if (file) {
char buffer[64];
while (!feof(file)) {
if (fgets(buffer, sizeof(buffer), file) != NULL) {
strip(buffer);
add_filter(buffer);
}
}
if (!fileName) {
// No problem, we don't filter, then
return FALSE;
}

fclose(file);
return TRUE;
FILE* file = fopen(fileName, "r");

if (!file) {
logLine("Failed to open '%s'\n", fileName);
return FALSE;
}

char buffer[64];
while (!feof(file)) {
if (fgets(buffer, sizeof(buffer), file) != NULL) {
strip(buffer);
add_filter(buffer);
}
}

return FALSE;
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 < MAX_FILTER; i++) {
if (filters[i] && strstr(name, filters[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;
}
Expand All @@ -83,7 +100,7 @@ BOOL match(const char* const name)
void free_filters(void)
{
size_t i;
for (i = 0; i < MAX_FILTER; i++) {
for (i = 0; i < count; i++) {
free(filters[i]);
filters[i] = NULL;
}
Expand Down

0 comments on commit 8901c5d

Please sign in to comment.