]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-length.c
Merge
[irc/quakenet/newserv.git] / newsearch / ns-length.c
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
12 void length_free(struct searchNode *thenode);
13 void *length_exe(struct searchNode *thenode, void *theinput);
14
15 struct searchNode *length_parse(int type, int argc, char **argv) {
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
34 childnode = search_parse(type, argv[0]);
35 if (!(thenode->localdata = coerceNode(childnode, RETURNTYPE_STRING))) {
36 length_free(thenode);
37 return NULL;
38 }
39
40 return thenode;
41 }
42
43 void length_free(struct searchNode *thenode) {
44 struct searchNode *anode;
45
46 if ((anode=thenode->localdata))
47 (anode->free)(anode);
48
49 free(thenode);
50 }
51
52 void *length_exe(struct searchNode *thenode, void *theinput) {
53 char *strval;
54 struct searchNode *anode=thenode->localdata;
55
56 strval=(char *)(anode->exe)(anode, theinput);
57
58 return (void *)strlen(strval);
59 }