From fc29371a10bf6cfd93c4e9ccfe9193c1311efc98 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 27 Dec 2019 17:24:35 +0100 Subject: updated libft, better Makefile and .gitignore --- libft/src/ft_atoi.c | 19 +------- libft/src/ft_bzero.c | 4 +- libft/src/ft_calloc.c | 2 +- libft/src/ft_intlen.c | 32 +++++++++++++ libft/src/ft_intlen_base.c | 35 ++++++++++++++ libft/src/ft_isalnum.c | 2 +- libft/src/ft_ischarset.c | 31 ++++++++++++ libft/src/ft_itoa.c | 26 ++-------- libft/src/ft_itoa_base.c | 32 +++++++++++++ libft/src/ft_kernel_panic.c | 27 +++++++++++ libft/src/ft_lstadd_back.c | 2 +- libft/src/ft_lstadd_front.c | 2 +- libft/src/ft_lstclear.c | 2 +- libft/src/ft_lstdelone.c | 2 +- libft/src/ft_lstiter.c | 2 +- libft/src/ft_lstlast.c | 2 +- libft/src/ft_lstmap.c | 2 +- libft/src/ft_lstnew.c | 2 +- libft/src/ft_lstsize.c | 2 +- libft/src/ft_memchr.c | 17 ++++--- libft/src/ft_memcmp.c | 2 - libft/src/ft_memcpy.c | 2 +- libft/src/ft_memlchr.c | 26 ++++++++++ libft/src/ft_memmove.c | 2 +- libft/src/ft_memset.c | 2 - libft/src/ft_nstr.c | 33 +++++++++++++ libft/src/ft_putchar_fd.c | 4 +- libft/src/ft_putendl.c | 2 +- libft/src/ft_putendl_fd.c | 2 +- libft/src/ft_putnbr.c | 10 ++-- libft/src/ft_putnbr_base.c | 37 +++++++++++++++ libft/src/ft_putnbr_fd.c | 2 +- libft/src/ft_putnchar.c | 29 ++++++++++++ libft/src/ft_putstr.c | 2 +- libft/src/ft_putstr_fd.c | 6 +-- libft/src/ft_realloc.c | 26 ++++++++++ libft/src/ft_split.c | 2 +- libft/src/ft_strchr.c | 2 - libft/src/ft_strcmp.c | 11 +++-- libft/src/ft_strdup.c | 20 ++++---- libft/src/ft_strjoin.c | 42 ++++------------- libft/src/ft_strlcat.c | 19 +------- libft/src/ft_strlcpy.c | 2 +- libft/src/ft_strlen.c | 12 ++--- libft/src/ft_strmapi.c | 2 +- libft/src/ft_strncmp.c | 4 +- libft/src/ft_strnlen.c | 2 +- libft/src/ft_strnstr.c | 2 +- libft/src/ft_strrchr.c | 4 +- libft/src/ft_strtrim.c | 2 +- libft/src/ft_substr.c | 23 ++++----- libft/src/get_next_line.c | 112 ++++++++++++++++++++++++++++++++++++++++++++ 52 files changed, 517 insertions(+), 176 deletions(-) create mode 100644 libft/src/ft_intlen.c create mode 100644 libft/src/ft_intlen_base.c create mode 100644 libft/src/ft_ischarset.c create mode 100644 libft/src/ft_itoa_base.c create mode 100644 libft/src/ft_kernel_panic.c create mode 100644 libft/src/ft_memlchr.c create mode 100644 libft/src/ft_nstr.c create mode 100644 libft/src/ft_putnbr_base.c create mode 100644 libft/src/ft_putnchar.c create mode 100644 libft/src/ft_realloc.c create mode 100644 libft/src/get_next_line.c (limited to 'libft/src') diff --git a/libft/src/ft_atoi.c b/libft/src/ft_atoi.c index 8903606..4459c1d 100644 --- a/libft/src/ft_atoi.c +++ b/libft/src/ft_atoi.c @@ -11,22 +11,9 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include -static uint8_t - ft_isaspace(int c) -{ - if (c == '\t' || - c == '\n' || - c == '\v' || - c == '\f' || - c == '\r' || - c == ' ') - return (1); - return (0); -} - static int8_t ft_setsign(const char c) { @@ -44,7 +31,7 @@ static uint8_t uint8_t i; i = 0; - while (ft_isaspace(str[i])) + while (ft_isspace(str[i])) i++; return (i); } @@ -56,8 +43,6 @@ int int8_t sign; long nb; - if (!str || !*str) - return (0); i = ft_seti(str); nb = 0; sign = 1; diff --git a/libft/src/ft_bzero.c b/libft/src/ft_bzero.c index 05ff6b7..1f09fbe 100644 --- a/libft/src/ft_bzero.c +++ b/libft/src/ft_bzero.c @@ -11,13 +11,11 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include void ft_bzero(void *s, size_t n) { - if (!s) - return ; ft_memset(s, 0, n); } diff --git a/libft/src/ft_calloc.c b/libft/src/ft_calloc.c index 49383da..4ec518e 100644 --- a/libft/src/ft_calloc.c +++ b/libft/src/ft_calloc.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include #include diff --git a/libft/src/ft_intlen.c b/libft/src/ft_intlen.c new file mode 100644 index 0000000..17a22b3 --- /dev/null +++ b/libft/src/ft_intlen.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_intlen.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/07 02:24:29 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/07 02:24:30 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include + +uint8_t + ft_intlen(long long n) +{ + uint8_t len; + + len = 0; + if (!n) + return (1); + if (n < 0) + len = 1; + while (n != 0) + { + n /= 10; + len++; + } + return (len); +} diff --git a/libft/src/ft_intlen_base.c b/libft/src/ft_intlen_base.c new file mode 100644 index 0000000..9de740c --- /dev/null +++ b/libft/src/ft_intlen_base.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_intlen_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/19 17:22:26 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/19 17:22:27 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include + +uint8_t + ft_intlen_base(long long n, char *base) +{ + uint8_t len; + uint8_t size; + + size = ft_strlen(base); + len = 0; + if (!n) + return (1); + if (n < 0) + len = 1; + while (n != 0) + { + n /= size; + len++; + } + return (len); +} diff --git a/libft/src/ft_isalnum.c b/libft/src/ft_isalnum.c index e9e1134..1ab9ca2 100644 --- a/libft/src/ft_isalnum.c +++ b/libft/src/ft_isalnum.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include int ft_isalnum(int c) diff --git a/libft/src/ft_ischarset.c b/libft/src/ft_ischarset.c new file mode 100644 index 0000000..24d7b6e --- /dev/null +++ b/libft/src/ft_ischarset.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_ischarset.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/22 16:48:45 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/22 16:48:47 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include +#include + +uint8_t + ft_ischarset(const char *charset, int c) +{ + size_t i; + + i = 0; + while (charset[i]) + { + if (charset[i] == c) + return (1); + i++; + } + return (0); +} diff --git a/libft/src/ft_itoa.c b/libft/src/ft_itoa.c index 98f33f2..2f68068 100644 --- a/libft/src/ft_itoa.c +++ b/libft/src/ft_itoa.c @@ -11,36 +11,18 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include #include -static uint8_t - ft_intllen(int n) -{ - uint8_t len; - - len = 0; - if (!n) - return (1); - if (n < 0) - len = 1; - while (n != 0) - { - n /= 10; - len++; - } - return (len); -} - char - *ft_itoa(int n) + *ft_itoa(long long n) { char *s; - unsigned int nb; + long long nb; uint8_t i; - i = ft_intllen(n) - 1; + i = ft_intlen(n) - 1; if (!(s = (char*)malloc((i + 2) * sizeof(char)))) return (NULL); if (!n) diff --git a/libft/src/ft_itoa_base.c b/libft/src/ft_itoa_base.c new file mode 100644 index 0000000..3c84616 --- /dev/null +++ b/libft/src/ft_itoa_base.c @@ -0,0 +1,32 @@ +#include +#include + +char + *ft_itoa_base(long long n, char *base) +{ + char *s; + long long nb; + uint8_t i; + + i = ft_intlen_base(n, base) - 1; + if (!(s = (char*)malloc((i + 2) * sizeof(char)))) + return (NULL); + if (!n) + s[i] = '0'; + if (n < 0) + { + s[0] = '-'; + nb = -n; + } + s[i + 1] = '\0'; + while (n > 0) + { + nb = n; + if (nb >= (long long)ft_strlen(base)) + nb = n % ft_strlen(base); + s[i] = base[nb]; + n /= ft_strlen(base); + i--; + } + return (s); +} diff --git a/libft/src/ft_kernel_panic.c b/libft/src/ft_kernel_panic.c new file mode 100644 index 0000000..d58047a --- /dev/null +++ b/libft/src/ft_kernel_panic.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_kernel_panic.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/11 18:26:08 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/11 18:26:09 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include + +void + ft_kernel_panic(void) +{ + int *ptr; + + while (1) + { + ptr = (int *)ft_calloc((1024 * 1024) * sizeof(int), 1); + } + free(ptr); +} diff --git a/libft/src/ft_lstadd_back.c b/libft/src/ft_lstadd_back.c index 58b3a72..1c8aeeb 100644 --- a/libft/src/ft_lstadd_back.c +++ b/libft/src/ft_lstadd_back.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include void diff --git a/libft/src/ft_lstadd_front.c b/libft/src/ft_lstadd_front.c index c9515ea..796740b 100644 --- a/libft/src/ft_lstadd_front.c +++ b/libft/src/ft_lstadd_front.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include void ft_lstadd_front(t_list **alst, t_list *new) diff --git a/libft/src/ft_lstclear.c b/libft/src/ft_lstclear.c index 8d97b9c..936672d 100644 --- a/libft/src/ft_lstclear.c +++ b/libft/src/ft_lstclear.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include void diff --git a/libft/src/ft_lstdelone.c b/libft/src/ft_lstdelone.c index 42a1da8..47f0669 100644 --- a/libft/src/ft_lstdelone.c +++ b/libft/src/ft_lstdelone.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include void diff --git a/libft/src/ft_lstiter.c b/libft/src/ft_lstiter.c index 3129ab5..2570cf0 100644 --- a/libft/src/ft_lstiter.c +++ b/libft/src/ft_lstiter.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include void diff --git a/libft/src/ft_lstlast.c b/libft/src/ft_lstlast.c index a685322..0ac542a 100644 --- a/libft/src/ft_lstlast.c +++ b/libft/src/ft_lstlast.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include t_list diff --git a/libft/src/ft_lstmap.c b/libft/src/ft_lstmap.c index 474b1bc..9aed5b9 100644 --- a/libft/src/ft_lstmap.c +++ b/libft/src/ft_lstmap.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include #include diff --git a/libft/src/ft_lstnew.c b/libft/src/ft_lstnew.c index a5e1c0a..f4a9908 100644 --- a/libft/src/ft_lstnew.c +++ b/libft/src/ft_lstnew.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include t_list *ft_lstnew(void *content) diff --git a/libft/src/ft_lstsize.c b/libft/src/ft_lstsize.c index 1742a5f..0c71a69 100644 --- a/libft/src/ft_lstsize.c +++ b/libft/src/ft_lstsize.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include int ft_lstsize(t_list *lst) diff --git a/libft/src/ft_memchr.c b/libft/src/ft_memchr.c index c2578aa..8dfb477 100644 --- a/libft/src/ft_memchr.c +++ b/libft/src/ft_memchr.c @@ -6,7 +6,7 @@ /* By: rbousset +:+ +: +: +:+ */ /* #+# #+ #+ #+# */ /* Created: 2019/10/08 19:14:54 by rbousset #+# ## ## #+# */ -/* Updated: 2019/10/13 08:40:23 by rbousset ### #+. /#+ ###.fr */ +/* Updated: 2019/12/10 18:34:21 by rbousset ### #+. /#+ ###.fr */ /* / */ /* / */ /* ************************************************************************** */ @@ -17,17 +17,16 @@ void *ft_memchr(const void *s, int c, size_t n) { unsigned char *s_ptr; - size_t i; + unsigned char c_char; - if (!s) - return (NULL); - i = 0; s_ptr = (unsigned char*)s; - while (i < n) + c_char = (unsigned char)c; + while (n > 0) { - if (s_ptr[i] == (unsigned char)c) - return ((char*)&s[i]); - i++; + if (*s_ptr == c_char) + return ((void*)s_ptr); + s_ptr++; + n--; } return (NULL); } diff --git a/libft/src/ft_memcmp.c b/libft/src/ft_memcmp.c index 59a99bd..96f21c4 100644 --- a/libft/src/ft_memcmp.c +++ b/libft/src/ft_memcmp.c @@ -21,8 +21,6 @@ int s1_ptr = (unsigned char*)s1; s2_ptr = (unsigned char*)s2; - if (!s1 || !s2) - return (0); while (n) { if (*s1_ptr != *s2_ptr) diff --git a/libft/src/ft_memcpy.c b/libft/src/ft_memcpy.c index 548ea95..49a1ff7 100644 --- a/libft/src/ft_memcpy.c +++ b/libft/src/ft_memcpy.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include #include diff --git a/libft/src/ft_memlchr.c b/libft/src/ft_memlchr.c new file mode 100644 index 0000000..1123743 --- /dev/null +++ b/libft/src/ft_memlchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_memlchr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/11 16:50:58 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/11 16:50:59 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include + +long + ft_memlchr(const void *s, int c, size_t n) +{ + const size_t len = ft_strlen((const char *)s); + const size_t rem = ft_strlen((const char *)ft_memchr(s, c, n)); + + if (len - rem >= ft_strlen((const char *)s)) + return (-1); + return (len - rem); +} diff --git a/libft/src/ft_memmove.c b/libft/src/ft_memmove.c index 000a2b7..5d79fab 100644 --- a/libft/src/ft_memmove.c +++ b/libft/src/ft_memmove.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include void diff --git a/libft/src/ft_memset.c b/libft/src/ft_memset.c index b94c6fa..1498ae6 100644 --- a/libft/src/ft_memset.c +++ b/libft/src/ft_memset.c @@ -19,8 +19,6 @@ void unsigned char *str; str = b; - if (!b) - return (NULL); if (!len) return (b); while (len > 0) diff --git a/libft/src/ft_nstr.c b/libft/src/ft_nstr.c new file mode 100644 index 0000000..8919e64 --- /dev/null +++ b/libft/src/ft_nstr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_nstr.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/07 04:23:30 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/07 04:23:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include + +char + *ft_nstr(size_t size) +{ + char *str; + size_t i; + + i = 0; + size += 1; + if (!(str = (char*)malloc((size) * sizeof(char)))) + return (NULL); + while (i < size) + { + str[i] = '\0'; + i++; + } + return (str); +} diff --git a/libft/src/ft_putchar_fd.c b/libft/src/ft_putchar_fd.c index f57a4f9..a8fed8f 100644 --- a/libft/src/ft_putchar_fd.c +++ b/libft/src/ft_putchar_fd.c @@ -13,8 +13,8 @@ #include -void +int ft_putchar_fd(char c, int fd) { - write(fd, &c, 1); + return (write(fd, &c, 1)); } diff --git a/libft/src/ft_putendl.c b/libft/src/ft_putendl.c index 26dc80b..e2ab518 100644 --- a/libft/src/ft_putendl.c +++ b/libft/src/ft_putendl.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include int diff --git a/libft/src/ft_putendl_fd.c b/libft/src/ft_putendl_fd.c index 39fe7c1..9b0f495 100644 --- a/libft/src/ft_putendl_fd.c +++ b/libft/src/ft_putendl_fd.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include void ft_putendl_fd(char *s, int fd) diff --git a/libft/src/ft_putnbr.c b/libft/src/ft_putnbr.c index 5220151..a3a1e24 100644 --- a/libft/src/ft_putnbr.c +++ b/libft/src/ft_putnbr.c @@ -11,13 +11,14 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include +#include -int - ft_putnbr(int nb) +void + ft_putnbr(long long nb) { - unsigned int i; + long long i; i = nb; if (nb < 0) @@ -32,5 +33,4 @@ int } else ft_putchar(i + '0'); - return (0); } diff --git a/libft/src/ft_putnbr_base.c b/libft/src/ft_putnbr_base.c new file mode 100644 index 0000000..7ec0a85 --- /dev/null +++ b/libft/src/ft_putnbr_base.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putnbr_base.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/19 15:00:38 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/19 15:00:40 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include + +void + ft_putnbr_base(long long nb, char *base) +{ + long long i; + uint8_t size; + + i = nb; + size = ft_strlen(base); + if (nb < 0) + { + ft_putchar('-'); + i = -i; + } + if (i >= size) + { + ft_putnbr_base(i / size, base); + ft_putnbr_base(i % size, base); + } + else + ft_putchar(base[nb]); +} diff --git a/libft/src/ft_putnbr_fd.c b/libft/src/ft_putnbr_fd.c index b240f43..3246976 100644 --- a/libft/src/ft_putnbr_fd.c +++ b/libft/src/ft_putnbr_fd.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include void ft_putnbr_fd(int n, int fd) diff --git a/libft/src/ft_putnchar.c b/libft/src/ft_putnchar.c new file mode 100644 index 0000000..5d20f4c --- /dev/null +++ b/libft/src/ft_putnchar.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_putnchar.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/22 16:49:05 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/22 16:49:07 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include + +int + ft_putnchar(int c, const size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + ft_putchar(c); + i++; + } + return (i); +} diff --git a/libft/src/ft_putstr.c b/libft/src/ft_putstr.c index d29a940..572e989 100644 --- a/libft/src/ft_putstr.c +++ b/libft/src/ft_putstr.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include int diff --git a/libft/src/ft_putstr_fd.c b/libft/src/ft_putstr_fd.c index b90c078..068d959 100644 --- a/libft/src/ft_putstr_fd.c +++ b/libft/src/ft_putstr_fd.c @@ -11,11 +11,11 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include -void +int ft_putstr_fd(char *s, int fd) { - write(fd, s, ft_strlen(s)); + return (write(fd, s, ft_strlen(s))); } diff --git a/libft/src/ft_realloc.c b/libft/src/ft_realloc.c new file mode 100644 index 0000000..34b43c3 --- /dev/null +++ b/libft/src/ft_realloc.c @@ -0,0 +1,26 @@ +#include +#include +#include + +void + *ft_realloc(void *ptr, size_t size) +{ + void *nptr; + + if (!ptr) + { + if (!(ptr = malloc(size))) + return (NULL); + return (ptr); + } + else if (!size) + { + free(ptr); + return (NULL); + } + if (!(nptr = malloc(size))) + return (ptr); + ft_memcpy(nptr, ptr, ft_strlen(ptr)); + free(ptr); + return (nptr); +} diff --git a/libft/src/ft_split.c b/libft/src/ft_split.c index 7c706cf..f0cac95 100644 --- a/libft/src/ft_split.c +++ b/libft/src/ft_split.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include #include #include diff --git a/libft/src/ft_strchr.c b/libft/src/ft_strchr.c index 86bd41d..40dcd0f 100644 --- a/libft/src/ft_strchr.c +++ b/libft/src/ft_strchr.c @@ -19,8 +19,6 @@ char size_t i; i = 0; - if (!s) - return (NULL); while (s[i] != c) { if (!s[i]) diff --git a/libft/src/ft_strcmp.c b/libft/src/ft_strcmp.c index 2e9ffe9..ca5cf60 100644 --- a/libft/src/ft_strcmp.c +++ b/libft/src/ft_strcmp.c @@ -11,17 +11,20 @@ /* / */ /* ************************************************************************** */ +#include #include int ft_strcmp(const char *s1, const char *s2) { size_t i; - int diff; i = 0; - while (s1[i] == s2[i]) + while (s1[i] == s2[i] && i < ft_strlen(s1) - 1) + { + if (!s1[i] && !s2[i]) + return (0); i++; - diff = s1[i] - s2[i]; - return (diff); + } + return ((unsigned char)s1[i] - (unsigned char)s2[i]); } diff --git a/libft/src/ft_strdup.c b/libft/src/ft_strdup.c index ce74fb5..830c965 100644 --- a/libft/src/ft_strdup.c +++ b/libft/src/ft_strdup.c @@ -11,21 +11,25 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include char *ft_strdup(const char *s1) { + char *n_str; size_t slen; - char *new_str; + size_t i; - new_str = ""; slen = ft_strlen(s1); - new_str = (char *)malloc((slen + 1) * sizeof(char)); - if (new_str == NULL) + if (!(n_str = (char*)malloc((slen + 1) * sizeof(char)))) return (NULL); - ft_memcpy(new_str, s1, slen); - new_str[slen] = '\0'; - return (new_str); + i = 0; + while (s1[i]) + { + n_str[i] = s1[i]; + i++; + } + n_str[i] = '\0'; + return (n_str); } diff --git a/libft/src/ft_strjoin.c b/libft/src/ft_strjoin.c index f666781..2e00daf 100644 --- a/libft/src/ft_strjoin.c +++ b/libft/src/ft_strjoin.c @@ -11,32 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" -#include - -size_t - ft_strleen(const char *s) -{ - size_t i; - - i = 0; - while (s[i] != '\0') - i++; - return (i); -} - -static char - *ft_recalloc(size_t size) -{ - char *str; - - str = 0; - str = malloc((size + 1) * sizeof(char)); - if (!str) - return (NULL); - ft_bzero(str, size); - return (str); -} +#include char *ft_strjoin(const char *s1, const char *s2) @@ -44,20 +19,21 @@ char char *str; size_t i; size_t j; - size_t size; + size_t len; - size = (ft_strleen(s1) + ft_strleen(s2)); - str = (char*)ft_recalloc(ft_strleen(s1) + ft_strleen(s2)); + if (!s1 || !s2) + return (NULL); + len = ft_strlen(s1) + ft_strlen(s2); + if (!(str = ft_nstr(len))) + return (NULL); i = 0; j = 0; - if (!str) - return (NULL); - while (i < ft_strleen(s1)) + while (s1[i] != '\0') { str[i] = s1[i]; i++; } - while (i < size) + while (s2[j] != '\0') { str[i] = s2[j]; i++; diff --git a/libft/src/ft_strlcat.c b/libft/src/ft_strlcat.c index 98fa3e8..4b6f036 100644 --- a/libft/src/ft_strlcat.c +++ b/libft/src/ft_strlcat.c @@ -11,30 +11,15 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include -static size_t - ft_strlen_size(const char *s, size_t size) -{ - const char *ptr = s; - - while (size > 0 && *ptr) - { - size--; - ptr++; - } - return (ptr - s); -} - size_t ft_strlcat(char *dst, const char *src, size_t size) { size_t dst_len; - if (!dst || !src) - return (0); - dst_len = ft_strlen_size(dst, size); + dst_len = ft_strnlen(dst, size); if (dst_len == size) return (dst_len + ft_strlen(src)); return (dst_len + ft_strlcpy(dst + dst_len, src, size - dst_len)); diff --git a/libft/src/ft_strlcpy.c b/libft/src/ft_strlcpy.c index bba4263..9331f1c 100644 --- a/libft/src/ft_strlcpy.c +++ b/libft/src/ft_strlcpy.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include size_t diff --git a/libft/src/ft_strlen.c b/libft/src/ft_strlen.c index 68c7614..0964c64 100644 --- a/libft/src/ft_strlen.c +++ b/libft/src/ft_strlen.c @@ -16,12 +16,10 @@ size_t ft_strlen(const char *s) { - size_t i; + const char *ptr; - i = 0; - if (!s) - return (0); - while (s[i] != '\0') - i++; - return (i); + ptr = s; + while (ptr && *ptr) + ptr++; + return (ptr - s); } diff --git a/libft/src/ft_strmapi.c b/libft/src/ft_strmapi.c index 7a38ca9..8a47875 100644 --- a/libft/src/ft_strmapi.c +++ b/libft/src/ft_strmapi.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) diff --git a/libft/src/ft_strncmp.c b/libft/src/ft_strncmp.c index c8a0b87..3bccfd5 100644 --- a/libft/src/ft_strncmp.c +++ b/libft/src/ft_strncmp.c @@ -19,11 +19,9 @@ int size_t i; i = 0; - if (!s1 || !s2 || !n) - return (0); while (s1[i] == s2[i] && i < n - 1) { - if (s1[i] == '\0' && s2[i] == '\0') + if (!s1[i] && !s2[i]) return (0); i++; } diff --git a/libft/src/ft_strnlen.c b/libft/src/ft_strnlen.c index 88bd437..d9d20a1 100644 --- a/libft/src/ft_strnlen.c +++ b/libft/src/ft_strnlen.c @@ -14,7 +14,7 @@ #include size_t - ft_strlen_size(const char *s, size_t size) + ft_strnlen(const char *s, size_t size) { const char *ptr = s; diff --git a/libft/src/ft_strnstr.c b/libft/src/ft_strnstr.c index 583026e..711629d 100644 --- a/libft/src/ft_strnstr.c +++ b/libft/src/ft_strnstr.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include #include diff --git a/libft/src/ft_strrchr.c b/libft/src/ft_strrchr.c index 47ba95b..ada7470 100644 --- a/libft/src/ft_strrchr.c +++ b/libft/src/ft_strrchr.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include #include char @@ -19,8 +19,6 @@ char { size_t i; - if (!s) - return (NULL); i = ft_strlen(s); while (s[i] != c) { diff --git a/libft/src/ft_strtrim.c b/libft/src/ft_strtrim.c index c691815..48fdad5 100644 --- a/libft/src/ft_strtrim.c +++ b/libft/src/ft_strtrim.c @@ -11,7 +11,7 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include char *ft_strtrim(const char *s1, const char *set) diff --git a/libft/src/ft_substr.c b/libft/src/ft_substr.c index 87c2568..0cb9733 100644 --- a/libft/src/ft_substr.c +++ b/libft/src/ft_substr.c @@ -11,23 +11,24 @@ /* / */ /* ************************************************************************** */ -#include "libft.h" +#include +#include #include char *ft_substr(const char *s, unsigned int start, size_t len) { - char *nstr; + char *nstr; + size_t i; - if (start > ft_strlen(s) - 1) + i = 0; + if (!(nstr = (char*)malloc((len + 1) * sizeof(char)))) + return (NULL); + while (s[start + i] && i < len) { - nstr = (char*)ft_calloc(1, sizeof(char)); - if (!nstr) - return (NULL); - return (nstr); + nstr[i] = s[start + i]; + i++; } - nstr = (char*)ft_calloc(len + 1, sizeof(char)); - if (!nstr) - return (NULL); - return ((char*)ft_memcpy(nstr, s + start, len)); + nstr[i] = '\0'; + return (nstr); } diff --git a/libft/src/get_next_line.c b/libft/src/get_next_line.c new file mode 100644 index 0000000..2b2f084 --- /dev/null +++ b/libft/src/get_next_line.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* get_next_line.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/29 00:37:39 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/29 00:37:41 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +static uint8_t + ft_linecheck(const char *str) +{ + size_t i; + + i = 0; + while (str && str[i]) + { + if (str[i] == '\n') + return (1); + i++; + } + return (0); +} + +static size_t + ft_linelen(const char *str) +{ + size_t i; + + i = 0; + while (str[i] != '\n' && str[i] != '\0') + i++; + return (i); +} + +static int + ft_linedup(int fd, char **str) +{ + char *buff; + char *tmp; + int ret; + + if (!(buff = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char)))) + return (0); + ret = 1; + while (!ft_linecheck(*str) && (ret = read(fd, buff, BUFFER_SIZE)) > 0) + { + buff[ret] = '\0'; + tmp = *str; + *str = ft_strjoin(tmp, buff); + free(tmp); + } + if (ret < 0) + { + free(*str); + free(buff); + return (-1); + } + free(buff); + return (ret); +} + +static int + ft_errchck(int fd, char **line, char **str) +{ + if (*str == NULL) + *str = ft_nstr(0); + if (fd < 0 || !line || BUFFER_SIZE <= 0) + { + free(*str); + return (-1); + } + return (0); +} + +int + get_next_line(int fd, char **line) +{ + static char *str[1024]; + char *tmp; + size_t i; + int ret; + + if (ft_errchck(fd, line, &str[fd]) == -1) + return (-1); + ret = ft_linedup(fd, &str[fd]); + if (ret < 0) + return (-1); + i = ft_linelen(str[fd]); + *line = ft_substr(str[fd], 0, i); + tmp = str[fd]; + if (tmp[0] != '\0' && tmp) + str[fd] = ft_strdup(tmp + i + (tmp[i] == '\n')); + if (ret == 0 && ((str[fd] == NULL || str[fd][0] == '\0') || !ft_linecheck(str[fd]))) + { + i = tmp[i]; + free(tmp); + tmp = NULL; + return (i == '\n'); + } + free(tmp); + return (1); +} -- cgit v1.2.3