-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenizer.c
More file actions
36 lines (33 loc) · 838 Bytes
/
tokenizer.c
File metadata and controls
36 lines (33 loc) · 838 Bytes
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "tokenizer.h"
tokenizer* init_tokenizer(char* str, char* delim){
tokenizer* t = malloc(sizeof(tokenizer));
t->str = malloc(sizeof(char) * (strlen(str) + 1));
strcpy(t->str, str);
t->pos = str;
t->delim = malloc(sizeof(char) * (strlen(delim) + 1));
strcpy(t->delim, delim);
return t;
}
char* get_next_token(tokenizer* t) {
if(t->pos == NULL) {return NULL; }
char* delim = t->delim;
int slen= strlen(t->pos);
for(int index = 0; index < slen; index++) {
for(int d = 0; d < strlen(delim); d ++) {
if(delim[d] == t->pos[index]) {
char* result = t->pos + index;
t->pos += (index + 1);
return result;
}
}
}
return NULL;
}
void free_tokenizer(tokenizer* t){
free(t->str);
free(t->delim);
free(t);
}