-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsefields.c
48 lines (40 loc) · 832 Bytes
/
parsefields.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "ipf.h"
extern int nohdrfields;
wordtab_t *parsefields(table, arg)
wordtab_t *table;
char *arg;
{
wordtab_t *f, *fields;
char *s, *t;
int num;
fields = NULL;
num = 0;
for (s = strtok(arg, ","); s != NULL; s = strtok(NULL, ",")) {
t = strchr(s, '=');
if (t != NULL) {
*t++ = '\0';
if (*t == '\0')
nohdrfields = 1;
}
f = findword(table, s);
if (f == NULL) {
fprintf(stderr, "Unknown field '%s'\n", s);
exit(1);
}
num++;
if (fields == NULL) {
fields = malloc(2 * sizeof(*fields));
} else {
fields = realloc(fields, (num + 1) * sizeof(*fields));
}
if (t == NULL) {
fields[num - 1].w_word = f->w_word;
} else {
fields[num - 1].w_word = t;
}
fields[num - 1].w_value = f->w_value;
fields[num].w_word = NULL;
fields[num].w_value = 0;
}
return fields;
}