]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-length.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[irc/quakenet/newserv.git] / newsearch / ns-length.c
CommitLineData
c7f7a584 1/*
2 * length functionality - returns the length of a string
3 */
4
5#include "newsearch.h"
6
7#include "../lib/irc_string.h"
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
c8be5183
CP
12void length_free(searchCtx *ctx, struct searchNode *thenode);
13void *length_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
c7f7a584 14
f33f3f52 15struct searchNode *length_parse(searchCtx *ctx, int argc, char **argv) {
c7f7a584 16 struct searchNode *thenode, *childnode;
17
18 if (!(thenode = (struct searchNode *)malloc(sizeof(struct searchNode)))) {
19 parseError = "malloc: could not allocate memory for this search.";
20 return NULL;
21 }
22
23 thenode->localdata = NULL;
24 thenode->returntype = RETURNTYPE_INT;
25 thenode->exe = length_exe;
26 thenode->free = length_free;
27
28 if (argc != 1) {
29 parseError = "length: usage: length <string>";
30 free(thenode);
31 return NULL;
32 }
33
f33f3f52 34 childnode = ctx->parser(ctx, argv[0]);
c8be5183
CP
35 if (!(thenode->localdata = coerceNode(ctx, childnode, RETURNTYPE_STRING))) {
36 length_free(ctx, thenode);
c7f7a584 37 return NULL;
38 }
39
40 return thenode;
41}
42
c8be5183 43void length_free(searchCtx *ctx, struct searchNode *thenode) {
c7f7a584 44 struct searchNode *anode;
45
46 if ((anode=thenode->localdata))
c8be5183 47 (anode->free)(ctx, anode);
c7f7a584 48
49 free(thenode);
50}
51
c8be5183 52void *length_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c7f7a584 53 char *strval;
54 struct searchNode *anode=thenode->localdata;
55
c8be5183 56 strval=(char *)(anode->exe)(ctx, anode, theinput);
c7f7a584 57
58 return (void *)strlen(strval);
59}