-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_utils1.c
More file actions
103 lines (92 loc) · 2.1 KB
/
parser_utils1.c
File metadata and controls
103 lines (92 loc) · 2.1 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_utils1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yucchen <yucchen@student.42singapore.sg +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:11:47 by sileow #+# #+# */
/* Updated: 2026/01/09 14:37:28 by yucchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
// string list helper for argv
int add_arg(t_strlist **args, const char *value)
{
t_strlist *node;
t_strlist *temp;
node = malloc(sizeof(t_strlist));
if (!node)
return (0);
node->str = ft_strdup(value);
if (!node->str)
{
free(node);
return (0);
}
node->next = NULL;
if (!*args)
*args = node;
else
{
temp = *args;
while (temp->next)
temp = temp->next;
temp->next = node;
}
return (1);
}
static int strlist_size(t_strlist *args)
{
int cnt;
cnt = 0;
while (args)
{
cnt++;
args = args->next;
}
return (cnt);
}
static void free_strlist_nodes(t_strlist *lst)
{
t_strlist *next;
while (lst)
{
next = lst->next;
free(lst);
lst = next;
}
}
// free nodes + strings
void free_strlist_all(t_strlist *lst)
{
t_strlist *next;
while (lst)
{
next = lst->next;
free(lst->str);
free(lst);
lst = next;
}
}
char **strlist_to_argv(t_strlist *args)
{
t_strlist *head;
char **argv;
int size;
int i;
head = args;
i = 0;
size = strlist_size(args);
argv = malloc((size + 1) * sizeof(char *));
if (!argv)
return (free_strlist_all(head), NULL);
while (args)
{
argv[i] = args->str;
i++;
args = args->next;
}
argv[i] = NULL;
return (free_strlist_nodes(head), argv);
}