]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-and.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[irc/quakenet/newserv.git] / newsearch / ns-and.c
CommitLineData
c86edd1d
Q
1/*
2 * AND functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
c8be5183
CP
10void and_free(searchCtx *ctx, struct searchNode *thenode);
11void *and_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
c86edd1d
Q
12
13struct and_localdata {
14 int count;
15 searchNode **nodes;
16};
17
f33f3f52 18struct searchNode *and_parse(searchCtx *ctx, int argc, char **argv) {
c86edd1d
Q
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 */
9ce4f0be
IB
24 if (!(localdata=(struct and_localdata *)malloc(sizeof(struct and_localdata)))) {
25 parseError = "malloc: could not allocate memory for this search.";
26 return NULL;
27 }
28 if (!(localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *)))) {
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 }
c86edd1d
Q
34 localdata->count=0;
35
36 /* Allocate our actual node */
9ce4f0be
IB
37 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
38 /* couldn't malloc() memory for thenode, so free localdata->nodes and localdata to avoid leakage */
39 parseError = "malloc: could not allocate memory for this search.";
40 free(localdata->nodes);
41 free(localdata);
42 return NULL;
43 }
c86edd1d
Q
44
45 thenode->returntype = RETURNTYPE_BOOL;
46 thenode->localdata = localdata;
47 thenode->exe = and_exe;
48 thenode->free = and_free;
49
50 for (i=0;i<argc;i++) {
f33f3f52 51 subnode=ctx->parser(ctx, argv[i]); /* Propogate the search type */
c8be5183 52 subnode=coerceNode(ctx, subnode, RETURNTYPE_BOOL); /* Needs to return BOOL */
c86edd1d
Q
53 if (subnode) {
54 localdata->nodes[localdata->count++] = subnode;
55 } else {
c8be5183 56 and_free(ctx, thenode); /* ?? */
c86edd1d
Q
57 return NULL;
58 }
59 }
60
61 return thenode;
62}
63
c8be5183 64void and_free(searchCtx *ctx, struct searchNode *thenode) {
c86edd1d
Q
65 struct and_localdata *localdata;
66 int i;
67
68 localdata=thenode->localdata;
69 for (i=0;i<localdata->count;i++) {
c8be5183 70 (localdata->nodes[i]->free)(ctx, localdata->nodes[i]);
c86edd1d
Q
71 }
72
73 free(localdata->nodes);
74 free(localdata);
75 free(thenode);
76}
77
c8be5183 78void *and_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c86edd1d 79 int i;
c86edd1d
Q
80 struct and_localdata *localdata;
81
82 localdata=thenode->localdata;
83
84 for (i=0;i<localdata->count;i++) {
c8be5183 85 if (!(localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput))
c7f7a584 86 return NULL;
c86edd1d 87 }
c7f7a584 88 return (void *)1;
c86edd1d 89}