-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_lstmap.c
56 lines (51 loc) · 1.55 KB
/
ft_lstmap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadhesiv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/18 14:23:49 by oadhesiv #+# #+# */
/* Updated: 2021/01/28 14:41:26 by oadhesiv ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static void cleanup(void *content, size_t len)
{
(void)len;
ft_memdel((void **)&content);
}
static char check(t_list **lst, t_list *elem)
{
if (!elem)
{
ft_lstdel(lst, &cleanup);
return (0);
}
return (1);
}
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *ret;
t_list *prev;
t_list *tmp;
ret = (void *)0;
prev = (void *)0;
while (lst)
{
tmp = ft_lstnew(lst->content, lst->content_size);
if (!check(&ret, tmp))
return ((void *)0);
tmp = f(tmp);
if (!check(&ret, tmp))
return ((void *)0);
if (!ret)
ret = tmp;
if (prev)
prev->next = tmp;
lst = lst->next;
prev = tmp;
}
return (ret);
}