diff options
Diffstat (limited to 'libft')
54 files changed, 746 insertions, 326 deletions
diff --git a/libft/Makefile b/libft/Makefile index 1b41ad7..d5d6e5d 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -5,113 +5,172 @@ # +:+:+ +: +: +:+:+ # # By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ # # #+# #+ #+ #+# # -# Created: 2019/10/08 15:04:55 by rbousset #+# ## ## #+# # -# Updated: 2019/10/13 14:01:21 by rbousset ### #+. /#+ ###.fr # +# Created: 2019/12/11 13:14:43 by rbousset #+# ## ## #+# # +# Updated: 2019/12/11 18:34:05 by rbousset ### #+. /#+ ###.fr # # / # # / # # **************************************************************************** # -SRCS_DIR = src/ -SRCS = \ - ${SRCS_DIR}ft_memset.c \ - ${SRCS_DIR}ft_bzero.c \ - ${SRCS_DIR}ft_memcpy.c \ - ${SRCS_DIR}ft_memccpy.c \ - ${SRCS_DIR}ft_memmove.c \ - ${SRCS_DIR}ft_memchr.c \ - ${SRCS_DIR}ft_memcmp.c \ - ${SRCS_DIR}ft_strlen.c \ - ${SRCS_DIR}ft_isalpha.c \ - ${SRCS_DIR}ft_isdigit.c \ - ${SRCS_DIR}ft_isalnum.c \ - ${SRCS_DIR}ft_isascii.c \ - ${SRCS_DIR}ft_isprint.c \ - ${SRCS_DIR}ft_tolower.c \ - ${SRCS_DIR}ft_toupper.c \ - ${SRCS_DIR}ft_strchr.c \ - ${SRCS_DIR}ft_strrchr.c \ - ${SRCS_DIR}ft_strncmp.c \ - ${SRCS_DIR}ft_strlcpy.c \ - ${SRCS_DIR}ft_strlcat.c \ - ${SRCS_DIR}ft_strnstr.c \ - ${SRCS_DIR}ft_atoi.c \ - ${SRCS_DIR}ft_calloc.c \ - ${SRCS_DIR}ft_strdup.c \ - \ - ${SRCS_DIR}ft_substr.c \ - ${SRCS_DIR}ft_strjoin.c \ - ${SRCS_DIR}ft_strtrim.c \ - ${SRCS_DIR}ft_split.c \ - ${SRCS_DIR}ft_itoa.c \ - ${SRCS_DIR}ft_strmapi.c \ - ${SRCS_DIR}ft_putchar_fd.c \ - ${SRCS_DIR}ft_putstr_fd.c \ - ${SRCS_DIR}ft_putendl_fd.c \ - ${SRCS_DIR}ft_putnbr_fd.c \ - \ - ${SRCS_DIR}ft_lstnew.c \ - ${SRCS_DIR}ft_lstadd_front.c \ - ${SRCS_DIR}ft_lstsize.c \ - ${SRCS_DIR}ft_lstlast.c \ - ${SRCS_DIR}ft_lstadd_back.c \ - ${SRCS_DIR}ft_lstdelone.c \ - ${SRCS_DIR}ft_lstclear.c \ - ${SRCS_DIR}ft_lstiter.c \ - ${SRCS_DIR}ft_lstmap.c \ - ${SRCS_DIR}ft_putchar.c \ - ${SRCS_DIR}ft_putstr.c \ - ${SRCS_DIR}ft_putendl.c \ - ${SRCS_DIR}ft_putnbr.c \ - ${SRCS_DIR}ft_strnlen.c \ - ${SRCS_DIR}ft_strcat.c \ - ${SRCS_DIR}ft_strcmp.c \ - ${SRCS_DIR}ft_isspace.c \ - ${SRCS_DIR}ft_sqrt.c - -OBJS_DIR = obj/ -OBJS = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS}) - -INCS_DIR = inc/ -INCS = libft.h - -CC = clang -CFLAGS = -Wall -Wextra -Werror - -DEBUG = -g3 -FSANITIZE = -fsanitize=address - -AR = ar rcs -NAME = libft.a - -MKDIR = mkdir -p -RM = rm -rf - - +default: all +#==============================================================================# +#--------------------------------- Shell --------------------------------------# +#==============================================================================# +SHELL := /bin/sh +#==============================================================================# +#------------------------------ Directories -----------------------------------# +#==============================================================================# +INCS_DIR = inc/ +SRCS_DIR = src/ +OBJS_DIR = obj/ +#==============================================================================# +#--------------------------------- Files --------------------------------------# +#==============================================================================# +INCS = libft.h +#------------------------------------------------------------------------------# +OBJS = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS}) +#------------------------------------------------------------------------------# +SRCS = ${SRCS_DIR}ft_memset.c +SRCS += ${SRCS_DIR}ft_bzero.c +SRCS += ${SRCS_DIR}ft_memcpy.c +SRCS += ${SRCS_DIR}ft_memccpy.c +SRCS += ${SRCS_DIR}ft_memmove.c +SRCS += ${SRCS_DIR}ft_memchr.c +SRCS += ${SRCS_DIR}ft_memlchr.c +SRCS += ${SRCS_DIR}ft_memcmp.c +SRCS += ${SRCS_DIR}ft_strlen.c +SRCS += ${SRCS_DIR}ft_isalpha.c +SRCS += ${SRCS_DIR}ft_isdigit.c +SRCS += ${SRCS_DIR}ft_isalnum.c +SRCS += ${SRCS_DIR}ft_isascii.c +SRCS += ${SRCS_DIR}ft_isprint.c +SRCS += ${SRCS_DIR}ft_ischarset.c +SRCS += ${SRCS_DIR}ft_tolower.c +SRCS += ${SRCS_DIR}ft_toupper.c +SRCS += ${SRCS_DIR}ft_strchr.c +SRCS += ${SRCS_DIR}ft_strrchr.c +SRCS += ${SRCS_DIR}ft_strncmp.c +SRCS += ${SRCS_DIR}ft_strlcpy.c +SRCS += ${SRCS_DIR}ft_strlcat.c +SRCS += ${SRCS_DIR}ft_strnstr.c +SRCS += ${SRCS_DIR}ft_atoi.c +SRCS += ${SRCS_DIR}ft_calloc.c +SRCS += ${SRCS_DIR}ft_realloc.c +SRCS += ${SRCS_DIR}ft_strdup.c +SRCS += ${SRCS_DIR}ft_substr.c +SRCS += ${SRCS_DIR}ft_strjoin.c +SRCS += ${SRCS_DIR}ft_strtrim.c +SRCS += ${SRCS_DIR}ft_split.c +SRCS += ${SRCS_DIR}ft_itoa.c +SRCS += ${SRCS_DIR}ft_itoa_base.c +SRCS += ${SRCS_DIR}ft_strmapi.c +SRCS += ${SRCS_DIR}ft_putchar_fd.c +SRCS += ${SRCS_DIR}ft_putstr_fd.c +SRCS += ${SRCS_DIR}ft_putendl_fd.c +SRCS += ${SRCS_DIR}ft_putnbr_fd.c +SRCS += ${SRCS_DIR}ft_lstnew.c +SRCS += ${SRCS_DIR}ft_lstadd_front.c +SRCS += ${SRCS_DIR}ft_lstsize.c +SRCS += ${SRCS_DIR}ft_lstlast.c +SRCS += ${SRCS_DIR}ft_lstadd_back.c +SRCS += ${SRCS_DIR}ft_lstdelone.c +SRCS += ${SRCS_DIR}ft_lstclear.c +SRCS += ${SRCS_DIR}ft_lstiter.c +SRCS += ${SRCS_DIR}ft_lstmap.c +SRCS += ${SRCS_DIR}ft_putchar.c +SRCS += ${SRCS_DIR}ft_putnchar.c +SRCS += ${SRCS_DIR}ft_putstr.c +SRCS += ${SRCS_DIR}ft_putendl.c +SRCS += ${SRCS_DIR}ft_putnbr.c +SRCS += ${SRCS_DIR}ft_putnbr_base.c +SRCS += ${SRCS_DIR}ft_strnlen.c +SRCS += ${SRCS_DIR}ft_strcat.c +SRCS += ${SRCS_DIR}ft_strcmp.c +SRCS += ${SRCS_DIR}ft_isspace.c +SRCS += ${SRCS_DIR}ft_sqrt.c +SRCS += ${SRCS_DIR}ft_intlen.c +SRCS += ${SRCS_DIR}ft_intlen_base.c +SRCS += ${SRCS_DIR}ft_nstr.c +SRCS += ${SRCS_DIR}ft_kernel_panic.c +SRCS += ${SRCS_DIR}get_next_line.c +#==============================================================================# +#-------------------------------- Compiler ------------------------------------# +#==============================================================================# +CC = gcc +CFLAGS = -Wall +CFLAGS += -Wextra +CFLAGS += -Werror +CFLAGS += ${DEBUG} +CFLAGS += ${FSANITIZE} +#------------------------------------------------------------------------------# +DEBUG = -Og -ggdb +FSANITIZE = -fsanitize=address +#------------------------------------------------------------------------------# +NAME = libft.a +#==============================================================================# +#-------------------------------- Library -------------------------------------# +#==============================================================================# +AR = ar rcs +#==============================================================================# +#--------------------------------- UNIX ---------------------------------------# +#==============================================================================# +MKDIR = mkdir -p +RM = rm -rf +#==============================================================================# +#--------------------------------- Rules --------------------------------------# +#==============================================================================# ${OBJS_DIR}%.o: ${SRCS_DIR}%.c ${INCS_DIR}${INCS} - @if [ ! -a ${OBJS_DIR} ]; then \ - ${MKDIR} ${OBJS_DIR}; \ - fi - @${CC} ${CFLAGS} -I${INCS_DIR} -c $< -o $@ - @echo "Compiled $(subst ${OBJS_DIR},,$@)!" - -$(NAME): ${OBJS} - @${AR} ${NAME} ${OBJS} - @echo - @echo "libft.a linked!" - -all: ${NAME} - + @${MKDIR} ${OBJS_DIR} + ${CC} ${CFLAGS} -I${INCS_DIR} -o $@ -c $< +#------------------------------------------------------------------------------# +${NAME}: ${OBJS} + ${AR} ${NAME} ${OBJS} +#------------------------------------------------------------------------------# +all: + @$(MAKE) --no-print-directory -j5 ${NAME} +#------------------------------------------------------------------------------# clean: - @${RM} ${OBJS} - @echo "Removed obj files!" - + ${RM} ${OBJS_DIR} +#------------------------------------------------------------------------------# fclean: clean - @${RM} ${OBJS_DIR} - @${RM} ${NAME} - @echo "Removed everything!" - + ${RM} ${NAME} +#------------------------------------------------------------------------------# re: fclean all - -default: all - -.PHONY: all clean clean fclean re +#------------------------------------------------------------------------------# +build: ${OBJS} + ${CC} ${CFLAGS} -Iinc/ -o a.out ${OBJS} ${SRCS_DIR}main.c +#------------------------------------------------------------------------------# +.PHONY: all clean clean fclean re default +#==============================================================================# +#-------------------------------- Colors --------------------------------------# +#==============================================================================# +_END = \033[0m +_BOLD = \033[1m +_UNDER = \033[4m +#------------------------------------------------------------------------------# +_NBLACK = \033[0;30m +_NRED = \033[0;31m +_NGREEN = \033[0;32m +_NYELLOW = \033[0;33m +_NBLUE = \033[0;34m +_NMAGENTA = \033[0;35m +_NCYAN = \033[0;36m +_NWHITE = \033[0;37m +#------------------------------------------------------------------------------# +_BBLACK = \033[1;30m +_BRED = \033[1;31m +_BGREEN = \033[1;32m +_BYELLOW = \033[1;33m +_BBLUE = \033[1;34m +_BMAGENTA = \033[1;35m +_BCYAN = \033[1;36m +_BWHITE = \033[1;37m +#------------------------------------------------------------------------------# +_IBLACK = \033[1;40m +_IRED = \033[1;41m +_IGREEN = \033[1;42m +_IYELLOW = \033[1;43m +_IBLUE = \033[1;44m +_IMAGENTA = \033[1;45m +_ICYAN = \033[1;46m +_IWHITE = \033[1;47m +#==================================== EOF =====================================# diff --git a/libft/inc/libft.h b/libft/inc/libft.h index 83a0177..1a47877 100644 --- a/libft/inc/libft.h +++ b/libft/inc/libft.h @@ -17,82 +17,102 @@ #include <stddef.h> #include <inttypes.h> +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 72 +# endif + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + /* -** Part 1 +** VOID */ void *ft_memset(void *b, int c, size_t len); void ft_bzero(void *s, size_t n); void *ft_memcpy(void *dst, const void *src, size_t n); void *ft_memccpy(void *dst, const void *src, - int c, size_t n); + int c, size_t n); void *ft_memmove(void *dst, const void *src, size_t len); void *ft_memchr(const void *s, int c, size_t n); -int ft_memcmp(const void *s1, const void *s2, size_t n); -size_t ft_strlen(const char *s); -int ft_isalpha(int c); -int ft_isdigit(int c); -int ft_isalnum(int c); -int ft_isascii(int c); -int ft_isprint(int c); -int ft_toupper(int c); -int ft_tolower(int c); -char *ft_strchr(const char *s, int c); -char *ft_strrchr(const char *s, int c); -int ft_strncmp(const char *s1, const char *s2, size_t n); -size_t ft_strlcpy(char *dst, const char *src, size_t size); -size_t ft_strlcat(char *dst, const char *src, size_t size); -char *ft_strnstr(const char *haystack, const char *needle, - size_t len); -int ft_atoi(const char *str); void *ft_calloc(size_t count, size_t size); -char *ft_strdup(const char *s1); +void *ft_realloc(void *ptr, size_t size); +int ft_putchar_fd(char c, int fd); +int ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_lstadd_front(t_list **alst, t_list *new); +void ft_lstadd_back(t_list **alst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +void ft_kernel_panic(void); +void ft_putnbr(long long nb); +void ft_putnbr_base(long long nb, char *base); /* -** Part 2 +** CHAR */ +char *ft_strcat(char *s1, const char *s2); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strnstr(const char *haystack, const char *needle, + size_t len); +char *ft_strdup(const char *s1); char *ft_substr(const char *s, unsigned int start, - size_t len); + size_t len); char *ft_strjoin(const char *s1, const char *s2); char *ft_strtrim(const char *s1, const char *set); -char **ft_split(const char *s, char c); -char *ft_itoa(int n); +char *ft_itoa(long long n); +char *ft_itoa_base(long long n, char *base); char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *s, int fd); -void ft_putendl_fd(char *s, int fd); -void ft_putnbr_fd(int n, int fd); +char *ft_nstr(size_t size); +char **ft_split(const char *s, char c); /* -** Bonus +** INT */ -typedef struct s_list -{ - void *content; - struct s_list *next; -} t_list; - -t_list *ft_lstnew(void *content); -void ft_lstadd_front(t_list **alst, t_list *new); +uint8_t ft_isspace(int c); +uint8_t ft_ischarset(const char *charset, int c); +uint8_t ft_intlen(long long n); +uint8_t ft_intlen_base(long long n, char *base); +int ft_memcmp(const void *s1, const void *s2, size_t n); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_toupper(int c); +int ft_tolower(int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_lstsize(t_list *lst); -t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **alst, t_list *new); -void ft_lstdelone(t_list *lst, void (*del)(void *)); -void ft_lstclear(t_list **lst, void (*del)(void *)); -void ft_lstiter(t_list *lst, void (*f)(void *)); -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), - void (*del)(void *)); +int ft_atoi(const char *str); int ft_putchar(int c); +int ft_putnchar(int c, const size_t n); int ft_putstr(const char *s); int ft_putendl(const char *s); -int ft_putnbr(int nb); -char *ft_strcat(char *s1, const char *s2); -uint8_t ft_isspace(int c); int ft_strcmp(const char *s1, const char *s2); -double ft_sqrt(double x); +int get_next_line(int fd, char **line); +long ft_memlchr(const void *s, int c, size_t n); +size_t ft_strlen(const char *s); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlcat(char *dst, const char *src, size_t size); size_t ft_strnlen(const char *s, size_t size); +double ft_sqrt(double x); + +/* +** LIST +*/ + +t_list *ft_lstnew(void *content); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), + void (*del)(void *)); # endif 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 <libft.h> #include <inttypes.h> -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 <libft.h> #include <stddef.h> 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 <libft.h> #include <stddef.h> #include <stdlib.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/07 02:24:29 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/07 02:24:30 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <inttypes.h> + +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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/19 17:22:26 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/19 17:22:27 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> + +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 <libft.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/22 16:48:45 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/22 16:48:47 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> +#include <inttypes.h> + +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 <libft.h> #include <inttypes.h> #include <stdlib.h> -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 <libft.h> +#include <stdlib.h> + +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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/11 18:26:08 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/11 18:26:09 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +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 <libft.h> #include <stddef.h> 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 <libft.h> 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 <libft.h> #include <stdlib.h> 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 <libft.h> #include <stdlib.h> 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 <libft.h> #include <stddef.h> 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 <libft.h> #include <stddef.h> 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 <libft.h> #include <stddef.h> #include <stdlib.h> 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 <libft.h> 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 <libft.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ /* #+# #+ #+ #+# */ /* 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 <libft.h> #include <stddef.h> #include <stdio.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/11 16:50:58 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/11 16:50:59 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +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 <libft.h> #include <stddef.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/07 04:23:30 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/07 04:23:32 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <stddef.h> + +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 <unistd.h> -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 <libft.h> #include <unistd.h> 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 <libft.h> 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 <libft.h> #include <unistd.h> +#include <inttypes.h> -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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/19 15:00:38 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/19 15:00:40 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <inttypes.h> + +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 <libft.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/12/22 16:49:05 by rbousset #+# ## ## #+# */ +/* Updated: 2019/12/22 16:49:07 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stddef.h> + +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 <libft.h> #include <unistd.h> 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 <libft.h> #include <unistd.h> -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 <libft.h> +#include <stddef.h> +#include <stdlib.h> + +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 <libft.h> #include <stddef.h> #include <stdlib.h> #include <inttypes.h> 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 <libft.h> #include <stddef.h> 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 <libft.h> #include <stdlib.h> 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 <stdlib.h> - -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 <libft.h> 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 <libft.h> #include <stddef.h> -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 <libft.h> #include <stddef.h> 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 <libft.h> 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 <stddef.h> 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 <libft.h> #include <stddef.h> #include <inttypes.h> 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 <libft.h> #include <stddef.h> 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 <libft.h> 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 <libft.h> +#include <stdlib.h> #include <stddef.h> 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 <marvin@le-101.fr> +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2019/10/29 00:37:39 by rbousset #+# ## ## #+# */ +/* Updated: 2019/10/29 00:37:41 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> + +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); +} |