-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_helpers.c
145 lines (122 loc) · 2.3 KB
/
parser_helpers.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "main.h"
/**
* cat_token - concatenates a token to a string
*
* @string: string
* @token: token to concatenate
*/
void cat_token(char **string, char **token)
{
if (*token == NULL)
return;
string_cat(string, *token);
free(*token);
*token = NULL;
}
/**
* push_token - pushes a token to tokens array
*
* @tokens: tokens array
* @token: token to push
*/
void push_token(char ***tokens, char **token)
{
char *trimmed = NULL;
if (*token == NULL)
return;
trimmed = trim_whitespace(*token);
string_array_push(tokens, trimmed);
free(*token);
*token = NULL;
}
/**
* cut_first_token - cuts the first token from a string
*
* @string: string
*
* Return: token
*/
char *cut_first_token(char *string)
{
char *token = NULL;
char *string_copy = NULL;
char *cut_string = NULL;
if (string == NULL)
return (NULL);
string_copy = strdup(string);
if (string_copy == NULL)
return (NULL);
token = _strtok(string_copy, " \t\n");
if (token == NULL)
{
free(string_copy);
return (NULL);
}
cut_string = strdup(string + strlen(token));
if (cut_string == NULL)
{
free(string_copy);
return (NULL);
}
free(string_copy);
return (cut_string);
}
/**
* get_first_token - gets the first token from a string
*
* @string: string
*
* Return: token
*/
char *get_first_token(char *string)
{
char *string_copy = NULL;
char *token = NULL;
if (string == NULL)
return (NULL);
string_copy = strdup(string);
if (string_copy == NULL)
return (NULL);
token = _strtok(string_copy, " \t\n");
if (token == NULL)
{
free(string_copy);
return (NULL);
}
token = strdup(token);
if (token == NULL)
{
free(string_copy);
return (NULL);
}
free(string_copy);
return (token);
}
/**
* parse_delimiter - parses a delimiter
*
* @string: string to parse
* @delim: delimiter
*
* Return: parsed string
*/
char *parse_delimiter(const char *string, char delim)
{
char *parsed_delimter = NULL;
const char *delimiter_end = NULL;
string++;
delimiter_end = strchr(string, delim);
if (delimiter_end == NULL)
{
fprintf(stderr, "Syntax Error: Unmatched delimiter %c\n", delim);
return (NULL);
}
if (string == delimiter_end)
string_cat_char(&parsed_delimter, '\0');
while (string != delimiter_end)
{
string_cat_char(&parsed_delimter, *string);
string++;
}
return (parsed_delimter);
}