/
parse.c
53 lines (45 loc) · 982 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parse.h"
typedef int BOOLEAN;
char *buffer;
char *initialBuffer;
int bufferSize;
char* removeToken(char* substring, char* string) {
int len = strlen(substring);
if (bufferSize == 1) {
string += len;
} else {
string += len+1;
}
return string;
}
void initBuffer(char *inputLine) {
buffer = inputLine;
bufferSize = 1;
int len = strlen(buffer);
for(int i=0; i <= len; i++) {
if(buffer[i] == ' ')
bufferSize++;
}
initialBuffer = malloc(bufferSize);
strcpy(initialBuffer, buffer);
}
BOOLEAN hasNextToken() {
if (bufferSize != 0)
return 1;
return 0;
}
char *nextToken() {
char *copy = malloc(bufferSize);
strcpy(copy, buffer);
char *next = strtok(copy, " ");
buffer = removeToken(next, buffer);
bufferSize--;
free(copy);
return next;
}
void myRewind() {
buffer = initialBuffer;
}