]>
Commit | Line | Data |
---|---|---|
f1903ace CP |
1 | /* |
2 | * GT functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
4ad1cf7a | 6 | #include "../lib/irc_string.h" |
f1903ace CP |
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 | ||
9ce4f0be IB |
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 | } | |
f1903ace CP |
34 | localdata->count = 0; |
35 | ||
9ce4f0be IB |
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 | } | |
f1903ace CP |
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; | |
f1903ace CP |
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: | |
c3db6f7e | 88 | intval = (int)((long)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_INT, theinput)); |
f1903ace | 89 | for (i=1;i<localdata->count;i++) { |
c3db6f7e | 90 | if ((int)((long)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_INT, theinput) < intval)) |
f1903ace CP |
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 |