correction memcpy pour le cas ou dst et src sont nuls

This commit is contained in:
Hugo LAMY
2019-11-28 13:51:50 +01:00
parent feede2804f
commit b542d8adc0
152 changed files with 3206 additions and 5 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:16 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:56:17 by hulamy ### ########.fr */
/* Updated: 2019/11/28 13:47:43 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,18 +14,31 @@
** copy n characters from src to dst and return dst
*/
/*
** #include <libc.h>
**
** void *ft_memcpy(void *dst, const void *src, size_t n);
**
** int main(int ac, char **av)
** {
** if (ac == 4)
** printf("%s\n", ft_memcpy(av[1], av[2], atoi(av[3])));
** return (0);
** }
*/
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
size_t i;
char *ptr;
char *ptr2;
ptr = (char *)dst;
ptr2 = (char *)src;
i = -1;
while (++i < n)
ptr[i] = ptr2[i];
if (dst == src)
return (dst);
while (n--)
ptr[n] = ptr2[n];
return (dst);
}

BIN
srcs/.DS_Store vendored

Binary file not shown.

26
srcs/ft_any.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_any.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:14:49 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:14:53 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_any(char **tab, int (*f)(char*))
{
int i;
i = -1;
if (!tab)
return (0);
while (tab[++i])
if (f(tab[i]) == 1)
return (1);
return (0);
}

28
srcs/ft_arraymap.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arraymap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:17:24 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:17:27 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int *ft_arraymap(int *tab, int length, int (*f)(int))
{
int i;
int *newtab;
i = -1;
if (!tab)
return (NULL);
if (!(newtab = (int*)malloc(sizeof(*newtab) * (length + 1))))
return (NULL);
while (++i < length)
newtab[i] = (*f)(tab[i]);
return (newtab);
}

39
srcs/ft_atoi.c Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:54:29 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:54:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
long long nbr;
int i;
int n;
i = 0;
n = 1;
nbr = 0;
while ((str[i] == 32) || (str[i] > 8 && str[i] < 14))
i++;
if (str[i] == '-')
n = -1;
if (str[i] == '+' || str[i] == '-')
i++;
while (str[i] >= '0' && str[i] <= '9')
{
if ((nbr >= 922337203685477580
&& ((str[i] > 8 && n < 0) || (str[i] > 7 && n > 0))))
return ((n > 0) ? -1 : 0);
else
nbr = nbr * 10 + (str[i++] - '0');
}
return (nbr * n);
}

75
srcs/ft_atoibase.c Normal file
View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoibase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:15:31 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:22:34 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_valid_base(char *base, int i, int j)
{
while (base[i])
{
j = i + 1;
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
if (base[i] == '-' || base[i] == '+')
return (0);
i++;
}
if (i >= 2)
return (1);
return (0);
}
static int skip(int i, char *str, int *n)
{
while ((str[i] == 32) || (str[i] > 8 && str[i] < 14))
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
*n = -1;
i++;
}
return (i);
}
int ft_atoibase(char *str, char *base)
{
int i;
int j;
int length;
int res;
int n;
length = 0;
res = 0;
n = 1;
if (!is_valid_base(base, 0, 0))
return (0);
while (base[length])
length++;
i = skip(0, str, &n);
while (str[i] && str[i] > 32 && str[i] != '-' && str[i] != '+')
{
j = 0;
while (str[i] != base[j] && base[j])
j++;
if (base[j] == '\0')
return (0);
res = (res * length) + j;
i++;
}
return (res * n);
}

27
srcs/ft_bzero.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:54:43 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:54:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
size_t i;
unsigned char *ptr;
if (n)
{
ptr = (unsigned char *)s;
i = 0;
while (i < n)
ptr[i++] = '\0';
}
}

33
srcs/ft_calloc.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:54:53 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:28:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** exemple allocation for 5 integers with malloc then calloc :
** a = (int *)malloc(5 * sizeof(int)); //5*4bytes = 20 bytes
** free(a);
** a = (int *)calloc(5, sizeof(int));
**
** allocate count * size byte of memory and
** return a pointer to the allocated memory
*/
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *tmp;
if (!count || !size || !(tmp = malloc(count * size)))
return (NULL);
ft_bzero(tmp, count * size);
return (tmp);
}

86
srcs/ft_convertbase.c Normal file
View File

