]>
Commit | Line | Data |
---|---|---|
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(searchCtx *ctx, struct searchNode *thenode); | |
13 | void *length_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
14 | ||
15 | struct searchNode *length_parse(searchCtx *ctx, 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 = ctx->parser(ctx, argv[0]); | |
35 | if (!(thenode->localdata = coerceNode(ctx, childnode, RETURNTYPE_STRING))) { | |
36 | length_free(ctx, thenode); | |
37 | return NULL; | |
38 | } | |
39 | ||
40 | return thenode; | |
41 | } | |
42 | ||
43 | void length_free(searchCtx *ctx, struct searchNode *thenode) { | |
44 | struct searchNode *anode; | |
45 | ||
46 | if ((anode=thenode->localdata)) | |
47 | (anode->free)(ctx, anode); | |
48 | ||
49 | free(thenode); | |
50 | } | |
51 | ||
52 | void *length_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
53 | char *strval; | |
54 | struct searchNode *anode=thenode->localdata; | |
55 | ||
56 | strval=(char *)(anode->exe)(ctx, anode, theinput); | |
57 | ||
58 | return (void *)strlen(strval); | |
59 | } |