diff --git a/libft/Makefile b/libft/Makefile index 193e6ae..a56a743 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -3,99 +3,113 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: adjoly +#+ +:+ +#+ # +# By: mmoussou +# include +# include +# include +# include +# include + +/* INT */ + +int ft_min(int a, int b); +int ft_max(int a, int b); +int ft_abs(int n); + +/* CHAR */ + +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); + +/* MEMORY */ + +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memset(void *s, int c, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dst, const void *src, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +void ft_bzero(void *s, size_t n); +void *ft_calloc(size_t n, size_t elsize); + +void ft_free(const char *str, ...); + +/* INT & ARG */ + +int ft_atoi(const char *str); +long long ft_atol(const char *str); +char *ft_itoa(int n); +char *ft_ltoa(long long n); + +/* STRINGS */ + +size_t ft_strlen(char const *s); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, unsigned int n); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strdup(char *src); +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_strjoin(const char *s1, const char *s2); +char *ft_strjoin_free(char *s1, char *s2); +char *ft_strjoin_free_s1(char *s1, const char *s2); +char *ft_strjoin_free_s2(const char *s1, char *s2); +char *ft_strtrim(char const *s1, char const *set); +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); + +char *ft_itoa(int n); +char **ft_split(const char *str, char ch); +char *ft_substr(char const *s, unsigned int start, size_t len); + +int ft_toupper(int c); +int ft_tolower(int c); + +/* FD PUT FUNC */ + +int ft_putchar_fd(char c, int fd); +int ft_putstr_fd(char *s, int fd); +int ft_putendl_fd(char *s, int fd); +int ft_putnbr_fd(int n, int fd); +int ft_putuhex_fd(unsigned long long nbr, char *base, int fd); + +int ft_printf(const char *str, ...); +int ft_printf_fd(int fd, const char *str, ...); + +// ---------------- LINKED LISTS ---------------- // + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +/* LISTS */ + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +uint ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, 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 *)); + +typedef struct s_stack +{ + int nb_init; + int nb; + struct s_stack *next; +} t_stack; + +/* ------ STACK UTILS ------ */ + +t_stack *ft_stacknew(int content); +uint ft_stacksize(t_stack *stack); +t_stack *ft_stacklast(t_stack *stack); +void ft_stackadd_back(t_stack **stack, t_stack *new); +void ft_stackadd_front(t_stack **stack, t_stack *new); + +#endif diff --git a/libft/io/get_next_line/Makefile b/libft/io/get_next_line/Makefile deleted file mode 100644 index 6a62d50..0000000 --- a/libft/io/get_next_line/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: adjoly +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2023/11/01 11:03:22 by adjoly #+# #+# # -# Updated: 2024/03/04 13:30:36 by adjoly ### ########.fr # -# # -# **************************************************************************** # - -NAME = get_next_line.a - -CC = clang - -SRCS = get_next_line.c \ - get_next_line_utils.c \ - ../../mem/ft_calloc.c \ - ../../str/ft_strlen.c \ - -OBJS = $(SRCS:.c=.o) - -FLAGS = -Werror -Wall -Wextra - -HEADER = get_next_line.h - -$(NAME): $(OBJS) - @ar -rcs $(NAME) $(OBJS) - -%.o: %.c - @$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ - -all: $(NAME) - -clean: - @rm -f $(OBJS) - -fclean: clean - @rm -f $(NAME) - -re: fclean all - -.PHONY: clean all re fclean diff --git a/libft/io/get_next_line/get_next_line.c b/libft/io/get_next_line/get_next_line.c deleted file mode 100644 index 6eb5cda..0000000 --- a/libft/io/get_next_line/get_next_line.c +++ /dev/null @@ -1,74 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:25:28 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../../libft.h" - -char check_line(char *res, char *buf) -{ - int i; - int j; - - i = 0; - j = 0; - while (res[i] && res[i] != '\n') - i++; - if (res[i] == '\n') - { - i++; - while (res[i]) - { - buf[j] = res[i]; - i++; - j++; - } - buf[j] = 0; - res[i - j] = 0; - return (1); - } - return (0); -} - -char *ft_read_error(char **buf, char *res) -{ - free(*buf); - *buf = NULL; - if (res[0] != 0) - return (res); - free(res); - return (NULL); -} - -char *get_next_line(int fd) -{ - static char *buf; - char *res; - ssize_t bytes_read; - - if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023) - return (NULL); - if (!buf) - buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1); - res = ft_calloc(1, 1); - while (buf) - { - res = ft_strjoin_gnl(res, buf); - if (!res) - return (NULL); - if (check_line(res, buf)) - return (res); - bytes_read = read(fd, buf, BUFFER_SIZE); - if (bytes_read < 1) - return (ft_read_error(&buf, res)); - buf[bytes_read] = 0; - } - return (NULL); -} diff --git a/libft/libft.h b/libft/libft.h deleted file mode 100644 index b0213c0..0000000 --- a/libft/libft.h +++ /dev/null @@ -1,113 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* libft.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:27:45 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef LIBFT_H -# define LIBFT_H - -# include -# include -# include -# include - -# define INT_MAX 2147483647 -# define INT_MIN -2147483648 - -# ifndef BUFFER_SIZE -# define BUFFER_SIZE 5 -# endif - -typedef struct s_list -{ - void *content; - struct s_list *next; -} t_list; - -typedef enum s_boolean -{ - ERROR = -1, - FALSE, - TRUE, -} t_boolean; - -char *ft_strjoinvaarg(char *src, ...); -long long ft_atoll(const char *nptr); -int ft_atoi(const char *nptr); -void *ft_calloc(size_t nmemb, size_t size); -int ft_isalnum(int c); -int ft_isalpha(int c); -int ft_isascii(int c); -int ft_isdigit(int c); -void ft_bzero(void *s, size_t n); -void *ft_memset(void *s, int c, size_t n); -void *ft_memmove(void *dest, const void *src, size_t n); -void *ft_memcpy(void *dest, const void *src, size_t n); -void ft_putchar_fd(char c, int fd); -void ft_putnbr_fd(int n, int fd); -char *ft_strdup(const char *s); -void ft_putstr_fd(char *s, int fd); -char *ft_strchr(const char *s, int c); -size_t ft_strlcpy(char *dst, const char *src, size_t size); -size_t ft_strlen(const char *s); -char *ft_substr(char const *s, unsigned int start, size_t len); -int ft_tolower(int c); -int ft_toupper(int c); -size_t ft_strlcat(char *dst, const char *src, size_t size); -char *ft_strjoin(char const *s1, char const *s2); -int ft_strncmp(const char *s1, const char *s2, size_t n); -int ft_isprint(int c); -void *ft_memchr(const void *s, int c, size_t n); -char *ft_itoa(int n); -void *ft_memmove(void *dest, const void *src, size_t n); -void *ft_memchr(const void *s, int c, size_t n); -int ft_memcmp(const void *s1, const void *s2, size_t n); -char *ft_strrchr(const char *s, int c); -char *ft_strnstr(const char *big, const char *little, size_t len); -void ft_putendl_fd(char *s, int fd); -char *ft_strtrim(char const *s1, char const *set); -char **ft_split(char const *s, char c); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -void ft_striteri(char *s, void (*f)(unsigned int, char *)); - -t_list *ft_lstnew(void *content); -void ft_lstadd_front(t_list **lst, t_list *new); -int ft_lstsize(t_list *lst); -t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **lst, 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 *)); - -char *get_next_line(int fd); -char *ft_strjoin_gnl(char *s1, char *s2); -void *ft_calloc(size_t nmemb, size_t size); - -void ft_putchar(char c); -void ft_putstr(char *s); -void ft_putnbrbase_fd(int n, char *base, int fd); -void ft_putnbrbase(int n, char *base); -void ft_putnbr(int n); - -int ft_printf(const char *format, ...); -int ft_printconversion(char conversion, va_list args); -int ft_putnbrulong(unsigned int n); -int ft_putaddr(void *ptr); -int ft_putstr_p(char *s); -int ft_putnbrbasep(unsigned long int n, char *base); -int ft_putnbrbase_pf(unsigned int n, char *base); -int ft_putchar_p(char c); -int ft_putnbr_p(int n); -size_t ft_strlen(const char *s); - -void ft_freearr(void **arr); - -#endif diff --git a/libft/obj/str/ft_strjoinvaarg.o b/libft/obj/str/ft_strjoinvaarg.o deleted file mode 100644 index 9fd129f..0000000 Binary files a/libft/obj/str/ft_strjoinvaarg.o and /dev/null differ diff --git a/libft/print/ft_putnbrbase.c b/libft/print/ft_putnbrbase.c deleted file mode 100644 index 8d9e48a..0000000 --- a/libft/print/ft_putnbrbase.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbrbase.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 11:13:15 by adjoly #+# #+# */ -/* Updated: 2024/02/04 15:01:31 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putnbrbase(int n, char *base) -{ - ft_putnbrbase_fd(n, base, 1); -} diff --git a/libft/print/ft_putnbrbase_fd.c b/libft/print/ft_putnbrbase_fd.c deleted file mode 100644 index 6d8d275..0000000 --- a/libft/print/ft_putnbrbase_fd.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbrbase_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:41:30 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putnbrbase_fd(int n, char *base, int fd) -{ - unsigned int nbr; - size_t base_len; - - base_len = ft_strlen(base); - if (n < 0) - { - write(fd, "-", 1); - nbr = -n; - } - else - nbr = n; - if (nbr < base_len) - write(fd, &base[nbr % base_len], 1); - else - { - ft_putnbrbase_fd(nbr / base_len, base, fd); - write(fd, &base[nbr % base_len], 1); - } -} diff --git a/libft/print/ft_putstr.c b/libft/print/ft_putstr.c deleted file mode 100644 index 3f4f976..0000000 --- a/libft/print/ft_putstr.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putstr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 18:35:32 by adjoly #+# #+# */ -/* Updated: 2024/03/16 21:45:41 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putstr(char *s) -{ - write(1, s, ft_strlen(s)); -} diff --git a/libft/print/printf/Makefile b/libft/print/printf/Makefile deleted file mode 100644 index 085430b..0000000 --- a/libft/print/printf/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: adjoly +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2023/11/17 12:35:27 by adjoly #+# #+# # -# Updated: 2024/02/04 15:18:46 by adjoly ### ########.fr # -# # -# **************************************************************************** # - -NAME = libftprintf.a - -CC = cc - -SRCS = ft_printf.c \ - ft_putchar.c \ - ft_putnbr.c \ - ft_putnbrbase.c \ - ft_putstr.c \ - ../../str/ft_strlen.c \ - -OBJS = $(SRCS:.c=.o) - -FLAGS = -Werror -Wall -Wextra - -HEADER = libftprintf.h - -$(NAME): $(OBJS) - ar -rcs $(NAME) $(OBJS) - -%.o: %.c - $(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ - -all: $(NAME) - -clean: - rm -f $(OBJS) - -fclean: clean - rm -f $(NAME) - -re: fclean all - -.PHONY: clean all re fclean diff --git a/libft/print/printf/ft_printf.c b/libft/print/printf/ft_printf.c deleted file mode 100644 index 4792193..0000000 --- a/libft/print/printf/ft_printf.c +++ /dev/null @@ -1,92 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_printf.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:27:13 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../../libft.h" - -int ft_putaddr(void *ptr) -{ - int r; - - if (ptr == NULL) - return (write(1, "(nil)", 5)); - write(1, "0x", 2); - r = ft_putnbrbasep((long unsigned int)ptr, "0123456789abcdef"); - return (2 + r); -} - -int ft_putnbrulong(unsigned int n) -{ - int len; - - len = 0; - if (n < 10) - len += write(1, &(char){n + '0'}, 1); - else - { - len += ft_putnbrulong(n / 10); - len += write(1, &(char){n % 10 + '0'}, 1); - } - return (len); -} - -int ft_printconversion(char conversion, va_list args) -{ - int count; - - count = 0; - if (conversion == '%') - count = ft_putchar_p('%'); - else if (conversion == 's') - count = ft_putstr_p(va_arg(args, char *)); - else if (conversion == 'c') - count = ft_putchar_p(va_arg(args, int)); - else if (conversion == 'i' || conversion == 'd') - count = ft_putnbr_p(va_arg(args, int)); - else if (conversion == 'u') - count = ft_putnbrulong(va_arg(args, unsigned int)); - else if (conversion == 'p') - count = ft_putaddr(va_arg(args, void *)); - else if (conversion == 'x') - count = ft_putnbrbasep(va_arg(args, unsigned long), "0123456789abcdef"); - else if (conversion == 'X') - count = ft_putnbrbasep(va_arg(args, unsigned long), "0123456789ABCDEF"); - return (count); -} - -int ft_printf(const char *format, ...) -{ - int i; - va_list args; - int count; - - va_start(args, format); - count = 0; - i = 0; - if (format == NULL) - return (-1); - while (format[i]) - { - if (format[i] == '%') - { - i++; - if (format[i]) - count += ft_printconversion(format[i], args); - else - return (-1); - } - else - count += ft_putchar_p(format[i]); - i++; - } - va_end(args); - return (count); -} diff --git a/libft/print/printf/ft_putnbr.c b/libft/print/printf/ft_putnbr.c deleted file mode 100644 index 7759a4b..0000000 --- a/libft/print/printf/ft_putnbr.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:24:27 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../../libft.h" - -int ft_putnbr_p(int n) -{ - unsigned int nbr; - int len; - - len = 0; - if (n < 0) - { - len += write(1, "-", 1); - nbr = -n; - } - else - nbr = n; - if (nbr < 10) - len += write(1, &(char){nbr + '0'}, 1); - else - { - len += ft_putnbr_p(nbr / 10); - len += write(1, &(char){nbr % 10 + '0'}, 1); - } - return (len); -} diff --git a/libft/print/printf/ft_putnbrbase.c b/libft/print/printf/ft_putnbrbase.c deleted file mode 100644 index 678c08f..0000000 --- a/libft/print/printf/ft_putnbrbase.c +++ /dev/null @@ -1,47 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbrbase.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:29:04 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../../libft.h" - -int ft_putnbrbase_pf(unsigned int n, char *base) -{ - unsigned int base_len; - int len; - - len = 0; - base_len = (int)ft_strlen(base); - if (n < base_len) - len += write(1, &base[n % base_len], 1); - else - { - len += ft_putnbrbase_pf(n / base_len, base); - len += write(1, &base[n % base_len], 1); - } - return (len); -} - -int ft_putnbrbasep(unsigned long int n, char *base) -{ - unsigned long int base_len; - int len; - - len = 0; - base_len = (int)ft_strlen(base); - if (n < base_len) - len += write(1, &base[n % base_len], 1); - else - { - len += ft_putnbrbasep(n / base_len, base); - len += write(1, &base[n % base_len], 1); - } - return (len); -} diff --git a/libft/print/printf/ft_putstr.c b/libft/print/printf/ft_putstr.c deleted file mode 100644 index 13a5ad0..0000000 --- a/libft/print/printf/ft_putstr.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putstr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:24:56 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../../libft.h" - -int ft_putstr_p(char *s) -{ - if (s == NULL) - { - write(1, "(null)", 6); - return (6); - } - return (write(1, s, ft_strlen(s))); -} diff --git a/libft/is/ft_isalnum.c b/libft/src/char/ft_isalnum.c similarity index 70% rename from libft/is/ft_isalnum.c rename to libft/src/char/ft_isalnum.c index b41e0b0..1c40f83 100644 --- a/libft/is/ft_isalnum.c +++ b/libft/src/char/ft_isalnum.c @@ -3,17 +3,16 @@ /* ::: :::::::: */ /* ft_isalnum.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9')) - return (1); - return (0); + return (ft_isalpha(c) || ft_isdigit(c)); } diff --git a/libft/is/ft_isalpha.c b/libft/src/char/ft_isalpha.c similarity index 72% rename from libft/is/ft_isalpha.c rename to libft/src/char/ft_isalpha.c index 5585ab7..893ad52 100644 --- a/libft/is/ft_isalpha.c +++ b/libft/src/char/ft_isalpha.c @@ -3,16 +3,14 @@ /* ::: :::::::: */ /* ft_isalpha.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) - return (1); - return (0); + return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); } diff --git a/libft/is/ft_isascii.c b/libft/src/char/ft_isascii.c similarity index 74% rename from libft/is/ft_isascii.c rename to libft/src/char/ft_isascii.c index 32648e4..642a77b 100644 --- a/libft/is/ft_isascii.c +++ b/libft/src/char/ft_isascii.c @@ -3,16 +3,14 @@ /* ::: :::::::: */ /* ft_isascii.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 0 && c <= 127) - return (1); - return (0); + return (c >= 0 && c <= 127); } diff --git a/libft/is/ft_isdigit.c b/libft/src/char/ft_isdigit.c similarity index 74% rename from libft/is/ft_isdigit.c rename to libft/src/char/ft_isdigit.c index 79e2778..99ad123 100644 --- a/libft/is/ft_isdigit.c +++ b/libft/src/char/ft_isdigit.c @@ -3,16 +3,14 @@ /* ::: :::::::: */ /* ft_isdigit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = '0' && c <= '9') - return (1); - return (0); + return (c >= '0' && c <= '9'); } diff --git a/libft/is/ft_isprint.c b/libft/src/char/ft_isprint.c similarity index 74% rename from libft/is/ft_isprint.c rename to libft/src/char/ft_isprint.c index e21f334..f08e787 100644 --- a/libft/is/ft_isprint.c +++ b/libft/src/char/ft_isprint.c @@ -3,16 +3,14 @@ /* ::: :::::::: */ /* ft_isprint.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 32 && c <= 126) - return (1); - return (0); + return (c >= 32 && c <= 126); } diff --git a/libft/print/ft_putchar.c b/libft/src/int/ft_abs.c similarity index 66% rename from libft/print/ft_putchar.c rename to libft/src/int/ft_abs.c index c44212a..ac6e3ee 100644 --- a/libft/print/ft_putchar.c +++ b/libft/src/int/ft_abs.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_putchar.c :+: :+: :+: */ +/* ft_abs.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou b) + return (a); + return (b); } diff --git a/libft/src/int/ft_min.c b/libft/src/int/ft_min.c new file mode 100644 index 0000000..8f460e0 --- /dev/null +++ b/libft/src/int/ft_min.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_min.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou nb_init, stack->nb) + + ft_putstack_fd(stack->next, fd)); + } + return (0); +} + +static void print_arg(va_list argsl, char type, size_t *l) +{ + if (type == 'c') + *l += ft_putchar_fd(va_arg(argsl, int), 1); + else if (type == 's') + *l += ft_putstr_fd(va_arg(argsl, char *), 1); + else if (type == 'p') + *l += ft_putptr_fd(va_arg(argsl, unsigned long long), 1); + else if (type == 'd' || type == 'i') + *l += ft_putnbr_fd(va_arg(argsl, int), 1); + else if (type == 'u') + *l += ft_putnbr_fd(va_arg(argsl, unsigned int), 1); + else if (type == 'x') + *l += ft_putuhex_fd(va_arg(argsl, unsigned int), "0123456789abcdef", 1); + else if (type == 'X') + *l += ft_putuhex_fd(va_arg(argsl, unsigned int), "0123456789ABCDEF", 1); + else if (type == 'S') + *l += ft_putstack_fd(va_arg(argsl, t_stack *), 1); + else if (type) + { + *l += ft_putchar_fd('%', 1); + if (type != '%') + *l += ft_putchar_fd(type, 1); + } + else + *l = -1; +} + +int ft_printf(const char *str, ...) +{ + va_list argsl; + size_t i; + size_t l; + + if (!str) + return (-1); + va_start(argsl, str); + i = 0; + l = 0; + while (str[i]) + { + if (str[i] == '%') + print_arg(argsl, str[++i], &l); + else + l += ft_putchar_fd(str[i], 1); + if (str[i]) + i++; + } + va_end(argsl); + return (l); +} diff --git a/libft/src/io/ft_printf_fd.c b/libft/src/io/ft_printf_fd.c new file mode 100644 index 0000000..582e0a2 --- /dev/null +++ b/libft/src/io/ft_printf_fd.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou nb_init, s->nb) + + ft_putstack_fd(s->next, fd)); + } + return (0); +} + +static void print_arg_fd(va_list argsl, char type, size_t *l, int fd) +{ + if (type == 'c') + *l += ft_putchar_fd(va_arg(argsl, int), fd); + else if (type == 's') + *l += ft_putstr_fd(va_arg(argsl, char *), fd); + else if (type == 'p') + *l += ft_putptr_fd(va_arg(argsl, unsigned long long), fd); + else if (type == 'd' || type == 'i') + *l += ft_putnbr_fd(va_arg(argsl, int), fd); + else if (type == 'u') + *l += ft_putnbr_fd(va_arg(argsl, uint32_t), fd); + else if (type == 'x') + *l += ft_putuhex_fd(va_arg(argsl, uint32_t), "0123456789abcdef", fd); + else if (type == 'X') + *l += ft_putuhex_fd(va_arg(argsl, uint32_t), "0123456789ABCDEF", fd); + else if (type == 'S') + *l += ft_putstack_fd(va_arg(argsl, t_stack *), fd); + else if (type) + { + *l += ft_putchar_fd('%', 1); + if (type != '%') + *l += ft_putchar_fd(type, 1); + } + else + *l = -1; +} + +int ft_printf_fd(int fd, const char *str, ...) +{ + va_list argsl; + size_t i; + size_t l; + + if (!str) + return (-1); + va_start(argsl, str); + i = 0; + l = 0; + while (str[i]) + { + if (str[i] == '%') + print_arg_fd(argsl, str[++i], &l, fd); + else + l += ft_putchar_fd(str[i], fd); + if (str[i]) + i++; + } + va_end(argsl); + return (l); +} diff --git a/libft/print/ft_putchar_fd.c b/libft/src/io/ft_putchar_fd.c similarity index 72% rename from libft/print/ft_putchar_fd.c rename to libft/src/io/ft_putchar_fd.c index d421314..7270571 100644 --- a/libft/print/ft_putchar_fd.c +++ b/libft/src/io/ft_putchar_fd.c @@ -3,16 +3,16 @@ /* ::: :::::::: */ /* ft_putchar_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou next = new; + tmp = *lst; + tmp = ft_lstlast(*lst); + tmp->next = new; } diff --git a/libft/lst/ft_lstadd_front.c b/libft/src/linked_lists/ft_lstadd_front.c similarity index 71% rename from libft/lst/ft_lstadd_front.c rename to libft/src/linked_lists/ft_lstadd_front.c index d9ea2a5..2d1a24e 100644 --- a/libft/lst/ft_lstadd_front.c +++ b/libft/src/linked_lists/ft_lstadd_front.c @@ -3,21 +3,24 @@ /* ::: :::::::: */ /* ft_lstadd_front.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou next = *lst; + t_list *tmp; + + tmp = *lst; *lst = new; + if (!new || !lst) + return ; + while (new->next) + new = new->next; + new->next = tmp; } diff --git a/libft/lst/ft_lstclear.c b/libft/src/linked_lists/ft_lstclear.c similarity index 74% rename from libft/lst/ft_lstclear.c rename to libft/src/linked_lists/ft_lstclear.c index 577838d..81d116c 100644 --- a/libft/lst/ft_lstclear.c +++ b/libft/src/linked_lists/ft_lstclear.c @@ -3,24 +3,25 @@ /* ::: :::::::: */ /* ft_lstclear.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou next; - ft_lstdelone((*lst), del); + ft_lstdelone(*lst, del); *lst = tmp; } } diff --git a/libft/lst/ft_lstdelone.c b/libft/src/linked_lists/ft_lstdelone.c similarity index 76% rename from libft/lst/ft_lstdelone.c rename to libft/src/linked_lists/ft_lstdelone.c index 0673766..feb14c3 100644 --- a/libft/lst/ft_lstdelone.c +++ b/libft/src/linked_lists/ft_lstdelone.c @@ -3,19 +3,20 @@ /* ::: :::::::: */ /* ft_lstdelone.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou content); free(lst); + lst = NULL; } diff --git a/libft/lst/ft_lstiter.c b/libft/src/linked_lists/ft_lstiter.c similarity index 76% rename from libft/lst/ft_lstiter.c rename to libft/src/linked_lists/ft_lstiter.c index b31e412..8580662 100644 --- a/libft/lst/ft_lstiter.c +++ b/libft/src/linked_lists/ft_lstiter.c @@ -3,22 +3,23 @@ /* ::: :::::::: */ /* ft_lstiter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: yosyo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/11 18:20:25 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:58:16 by adjoly ### ########.fr */ +/* Created: 2023/11/09 12:49:19 by yosyo #+# #+# */ +/* Updated: 2023/11/13 19:32:17 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../libft.h" +#include "libft.h" void ft_lstiter(t_list *lst, void (*f)(void *)) { if (!lst || !f) return ; - while (lst) + while (lst->next) { f(lst->content); lst = lst->next; } + f(lst->content); } diff --git a/libft/lst/ft_lstlast.c b/libft/src/linked_lists/ft_lstlast.c similarity index 79% rename from libft/lst/ft_lstlast.c rename to libft/src/linked_lists/ft_lstlast.c index eee993f..02833ce 100644 --- a/libft/lst/ft_lstlast.c +++ b/libft/src/linked_lists/ft_lstlast.c @@ -3,14 +3,14 @@ /* ::: :::::::: */ /* ft_lstlast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: yosyo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/11 18:24:49 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:58:45 by adjoly ### ########.fr */ +/* Created: 2023/11/09 12:53:23 by yosyo #+# #+# */ +/* Updated: 2023/11/13 19:33:20 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../libft.h" +#include "libft.h" t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) { - t_list *res; + t_list *r; t_list *tmp; - if (!lst || !f || !del) - return (NULL); - res = NULL; - while (lst) + r = NULL; + while (lst && f && del) { tmp = ft_lstnew(f(lst->content)); - if (tmp == NULL) + if (!tmp) { ft_lstclear(&tmp, del); return (NULL); } - ft_lstadd_back(&res, tmp); + ft_lstadd_back(&r, tmp); lst = lst->next; } - return (res); + return (r); } diff --git a/libft/lst/ft_lstnew.c b/libft/src/linked_lists/ft_lstnew.c similarity index 69% rename from libft/lst/ft_lstnew.c rename to libft/src/linked_lists/ft_lstnew.c index 2543b70..c23e458 100644 --- a/libft/lst/ft_lstnew.c +++ b/libft/src/linked_lists/ft_lstnew.c @@ -3,23 +3,23 @@ /* ::: :::::::: */ /* ft_lstnew.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou content = content; + r->next = NULL; + return (r); } diff --git a/libft/lst/ft_lstsize.c b/libft/src/linked_lists/ft_lstsize.c similarity index 71% rename from libft/lst/ft_lstsize.c rename to libft/src/linked_lists/ft_lstsize.c index a83cad4..3b01e1b 100644 --- a/libft/lst/ft_lstsize.c +++ b/libft/src/linked_lists/ft_lstsize.c @@ -3,21 +3,23 @@ /* ::: :::::::: */ /* ft_lstsize.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou next) { lst = lst->next; i++; diff --git a/libft/mem/ft_bzero.c b/libft/src/mem/ft_bzero.c similarity index 75% rename from libft/mem/ft_bzero.c rename to libft/src/mem/ft_bzero.c index 5b968e8..1fc71b7 100644 --- a/libft/mem/ft_bzero.c +++ b/libft/src/mem/ft_bzero.c @@ -3,16 +3,16 @@ /* ::: :::::::: */ /* ft_bzero.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou 4294967295)) + if (n <= 0 || elsize <= 0) + return (malloc(0)); + if ((elsize * n / n) != elsize) return (NULL); - if ((int)size < 0 && (int)nmemb < 0) + size = n * elsize; + t = malloc(size); + if (!t) return (NULL); - result = malloc(size * nmemb); - if (!result) - return (NULL); - while (i < (size * nmemb)) - { - *(unsigned char *)(result + i) = '\0'; - i++; - } - return (result); + ft_bzero(t, size); + return (t); } diff --git a/libft/src/mem/ft_free.c b/libft/src/mem/ft_free.c new file mode 100644 index 0000000..2dc15df --- /dev/null +++ b/libft/src/mem/ft_free.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 19:27:31 by mmoussou #+# #+# */ +/* Updated: 2024/01/18 01:05:44 by mmoussou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void free_str(char **str) +{ + free(*str); + *str = NULL; +} + +void free_stack(t_stack **stack) +{ + if (*stack && (*stack)->next) + free_stack(&(*stack)->next); + free(*stack); + *stack = NULL; +} + +void free_list(t_list **lst) +{ + if (*lst && (*lst)->next) + free_list(&(*lst)->next); + free(*lst); + *lst = NULL; +} + +void free_tab(char ***arr) +{ + int i; + + i = 0; + while ((*arr)[i]) + { + free((*arr)[i]); + i++; + } + free(*arr); + *arr = NULL; +} + +void ft_free(const char *str, ...) +{ + va_list args; + size_t i; + + if (!str) + return ; + va_start(args, str); + i = 0; + while (str[i]) + { + if (str[i] == 'c') + free_str(va_arg(args, char **)); + if (str[i] == 's') + free_stack(va_arg(args, t_stack **)); + if (str[i] == 'l') + free_list(va_arg(args, t_list **)); + if (str[i] == 'a') + free_tab(va_arg(args, char ***)); + i++; + } +} diff --git a/libft/mem/ft_memchr.c b/libft/src/mem/ft_memchr.c similarity index 72% rename from libft/mem/ft_memchr.c rename to libft/src/mem/ft_memchr.c index 93fe9f7..e664aa6 100644 --- a/libft/mem/ft_memchr.c +++ b/libft/src/mem/ft_memchr.c @@ -3,25 +3,29 @@ /* ::: :::::::: */ /* ft_memchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou src) - while (n-- > 0) - ((unsigned char *)dest)[n] = ((unsigned char *)src)[n]; - else if (dest < src) - while (++i < n) - ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; - return (dest); + i = 0; + if (dst && src) + { + ptr1 = (char *)dst; + ptr2 = (char *)src; + if (ptr1 > ptr2) + while (n--) + ptr1[n] = ptr2[n]; + else + { + while (i < n) + { + ptr1[i] = ptr2[i]; + i++; + } + } + } + return (dst); } diff --git a/libft/mem/ft_memset.c b/libft/src/mem/ft_memset.c similarity index 72% rename from libft/mem/ft_memset.c rename to libft/src/mem/ft_memset.c index 88c5805..f101034 100644 --- a/libft/mem/ft_memset.c +++ b/libft/src/mem/ft_memset.c @@ -3,24 +3,29 @@ /* ::: :::::::: */ /* ft_memset.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/18 00:36:32 by mmoussou #+# #+# */ +/* Updated: 2024/01/18 00:39:38 by mmoussou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_stackadd_back(t_stack **stack, t_stack *new) +{ + t_stack *tmp; + + if (!stack || !new) + return ; + if (!*stack) + { + *stack = new; + return ; + } + tmp = *stack; + tmp = ft_stacklast(*stack); + tmp->next = new; +} diff --git a/libft/print/printf/ft_putchar.c b/libft/src/stack/ft_stackadd_front.c similarity index 63% rename from libft/print/printf/ft_putchar.c rename to libft/src/stack/ft_stackadd_front.c index 558d5e0..993211c 100644 --- a/libft/print/printf/ft_putchar.c +++ b/libft/src/stack/ft_stackadd_front.c @@ -1,19 +1,26 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_putchar.c :+: :+: :+: */ +/* ft_stackadd_front.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:22:30 by adjoly ### ########.fr */ +/* Created: 2024/01/18 00:36:50 by mmoussou #+# #+# */ +/* Updated: 2024/01/18 00:37:05 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include "libft.h" -int ft_putchar_p(char c) +void ft_stackadd_front(t_stack **stack, t_stack *new) { - write(1, &c, 1); - return (1); + t_stack *tmp; + + tmp = *stack; + *stack = new; + if (!new || !stack) + return ; + while (new->next) + new = new->next; + new->next = tmp; } diff --git a/libft/mem/ft_freetab.c b/libft/src/stack/ft_stacklast.c similarity index 61% rename from libft/mem/ft_freetab.c rename to libft/src/stack/ft_stacklast.c index 3175d73..926084e 100644 --- a/libft/mem/ft_freetab.c +++ b/libft/src/stack/ft_stacklast.c @@ -1,26 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_freetab.c :+: :+: :+: */ +/* ft_stacklast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/27 15:33:20 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:20:24 by adjoly ### ########.fr */ +/* Created: 2024/01/18 00:36:13 by mmoussou #+# #+# */ +/* Updated: 2024/01/18 00:36:15 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include "libft.h" -void ft_freearr(void **arr) +t_stack *ft_stacklast(t_stack *stack) { - void **tmp; - - tmp = arr; - while (*tmp) + if (stack) { - free(*tmp); - tmp++; + while (stack->next) + stack = stack->next; + return (stack); } - free(arr); + return (NULL); } diff --git a/libft/src/stack/ft_stacknew.c b/libft/src/stack/ft_stacknew.c new file mode 100644 index 0000000..53a4f47 --- /dev/null +++ b/libft/src/stack/ft_stacknew.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stacknew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/18 00:35:18 by mmoussou #+# #+# */ +/* Updated: 2024/01/18 00:35:30 by mmoussou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_stack *ft_stacknew(int content) +{ + t_stack *new_element; + + new_element = malloc(sizeof(t_stack)); + if (!new_element) + return (NULL); + new_element->nb_init = content; + new_element->nb = -1; + new_element->next = NULL; + return (new_element); +} diff --git a/libft/src/stack/ft_stacksize.c b/libft/src/stack/ft_stacksize.c new file mode 100644 index 0000000..fdc27c1 --- /dev/null +++ b/libft/src/stack/ft_stacksize.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stacksize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou next) + { + stack = stack->next; + i++; + } + } + return (i); +} diff --git a/libft/str/ft_atoi.c b/libft/src/str/XtoX/ft_atoi.c similarity index 58% rename from libft/str/ft_atoi.c rename to libft/src/str/XtoX/ft_atoi.c index 541098d..ac135a0 100644 --- a/libft/str/ft_atoi.c +++ b/libft/src/str/XtoX/ft_atoi.c @@ -3,35 +3,38 @@ /* ::: :::::::: */ /* ft_atoi.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 7 && nptr[i] <= 13) || nptr[i] == 32) + s = 1; + if (!str) + return (0); + while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13)) i++; - if (nptr[i] == '-') + if (str[i] == '-' || str[i] == '+') { - sign *= -1; + if (str[i] == '-') + s *= -1; i++; } - else if (nptr[i] == '+') - i++; - while (nptr[i] >= '0' && nptr[i] <= '9') + while (str[i] && (str[i] >= '0' && str[i] <= '9')) { - nbr = nbr * 10 + (nptr[i] - '0'); + r = (r * 10) + (str[i] - '0'); i++; } - return (nbr * sign); + return (r * s); } diff --git a/libft/io/get_next_line/get_next_line_utils.c b/libft/src/str/XtoX/ft_atol.c similarity index 51% rename from libft/io/get_next_line/get_next_line_utils.c rename to libft/src/str/XtoX/ft_atol.c index 25995df..7d5427c 100644 --- a/libft/io/get_next_line/get_next_line_utils.c +++ b/libft/src/str/XtoX/ft_atol.c @@ -1,42 +1,40 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line_utils.c :+: :+: :+: */ +/* ft_atol.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 9 && str[i] <= 13)) + i++; + if (str[i] == '-' || str[i] == '+') { - result[i] = s1[i]; + if (str[i] == '-') + s *= -1; i++; } - while (s2[j]) + while (str[i] && (str[i] >= '0' && str[i] <= '9')) { - result[i] = s2[j]; + r = (r * 10) + (str[i] - '0'); i++; - j++; } - free(s1); - result[i] = '\0'; - return (result); + return (r * s); } diff --git a/libft/src/str/XtoX/ft_itoa.c b/libft/src/str/XtoX/ft_itoa.c new file mode 100644 index 0000000..88bf179 --- /dev/null +++ b/libft/src/str/XtoX/ft_itoa.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou = 1) + { + s++; + n /= 10; + } + return (s); +} + +static size_t pwten(int n) +{ + int i; + + i = 1; + while (n) + { + i *= 10; + n--; + } + return (i); +} + +static char *itoa_rec(char *r, int n) +{ + int i; + + if (n > 9) + { + r[0] = n / (pwten(n_size(n) - 1)) + '0'; + i = 1; + while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i) + { + r[i] = '0'; + i++; + } + itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \ + n % pwten(n_size(n) - 1)); + return (r); + } + r[0] = n + '0'; + return (r); +} + +char *ft_itoa(int n) +{ + char *r; + + if (n == -2147483648) + return (ft_strdup("-2147483648")); + r = ft_calloc(sizeof(char), n_size(n) + 1); + if (!r) + return (NULL); + if (n == 0) + { + r[0] = '0'; + return (r); + } + if (n < 0) + { + r[0] = '-'; + itoa_rec(&r[1], n * -1); + return (r); + } + itoa_rec(r, n); + return (r); +} diff --git a/libft/src/str/XtoX/ft_ltoa.c b/libft/src/str/XtoX/ft_ltoa.c new file mode 100644 index 0000000..c136146 --- /dev/null +++ b/libft/src/str/XtoX/ft_ltoa.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ltoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou = 1) + { + s++; + n /= 10; + } + return (s); +} + +static size_t pwten(int n) +{ + int i; + + i = 1; + while (n) + { + i *= 10; + n--; + } + return (i); +} + +static char *itoa_rec(char *r, int n) +{ + int i; + + if (n > 9) + { + r[0] = n / (pwten(n_size(n) - 1)) + '0'; + i = 1; + while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i) + { + r[i] = '0'; + i++; + } + itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \ + n % pwten(n_size(n) - 1)); + return (r); + } + r[0] = n + '0'; + return (r); +} + +char *ft_ltoa(long long n) +{ + char *r; + + if (n == -9223372036854775807) + return (ft_strdup("-9223372036854775807")); + r = ft_calloc(sizeof(char), n_size(n) + 1); + if (!r) + return (NULL); + if (n == 0) + { + r[0] = '0'; + return (r); + } + if (n < 0) + { + r[0] = '-'; + itoa_rec(&r[1], n * -1); + return (r); + } + itoa_rec(r, n); + return (r); +} diff --git a/libft/src/str/ft_split.c b/libft/src/str/ft_split.c new file mode 100644 index 0000000..a5e426d --- /dev/null +++ b/libft/src/str/ft_split.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ +/* By: mmoussou 0) + if (!s) + return (NULL); + i = ft_strlen(s); + if (!c) + return (&((char *)s)[i]); + while (i + 1) { - if (s[i] == (char)c) + if (s[i] == (unsigned char)c) return (&((char *)s)[i]); i--; } - if (s[0] == (char)c) - return (&((char *)s)[0]); return (NULL); } diff --git a/libft/src/str/ft_strtrim.c b/libft/src/str/ft_strtrim.c new file mode 100644 index 0000000..556ba7e --- /dev/null +++ b/libft/src/str/ft_strtrim.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou 0 && is_charset(s1[o + i], set)) + i--; + if (o == ft_strlen(s1) - 1) + return (malloc(0)); + r = ft_calloc(sizeof(char), i + 2); + if (!r) + return (NULL); + while (i + 1) + { + r[i] = s1[o + i]; + i--; + } + return (r); +} diff --git a/libft/str/ft_substr.c b/libft/src/str/ft_substr.c similarity index 58% rename from libft/str/ft_substr.c rename to libft/src/str/ft_substr.c index 0c8aea5..08c191e 100644 --- a/libft/str/ft_substr.c +++ b/libft/src/str/ft_substr.c @@ -3,35 +3,37 @@ /* ::: :::::::: */ /* ft_substr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/02 17:59:58 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:48:54 by adjoly ### ########.fr */ +/* Created: 2023/10/30 17:46:28 by mmoussou #+# #+# */ +/* Updated: 2023/11/11 11:45:52 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../libft.h" +#include "libft.h" char *ft_substr(char const *s, unsigned int start, size_t len) { - size_t i; - char *result; + size_t l; + size_t nl; + char *r; - i = 0; - if (s == NULL) - return (0); - if (len >= ft_strlen(s)) - len = ft_strlen(s) - start; - if (len == 0 || start >= ft_strlen(s)) - return (ft_calloc(1, 1)); - result = malloc((len + 1) * sizeof(char)); - if (result == NULL) + if (!s) return (NULL); - while (i < len && s[start + i]) + l = ft_strlen(s); + if (start > l) { - result[i] = s[start + i]; - i++; + r = ft_calloc(1, 1); + return (r); } - result[i] = '\0'; - return (result); + nl = ft_strlen(s + start); + if (nl > len) + nl = len; + r = malloc((nl + 1) * sizeof(char)); + if (!r) + return (NULL); + r[nl] = 0; + while (nl--) + r[nl] = s[start + nl]; + return (r); } diff --git a/libft/str/ft_tolower.c b/libft/src/str/ft_tolower.c similarity index 76% rename from libft/str/ft_tolower.c rename to libft/src/str/ft_tolower.c index 56bb63e..3edad3f 100644 --- a/libft/str/ft_tolower.c +++ b/libft/src/str/ft_tolower.c @@ -3,16 +3,18 @@ /* ::: :::::::: */ /* ft_tolower.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 'A' && c <= 'Z') - return (c + 32); + return (c + ('a' - 'A')); return (c); } diff --git a/libft/str/ft_toupper.c b/libft/src/str/ft_toupper.c similarity index 76% rename from libft/str/ft_toupper.c rename to libft/src/str/ft_toupper.c index 5066e55..e160148 100644 --- a/libft/str/ft_toupper.c +++ b/libft/src/str/ft_toupper.c @@ -3,16 +3,18 @@ /* ::: :::::::: */ /* ft_toupper.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou = 'a' && c <= 'z') - return (c - 32); + return (c - ('a' - 'A')); return (c); } diff --git a/libft/str/ft_atoll.c b/libft/str/ft_atoll.c deleted file mode 100644 index 276e497..0000000 --- a/libft/str/ft_atoll.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_atoll.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/10/31 09:00:27 by adjoly #+# #+# */ -/* Updated: 2024/02/15 13:49:15 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -long long ft_atoll(const char *nptr) -{ - char sign; - long long nbr; - - sign = 1; - nbr = 0; - while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32) - nptr++; - if (*nptr == '-') - { - sign *= -1; - nptr++; - } - else if (*nptr == '+') - nptr++; - while (*nptr >= '0' && *nptr <= '9') - { - nbr = nbr * 10 + (*nptr - '0'); - nptr++; - } - return (nbr * sign); -} diff --git a/libft/str/ft_itoa.c b/libft/str/ft_itoa.c deleted file mode 100644 index 79de97c..0000000 --- a/libft/str/ft_itoa.c +++ /dev/null @@ -1,58 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_itoa.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/05 16:26:26 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:45:39 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -static int ft_count_digit(int n) -{ - int i; - - i = 0; - if (n < 0) - i++; - if (n == 0) - return (1); - while (n) - { - n /= 10; - i++; - } - return (i); -} - -char *ft_itoa(int n) -{ - char *result; - int i; - - i = ft_count_digit(n); - if (n == 0) - return (ft_strdup("0")); - if (n == -2147483648) - return (ft_strdup("-2147483648")); - result = malloc(sizeof(char) * (i + 1)); - if (result == NULL) - return (NULL); - result[i--] = '\0'; - if (n < 0) - { - result[0] = '-'; - n = -n; - } - while (n) - { - result[i] = n % 10 + '0'; - n /= 10; - i--; - } - return (result); -} diff --git a/libft/str/ft_split.c b/libft/str/ft_split.c deleted file mode 100644 index 1e5e3bf..0000000 --- a/libft/str/ft_split.c +++ /dev/null @@ -1,94 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_split.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/12 09:14:19 by adjoly #+# #+# */ -/* Updated: 2024/04/27 17:21:29 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -static int ft_countword(char const *s, char sep) -{ - int i; - int w_count; - - i = 0; - w_count = 0; - while (s[i]) - { - if (s[i] != sep && (i == 0 || s[i - 1] == sep)) - w_count++; - i++; - } - return (w_count); -} - -static int ft_countletter(char const *s, char sep) -{ - int i; - - i = 0; - while (s[i] != sep && s[i] != '\0') - i++; - return (i); -} - -static void *ft_freearr_s(char **arr) -{ - int i; - - i = 0; - while (arr[i]) - { - free(arr[i]); - i++; - } - free(arr); - return (NULL); -} - -char **ft_split_to_result(char **tab, char const *s, char c) -{ - int i; - int j; - int k; - - i = 0; - j = 0; - while (s[i]) - { - if (s[i] != c) - { - k = 0; - tab[j] = ft_calloc(ft_countletter(&s[i], c) + 1, sizeof(char)); - if (!tab[j]) - return (ft_freearr_s(tab)); - while (s[i] && s[i] != c) - tab[j][k++] = s [i++]; - tab[j][k] = '\0'; - j++; - } - else - i++; - } - tab[j] = NULL; - return (tab); -} - -char **ft_split(char const *s, char c) -{ - char **result; - - if (s == NULL) - return (NULL); - result = ft_calloc(ft_countword(s, c) + 1, sizeof(char *)); - if (!result) - return (NULL); - result = ft_split_to_result(result, s, c); - return (result); -} diff --git a/libft/str/ft_strjoin.c b/libft/str/ft_strjoin.c deleted file mode 100644 index 574d390..0000000 --- a/libft/str/ft_strjoin.c +++ /dev/null @@ -1,41 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strjoin.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/04 13:44:09 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:45:30 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strjoin(char const *s1, char const *s2) -{ - char *result; - size_t i; - size_t j; - - i = 0; - j = 0; - if (s1 == NULL || s2 == NULL) - return (NULL); - result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char)); - if (result == NULL) - return (NULL); - while (s1[i]) - { - result[i] = s1[i]; - i++; - } - while (s2[j]) - { - result[i] = s2[j]; - i++; - j++; - } - result[i] = '\0'; - return (result); -} diff --git a/libft/str/ft_strtrim.c b/libft/str/ft_strtrim.c deleted file mode 100644 index c67c7e0..0000000 --- a/libft/str/ft_strtrim.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strtrim.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/11 00:37:37 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:48:45 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strtrim(char const *s1, char const *set) -{ - int i; - int j; - char *result; - - i = 0; - if (s1 == NULL || set == NULL) - return (NULL); - j = ft_strlen(s1); - while (ft_strchr(set, s1[i]) != 0) - i++; - while (ft_strrchr(set, s1[j]) != 0) - j--; - result = ft_substr(s1, i, (j - i) + 1); - return (result); -}