@@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convertbase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:15:55 by hulamy #+# #+# */
/* Updated: 2019/04/17 17:09:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_malloc_size(int decimal, int length, int i)
{
if (decimal <= 0)
i++;
while (decimal)
{
decimal /= length;
i++;
}
return (i);
}
static char *ft_decimal_to_base(int decimal, char *base, char *res, int size)
{
long nb;
int i;
nb = decimal;
i = 0;
while (base[i])
i++;
if (nb < 0)
nb = -nb;
while (--size >= 0)
{
res[size] = base[nb % i];
nb /= i;
}
return (res);
}
static int ft_base_to_decimal(char *nbr, char *base, int length, int i)
{
long decimal;
int j;
decimal = 0;
if (nbr[i] == '-')
i++;
while (nbr[i])
{
j = 0;
while (nbr[i] != base[j] && base[j])
j++;
decimal = (decimal * length) + j;
i++;
}
if (nbr[0] == '-')
decimal = -decimal;
return (decimal);
}
char *ft_convertbase(char *nbr, char *base_from, char *base_to)
{
int length;
int size;
int decimal;
char *res;
res = 0;
length = 0;
while (base_from[length])
length++;
decimal = ft_base_to_decimal(nbr, base_from, length, 0);
length = 0;
while (base_to[length])
length++;
size = ft_malloc_size(decimal, length, 0);
res = (char *)malloc(sizeof(char) * (size + 1));
res[size] = '\0';
return (ft_decimal_to_base(decimal, base_to, res, size));
}

22
srcs/ft_foreach.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_foreach.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:16:10 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:16:11 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_foreach(int *tab, int length, void (*f)(int))
{
int i;
i = 0;
while (i < length && tab && tab[i])
(*f)(tab[i++]);
}

18
srcs/ft_isalnum.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:05 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:06 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

18
srcs/ft_isalpha.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:15 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:17 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

18
srcs/ft_isascii.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:24 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:25 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

18
srcs/ft_isdigit.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:32 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:33 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

18
srcs/ft_isprint.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:43 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (c >= 32 && c < 127);
}

26
srcs/ft_issort.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_issort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:18:14 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:18:15 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_issort(int *tab, int length, int (*f)(int, int))
{
int i;
i = -1;
if (!tab)
return (0);
while (++i < length - 1)
if (f(tab[i], tab[i + 1]) > 0)
return (0);
return (1);
}

55
srcs/ft_itoa.c Normal file
View File

@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:01 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:21:01 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** take an integer and give a string
*/
/*
** #include <libc.h>
**
** char *ft_itoa(int n)
**
** int main(int ac, char **av)
** {
** if (ac == 2)
** {
** printf("%s\n",ft_itoa(atoi(av[1])));
** }
** return 0;
** }
*/
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
int len;
long int nbis;
len = (n < 0) ? 2 : 1;
nbis = n;
while (nbis /= 10)
len++;
nbis = n;
nbis *= (nbis < 0) ? -1 : 1;
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len] = '\0';
str[--len] = nbis % 10 + '0';
while (nbis /= 10)
str[--len] = nbis % 10 + '0';
if (n < 0)
str[0] = '-';
return (str);
}

