]>
Commit | Line | Data |
---|---|---|
1 | #include <stdlib.h> | |
2 | #include <stdio.h> /* for debug */ | |
3 | #include <string.h> | |
4 | ||
5 | #include "hcensor.h" | |
6 | ||
7 | #include "hgen.h" | |
8 | ||
9 | hcensor *hcensor_get_by_pattern(hcensor *hcens, const char *pat) | |
10 | { | |
11 | for (;hcens;hcens = hcens->next) | |
12 | if (!ci_strcmp(pat, hcens->pattern->content)) | |
13 | return hcens; | |
14 | return NULL; | |
15 | } | |
16 | ||
17 | hcensor *hcensor_get_by_index(hcensor *hcens, int index) | |
18 | { | |
19 | if (index > 512) /* some sanity */ | |
20 | return NULL; | |
21 | ||
22 | while (index && hcens) | |
23 | { | |
24 | hcens = hcens->next; | |
25 | index--; | |
26 | } | |
27 | ||
28 | return hcens; | |
29 | } | |
30 | ||
31 | hcensor *hcensor_check(hcensor *hcens, const char *str) | |
32 | { | |
33 | for (;hcens;hcens = hcens->next) | |
34 | if (strregexp(str, hcens->pattern->content)) | |
35 | return hcens; | |
36 | return NULL; | |
37 | } | |
38 | ||
39 | hcensor *hcensor_add(hcensor **hcens, const char *pat, const char *rsn) | |
40 | { | |
41 | hcensor *tmp; | |
42 | ||
43 | if (hcensor_get_by_pattern(*hcens, pat)) | |
44 | return NULL; | |
45 | ||
46 | tmp = malloc(sizeof(hcensor)); | |
47 | tmp->next = *hcens; | |
48 | ||
49 | tmp->pattern = getsstring(pat, strlen(pat)); | |
50 | if (rsn) | |
51 | tmp->reason = getsstring(rsn, strlen(rsn)); | |
52 | else | |
53 | tmp->reason = NULL; | |
54 | ||
55 | *hcens = tmp; | |
56 | ||
57 | return tmp; | |
58 | } | |
59 | ||
60 | hcensor *hcensor_del(hcensor **hcens, hcensor *ptr) | |
61 | { | |
62 | for (;*hcens;hcens = &(*hcens)->next) | |
63 | if (*hcens == ptr) | |
64 | { | |
65 | hcensor *tmp = (*hcens)->next; | |
66 | freesstring((*hcens)->pattern); | |
67 | freesstring((*hcens)->reason); | |
68 | free (*hcens); | |
69 | *hcens = tmp; | |
70 | return NULL; | |
71 | } | |
72 | return ptr; | |
73 | } | |
74 | ||
75 | void hcensor_del_all(hcensor **hcens) | |
76 | { | |
77 | while (*hcens) | |
78 | hcensor_del(hcens, *hcens); | |
79 | } | |
80 | ||
81 | int hcensor_count(hcensor *hcens) | |
82 | { | |
83 | int count = 0; | |
84 | for (;hcens;hcens = hcens->next, count++); | |
85 | return count; | |
86 | } |