-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from mariux64/add-blackwhitelists
Add black and whitelists
- Loading branch information
Showing
18 changed files
with
692 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set tabstop=4 | ||
set shiftwidth=4 | ||
set softtabstop=4 | ||
set expandtab | ||
set nosmarttab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
#include "keywordset.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <ctype.h> | ||
#include <string.h> | ||
#include "xmalloc.h" | ||
|
||
#define KEYWORDSET_INITIAL_SLOTS (4-2) | ||
|
||
struct keywordset { | ||
int nr_slots; | ||
int used; | ||
char **names; | ||
}; | ||
|
||
static int find_name(struct keywordset *kws, char *name, size_t len) { | ||
int i; | ||
int j; | ||
for ( i = 0; i < kws->used ; i++ ) { | ||
j = 0; | ||
while(1) { | ||
if (kws->names[i][j] == 0) | ||
break; | ||
if (kws->names[i][j] != name[j]) | ||
break; | ||
j++; | ||
if (j==len) | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
static void expand(struct keywordset *kws) { | ||
int new_slots=(kws->nr_slots+2)*2-2; | ||
kws->names=xrealloc(kws->names,new_slots*sizeof(*kws->names)); | ||
kws->nr_slots=new_slots; | ||
} | ||
|
||
static void add_name(struct keywordset *kws, char *name, size_t len) { | ||
int i=find_name(kws, name, len); | ||
if (i>=0) { | ||
free(kws->names[i]); | ||
kws->names[i] = xstrndup(name, len); | ||
} else { | ||
if (kws->used == kws->nr_slots) | ||
expand(kws); | ||
kws->names[kws->used++] = xstrndup(name, len); | ||
} | ||
} | ||
|
||
static void remove_name(struct keywordset *kws, char *name, size_t len) { | ||
int i=find_name(kws, name, len); | ||
if (i>=0) { | ||
free(kws->names[i]); | ||
memmove(&(kws->names[i]), &(kws->names[i+1]), (kws->used-i-1)*sizeof(*kws->names)); | ||
kws->used--; | ||
} | ||
} | ||
|
||
void keywordset_purge(struct keywordset *kws) { | ||
int i; | ||
for ( i = 0 ; i < kws->used ; i++) | ||
free(kws->names[i]); | ||
kws->used = 0; | ||
} | ||
|
||
enum PHASE { | ||
PHASE_SET, | ||
PHASE_UPDATE, | ||
}; | ||
|
||
static void keywordset_update_phase(struct keywordset *kws, char *input, enum PHASE phase) { | ||
char *c=input; | ||
char *name_start; | ||
char action; | ||
int purged=0; | ||
while (*c) { | ||
while (*c && isspace(*c)) | ||
c++; | ||
|
||
if (*c == '+' || *c == '-') { | ||
action = *c; | ||
c++; | ||
} else { | ||
action = ' '; | ||
} | ||
|
||
if (*c) { | ||
name_start=c++; | ||
while (*c && !isspace(*c)) | ||
c++; | ||
if (phase == PHASE_SET && action==' ') { | ||
if (!purged) { | ||
keywordset_purge(kws); | ||
purged = 1; | ||
} | ||
add_name(kws, name_start, c-name_start); | ||
} else if (phase == PHASE_UPDATE) { | ||
if (action == '+') | ||
add_name(kws, name_start, c-name_start); | ||
else if (action == '-') | ||
remove_name(kws, name_start, c-name_start); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void keywordset_update(struct keywordset *kws, char *input) { | ||
keywordset_update_phase(kws, input, PHASE_SET); | ||
keywordset_update_phase(kws, input, PHASE_UPDATE); | ||
} | ||
|
||
struct keywordset *keywordset_new(char *input) { | ||
struct keywordset *kws = xmalloc(sizeof(*kws)); | ||
kws->nr_slots = KEYWORDSET_INITIAL_SLOTS; | ||
kws->used = 0; | ||
kws->names = xmalloc(KEYWORDSET_INITIAL_SLOTS*sizeof(*kws->names)); | ||
if (input) | ||
keywordset_update(kws, input); | ||
return kws; | ||
} | ||
|
||
static int cmp(const void *a, const void *b) { | ||
return strcmp(*(char **)a, *(char **)b); | ||
} | ||
|
||
char *keywordset_get(struct keywordset *kws) { | ||
char **names=xmalloc(kws->used * sizeof(*names)); | ||
memcpy(names, kws->names, kws->used * sizeof(*names)); | ||
qsort(names, kws->used, sizeof(*names), cmp); | ||
size_t len = 0; | ||
int i; | ||
for (i=0; i<kws->used; i++) { | ||
len += strlen(names[i]); | ||
} | ||
size_t outlen = len + (kws->used >= 2 ? kws->used-1 : 0); | ||
char *out=xmalloc(outlen + 1 ); | ||
char *p=out; | ||
for ( i = 0 ; i < kws->used ; i++) { | ||
p=stpcpy(p, names[i]); | ||
*p++ = ' '; | ||
} | ||
out[outlen] = 0; | ||
free(names); | ||
return(out); | ||
} | ||
|
||
int keywordset_ismember(struct keywordset *kws, char *name) { | ||
if (find_name(kws, name, strlen(name)) >= 0) | ||
return 1; | ||
else | ||
return 0; | ||
} | ||
|
||
void keywordset_free(struct keywordset *kws) { | ||
int i; | ||
for ( i = 0 ; i < kws->used ; i++) | ||
free(kws->names[i]); | ||
free(kws->names); | ||
free(kws); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef _KEYWORDSET_H | ||
#define _KEYWORDSET_H | ||
|
||
struct keywordset *keywordset_new(char *input); | ||
void keywordset_update(struct keywordset *kws, char *input); | ||
char *keywordset_get(struct keywordset *kws); | ||
int keywordset_ismember(struct keywordset *kws, char *name); | ||
void keywordset_purge(struct keywordset *kws); | ||
void keywordset_free(struct keywordset *kws); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.