91
srcs/ft_lstadd_back.c Normal file
View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:11:53 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:12 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the end of a list
** or first if list has no element so far
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new);
**
** int main(void)
** {
** char tresor;
** char matos;
** char friends;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** matos = 'b';
** friends = 'c';
** toto = ft_lstnew(&tresor);
** printf("toto->data :%c\n",*(char*)(toto->content));
** tmp = ft_lstnew(&matos);
** ft_lstadd_back(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** tmp = ft_lstnew(&friends);
** ft_lstadd_back(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstadd_back(t_list **alst, t_list *new)
{
t_list *tmp;
if (alst)
{
tmp = *alst;
if (!tmp)
*alst = new;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
new->next = NULL;
}
}

94
srcs/ft_lstadd_front.c Normal file
View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:12:02 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the begining of a list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** void *ft_memcpy(void *dst, const void *src, size_t n)
** {
** size_t i;
** char *ptr;
** char *ptr2;
**
** ptr = (char *)dst;
** ptr2 = (char *)src;
** i = -1;
** while (++i < n)
** ptr[i] = ptr2[i];
** return (dst);
** }
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** {
** if (!(lst->content = malloc(sizeof(content))))
** return (NULL);
** ft_memcpy(lst->content, content, sizeof(content));
** }
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_front(t_list **alst, t_list *new);
**
** int main(void)
** {
** char tresor;
** char matos;
** char friends;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** matos = 'b';
** friends = 'c';
** toto = ft_lstnew(&tresor);
** printf("toto->data :%c\n",*(char*)(toto->content));
** tmp = ft_lstnew(&matos);
** ft_lstadd_front(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** tmp = ft_lstnew(&friends);
** ft_lstadd_front(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->dqta:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
void ft_lstadd_front(t_list **alst, t_list *new)
{
new->next = *alst;
*alst = new;
}

102
srcs/ft_lstclear.c Normal file
View File

@@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:13:30 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:31:30 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** delete and free an element of the list and all the followings
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new)
** {
** t_list *tmp;
**
** if (alst)
** {
** tmp = *alst;
** if (!tmp)
** *alst = new;
** else
** {
** while (tmp->next)
** tmp = tmp->next;
** tmp->next = new;
** }
** new->next = NULL;
** }
** }
**
** void ft_delete(void *element)
** {
** *(char*)element = '\0';
** }
**
** void ft_lstdelone(t_list *lst, void (*del)(void *))
** {
** del(lst->content);
** free(lst);
** lst = NULL;
** }
**
** void ft_lstclear(t_list **lst, void (*del)(void *));
**
** int main(void)
** {
** t_list *toto;
** void (ft_delete)(void*);
**
** printf("sizeof(t_list)%lu\n",sizeof(t_list));
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** ft_lstclear(&(toto->next), ft_delete);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt :%s\n",(char*)(toto->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
if ((*lst)->next)
ft_lstclear(&(*lst)->next, del);
ft_lstdelone(*lst, del);
}

96
srcs/ft_lstdelone.c Normal file
View File

@@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:03 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:35:53 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** free an element and delete its content with del
** next is not free
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new)
** {
** t_list *tmp;
**
** if (alst)
** {
** tmp = *alst;
** if (!tmp)
** *alst = new;
** else
** {
** while (tmp->next)
** tmp = tmp->next;
** tmp->next = new;
** }
** new->next = NULL;
** }
** }
**
** void ft_delete(void *element)
** {
** *(char*)element = '\0';
** }
**
** void ft_lstdelone(t_list *lst, void (*del)(void *));
**
** int main(void)
** {
** t_list *toto;
** void (ft_delete)(void*);
**
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** ft_lstdelone(toto->next, ft_delete);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
del(lst->content);
free(lst);
lst = NULL;
}

83
srcs/ft_lstiter.c Normal file
View File

@@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:11 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:34:59 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** go through all elements of the list and apply the function f to each of them
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstiter(t_list *lst, void (*f)(void*));
**
** void to_uppercase(void *element)
** {
** // *(char*)(((t_list*)element)->content) -= 32;
** // or :
** t_list *tmp;
**
** tmp = (t_list*)element;
** *(char*)(tmp->content) -= 32;
** }
**
** int main(void)
** {
** t_list *toto;
** void to_uppercase(void*);
**
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** printf("---------------------------\n");
** ft_lstiter(toto, to_uppercase);
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst)
return ;
if (lst->next)
ft_lstiter(lst->next, f);
f(lst);
}

99
srcs/ft_lstlast.c Normal file
View File

@@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:49 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:30:20 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return a pointer to the last element of a list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** void *ft_memcpy(void *dst, const void *src, size_t n)
** {
** size_t i;
** char *ptr;
** char *ptr2;
**
** ptr = (char *)dst;
** ptr2 = (char *)src;
** i = -1;
** while (++i < n)
** ptr[i] = ptr2[i];
** return (dst);
** }
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** {
** if (!(lst->content = malloc(sizeof(content))))
** return (NULL);
** ft_memcpy(lst->content, content, sizeof(content));
** }
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_front(t_list **alst, t_list *new)
** {
** new->next = *alst;
** *alst = new;
** }
**
** t_list *ft_lstlast(t_list *lst);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** toto = ft_lstnew(&tresor);
** tresor = 'b';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** tresor = 'c';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** tmp = ft_lstlast(toto);
** printf("%c\n",*(char*)(tmp->content));
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
while (lst->next)
lst = lst->next;
return (lst);
}

119
srcs/ft_lstmap.c Normal file
View File

@@ -0,0 +1,119 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:15:42 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:34:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** iterate trhough linked list and apply to each element a function f
** if necessary the function del is used to delete an element
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new)
** {
** t_list *tmp;
**
** if (alst)
** {
** tmp = *alst;
** if (!tmp)
** *alst = new;
** else
** {
** while (tmp->next)
** tmp = tmp->next;
** tmp->next = new;
** }
** new->next = NULL;
** }
** }
**
** void *to_uppercase(void *element)
** {
** *(char*)(((t_list*)element)->content) -= 32;
** return (element);
** }
**
** void ft_delete(void *element)
** {
** *(char*)element = '\0';
** }
**
** t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
**
** int main(void)
** {
** t_list *toto;
** void *(to_uppercase)(void *);
** void (ft_delete)(void*);
**
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** ft_lstmap(toto->next, to_uppercase, ft_delete);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new;
t_list *tmp;
if (!lst)
return (NULL);
tmp = (t_list*)f(lst);
new = tmp;
while (lst->next)
{
lst = lst->next;
if (!(tmp->next = (t_list*)f(lst)))
{
del(tmp->next->content);
free(tmp->next);
return (NULL);
}
tmp = tmp->next;
}
return (new);
}

59
srcs/ft_lstnew.c Normal file
View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:16:20 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:29:46 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
**
** tresor = 'd';
** printf("tresor : %c\n",tresor);
** toto = ft_lstnew(&tresor);
** //toto->content was alocated as void* so it need cast
** printf("toto->content : %c\n",*(char*)(toto->content));
** tresor = 'D';
** printf("transform tresor : %c\n",tresor);
** printf("and also toto->content: %c\n",*(char*)(toto->content));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *lst;
if (!(lst = (t_list *)malloc(sizeof(*lst))))
return (NULL);
if (!content)
lst->content = NULL;
else
lst->content = content;
lst->next = NULL;
return (lst);
}

Some files were not shown because too many files have changed in this diff Show More