]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-lt.c
branched in all of pauls stuff
[irc/quakenet/newserv.git] / newsearch / ns-lt.c
1 /*
2 * GT functionality
3 */
4
5 #include "newsearch.h"
6 #include "../lib/irc_string.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 struct lt_localdata {
12 int count;
13 struct searchNode **nodes;
14 };
15
16 void lt_free(struct searchNode *thenode);
17 void *lt_exe(struct searchNode *thenode, int type, void *theinput);
18
19 struct searchNode *lt_parse(int type, int argc, char **argv) {
20 struct lt_localdata *localdata;
21 struct searchNode *thenode;
22 int i;
23
24 if (!(localdata = (struct lt_localdata *)malloc(sizeof(struct lt_localdata)))) {
25 parseError = "malloc: could not allocate memory for this search.";
26 return NULL;
27 }
28 if (!(localdata->nodes = (struct searchNode **)malloc(sizeof(struct searchNode *) * argc))) {
29 /* couldn't malloc() memory for localdata->nodes, so free localdata to avoid leakage */
30 parseError = "malloc: could not allocate memory for this search.";
31 free(localdata);
32 return NULL;
33 }
34 localdata->count = 0;
35
36 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
37 /* couldn't malloc() memory for thenode, so free localdata->nodes and localdata to avoid leakage */
38 parseError = "malloc: could not allocate memory for this search.";
39 free(localdata->nodes);
40 free(localdata);
41 return NULL;
42 }
43
44 thenode->localdata = localdata;
45 thenode->returntype = RETURNTYPE_BOOL;
46 thenode->exe = lt_exe;
47 thenode->free = lt_free;
48
49 for (i=0;i<argc;i++) {
50 if (!(localdata->nodes[i] = search_parse(type, argv[i]))) {
51 lt_free(thenode);
52 return NULL;
53 }
54 localdata->count++;
55 }
56
57 return thenode;
58 }
59
60 void lt_free(struct searchNode *thenode) {
61 struct lt_localdata *localdata;
62 int i;
63
64 localdata=thenode->localdata;
65
66 for (i=0;i<localdata->count;i++) {
67 (localdata->nodes[i]->free)(localdata->nodes[i]);
68 }
69
70 free(localdata->nodes);
71 free(localdata);
72 free(thenode);
73 }
74
75 void *lt_exe(struct searchNode *thenode, int type, void *theinput) {
76 int i;
77 char *strval;
78 int intval;
79 struct lt_localdata *localdata;
80
81 localdata=thenode->localdata;
82
83 if (localdata->count==0)
84 return trueval(type);
85
86 switch (localdata->nodes[0]->returntype & RETURNTYPE_TYPE) {
87 case RETURNTYPE_INT:
88 intval = (int)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_INT, theinput);
89 for (i=1;i<localdata->count;i++) {
90 if ((int)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_INT, theinput) < intval)
91 return falseval(type);
92 }
93
94 return trueval(type);
95
96 case RETURNTYPE_STRING:
97 strval = (char *)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_STRING, theinput);
98 for (i=1;i<localdata->count;i++) {
99 if (ircd_strcmp(strval, (char *)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_STRING, theinput)) < 0)
100 return falseval(type);
101 }
102
103 return trueval(type);
104
105 default:
106 return falseval(type);
107 }
108 }
109