-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
122 lines (111 loc) · 2.36 KB
/
get_next_line_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yrhiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/16 02:57:40 by yrhiba #+# #+# */
/* Updated: 2022/10/19 04:19:26 by yrhiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_join(char *s1, char *s2)
{
char *rtn;
char *tmp;
int i;
rtn = (char *)malloc(sizeof(char) * (ft_strlc(s1, 0) + ft_strlc(s2, 0)
+ 1));
if (!rtn)
return (NULL);
tmp = rtn;
i = 0;
while (s1[i])
*tmp++ = s1[i++];
i = 0;
while (s2[i])
*tmp++ = s2[i++];
*tmp = '\0';
free(s1);
return (rtn);
}
int ft_strlc(char *s, int len0_or_chr1)
{
int i;
i = 0;
while (s[i] && len0_or_chr1)
{
if (s[i] == '\n')
return (i);
i++;
}
if (!len0_or_chr1)
{
while (s[i])
i++;
return (i);
}
return (-1);
}
t_list *ft_lstnew(char *content, int fd)
{
t_list *node;
node = (t_list *)malloc(sizeof(t_list));
if (!node)
return (NULL);
node->content = content;
node->fd = fd;
node->next = 0;
return (node);
}
char *ft_get_content(t_list **list_o, int fd)
{
t_list *new;
t_list *list;
char *rtn;
list = *list_o;
while (list)
{
if (list->fd == fd)
return (list->content);
list = list->next;
}
rtn = (char *)malloc(sizeof(char));
if (!rtn)
return (NULL);
*rtn = '\0';
new = ft_lstnew(rtn, fd);
if (!new)
{
free(rtn);
return (NULL);
}
new->next = *list_o;
*list_o = new;
return ((*list_o)->content);
}
void delete_node(t_list **list, int fd)
{
t_list *tmp;
t_list *nex;
t_list *prev;
tmp = *list;
prev = NULL;
while (tmp)
{
nex = tmp->next;
if (tmp->fd == fd)
{
free(tmp->content);
free(tmp);
if (prev)
prev->next = nex;
else
*list = nex;
return ;
}
prev = tmp;
tmp = tmp->next;
}
}