You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

61 lines
1.6 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:39:03 by emartine #+# #+# */
/* Updated: 2017/11/09 17:39:04 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Potentially unsafe depending to the real way to free the content.
*/
static t_list *ft_lstmap_del(t_list *r)
{
t_list *t;
while (r)
{
t = r;
r = r->next;
if (t->content)
free(t->content);
free(t);
}
return (NULL);
}
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *r;
t_list *c;
t_list *t;
size_t len;
len = ft_lstlen(lst);
if (!(r = ft_lstncreate(len)))
return (NULL);
c = r;
while (len--)
{
t = f(lst);
if (t->content == NULL)
((c->content = NULL)
|| (c->content_size = 0));
else
{
if (!(c->content = ft_memdup(t->content, t->content_size)))
return (ft_lstmap_del(r));
c->content_size = t->content_size;
}
lst = lst->next;
c = c->next;
}
return (r);
}