]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-and.c
Initial Import
[irc/quakenet/newserv.git] / newsearch / ns-and.c
1 /*
2 * AND functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 void and_free(struct searchNode *thenode);
11 void *and_exe(struct searchNode *thenode, int type, void *theinput);
12
13 struct and_localdata {
14 int count;
15 searchNode **nodes;
16 };
17
18 struct searchNode *and_parse(int type, int argc, char **argv) {
19 searchNode *thenode, *subnode;
20 struct and_localdata *localdata;
21 int i;
22
23 /* Set up our local data - a list of nodes to AND together */
24 localdata=(struct and_localdata *)malloc(sizeof(struct and_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 = and_exe;
34 thenode->free = and_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 and_free(thenode);
42 return NULL;
43 }
44 }
45
46 return thenode;
47 }
48
49 void and_free(struct searchNode *thenode) {
50 struct and_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 *and_exe(struct searchNode *thenode, int type, void *theinput) {
64 int i;
65 void *ret;
66 struct and_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 == NULL) {
73 switch (type) {
74
75 case RETURNTYPE_INT:
76 case RETURNTYPE_BOOL:
77 return NULL;
78
79 case RETURNTYPE_STRING:
80 return "";
81 }
82 }
83 }
84
85 switch (type) {
86 case RETURNTYPE_INT:
87 case RETURNTYPE_BOOL:
88 return (void *)1;
89
90 case RETURNTYPE_STRING:
91 return "1";
92 }
93
94 return NULL;
95 }