-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
82 lines (66 loc) · 1.88 KB
/
parser.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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "lib/dbg.h"
#include "parser.h"
#include <stdio.h>
#include <stdlib.h>
char **tokenize_str(char line[])
{
/*
* Divides the string 'string' into several strings.
* The string is divided on each position where the char 'delimiter' is
* found.
* Returns a array of strings.
*/
int bufsize = TOK_BUFSIZE;
int position = 0;
char **tokens = malloc(sizeof(char*) * bufsize);
check(tokens != NULL, "Memory allocation failed.");
char *token = strtok(line, TOK_DELIMITERS);
while (token) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += TOK_BUFSIZE;
tokens = realloc(tokens, sizeof(char*) * bufsize);
check(tokens != NULL, "Memory allocation failed.");
}
// Pass NULL to obtain the next token.
token = strtok(NULL, TOK_DELIMITERS);
}
// Descrease the size of the array to avoid using unnecessary space.
tokens = realloc(tokens, sizeof(char*) * (position+1));
check(tokens != NULL, "Memory reallocation failed.");
tokens[position] = NULL;
return tokens;
error:
if(tokens) free(tokens);
exit(-1);
}
char *readline(void)
{
int bufsize = RL_BUFSIZE;
int position = 0;
char *buffer = malloc(sizeof(char) * bufsize);
int c;
check(buffer != NULL, "Allocation error.");
while (1) {
c = getchar();
// If we hit EOF or a new line, replace it with a null character and
// return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
check(buffer != NULL, "Allocation error.");
}
}
return buffer;
error:
exit(EXIT_FAILURE);
}