]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * OR functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | void or_free(struct searchNode *thenode); | |
11 | void *or_exe(struct searchNode *thenode, int type, void *theinput); | |
12 | ||
13 | struct or_localdata { | |
14 | int count; | |
15 | searchNode **nodes; | |
16 | }; | |
17 | ||
18 | struct searchNode *or_parse(int type, int argc, char **argv) { | |
19 | searchNode *thenode, *subnode; | |
20 | struct or_localdata *localdata; | |
21 | int i; | |
22 | ||
23 | /* Set up our local data - a list of nodes to OR together */ | |
24 | localdata=(struct or_localdata *)malloc(sizeof(struct or_localdata)); | |
25 | localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *)); | |
26 | localdata->count=0; | |
27 | ||
28 | /* Allocate our actual node */ | |
29 | thenode=(searchNode *)malloc(sizeof(searchNode)); | |
30 | ||
31 | thenode->returntype = RETURNTYPE_BOOL; | |
32 | thenode->localdata = localdata; | |
33 | thenode->exe = or_exe; | |
34 | thenode->free = or_free; | |
35 | ||
36 | for (i=0;i<argc;i++) { | |
37 | subnode=search_parse(type, argv[i]); /* Propogate the search type */ | |
38 | if (subnode) { | |
39 | localdata->nodes[localdata->count++] = subnode; | |
40 | } else { | |
41 | or_free(thenode); | |
42 | return NULL; | |
43 | } | |
44 | } | |
45 | ||
46 | return thenode; | |
47 | } | |
48 | ||
49 | void or_free(struct searchNode *thenode) { | |
50 | struct or_localdata *localdata; | |
51 | int i; | |
52 | ||
53 | localdata=thenode->localdata; | |
54 | for (i=0;i<localdata->count;i++) { | |
55 | (localdata->nodes[i]->free)(localdata->nodes[i]); | |
56 | } | |
57 | ||
58 | free(localdata->nodes); | |
59 | free(localdata); | |
60 | free(thenode); | |
61 | } | |
62 | ||
63 | void *or_exe(struct searchNode *thenode, int type, void *theinput) { | |
64 | int i; | |
65 | void *ret; | |
66 | struct or_localdata *localdata; | |
67 | ||
68 | localdata=thenode->localdata; | |
69 | ||
70 | for (i=0;i<localdata->count;i++) { | |
71 | ret = (localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput); | |
72 | if (ret) { | |
73 | switch (type) { | |
74 | case RETURNTYPE_STRING: | |
75 | return "1"; | |
76 | ||
77 | case RETURNTYPE_INT: | |
78 | case RETURNTYPE_BOOL: | |
79 | default: | |
80 | return (void *)1; | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | switch(type) { | |
86 | case RETURNTYPE_STRING: | |
87 | return ""; | |
88 | ||
89 | case RETURNTYPE_INT: | |
90 | case RETURNTYPE_BOOL: | |
91 | default: | |
92 | return NULL; | |
93 | } | |
94 | } |