-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
69 lines (53 loc) · 1.47 KB
/
parse.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "parse.h"
/* Get tokens from a line of characters */
/* Return: new array of pointers to tokens */
/* Effects: token separators in line are replaced with NULL */
/* Storage: Resulting token array points into original line */
#define TOKseparator " \n:"
#define TBracket "["
tok_t *getToks(char *line) {
int i;
char *c;
tok_t *toks = malloc(MAXTOKS*sizeof(tok_t));
for (i=0; i<MAXTOKS; i++) toks[i] = NULL; /* empty token array */
c = strtok(line,TOKseparator); /* Start tokenizer on line */
for (i=0; c && (i < MAXTOKS); i++) {
toks[i] = c;
c = strtok(NULL,TOKseparator); /* scan for next token */
}
return toks;
}
void freeToks(tok_t *toks) {
free(toks);
}
void fprintTok(FILE *ofile, tok_t *t) {
int i;
for (i=0; i<MAXTOKS && t[i]; i++) {
fprintf(ofile,"%s ", t[i]);
}
fprintf(ofile,"\n");
}
/* Locate special processing character */
int isDirectTok(tok_t *t, char *R) {
int i;
for (i=0; i<MAXTOKS-1 && t[i]; i++) {
if (strncmp(t[i],R,1) == 0) return i;
}
return 0;
}
tok_t *getToksB(char *line) {
int i;
char *c;
tok_t *toks = malloc(MAXTOKS*sizeof(tok_t));
for (i=0; i<MAXTOKS; i++) toks[i] = NULL; /* empty token array */
c = strtok(line,TBracket); /* Start tokenizer on line */
for (i=0; c && (i < MAXTOKS); i++) {
toks[i] = c;
c = strtok(NULL,TBracket); /* scan for next token */
}
return toks;
}