63 lines
1.8 KiB
C
Executable file
63 lines
1.8 KiB
C
Executable file
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_lstmap_bonus.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/10/16 13:22:26 by thrieg #+# #+# */
|
|
/* Updated: 2025/02/16 19:04:01 by thrieg ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
static t_list *ft_safe_create(
|
|
void *(*f)(void *),
|
|
void (*del)(void *),
|
|
void *content)
|
|
{
|
|
void *temp;
|
|
t_list *ret;
|
|
|
|
temp = f(content);
|
|
if (!temp)
|
|
return (NULL);
|
|
ret = ft_lstnew(temp);
|
|
if (!ret)
|
|
{
|
|
del(temp);
|
|
return (NULL);
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
//returns NULL without doing anything else if any argument is NULL
|
|
//returns NULL if any allocation fails
|
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
|
{
|
|
t_list *new_list;
|
|
t_list *new_node;
|
|
t_list *temp;
|
|
|
|
if (!lst || !f || !del)
|
|
return (NULL);
|
|
new_node = ft_safe_create(f, del, lst->content);
|
|
if (!new_node)
|
|
return (NULL);
|
|
new_list = new_node;
|
|
lst = lst->next;
|
|
while (lst)
|
|
{
|
|
temp = ft_safe_create(f, del, lst->content);
|
|
if (!temp)
|
|
{
|
|
ft_lstclear(&new_list, del);
|
|
return (NULL);
|
|
}
|
|
new_node->next = temp;
|
|
new_node = new_node->next;
|
|
lst = lst->next;
|
|
}
|
|
return (new_list);
|
|
}
|