]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-eq.c
Initial Import
[irc/quakenet/newserv.git] / newsearch / ns-eq.c
1 /*
2 * EQ functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 struct eq_localdata {
11 int count;
12 struct searchNode **nodes;
13 };
14
15 void eq_free(struct searchNode *thenode);
16 void *eq_exe(struct searchNode *thenode, int type, void *theinput);
17
18 struct searchNode *eq_parse(int type, int argc, char **argv) {
19 struct eq_localdata *localdata;
20 struct searchNode *thenode;
21 int i;
22
23 localdata = (struct eq_localdata *)malloc(sizeof(struct eq_localdata));
24 localdata->nodes = (struct searchNode **)malloc(sizeof(struct searchNode *) * argc);
25 localdata->count = 0;
26
27 thenode = (struct searchNode *)malloc(sizeof(struct searchNode));
28
29 thenode->localdata = localdata;
30 thenode->returntype = RETURNTYPE_BOOL;
31 thenode->exe = eq_exe;
32 thenode->free = eq_free;
33
34 for (i=0;i<argc;i++) {
35 if (!(localdata->nodes[i] = search_parse(type, argv[i]))) {
36 eq_free(thenode);
37 return NULL;
38 }
39 localdata->count++;
40 }
41
42 return thenode;
43 }
44
45 void eq_free(struct searchNode *thenode) {
46 struct eq_localdata *localdata;
47 int i;
48
49 localdata=thenode->localdata;
50
51 for (i=0;i<localdata->count;i++) {
52 (localdata->nodes[i]->free)(localdata->nodes[i]);
53 }
54
55 free(localdata->nodes);
56 free(localdata);
57 free(thenode);
58 }
59
60 void *eq_exe(struct searchNode *thenode, int type, void *theinput) {
61 int i;
62 char *strval;
63 int intval;
64 int rval;
65 struct eq_localdata *localdata;
66
67 localdata=thenode->localdata;
68
69 if (localdata->count==0)
70 return trueval(type);
71
72 switch (localdata->nodes[0]->returntype & RETURNTYPE_TYPE) {
73 case RETURNTYPE_INT:
74 intval = (int)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_INT, theinput);
75 for (i=1;i<localdata->count;i++) {
76 if ((int)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_INT, theinput) != intval)
77 return falseval(type);
78 }
79
80 return trueval(type);
81
82 case RETURNTYPE_BOOL:
83 intval = (int)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_BOOL, theinput);
84 for (i=1;i<localdata->count;i++) {
85 rval=(int)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput);
86 if ((rval && !intval) || (!rval && intval)) { /* LOGICAL XOR GOES HERE FS */
87 return falseval(type);
88 }
89 }
90
91 return trueval(type);
92
93 case RETURNTYPE_STRING:
94 strval = (char *)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_STRING, theinput);
95 for (i=1;i<localdata->count;i++) {
96 if (ircd_strcmp(strval, (char *)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_STRING, theinput)))
97 return falseval(type);
98 }
99
100 return trueval(type);
101
102 default:
103 return falseval(type);
104 }
105 }
106