-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineParser.c
More file actions
48 lines (42 loc) · 824 Bytes
/
lineParser.c
File metadata and controls
48 lines (42 loc) · 824 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
37
38
39
40
41
42
43
44
45
46
47
48
#include "main.h"
/**
*lineParser - split a string into multiple strings
*@line: string to be splited
*
*Return: pointer that points to the new array
*/
char **lineParser(char *line)
{
int bufsize = 64;
int i = 0;
char **tokens = malloc(bufsize * sizeof(char *));
char *token;
if (!tokens)
{
fprintf(stderr, "allocation error in split_line: tokens\n");
exit(EXIT_FAILURE);
}
token = strtok(line, TOK_DELIM);
while (token != NULL)
{
if (token[0] == '#')
{
break;
}
tokens[i] = token;
i++;
if (i >= bufsize)
{
bufsize += bufsize;
tokens = realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
fprintf(stderr, "reallocation error in split_line: tokens");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, TOK_DELIM);
}
tokens[i] = NULL;
return (tokens);
}