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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* s_lvars.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include "s_struct.h"
void
lvars_rebind(t_lvars **lvars,
const char name[],
const char newval[])
{
t_lvars *tmp;
tmp = *lvars;
while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name) + 1))
{
tmp = tmp->next;
}
if (tmp == NULL)
{
return ;
}
ft_memdel((void*)&tmp->val);
if (!(tmp->val = ft_strdup(newval)))
{
ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno));
exit(FT_RET_ALLOC);
}
/* TODO: delete this */
tmp = *lvars;
while (tmp) {
ft_printf("[%s]: [%s]\n", tmp->name, tmp->val);
tmp = tmp->next;
}
}
void
lvars_delone(t_lvars **lvars,
const char name[])
{
t_lvars *tmp;
t_lvars *prev;
tmp = *lvars;
if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name) + 1))
{
*lvars = tmp->next;
ft_memdel((void*)&tmp->name);
ft_memdel((void*)&tmp->val);
ft_memdel((void*)&tmp);
return ;
}
while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name) + 1))
{
prev = tmp;
tmp = tmp->next;
}
if (tmp == NULL)
return ;
prev->next = tmp->next;
ft_memdel((void*)&tmp->name);
ft_memdel((void*)&tmp->val);
ft_memdel((void*)&tmp);
}
void
lvars_add_front(t_lvars **alvars,
t_lvars *new)
{
if (!alvars || !new)
{
return ;
}
new->next = *alvars;
*alvars = new;
}
void
lvars_clear(t_lvars **lvars)
{
t_lvars *tmp;
t_lvars *renext;
if (!lvars)
return ;
tmp = *lvars;
while (tmp)
{
renext = tmp->next;
ft_memdel((void*)&tmp->name);
ft_memdel((void*)&tmp->val);
ft_memdel((void*)&tmp);
tmp = renext;
}
*lvars = NULL;
}
t_lvars
*lvars_new(const char name[],
const char val[])
{
t_lvars *link;
if (!(link = (t_lvars*)malloc(sizeof(t_lvars))))
{
return (NULL);
}
if (!(link->name = ft_strdup(name)))
{
return (NULL);
}
if (!(link->val = ft_strdup(val)))
{
return (NULL);
}
link->next = NULL;
return (link);
}
|