diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..47bee9e --- /dev/null +++ b/Makefile @@ -0,0 +1,80 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: okoca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/08 11:20:19 by okoca #+# #+# # +# Updated: 2024/08/15 00:49:16 by tsuchen ### ########.fr # +# # +# **************************************************************************** # + +NAME = cub3d + +SRCS_M = main.c + +SRCS_REPO1 = + +SRCS_REPO2 = + +SRC = srcs/ + +REPO1_PATH = srcs/repo1/ +REPO2_PATH = srcs/repo2/ + +SRCS = $(addprefix $(SRC), $(SRCS_M)) \ + $(addprefix $(REPO1_PATH), $(SRCS_REPO1)) \ + $(addprefix $(REPO2_PATH), $(SRCS_REPO2)) \ + +HEADERS = cub3d.h + +OBJS = $(SRCS:.c=.o) + +HEAD = includes/ + +CFLAGS = -Wall -Wextra -Werror # -g + +CC = cc + +H_DEPS = $(addprefix $(HEAD), $(HEADERS)) + +LIBFT_PATH = libft/ +LIBFT_H_PATH = libft/includes/ +LIBFT = $(LIBFT_PATH)libft.a + +MLX = mlx_linux +MLXFLAGS = -lmlx_Linux -lXext -lX11 -lm -lz + +all: $(NAME) + +$(LIBFT): + $(MAKE) -C $(LIBFT_PATH) + +$(NAME): $(LIBFT) $(OBJS) $(H_DEPS) + $(CC) $(CFLAGS) $(OBJS) -L$(MLX) -I$(MLX) $(MLXFLAGS) $(LIBFT) -o $(NAME) + +bonus: $(NAME) + +%.o: %.c + $(CC) $(CFLAGS) -I$(HEAD) -I$(LIBFT_H_PATH) -I$(MLX) -c $< -o $@ + +clean: libft-clean root-clean + +libft-clean: + $(MAKE) -C $(LIBFT_PATH) clean + +root-clean: + rm -f $(OBJS) + +fclean: libft-fclean root-fclean + +libft-fclean: + $(MAKE) -C $(LIBFT_PATH) fclean + +root-fclean: root-clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re bonus libft libft-clean libft-fclean root-clean root-fclean diff --git a/includes/cub3d.h b/includes/cub3d.h new file mode 100644 index 0000000..4335995 --- /dev/null +++ b/includes/cub3d.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ +/* Updated: 2024/08/15 00:34:12 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CUB3D_H +# define CUB3D_H + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include "mlx.h" +# include "libft.h" +# include "ft_printf.h" +# include "get_next_line_bonus.h" + +# define OUT STDOUT_FILENO +# define IN STDIN_FILENO +# define ER STDERR_FILENO +# define P_NAME "cub3d" + +# define KEY_PRESS 2 +# define MOUSE_PRESS 4 +# define MOUSE_MOVE 6 +# define CLOSE_BUTTON 17 + +#endif diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..50827d3 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,86 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: okoca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/06/15 10:05:55 by tsuchen #+# #+# # +# Updated: 2024/08/15 00:48:25 by tsuchen ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a + +SRCS_LIBC = ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c \ + ft_strlen.c ft_memset.c ft_bzero.c ft_memcpy.c ft_memmove.c \ + ft_strlcpy.c ft_strlcat.c ft_toupper.c ft_tolower.c ft_strchr.c \ + ft_strrchr.c ft_strncmp.c ft_memchr.c ft_memcmp.c ft_strnstr.c \ + ft_atoi.c ft_calloc.c ft_strdup.c ft_strcmp.c ft_atof.c ft_strndup.c \ + ft_atol.c + +SRCS_ARRAY = ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c \ + ft_strmapi.c ft_striteri.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c \ + ft_putnbr_fd.c ft_split_quote.c ft_advanced_sort_string_tab.c \ + ft_arr_size.c ft_free_all.c ft_split2.c ft_ltoa.c + +SRCS_LIST = ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c \ + ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c \ + ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c + +SRCS_PRINTF = ft_print_char.c ft_print_digit.c ft_printf.c ft_print_mem.c \ + ft_print_spec.c ft_print_str.c ft_is_spec.c ft_is_left_bonus.c \ + ft_gen_wid_pre_bonus.c ft_dgt_cnt_abs_bonus.c ft_have_zero_bonus.c \ + ft_have_alt_bonus.c ft_have_space_bonus.c ft_have_plus_bonus.c \ + ft_print_char_f_bonus.c ft_print_str_f_bonus.c ft_print_mem_f_bonus.c \ + ft_print_spec_f_bonus.c ft_print_uint_f_bonus.c ft_print_sint_f_bonus.c \ + ft_print_sign_bonus.c ft_print_alt_bonus.c ft_procs_flag_bonus.c \ + ft_rm_plus_space_bonus.c ft_rm_alt_bonus.c + +SRCS_GNL = get_next_line_bonus.c get_next_line_utils_bonus.c + +HEADERS = ft_printf.h get_next_line_bonus.h libft.h + +LIBC_PATH = srcs/libc/ +ARRAY_PATH = srcs/array/ +LIST_PATH = srcs/list/ +PRINTF_PATH = srcs/ft_printf/ +GNL_PATH = srcs/gnl/ + +SRCS = $(addprefix $(LIBC_PATH), $(SRCS_LIBC)) \ + $(addprefix $(ARRAY_PATH), $(SRCS_ARRAY)) \ + $(addprefix $(LIST_PATH), $(SRCS_LIST)) \ + $(addprefix $(PRINTF_PATH), $(SRCS_PRINTF)) \ + $(addprefix $(GNL_PATH), $(SRCS_GNL)) + +OBJS = $(SRCS:.c=.o) + +HEAD = includes/ + +HDRS_DEPS = $(addprefix $(HEAD), $(HEADERS)) + +CFLAGS = -Wall -Wextra -Werror + +ARC = ar rcs + +CC = cc + +all: $(NAME) + +$(NAME): $(OBJS) $(HDRS_DEPS) + @$(ARC) $(NAME) $(OBJS) + +bonus: $(NAME) + +.c.o: + @$(CC) $(CFLAGS) -c $< -o $@ -I$(HEAD) + +clean: + @rm -f $(OBJS) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re bonus diff --git a/libft/includes/ft_printf.h b/libft/includes/ft_printf.h new file mode 100644 index 0000000..a2802ef --- /dev/null +++ b/libft/includes/ft_printf.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 10:42:50 by tsuchen #+# #+# */ +/* Updated: 2024/06/15 12:37:03 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include "libft.h" +# include +# define ER STDERR_FILENO +# define AD_SIZE 16 +# define HEX_SIZE 16 +# define DEC_TAB "0123456789" +# define HEX_TAB "0123456789abcdef" +# define HEX_TAB_U "0123456789ABCDEF" + +int ft_print_spec(char c, va_list *ap); +int ft_print_spec_f(char c, va_list *ap, char *flags, char **wid_pre); +int ft_print_char(char c); +int ft_print_char_f(char c, char *flags, char **wid_pre); +int ft_print_str(char *s); +int ft_print_str_f(char *s, char *flags, char **wid_pre); +int ft_print_mem(void *addr); +int ft_print_mem_f(void *addr, char *flags, char **wid_pre); +int ft_print_digit(long nbr, char *base); +int ft_print_uint_f(long nbr, char *base, char *flags, char **wid_pre); +int ft_print_sint_f(long nbr, char *base, char *flags, char **wid_pre); +int ft_print_sign(long *nbr, char *flags); +int ft_print_alt(char *base); +int ft_printf(const char *fmt, ...); +int ft_dgt_cnt_abs(long nbr, char *base); +int ft_is_spec(char c); +int ft_is_left(char *str); +int ft_have_zero(char *str); +int ft_have_alt(char *str); +int ft_have_space(char *str); +int ft_have_plus(char *str); +const char *ft_procs_flag(const char *fmt, va_list *ap, int *count); +void ft_gen_wid_pre(const char *fmt, size_t len, char **wid_pre); +void ft_rm_plus_space(char *str); +void ft_rm_alt(char *str); + +#endif diff --git a/libft/includes/get_next_line_bonus.h b/libft/includes/get_next_line_bonus.h new file mode 100644 index 0000000..0172598 --- /dev/null +++ b/libft/includes/get_next_line_bonus.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/29 17:54:22 by tsuchen #+# #+# */ +/* Updated: 2024/06/15 13:59:19 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_BONUS_H +# define GET_NEXT_LINE_BONUS_H + +# include "libft.h" +# include +# include +# include +# include +# include +# include +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif +# define MAX_FD 1024 +# define ER STDERR_FILENO + +typedef struct s_list_gnl +{ + char *str; + struct s_list_gnl *next; +} t_list_gnl; +void ft_lst_append(t_list_gnl **lst, char *str); +void ft_update_list(t_list_gnl **lst); +void ft_delone(t_list_gnl *lst); +int ft_have_nl_lst(t_list_gnl *bgn_lst); +int ft_line_size(t_list_gnl *lst); +char *get_next_line(int fd); +char *ft_gen_nl(t_list_gnl *lst); +void ft_fetch_nl(int fd, t_list_gnl **bgn_lst); + +#endif diff --git a/libft/includes/libft.h b/libft/includes/libft.h new file mode 100644 index 0000000..8543afb --- /dev/null +++ b/libft/includes/libft.h @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: okoca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 10:42:50 by tsuchen #+# #+# */ +/* Updated: 2024/07/17 17:31:35 by okoca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include +# include + +/* libc functions */ +int ft_atoi(const char *nptr); +double ft_atof(const char *nptr); +long ft_atol(const char *nptr); +void ft_bzero(void *s, size_t n); +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); +int ft_isprint(int c); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *s, int c, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *s); +char *ft_strndup(const char *str, int nb); +size_t ft_strlcat(char *dst, const char *src, size_t siz); +size_t ft_strlcpy(char *dst, const char *src, size_t siz); +size_t ft_strlen(const char *s); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strrchr(const char *s, int c); +int ft_tolower(int c); +int ft_toupper(int c); + +/* array functions */ +char *ft_itoa(int n); +char *ft_ltoa(long n); +void ft_putchar_fd(char c, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putstr_fd(char *s, int fd); +char **ft_split(char const *s, char c); +char **ft_split2(char const *s, char *set); +char **ft_split_quote(char const *s, char c); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +char *ft_strtrim(char const *s1, char const *set); +char *ft_substr(char const *s, unsigned int start, size_t len); +void ft_advanced_sort_string_tab(char **tab, + int (*cmp)(const char *, const char *)); +int ft_arr_size(char **arr); +void ft_free_all(char **arr); + +/* linked list functions */ +typedef struct s_list +{ + void *content; + struct s_list *next; + struct s_list *prev; +} t_list; + +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_front(t_list **lst, t_list *new); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +t_list *ft_lstnew(void *content); +int ft_lstsize(t_list *lst); + +#endif diff --git a/libft/srcs/array/ft_advanced_sort_string_tab.c b/libft/srcs/array/ft_advanced_sort_string_tab.c new file mode 100644 index 0000000..2fc2210 --- /dev/null +++ b/libft/srcs/array/ft_advanced_sort_string_tab.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_advanced_sort_string_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: okoca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/18 11:08:30 by tsuchen #+# #+# */ +/* Updated: 2024/07/16 08:39:52 by okoca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_swap_pointer(char **a1, char **a2) +{ + char *temp; + + temp = *a1; + *a1 = *a2; + *a2 = temp; +} + +void ft_advanced_sort_string_tab(char **tab, + int (*cmp)(const char *, const char *)) +{ + int i; + int j; + + i = 0; + while (tab[i]) + { + j = 1; + while (tab[i + j]) + { + if (cmp(tab[i], tab[i + j]) > 0) + ft_swap_pointer(&tab[i], &tab[i + j]); + j++; + } + i++; + } +} +/* +char **ft_split(char *str, char *charset); +int ft_arr_len(char **arr); +int ft_strcmp(char *s1, char *s2); +int ft_desc(char *s1, char *s2); + +int main(void) +{ + char *str = "This is a simple test. If it works, it works!"; + char *charset = " ,!"; + char **tab; + int arr_len; + + tab = ft_split(str, charset); + arr_len = ft_arr_len(tab); + printf("==> Before sort string <==\n"); + for (int i = 0; i < arr_len; i++) + printf("Split str %d is: %s\n", i, tab[i]); + ft_advanced_sort_string_tab(tab, &ft_desc); + printf("==> After sort string <==\n"); + for (int j = 0; j < arr_len; j++) + printf("Split str %d is: %s\n", j, tab[j]); + //free mem + for (int k = 0; k < arr_len; k++) + free(tab[k]); + free(tab); + return (0); +} + +int ft_strcmp(char *s1, char *s2) +{ + while ((*s1 || *s2) && (*s1 == *s2)) + { + s1++; + s2++; + } + return (*s1 - *s2); +} + +int ft_desc(char *s1, char *s2) +{ + if (ft_strcmp(s1, s2) >= 0) + return (-1); + else + return (1); +}*/ diff --git a/libft/srcs/array/ft_arr_size.c b/libft/srcs/array/ft_arr_size.c new file mode 100644 index 0000000..9372a04 --- /dev/null +++ b/libft/srcs/array/ft_arr_size.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arr_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/11 11:14:25 by tsuchen #+# #+# */ +/* Updated: 2024/07/11 11:19:08 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_arr_size(char **arr) +{ + int i; + + i = 0; + if (!arr) + return (0); + while (arr[i]) + i++; + return (i); +} +/* Note + * 1. The function return 0 if arr is NULL + * 2. The intput arr must be terminated by a NULL string + * 3. The return value is the number of sub_string excluding the last NULL str + */ diff --git a/libft/srcs/array/ft_free_all.c b/libft/srcs/array/ft_free_all.c new file mode 100644 index 0000000..6487108 --- /dev/null +++ b/libft/srcs/array/ft_free_all.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/11 11:46:12 by tsuchen #+# #+# */ +/* Updated: 2024/07/11 11:48:24 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_free_all(char **arr) +{ + int i; + + i = 0; + while (arr[i]) + free(arr[i++]); + free(arr); +} +/* Note + * This functin is to free all string arrays. It does not protect if the arr + * pointer is NULL. + */ diff --git a/libft/srcs/array/ft_itoa.c b/libft/srcs/array/ft_itoa.c new file mode 100644 index 0000000..8a77792 --- /dev/null +++ b/libft/srcs/array/ft_itoa.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 16:14:02 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 11:05:40 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_count_digit(int n) +{ + int count; + + count = 0; + if (n < 0) + { + count += 1; + n *= -1; + } + if (n == 0) + count += 1; + while (n > 0) + { + count += 1; + n /= 10; + } + return (count); +} + +static int ft_power(int base, int pwr, int output) +{ + if (pwr == 0) + return (output); + else + return (ft_power(base, pwr - 1, output * base)); +} + +char *ft_itoa(int n) +{ + char *str; + int size; + int i; + + if (n == -2147483648) + return (ft_strdup("-2147483648")); + size = ft_count_digit(n); + str = (char *)malloc((size + 1) * sizeof(char)); + if (!str) + return (0); + i = -1; + if (n < 0) + { + str[++i] = '-'; + n *= -1; + } + while (++i < size) + { + str[i] = (n / ft_power(10, size - i - 1, 1)) + '0'; + n %= ft_power(10, size - i - 1, 1); + } + str[i] = '\0'; + return (str); +} +/* Note + * 1. if n exceed MAX_INT or MIN_INT, the output will overflow + * 2. if malloc failed, return NULL + */ +/* +#include + +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + int nb; + char *str; + + nb = atoi(av[1]); + str = ft_itoa(nb); + printf("nbr: %d | str: %s\n", nb, str); + free(str); + return (0); +}*/ diff --git a/libft/srcs/array/ft_ltoa.c b/libft/srcs/array/ft_ltoa.c new file mode 100644 index 0000000..30aa343 --- /dev/null +++ b/libft/srcs/array/ft_ltoa.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ltoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 14:49:23 by tsuchen #+# #+# */ +/* Updated: 2024/07/17 15:48:00 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_count_digit(long n) +{ + int count; + + count = 0; + if (n < 0) + { + count += 1; + n *= -1; + } + if (n == 0) + count += 1; + while (n > 0) + { + count += 1; + n /= 10; + } + return (count); +} + +static long ft_power(long base, int pwr, long output) +{ + if (pwr == 0) + return (output); + else + return (ft_power(base, pwr - 1, output * base)); +} + +char *ft_ltoa(long n) +{ + char *str; + int size; + int i; + + if (n == LONG_MIN) + return (ft_strdup("-9223372036854775808")); + size = ft_count_digit(n); + str = (char *)malloc((size + 1) * sizeof(char)); + if (!str) + return (0); + i = -1; + if (n < 0) + { + str[++i] = '-'; + n *= -1; + } + while (++i < size) + { + str[i] = (n / ft_power(10, size - i - 1, 1)) + '0'; + n %= ft_power(10, size - i - 1, 1); + } + str[i] = '\0'; + return (str); +} +/* Note + * 1. if n exceed MAX_INT or MIN_INT, the output will overflow + * 2. if malloc failed, return NULL + */ +/* +#include + +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + int nb; + char *str; + + nb = atoi(av[1]); + str = ft_itoa(nb); + printf("nbr: %d | str: %s\n", nb, str); + free(str); + return (0); +}*/ \ No newline at end of file diff --git a/libft/srcs/array/ft_putchar_fd.c b/libft/srcs/array/ft_putchar_fd.c new file mode 100644 index 0000000..f1e3f23 --- /dev/null +++ b/libft/srcs/array/ft_putchar_fd.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 20:10:47 by tsuchen #+# #+# */ +/* Updated: 2024/05/09 20:20:40 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} +/* +#include +int main(int ac, char *av[]) +{ + if (ac != 2 || strlen(av[1]) != 1) + return (0); + ft_putchar_fd(*(av[1]), 1); + return (0); +}*/ diff --git a/libft/srcs/array/ft_putendl_fd.c b/libft/srcs/array/ft_putendl_fd.c new file mode 100644 index 0000000..c19f569 --- /dev/null +++ b/libft/srcs/array/ft_putendl_fd.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 20:20:59 by tsuchen #+# #+# */ +/* Updated: 2024/05/16 15:13:01 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + write(fd, s, ft_strlen(s)); + write(fd, "\n", 1); +} +/* +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + ft_putendl_fd(av[1], 1); + return (0); +}*/ diff --git a/libft/srcs/array/ft_putnbr_fd.c b/libft/srcs/array/ft_putnbr_fd.c new file mode 100644 index 0000000..d52c512 --- /dev/null +++ b/libft/srcs/array/ft_putnbr_fd.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 20:29:35 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 11:35:01 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + long nb; + + nb = (long)n; + if (nb < 0) + { + write(fd, "-", 1); + nb *= -1; + } + if (nb >= 10) + ft_putnbr_fd((nb / 10), fd); + ft_putchar_fd((nb % 10) + '0', fd); +} +/* +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + ft_putnbr_fd(atoi(av[1]), 1); + return (0); +}*/ diff --git a/libft/srcs/array/ft_putstr_fd.c b/libft/srcs/array/ft_putstr_fd.c new file mode 100644 index 0000000..e4f7848 --- /dev/null +++ b/libft/srcs/array/ft_putstr_fd.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 20:12:32 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 11:30:06 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + write(fd, s, ft_strlen(s)); +} +/* Note + * 1. if s is NULL, it will crash at ft_strlen + * 2. if s is not NULL but fd is -1, write will return -1 and set errno to EBADF + */ +/* +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + ft_putstr_fd(av[1], 1); + return (0); +}*/ diff --git a/libft/srcs/array/ft_split.c b/libft/srcs/array/ft_split.c new file mode 100644 index 0000000..1b43879 --- /dev/null +++ b/libft/srcs/array/ft_split.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 15:13:36 by tsuchen #+# #+# */ +/* Updated: 2024/07/11 13:58:51 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_strchop(char const *s, int start, int end) +{ + char *str; + + str = (char *)malloc((end - start) * sizeof(char)); + if (!str) + return (0); + ft_strlcpy(str, s + start + 1, end - start); + return (str); +} + +static int ft_assign_arr(char **arr, char const *s, char c, int s_len) +{ + int i; + int j; + int anchor; + + i = -1; + j = 0; + anchor = i; + while (++i <= s_len) + { + if (s[i] == c || !s[i]) + { + if (i - anchor > 1) + { + arr[j] = ft_strchop(s, anchor, i); + if (!arr[j++]) + { + ft_free_all(arr); + return (0); + } + } + anchor = i; + } + } + arr[j] = 0; + return (1); +} + +static int ft_count_subset(char const *s, char c, int s_len) +{ + int i; + int cnt; + int anchor; + + i = -1; + cnt = 0; + anchor = i; + while (++i <= s_len) + { + if (s[i] == c || !s[i]) + { + if (i - anchor > 1) + cnt++; + anchor = i; + } + } + return (cnt); +} + +char **ft_split(char const *s, char c) +{ + char **arr; + int s_len; + int n_subset; + + s_len = (int)ft_strlen(s); + n_subset = ft_count_subset(s, c, s_len); + arr = (char **)malloc((n_subset + 1) * sizeof(char *)); + if (!arr) + return (0); + if (ft_assign_arr(arr, s, c, s_len) == 0) + return (0); + return (arr); +} +/* Note + * 1. if s is NULL, it will crash + * 2. if any of malloc fail, it will return NULL + * 3. if s is empty, it will return an empty arr with a NULL + */ +/* +#include +#include + +int main(int ac, char *av[]) +{ + if (ac != 3 || (strlen(av[2]) != 1)) + return (0); + char **arr; + int i; + + i = 0; + arr = ft_split(av[1], *(av[2])); + if (!arr) + return (0); + while (arr[i]) + { + printf("%s\n", arr[i]); + i++; + } + ft_free_all(arr, i); + return (0); +}*/ diff --git a/libft/srcs/array/ft_split2.c b/libft/srcs/array/ft_split2.c new file mode 100644 index 0000000..30da228 --- /dev/null +++ b/libft/srcs/array/ft_split2.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: okoca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 15:13:36 by tsuchen #+# #+# */ +/* Updated: 2024/07/18 13:25:06 by okoca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_check_in_set(char c, char *set) +{ + int i; + + i = 0; + while (set && set[i]) + { + if (c == set[i]) + return (1); + i++; + } + return (0); +} + +static char *ft_strchop(char const *s, int start, int end) +{ + char *str; + + str = (char *)malloc((end - start) * sizeof(char)); + if (!str) + return (0); + ft_strlcpy(str, s + start + 1, end - start); + return (str); +} + +static int ft_assign_arr(char **arr, char const *s, char *set, int s_len) +{ + int i; + int j; + int anchor; + + i = -1; + j = 0; + anchor = i; + while (++i <= s_len) + { + if (ft_check_in_set(s[i], set) || !s[i]) + { + if (i - anchor > 1) + { + arr[j] = ft_strchop(s, anchor, i); + if (!arr[j++]) + { + ft_free_all(arr); + return (0); + } + } + anchor = i; + } + } + arr[j] = 0; + return (1); +} + +static int ft_count_subset(char const *s, char *set, int s_len) +{ + int i; + int cnt; + int anchor; + + i = -1; + cnt = 0; + anchor = i; + while (++i <= s_len) + { + if (ft_check_in_set(s[i], set) || !s[i]) + { + if (i - anchor > 1) + cnt++; + anchor = i; + } + } + return (cnt); +} + +char **ft_split2(char const *s, char *set) +{ + char **arr; + int s_len; + int n_subset; + + s_len = (int)ft_strlen(s); + n_subset = ft_count_subset(s, set, s_len); + arr = (char **)malloc((n_subset + 1) * sizeof(char *)); + if (!arr) + return (0); + if (ft_assign_arr(arr, s, set, s_len) == 0) + return (0); + return (arr); +} diff --git a/libft/srcs/array/ft_split_quote.c b/libft/srcs/array/ft_split_quote.c new file mode 100644 index 0000000..4b91b19 --- /dev/null +++ b/libft/srcs/array/ft_split_quote.c @@ -0,0 +1,157 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_quote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 15:13:36 by tsuchen #+# #+# */ +/* Updated: 2024/06/28 13:48:46 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_strchop(char **arr, char *start, char *end, int index) +{ + char *str; + int i; + + i = 0; + str = (char *)malloc((end - start + 1) * sizeof(char)); + if (!str) + { + while (arr[i]) + free(arr[i++]); + free(arr); + return (0); + } + ft_strlcpy(str, start, end - start + 1); + arr[index] = str; + return (1); +} + +static char *ft_find_next_quote(char **arr, char *s, int *index) +{ + char *next_quote; + + next_quote = ft_strchr(s + 1, '\''); + if (next_quote - s > 1) + { + if (!ft_strchop(arr, s + 1, next_quote, *index)) + return (NULL); + *index += 1; + } + return (next_quote); +} + +static void ft_assign_arr(char **arr, char *s, char c) +{ + int j; + char *anchor; + + j = 0; + anchor = s; + while (*s) + { + if (*s == c || *s == '\'') + { + if (s - anchor > 0) + if (!ft_strchop(arr, anchor, s, j++)) + return ; + if (*s == '\'') + { + s = ft_find_next_quote(arr, s, &j); + if (!s) + return ; + } + anchor = s + 1; + } + s++; + } + if (s - anchor > 0) + if (!ft_strchop(arr, anchor, s, j++)) + return ; +} + +static int ft_count_subset(char *s, char c) +{ + int cnt; + char *anchor; + + cnt = 0; + anchor = s; + while (*s) + { + if (*s == c || *s == '\'') + { + if (s - anchor > 0) + cnt++; + if (*s == '\'') + { + if (ft_strchr(s + 1, '\'') - s > 1) + cnt++; + s = ft_strchr(s + 1, '\''); + } + anchor = s + 1; + } + s++; + } + if (s - anchor > 0) + cnt++; + return (cnt); +} + +char **ft_split_quote(char const *s, char c) +{ + char **arr; + int n_subset; + + n_subset = ft_count_subset((char *)s, c); + arr = (char **)malloc((n_subset + 1) * sizeof(char *)); + if (!arr) + return (NULL); + ft_assign_arr(arr, (char *)s, c); + if (!arr) + return (NULL); + arr[n_subset] = NULL; + return (arr); +} +/* Note + * 1. if s is NULL, it will crash + * 2. if any of malloc fail, it will return NULL + * 3. if s is empty, it will return an empty arr with a NULL + */ +/* +#include +#include + +void ft_free_all(char **arr) +{ + int i; + + i = 0; + while (arr[i]) + free(arr[i++]); + free(arr); +} + +int main(int ac, char *av[]) +{ + if (ac != 3 || (strlen(av[2]) != 1)) + return (0); + char **arr; + int i; + + i = 0; + arr = ft_split_quote(av[1], *(av[2])); + if (!arr) + return (0); + while (arr[i]) + { + printf("%s\n", arr[i]); + i++; + } + ft_free_all(arr); + return (0); +}*/ diff --git a/libft/srcs/array/ft_striteri.c b/libft/srcs/array/ft_striteri.c new file mode 100644 index 0000000..1284b77 --- /dev/null +++ b/libft/srcs/array/ft_striteri.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 19:10:55 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 11:24:02 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + i = 0; + while (s[i]) + { + f(i, (s + i)); + i++; + } +} +/* Note + * 1. if s or f is NULL, it will crash + */ +/* +#include +#include +#include +void ft_print_result(char const *s) +{ + int len; + + len = 0; + while (s[len]) + len++; + write(1, s, len); +} + +void iter(unsigned int i, char *c) +{ + static int indexArray[11] = {0}; + + if (i > 10 || indexArray[i] == 1) + write(1, "wrong index\n", 12); + else + indexArray[i] = 1; + if (*c >= 'a' && *c <= 'z') + *c = *c - 32; + else if (*c >= 'A' && *c <= 'Z') + *c = *c + 32; +} + +int main(int argc, const char *argv[]) +{ + char *str; + + alarm(5); + str = (char *)malloc(sizeof(*str) * 12); + if (argc == 1 || !str) + return (0); + else if (atoi(argv[1]) == 1) + { + strcpy(str, "LoReM iPsUm"); + ft_striteri(str, &iter); + ft_print_result(str); + } + free(str); + return (0); +} +void zero_even(unsigned int idx, char *s) +{ + if ((idx % 2) == 0) + s[idx] = '0'; +} + +void mute_odd(unsigned int idx, char *s) +{ + if ((idx % 2) == 1) + s[idx] = ' '; +} + +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + char *str1; + char *str2; + + str1 = strdup(av[1]); + str2 = strdup(av[1]); + ft_striteri(str1, &zero_even); + ft_striteri(str2, &mute_odd); + printf("Strmap funtion_1 is: %s\n", str1); + printf("Strmap funtion_2 is: %s\n", str2); + free(str1); + free(str2); + return (0); +}*/ diff --git a/libft/srcs/array/ft_strjoin.c b/libft/srcs/array/ft_strjoin.c new file mode 100644 index 0000000..caa1afa --- /dev/null +++ b/libft/srcs/array/ft_strjoin.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 14:30:30 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 09:34:28 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + char *tmp; + + str = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); + if (!str) + return (0); + tmp = str; + while (*s1) + *str++ = *s1++; + while (*s2) + *str++ = *s2++; + *str = '\0'; + return (tmp); +} +/* Note + * 1. if s1 || s2 is NULL, it will crash + * 2. if malloc failed, it will return NULL + */ +/* //Index Counter version +char *ft_strjoin(char const *s1, char const *s2) +{ + int i; + int j; + char *str; + + str = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); + if (!str) + return (0); + i = 0; + while (s1[i]) + { + str[i] = s1[i]; + i++; + } + j = 0; + while (s2[j]) + str[i++] = s2[j++]; + str[i] = '\0'; + return (str); +}*/ +/* +#include + +int main(int ac, char *av[]) +{ + if (ac != 3) + return (0); + char *str_join; + + str_join = ft_strjoin(av[1], av[2]); + printf("Joined str is: %s\n", str_join); + free(str_join); + return (0); +}*/ diff --git a/libft/srcs/array/ft_strmapi.c b/libft/srcs/array/ft_strmapi.c new file mode 100644 index 0000000..8877154 --- /dev/null +++ b/libft/srcs/array/ft_strmapi.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 19:10:55 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 11:19:43 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *str; + size_t s_len; + unsigned int i; + + s_len = ft_strlen(s); + str = (char *)malloc((s_len + 1) * sizeof(char)); + if (!str) + return (0); + i = 0; + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = '\0'; + return (str); +} +/* Note + * 1. if s or f is NULL, it will crash + * 2. if s is empty, it will return a new empty string + * 3. if malloc failed, return NULL + */ +/* +#include + +char zero_even(unsigned int idx, char c) +{ + if ((idx % 2) == 0) + return ('0'); + return (c); +} + +char mute_odd(unsigned int idx, char c) +{ + if ((idx % 2) == 1) + return (' '); + return (c); +} + +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + char *str_map1; + char *str_map2; + + str_map1 = ft_strmapi(av[1], &zero_even); + str_map2 = ft_strmapi(av[1], &mute_odd); + printf("Strmap funtion_1 is: %s\n", str_map1); + printf("Strmap funtion_2 is: %s\n", str_map2); + free(str_map1); + free(str_map2); + return (0); +}*/ diff --git a/libft/srcs/array/ft_strtrim.c b/libft/srcs/array/ft_strtrim.c new file mode 100644 index 0000000..3f13559 --- /dev/null +++ b/libft/srcs/array/ft_strtrim.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 14:49:23 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 10:11:29 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isset(char c, char const *set) +{ + int i; + + i = 0; + while (set[i]) + { + if (c == set[i]) + return (1); + i++; + } + return (0); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + char *output; + int start; + int end; + int len; + + start = 0; + end = ft_strlen(s1) - 1; + while (ft_isset(s1[start], set) == 1) + start++; + while (end > start && ft_isset(s1[end], set) == 1) + end--; + len = end - start + 1; + output = (char *)malloc((len + 1) * sizeof(char)); + if (!output) + return (NULL); + ft_strlcpy(output, s1 + start, (len + 1)); + return (output); +} +/* Note + * 1. if s1 || set is NULL, it will crash + * 2. if every char in s1 is part of set, it returns a free-able empty string "" + * 3. if malloc fail, it returns NULL + */ +/* +#include +#include + +void ft_print_result(char const *s) +{ + int len; + + len = 0; + while (s[len]) + len++; + write(1, s, len); +} + +int main(void) +{ + char s1[] = "lorem ipsum dolor sit amet"; + + char *strtrim; + + strtrim = ft_strtrim("tetetet0tetetete", "te"); + printf("%s\n", s1); + printf("%s\n", strtrim); + return (0); +}*/ +/* +int main(int ac, char *av[]) +{ + if (ac != 3) + return (0); + char *str_trim; + + str_trim = ft_strtrim(av[1], av[2]); + if (!str_trim) + return (0); + printf("Trimmed str is: %s\n", str_trim); + free(str_trim); + return (0); +}*/ diff --git a/libft/srcs/array/ft_substr.c b/libft/srcs/array/ft_substr.c new file mode 100644 index 0000000..e0f9c8f --- /dev/null +++ b/libft/srcs/array/ft_substr.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 12:02:22 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 09:25:06 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static unsigned int ft_min(unsigned int a, unsigned int b) +{ + if (a > b) + return (b); + return (a); +} + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *sub; + size_t i; + size_t size; + + i = 0; + if (start > ft_strlen(s)) + size = 0; + else + size = ft_min(ft_strlen(s) - start, len); + sub = (char *)malloc((size + 1) * sizeof(char)); + if (!sub) + return (0); + while (i < size && s[i + start]) + { + sub[i] = s[start + i]; + i++; + } + sub[i] = '\0'; + return (sub); +} +/* Note + * 1. if len is 0, it will return an empty str "" that is able to free + * 2. if s is NULL, it will crash since input is not protected + * 3. if malloc failed, it will return NULL + */ +/* +#include +#include + +int main(void) +{ + char *s; + + s = ft_substr("tripouille", 100, 1); + printf("substr :%s\n", s); + printf("strcmp :%d\n", strcmp(s, "")); + free(s); + return (0); +}*/ +/* +int main(int ac, char *av[]) +{ + char *substr; + + if (ac != 4) + return (0); + substr = ft_substr(av[1], atoi(av[2]), atoi(av[3])); + printf("substr is: %s\n", substr); + free(substr); + return (0); +}*/ diff --git a/libft/srcs/ft_printf/ft_dgt_cnt_abs_bonus.c b/libft/srcs/ft_printf/ft_dgt_cnt_abs_bonus.c new file mode 100644 index 0000000..6ba384a --- /dev/null +++ b/libft/srcs/ft_printf/ft_dgt_cnt_abs_bonus.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dgt_cnt_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 19:09:52 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 19:14:46 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_dgt_cnt_abs(long nbr, char *base) +{ + int count; + long base_len; + + count = 0; + base_len = (long)ft_strlen(base); + if (nbr < 0) + nbr *= -1; + if (nbr >= base_len) + count += ft_dgt_cnt_abs((nbr / base_len), base); + count += 1; + return (count); +} diff --git a/libft/srcs/ft_printf/ft_gen_wid_pre_bonus.c b/libft/srcs/ft_printf/ft_gen_wid_pre_bonus.c new file mode 100644 index 0000000..ab841ea --- /dev/null +++ b/libft/srcs/ft_printf/ft_gen_wid_pre_bonus.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_gen_wid_pre_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 18:38:12 by tsuchen #+# #+# */ +/* Updated: 2024/07/11 14:00:47 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_with_pre(char *s) +{ + while (*s) + { + if (*s == '.') + return (1); + s++; + } + return (0); +} + +static void ft_assign_wid_pre(char *cpy, char **arr, char **wid_pre) +{ + if (ft_arr_size(arr) == 0) + { + if (ft_with_pre(cpy) == 1) + wid_pre[1] = ft_strdup("0"); + return ; + } + else if (ft_arr_size(arr) == 1) + { + if (ft_with_pre(cpy) == 0) + wid_pre[0] = ft_strdup(arr[0]); + else if (*cpy == '.') + wid_pre[1] = ft_strdup(arr[0]); + else + { + wid_pre[0] = ft_strdup(arr[0]); + wid_pre[1] = ft_strdup("0"); + } + } + else + { + wid_pre[0] = ft_strdup(arr[0]); + wid_pre[1] = ft_strdup(arr[1]); + } +} + +void ft_gen_wid_pre(const char *fmt, size_t len, char **wid_pre) +{ + char *cpy; + char **arr; + + cpy = (char *)malloc((len + 1) * sizeof(char)); + if (!cpy) + return ; + ft_strlcpy(cpy, fmt, (len + 1)); + arr = ft_split(cpy, '.'); + if (!arr) + { + free(cpy); + return ; + } + ft_assign_wid_pre(cpy, arr, wid_pre); + ft_free_all(arr); + free(cpy); +} +/* Note + * If there is a '.' flag, the precision will always be not null + */ diff --git a/libft/srcs/ft_printf/ft_have_alt_bonus.c b/libft/srcs/ft_printf/ft_have_alt_bonus.c new file mode 100644 index 0000000..03811c6 --- /dev/null +++ b/libft/srcs/ft_printf/ft_have_alt_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_have_alt_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 10:20:54 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 10:43:59 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_have_alt(char *str) +{ + while (*str) + { + if (*str == '#') + return (1); + str++; + } + return (0); +} diff --git a/libft/srcs/ft_printf/ft_have_plus_bonus.c b/libft/srcs/ft_printf/ft_have_plus_bonus.c new file mode 100644 index 0000000..d73e49a --- /dev/null +++ b/libft/srcs/ft_printf/ft_have_plus_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_have_plus_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 12:17:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 12:18:07 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_have_plus(char *str) +{ + while (*str) + { + if (*str == '+') + return (1); + str++; + } + return (0); +} diff --git a/libft/srcs/ft_printf/ft_have_space_bonus.c b/libft/srcs/ft_printf/ft_have_space_bonus.c new file mode 100644 index 0000000..75d46b0 --- /dev/null +++ b/libft/srcs/ft_printf/ft_have_space_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_have_space_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 12:16:08 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 12:17:13 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_have_space(char *str) +{ + while (*str) + { + if (*str == ' ') + return (1); + str++; + } + return (0); +} diff --git a/libft/srcs/ft_printf/ft_have_zero_bonus.c b/libft/srcs/ft_printf/ft_have_zero_bonus.c new file mode 100644 index 0000000..731f716 --- /dev/null +++ b/libft/srcs/ft_printf/ft_have_zero_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_have_zero_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 18:39:04 by tsuchen #+# #+# */ +/* Updated: 2024/05/27 18:40:38 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_have_zero(char *str) +{ + while (*str) + { + if (*str == '0') + return (1); + str++; + } + return (0); +} diff --git a/libft/srcs/ft_printf/ft_is_left_bonus.c b/libft/srcs/ft_printf/ft_is_left_bonus.c new file mode 100644 index 0000000..97f4849 --- /dev/null +++ b/libft/srcs/ft_printf/ft_is_left_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isleft.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 13:42:32 by tsuchen #+# #+# */ +/* Updated: 2024/05/27 13:45:37 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_is_left(char *str) +{ + while (*str) + { + if (*str == '-') + return (1); + str++; + } + return (0); +} diff --git a/libft/srcs/ft_printf/ft_is_spec.c b/libft/srcs/ft_printf/ft_is_spec.c new file mode 100644 index 0000000..4051900 --- /dev/null +++ b/libft/srcs/ft_printf/ft_is_spec.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_is_spec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 15:49:11 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 15:59:06 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_is_spec(char c) +{ + if (c == '%' || c == 'd' || c == 'i' || c == 'u' || c == 'x' + || c == 'X' || c == 'c' || c == 's' || c == 'p') + return (1); + return (0); +} diff --git a/libft/srcs/ft_printf/ft_print_alt_bonus.c b/libft/srcs/ft_printf/ft_print_alt_bonus.c new file mode 100644 index 0000000..a6cf257 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_alt_bonus.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_alt_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 19:55:10 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 19:57:52 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_alt(char *base) +{ + int count; + + count = 0; + if (ft_strncmp(base, HEX_TAB, 16) == 0) + count += write(STDOUT_FILENO, "0x", 2); + else if (ft_strncmp(base, HEX_TAB_U, 16) == 0) + count += write(STDOUT_FILENO, "0X", 2); + else + count += 0; + return (count); +} diff --git a/libft/srcs/ft_printf/ft_print_char.c b/libft/srcs/ft_printf/ft_print_char.c new file mode 100644 index 0000000..9f8e9f4 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_char.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/17 18:11:14 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 15:59:20 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_char(char c) +{ + return (write(STDOUT_FILENO, &c, 1)); +} diff --git a/libft/srcs/ft_printf/ft_print_char_f_bonus.c b/libft/srcs/ft_printf/ft_print_char_f_bonus.c new file mode 100644 index 0000000..121502f --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_char_f_bonus.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_char_f_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 11:37:36 by tsuchen #+# #+# */ +/* Updated: 2024/05/29 16:12:22 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_char_f(char c, char *flags, char **wid_pre) +{ + int count; + int wid; + + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + count = 0; + if (ft_is_left(flags) == 1) + count += ft_print_char(c); + while (wid-- > 1) + { + if (c == '%' && ft_have_zero(flags) == 1) + count += write(STDOUT_FILENO, "0", 1); + else + count += write(STDOUT_FILENO, " ", 1); + } + if (ft_is_left(flags) == 0) + count += ft_print_char(c); + return (count); +} +/* Note + * For specifier "c", the flag can only be '-' + * There is no precision allowed in "c" + */ diff --git a/libft/srcs/ft_printf/ft_print_digit.c b/libft/srcs/ft_printf/ft_print_digit.c new file mode 100644 index 0000000..24d42a0 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_digit.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_digit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 12:43:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 15:59:38 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_digit(long nbr, char *base) +{ + int count; + long base_len; + + count = 0; + base_len = (long)ft_strlen(base); + if (nbr < 0) + { + count += write(STDOUT_FILENO, "-", 1); + nbr *= (-1); + } + if (nbr >= base_len) + count += ft_print_digit((nbr / base_len), base); + count += ft_print_char(base[(nbr % base_len)]); + return (count); +} diff --git a/libft/srcs/ft_printf/ft_print_mem.c b/libft/srcs/ft_printf/ft_print_mem.c new file mode 100644 index 0000000..3b01c05 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_mem.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_mem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 11:51:13 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 15:56:15 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_mem(void *addr) +{ + int count; + int i; + unsigned long ad; + char temp[(AD_SIZE + 1)]; + + i = 0; + if (!addr) + return (write(STDOUT_FILENO, "(nil)", 5)); + ad = (unsigned long)addr; + count = 0; + while (ad > 0) + { + temp[i++] = HEX_TAB[ad % HEX_SIZE]; + ad /= HEX_SIZE; + } + temp[i] = '\0'; + count += write(STDOUT_FILENO, "0x", 2); + while (--i >= 0) + count += write(STDOUT_FILENO, &temp[i], 1); + return (count); +} +/* Note + * 1. For some system, the output of NULL ptr is 0x0. need to change the + * pretection as below: + //if (!addr) + // temp[i++] = '0'; + */ +/* +#include +#include +int main(void) +{ + //int i = 3; + int *i; + + i = NULL; + ft_print_mem(i); + printf("\n%p\n", i); + return (0); +}*/ diff --git a/libft/srcs/ft_printf/ft_print_mem_f_bonus.c b/libft/srcs/ft_printf/ft_print_mem_f_bonus.c new file mode 100644 index 0000000..c475e88 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_mem_f_bonus.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_mem_f_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 15:31:48 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 13:13:45 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_mem_f(void *addr, char *flags, char **wid_pre) +{ + int count; + int wid; + int prt_len; + unsigned long ad; + + ad = (unsigned long)addr; + prt_len = 2; + while (ad > 0) + { + prt_len += 1; + ad /= HEX_SIZE; + } + if (!addr) + prt_len = 5; + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + count = 0; + if (ft_is_left(flags) == 1) + count += ft_print_mem(addr); + while (wid-- > prt_len) + count += write(STDOUT_FILENO, " ", 1); + if (ft_is_left(flags) == 0) + count += ft_print_mem(addr); + return (count); +} +/* Note + * 1. Only '-', '0' are acceptable flags. + * 2. There is no precision allowed in "p" -> use origin function + * 3. initial prt_len is 2 because must start with "0x" + */ diff --git a/libft/srcs/ft_printf/ft_print_sign_bonus.c b/libft/srcs/ft_printf/ft_print_sign_bonus.c new file mode 100644 index 0000000..90b8c29 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_sign_bonus.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_sign_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 18:55:06 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 19:48:05 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_sign(long *nbr, char *flags) +{ + int count; + + count = 0; + if (*nbr < 0) + { + count += write(STDOUT_FILENO, "-", 1); + *nbr *= -1; + } + else if (ft_have_space(flags) == 1) + count += write(STDOUT_FILENO, " ", 1); + else + count += write(STDOUT_FILENO, "+", 1); + return (count); +} diff --git a/libft/srcs/ft_printf/ft_print_sint_f_bonus.c b/libft/srcs/ft_printf/ft_print_sint_f_bonus.c new file mode 100644 index 0000000..b805a5a --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_sint_f_bonus.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_sint_f_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 12:43:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 19:58:53 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_print_pre(long *nbr, char *base, int *prt_len, char *flags) +{ + int count; + int dgt_cnt; + + dgt_cnt = ft_dgt_cnt_abs(*nbr, base); + count = 0; + if (*nbr < 0 || ft_have_space(flags) == 1 || ft_have_plus(flags) == 1) + { + count += ft_print_sign(nbr, flags); + *prt_len -= 1; + } + while (*prt_len > dgt_cnt++) + count += write(STDOUT_FILENO, "0", 1); + if (*prt_len) + count += ft_print_digit(*nbr, base); + return (count); +} + +static int ft_print_wid1(long *nbr, char **wid_pre, char *flags, int *prt_len) +{ + int count; + int wid; + + count = 0; + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + if ((*nbr < 0 || ft_have_space(flags) == 1 || ft_have_plus(flags) == 1) + && ft_have_zero(flags) == 1 && !wid_pre[1]) + { + wid -= 1; + count += ft_print_sign(nbr, flags); + ft_rm_plus_space(flags); + *prt_len -= 1; + } + while (wid > *prt_len) + { + if (ft_have_zero(flags) == 1 && !wid_pre[1]) + count += write(STDOUT_FILENO, "0", 1); + else + count += write(STDOUT_FILENO, " ", 1); + wid--; + } + return (count); +} + +static int ft_print_wid2(long nbr, char **wid_pre, char *flags, int *prt_len) +{ + int count; + int wid; + + count = 0; + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + if (nbr < 0 || ft_have_space(flags) == 1 || ft_have_plus(flags) == 1) + wid -= 1; + while (wid > *prt_len) + { + if (ft_have_zero(flags) == 1 && !wid_pre[1]) + count += write(STDOUT_FILENO, "0", 1); + else + count += write(STDOUT_FILENO, " ", 1); + wid--; + } + return (count); +} + +int ft_print_sint_f(long nbr, char *base, char *flags, char **wid_pre) +{ + int count; + int prt_len; + long nb; + + nb = nbr; + count = 0; + prt_len = ft_dgt_cnt_abs(nbr, base); + if (wid_pre[1] && ft_atoi(wid_pre[1]) > prt_len) + prt_len = ft_atoi(wid_pre[1]); + if (wid_pre[1] && ft_atoi(wid_pre[1]) == 0 && nbr == 0) + prt_len -= 1; + if (nb < 0 || ft_have_space(flags) == 1 || ft_have_plus(flags) == 1) + prt_len += 1; + if (ft_is_left(flags) == 1) + { + count += ft_print_pre(&nb, base, &prt_len, flags); + count += ft_print_wid2(nbr, wid_pre, flags, &prt_len); + } + else + { + count += ft_print_wid1(&nb, wid_pre, flags, &prt_len); + count += ft_print_pre(&nb, base, &prt_len, flags); + } + return (count); +} +/* Note + * 1. possible combination of flags for i d: '-' '0' '+' ' ' + * 2. if precision is not null, the '0' flag is ignored + */ diff --git a/libft/srcs/ft_printf/ft_print_spec.c b/libft/srcs/ft_printf/ft_print_spec.c new file mode 100644 index 0000000..a4cff21 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_spec.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_spec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 11:26:11 by tsuchen #+# #+# */ +/* Updated: 2024/05/29 17:13:25 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_spec(char c, va_list *ap) +{ + int count; + + count = 0; + if (c == 'c') + count += ft_print_char(va_arg(*ap, int)); + else if (c == 's') + count += ft_print_str(va_arg(*ap, char *)); + else if (c == 'p') + count += ft_print_mem(va_arg(*ap, void *)); + else if (c == 'd') + count += ft_print_digit(va_arg(*ap, int), DEC_TAB); + else if (c == 'i') + count += ft_print_digit(va_arg(*ap, int), DEC_TAB); + else if (c == 'u') + count += ft_print_digit(va_arg(*ap, unsigned int), DEC_TAB); + else if (c == 'x') + count += ft_print_digit(va_arg(*ap, unsigned int), HEX_TAB); + else if (c == 'X') + count += ft_print_digit(va_arg(*ap, unsigned int), HEX_TAB_U); + else if (c == '%') + count += ft_print_char(c); + else + return (count); + return (count); +} diff --git a/libft/srcs/ft_printf/ft_print_spec_f_bonus.c b/libft/srcs/ft_printf/ft_print_spec_f_bonus.c new file mode 100644 index 0000000..3d145a2 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_spec_f_bonus.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_spec_f_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 11:26:11 by tsuchen #+# #+# */ +/* Updated: 2024/05/29 16:12:10 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_spec_f(char c, va_list *ap, char *flags, char **wid_pre) +{ + int count; + + count = 0; + if (c == 'c') + count += ft_print_char_f(va_arg(*ap, int), flags, wid_pre); + else if (c == 's') + count += ft_print_str_f(va_arg(*ap, char *), flags, wid_pre); + else if (c == 'p') + count += ft_print_mem_f(va_arg(*ap, void *), flags, wid_pre); + else if (c == 'd' || c == 'i') + count += ft_print_sint_f(va_arg(*ap, int), DEC_TAB, flags, wid_pre); + else if (c == 'u') + count += ft_print_uint_f(va_arg(*ap, unsigned int), + DEC_TAB, flags, wid_pre); + else if (c == 'x') + count += ft_print_uint_f(va_arg(*ap, unsigned int), + HEX_TAB, flags, wid_pre); + else if (c == 'X') + count += ft_print_uint_f(va_arg(*ap, unsigned int), + HEX_TAB_U, flags, wid_pre); + else if (c == '%') + count += ft_print_char(c); + else + return (count); + return (count); +} diff --git a/libft/srcs/ft_printf/ft_print_str.c b/libft/srcs/ft_printf/ft_print_str.c new file mode 100644 index 0000000..d5c4a9c --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_str.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 11:00:15 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 13:53:41 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_print_str(char *s) +{ + int count; + char *tmp; + + if (!s) + tmp = "(null)"; + else + tmp = s; + count = 0; + count += write(STDOUT_FILENO, tmp, ft_strlen(tmp)); + return (count); +} diff --git a/libft/srcs/ft_printf/ft_print_str_f_bonus.c b/libft/srcs/ft_printf/ft_print_str_f_bonus.c new file mode 100644 index 0000000..ac3cd34 --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_str_f_bonus.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_str_f_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 13:50:23 by tsuchen #+# #+# */ +/* Updated: 2024/05/29 17:10:39 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_min(int a, int b) +{ + if (a > b) + return (b); + return (a); +} + +int ft_print_str_f(char *s, char *flags, char **wid_pre) +{ + int count; + int prt_len; + int wid; + char *tmp; + + tmp = s; + if (!s) + tmp = "(null)"; + if (!s && wid_pre[1] && ft_atoi(wid_pre[1]) < 6) + tmp = ""; + count = 0; + prt_len = ft_strlen(tmp); + if (wid_pre[1]) + prt_len = ft_min(ft_strlen(tmp), ft_atoi(wid_pre[1])); + if (ft_is_left(flags) == 1) + count += write(STDOUT_FILENO, tmp, prt_len); + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + while (wid-- > prt_len) + count += write(STDOUT_FILENO, " ", 1); + if (ft_is_left(flags) == 0) + count += write(STDOUT_FILENO, tmp, prt_len); + return (count); +} +/* Note + * For string, the only flag acceptable is '-' + */ diff --git a/libft/srcs/ft_printf/ft_print_uint_f_bonus.c b/libft/srcs/ft_printf/ft_print_uint_f_bonus.c new file mode 100644 index 0000000..ea8ac7b --- /dev/null +++ b/libft/srcs/ft_printf/ft_print_uint_f_bonus.c @@ -0,0 +1,144 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_uint_f_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 12:43:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 21:32:28 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_print_pre(long nbr, char *base, int *prt_len, char *flags) +{ + int count; + int dgt_cnt; + + dgt_cnt = ft_dgt_cnt_abs(nbr, base); + count = 0; + if (ft_have_alt(flags) == 1 && nbr != 0) + { + count += ft_print_alt(base); + *prt_len -= 2; + } + while (*prt_len > dgt_cnt++) + count += write(STDOUT_FILENO, "0", 1); + if (*prt_len) + count += ft_print_digit(nbr, base); + return (count); +} + +static int ft_print_wid(char **wid_pre, char *flags, int *prt_len, long nbr) +{ + int count; + int wid; + + count = 0; + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + if ((ft_have_alt(flags) == 1 && nbr != 0) + && ft_have_zero(flags) == 1 && !wid_pre[1]) + { + wid -= 2; + count += ft_print_alt(HEX_TAB); + ft_rm_alt(flags); + *prt_len -= 2; + } + while (wid > *prt_len) + { + if (ft_have_zero(flags) == 1 && !wid_pre[1]) + count += write(STDOUT_FILENO, "0", 1); + else + count += write(STDOUT_FILENO, " ", 1); + wid--; + } + return (count); +} + +static int ft_print_wid1(char **wid_pre, char *flags, int *prt_len, long nbr) +{ + int count; + int wid; + + count = 0; + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + if ((ft_have_alt(flags) == 1 && nbr != 0) + && ft_have_zero(flags) == 1 && !wid_pre[1]) + { + wid -= 2; + count += ft_print_alt(HEX_TAB_U); + ft_rm_alt(flags); + *prt_len -= 2; + } + while (wid > *prt_len) + { + if (ft_have_zero(flags) == 1 && !wid_pre[1]) + count += write(STDOUT_FILENO, "0", 1); + else + count += write(STDOUT_FILENO, " ", 1); + wid--; + } + return (count); +} + +static int ft_print_wid2(char **wid_pre, char *flags, int *prt_len, long nbr) +{ + int count; + int wid; + + count = 0; + wid = 0; + if (wid_pre[0]) + wid = ft_atoi(wid_pre[0]); + if (ft_have_alt(flags) == 1 && nbr != 0) + wid -= 2; + while (wid > *prt_len) + { + if (ft_have_zero(flags) == 1 && !wid_pre[1]) + count += write(STDOUT_FILENO, "0", 1); + else + count += write(STDOUT_FILENO, " ", 1); + wid--; + } + return (count); +} + +int ft_print_uint_f(long nbr, char *base, char *flags, char **wid_pre) +{ + int count; + int prt_len; + + count = 0; + prt_len = ft_dgt_cnt_abs(nbr, base); + if (wid_pre[1] && ft_atoi(wid_pre[1]) > prt_len) + prt_len = ft_atoi(wid_pre[1]); + if (wid_pre[1] && ft_atoi(wid_pre[1]) == 0 && nbr == 0) + prt_len -= 1; + if (ft_have_alt(flags) == 1 && nbr != 0) + prt_len += 2; + if (ft_is_left(flags) == 1) + { + count += ft_print_pre(nbr, base, &prt_len, flags); + count += ft_print_wid2(wid_pre, flags, &prt_len, nbr); + } + else + { + if (ft_strncmp(base, HEX_TAB, 16) == 0) + count += ft_print_wid(wid_pre, flags, &prt_len, nbr); + else + count += ft_print_wid1(wid_pre, flags, &prt_len, nbr); + count += ft_print_pre(nbr, base, &prt_len, flags); + } + return (count); +} +/* Note + * 1. possible combination of flags for u: '-' '0' + * 2. possible combination of flags for x X: '-' '0' '#' + * 3. if precision is not null, the '0' flag is ignored + */ diff --git a/libft/srcs/ft_printf/ft_printf.c b/libft/srcs/ft_printf/ft_printf.c new file mode 100644 index 0000000..4a0e902 --- /dev/null +++ b/libft/srcs/ft_printf/ft_printf.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/17 13:59:04 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 18:44:12 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_printf(const char *fmt, ...) +{ + va_list ap; + int count; + + if (!fmt) + return (-1); + va_start(ap, fmt); + count = 0; + while (*fmt) + { + if (*fmt == '%') + { + if (ft_is_spec(*(fmt + 1)) == 1) + count += ft_print_spec(*(++fmt), &ap); + else + fmt = ft_procs_flag(++fmt, &ap, &count); + } + else + count += ft_print_char(*fmt); + if (!fmt) + return (va_end(ap), -1); + ++fmt; + } + va_end(ap); + return (count); +} +/* +#include +int main(void) +{ + int test; + int real; + char a = 'G'; + + printf(" ==> Real function <==\n"); + real = printf("No arg %%\\\"\a\b\e\f\n\r\t\voct\060hex\x5auni\u2B50"); + printf("\n ==> My function <==\n"); + test = ft_printf("No arg %%\\\"\a\b\e\f\n\r\t\voct\060hex\x5auni\u2B50"); + printf("\n"); + printf("test: %d, real: %d\n", test, real); + printf(" ==> Real function 2 <== \n"); + real = printf("Spec c1:%c, %%, c2:%c, s:%s, p:%p\n", a, 'H', "STR", &a); + printf(" ==> My function 2 <== \n"); + test = ft_printf("Spec c1:%c, %%, c2:%c, s:%s, p:%p\n", a, 'H', "STR", &a); + printf("test: %d, real: %d\n", test, real); + printf(" ==> Real function 3 <== \n"); + real = printf("d:%d, i:%i, u:%u, x:%x, X:%X\n", -42, -42, -42, -42, -42); + printf(" ==> My function 3 <== \n"); + test = ft_printf("d:%d, i:%i, u:%u, x:%x, X:%X\n", -42, -42, -42, -42, -42); + printf("test: %d, real: %d\n", test, real); + return (0); +}*/ diff --git a/libft/srcs/ft_printf/ft_procs_flag_bonus.c b/libft/srcs/ft_printf/ft_procs_flag_bonus.c new file mode 100644 index 0000000..fb4e1c0 --- /dev/null +++ b/libft/srcs/ft_printf/ft_procs_flag_bonus.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_procs_flag_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 16:13:22 by tsuchen #+# #+# */ +/* Updated: 2024/07/11 14:02:42 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_is_flag(char c) +{ + if (c == '-' || c == '+' || c == ' ' || c == '#' || c == '0') + return (1); + return (0); +} + +static void ft_free_allflag(int n, char **arr, char *flags) +{ + int i; + + i = 0; + while (i < n) + { + if (arr[i]) + free(arr[i]); + i++; + } + free(arr); + free(flags); +} + +const char *ft_procs_flag(const char *fmt, va_list *ap, int *count) +{ + const char *tmp; + char *flags; + char **wid_pre; + + tmp = fmt; + while (ft_is_flag(*tmp) == 1) + tmp++; + flags = (char *)malloc((tmp - fmt + 1) * sizeof(char)); + if (!flags) + return (NULL); + ft_strlcpy(flags, fmt, (tmp - fmt + 1)); + fmt = tmp; + while (*tmp && ft_is_spec(*tmp) == 0) + tmp++; + wid_pre = (char **)ft_calloc(3, sizeof(char *)); + if (!wid_pre) + return (free(flags), NULL); + if (!*tmp) + return (ft_free_allflag(3, wid_pre, flags), NULL); + ft_gen_wid_pre(fmt, tmp - fmt, wid_pre); + *count += ft_print_spec_f(*(tmp), ap, flags, wid_pre); + ft_free_allflag(3, wid_pre, flags); + return (tmp); +} +/* the flags '-' '#' '+' '0' don't have a certain order + * but must be before width numbers and precisions '.' + */ +// 1. move to non-flags and save all the flags into a string (flags) +// 2. move to specifier and generate wid and pre into a tab of int (wid_pre) +// 3. send "flags" and "wid_pre" together with ap to print and return cnt +/* + if (sign == -1) + *count += ft_print_spec_left(*(fmt + i), ap, wid, pre); + else if (sign == 0) + *count += ft_print_spec_zero(*(fmt + i), ap, wid, pre); + else + *count += ft_print_spec_emty(*(fmt + i), ap, wid, pre); +*/ diff --git a/libft/srcs/ft_printf/ft_rm_alt_bonus.c b/libft/srcs/ft_printf/ft_rm_alt_bonus.c new file mode 100644 index 0000000..74963a0 --- /dev/null +++ b/libft/srcs/ft_printf/ft_rm_alt_bonus.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rm_alt_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 21:19:06 by tsuchen #+# #+# */ +/* Updated: 2024/05/30 21:34:05 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_rm_alt(char *str) +{ + while (*str) + { + if (*str == '#') + *str = ' '; + str++; + } +} diff --git a/libft/srcs/ft_printf/ft_rm_plus_space_bonus.c b/libft/srcs/ft_printf/ft_rm_plus_space_bonus.c new file mode 100644 index 0000000..2df21be --- /dev/null +++ b/libft/srcs/ft_printf/ft_rm_plus_space_bonus.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rm_plus_space_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 19:35:00 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 19:38:03 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_rm_plus_space(char *str) +{ + while (*str) + { + if (*str == ' ' || *str == '+') + *str = '#'; + str++; + } +} diff --git a/libft/srcs/gnl/get_next_line_bonus.c b/libft/srcs/gnl/get_next_line_bonus.c new file mode 100644 index 0000000..cf6286f --- /dev/null +++ b/libft/srcs/gnl/get_next_line_bonus.c @@ -0,0 +1,168 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/29 17:53:03 by tsuchen #+# #+# */ +/* Updated: 2024/06/15 13:57:55 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line_bonus.h" + +char *get_next_line(int fd) +{ + static t_list_gnl *bgn_lst[MAX_FD]; + char *next_line; + + if (fd == -1 || read(fd, &next_line, 0) < 0 || BUFFER_SIZE <= 0) + { + if (fd >= 0 && bgn_lst[fd]) + { + ft_delone(bgn_lst[fd]); + bgn_lst[fd] = NULL; + } + return (NULL); + } + if (!bgn_lst[fd] || ft_have_nl_lst(bgn_lst[fd]) == 0) + ft_fetch_nl(fd, &bgn_lst[fd]); + next_line = ft_gen_nl(bgn_lst[fd]); + if (bgn_lst[fd]) + ft_update_list(&bgn_lst[fd]); + if (!next_line && bgn_lst[fd]) + { + ft_delone(bgn_lst[fd]); + bgn_lst[fd] = NULL; + } + return (next_line); +} + +int ft_have_nl_lst(t_list_gnl *bgn_lst) +{ + t_list_gnl *tmp; + char *tmp2; + + tmp = bgn_lst; + while (tmp) + { + tmp2 = tmp->str; + while (*tmp2) + { + if (*tmp2 == '\n') + return (1); + tmp2++; + } + tmp = tmp->next; + } + return (0); +} + +void ft_fetch_nl(int fd, t_list_gnl **bgn_lst) +{ + int nu_rd; + char *buff; + + while (!ft_have_nl_lst(*bgn_lst)) + { + buff = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char)); + if (!buff) + return ; + nu_rd = read(fd, buff, BUFFER_SIZE); + if (!nu_rd || nu_rd == -1) + { + free(buff); + return ; + } + buff[nu_rd] = '\0'; + ft_lst_append(bgn_lst, buff); + } +} + +char *ft_gen_nl(t_list_gnl *lst) +{ + char *next_line; + char *tmp2; + int i; + + if (ft_line_size(lst) == 0) + return (NULL); + next_line = (char *)malloc((ft_line_size(lst) + 1) * sizeof(char)); + if (!next_line) + return (NULL); + i = 0; + while (lst) + { + tmp2 = lst->str; + while (*tmp2 && *tmp2 != '\n') + next_line[i++] = *tmp2++; + if (*tmp2 == '\n') + { + next_line[i++] = '\n'; + break ; + } + lst = lst->next; + } + next_line[i] = '\0'; + return (next_line); +} + +void ft_update_list(t_list_gnl **lst) +{ + t_list_gnl *tmp; + char *str_left; + char *tmp2; + + if (!lst || !*lst) + return ; + tmp = *lst; + while ((*lst)->next) + { + tmp = *lst; + *lst = (*lst)->next; + ft_delone(tmp); + } + tmp2 = (*lst)->str; + while (*tmp2 && *tmp2 != '\n') + tmp2++; + if (*tmp2 == '\n') + tmp2++; + str_left = ft_strdup(tmp2); + ft_delone(*lst); + *lst = NULL; + if (str_left && *str_left != '\0') + ft_lst_append(lst, str_left); + else + free(str_left); +} +/* +#include + +int main(int ac, char *av[]) +{ + int fd; + + if (ac == 1) + fd = STDIN_FILENO; + else if (ac == 2) + fd = open(av[1], O_RDONLY); + else + return (0); + int i = 0; + char *line; + + line = get_next_line(fd); + + while (line) + { + i++; + printf("%.3d call :%s", i, line); + free(line); + line = get_next_line(fd); + } + //printf("1st call\n%s", get_next_line(fd)); + //printf("2nd call\n%s", get_next_line(fd)); + close(fd); + return (0); +}*/ diff --git a/libft/srcs/gnl/get_next_line_utils_bonus.c b/libft/srcs/gnl/get_next_line_utils_bonus.c new file mode 100644 index 0000000..91cd21a --- /dev/null +++ b/libft/srcs/gnl/get_next_line_utils_bonus.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/02 16:25:45 by tsuchen #+# #+# */ +/* Updated: 2024/06/15 13:58:28 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line_bonus.h" + +void ft_lst_append(t_list_gnl **lst, char *str) +{ + t_list_gnl *new_lst; + t_list_gnl *tmp; + + new_lst = (t_list_gnl *)malloc(sizeof(t_list_gnl)); + if (!new_lst) + return ; + new_lst->str = str; + new_lst->next = NULL; + if (!(*lst)) + { + *lst = new_lst; + return ; + } + tmp = *lst; + while (tmp->next) + tmp = tmp->next; + tmp->next = new_lst; +} + +int ft_line_size(t_list_gnl *lst) +{ + int len; + t_list_gnl *tmp; + char *tmp2; + + len = 0; + tmp = lst; + while (tmp) + { + tmp2 = tmp->str; + while (*tmp2) + { + if (*tmp2 == '\n') + return (++len); + len++; + tmp2++; + } + tmp = tmp->next; + } + return (len); +} + +void ft_delone(t_list_gnl *lst) +{ + free(lst->str); + free(lst); +} +/* +size_t ft_strlen(const char *s) +{ + const char *ptr; + + ptr = s; + while (*ptr) + ptr++; + return (ptr - s); +} + +char *ft_strdup(const char *s) +{ + char *dup; + size_t n; + int i; + + i = 0; + n = ft_strlen(s); + dup = (char *)malloc((n + 1) * sizeof(char)); + if (!dup) + return (0); + while (s[i]) + { + dup[i] = s[i]; + i++; + } + dup[i] = '\0'; + return (dup); +}*/ diff --git a/libft/srcs/libc/ft_atof.c b/libft/srcs/libc/ft_atof.c new file mode 100644 index 0000000..612a5ce --- /dev/null +++ b/libft/srcs/libc/ft_atof.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:13:22 by tsuchen #+# #+# */ +/* Updated: 2024/07/05 10:20:56 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isblank(char c) +{ + if (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' + || c == '\v') + return (1); + return (0); +} + +static double ft_power(double base, int power, double output) +{ + if (power < 0) + return (0.0); + if (!power) + return (output); + else + return (ft_power(base, power - 1, output * base)); +} + +static void ft_handler(double *num, int i, const char *nptr) +{ + int j; + + j = 1; + while (nptr[i] >= '0' && nptr[i] <= '9') + { + *num = *num + (nptr[i] - 48) * ft_power(0.1, j++, 1); + i++; + } +} + +double ft_atof(const char *nptr) +{ + int i; + double sign; + double num; + + i = 0; + sign = 1.0; + num = 0.0; + while (ft_isblank(nptr[i]) == 1) + i++; + if (nptr[i] == '+' || nptr[i] == '-') + { + if (nptr[i] == '-') + sign *= (-1); + i++; + } + while (nptr[i] >= '0' && nptr[i] <= '9') + { + num = ((num * 10) + (nptr[i] - 48)); + i++; + } + if (nptr[i] == '.') + ft_handler(&num, ++i, nptr); + return ((num * sign)); +} +/* +#include +#include +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + printf("real atof :%f\n", atof(av[1])); + printf("test atof :%f\n", ft_atof(av[1])); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_atoi.c b/libft/srcs/libc/ft_atoi.c new file mode 100644 index 0000000..4614575 --- /dev/null +++ b/libft/srcs/libc/ft_atoi.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:13:22 by tsuchen #+# #+# */ +/* Updated: 2024/06/20 01:58:21 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isblank(char c) +{ + if (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' + || c == '\v') + return (1); + return (0); +} + +int ft_atoi(const char *nptr) +{ + int i; + int sign; + int num; + + i = 0; + sign = 1; + num = 0; + while (ft_isblank(nptr[i]) == 1) + i++; + if (nptr[i] == '+' || nptr[i] == '-') + { + if (nptr[i] == '-') + sign *= (-1); + i++; + } + while (nptr[i] >= '0' && nptr[i] <= '9') + { + num = ((num * 10) + (nptr[i] - 48)); + i++; + } + return ((num * sign)); +} diff --git a/libft/srcs/libc/ft_atol.c b/libft/srcs/libc/ft_atol.c new file mode 100644 index 0000000..8b7538f --- /dev/null +++ b/libft/srcs/libc/ft_atol.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 14:42:10 by tsuchen #+# #+# */ +/* Updated: 2024/07/17 16:09:10 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isblank(char c) +{ + if (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' + || c == '\v') + return (1); + return (0); +} + +long ft_atol(const char *nptr) +{ + int i; + long sign; + long num; + + i = 0; + sign = 1; + num = 0; + while (ft_isblank(nptr[i]) == 1) + i++; + if (nptr[i] == '+' || nptr[i] == '-') + { + if (nptr[i] == '-') + sign *= (-1); + i++; + } + while (nptr[i] >= '0' && nptr[i] <= '9') + { + num = ((num * 10) + (nptr[i] - '0')); + i++; + } + return ((num * sign)); +} diff --git a/libft/srcs/libc/ft_bzero.c b/libft/srcs/libc/ft_bzero.c new file mode 100644 index 0000000..cad8f0f --- /dev/null +++ b/libft/srcs/libc/ft_bzero.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:13:46 by tsuchen #+# #+# */ +/* Updated: 2024/05/28 19:34:33 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} +/* +#include +#include +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putstr(char *str, unsigned int len) +{ + unsigned int i; + + i = 0; + while (i < len) + ft_putchar(str[i++]); +} + +int main(void) +{ + char arr_test[9]; + char arr_real[9]; + + memset(arr_test, 49, sizeof(arr_test)); + memset(arr_real, 49, sizeof(arr_real)); + ft_putstr(arr_test, sizeof(arr_test)); + write(1, "\n", 1); + ft_putstr(arr_real, sizeof(arr_real)); + write(1, "\n", 1); + ft_bzero(arr_test, 6); + bzero(arr_real, 6); + ft_putstr(arr_test, sizeof(arr_test)); + write(1, "\n", 1); + ft_putstr(arr_real, sizeof(arr_real)); + write(1, "\n", 1); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_calloc.c b/libft/srcs/libc/ft_calloc.c new file mode 100644 index 0000000..5608511 --- /dev/null +++ b/libft/srcs/libc/ft_calloc.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 11:13:17 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 15:30:27 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + if (nmemb && SIZE_MAX / nmemb < size) + return (NULL); + ptr = (void *)malloc(nmemb * size); + if (!ptr) + return (NULL); + ft_memset(ptr, 0, nmemb * size); + return (ptr); +} +/* +#include +int main(void) +{ + int *tab_1; + char *arr_1; + int *tab_2; + char *arr_2; + + tab_1 = (int *)ft_calloc(10, sizeof(int)); + arr_1 = (char *)ft_calloc(10, sizeof(char)); + tab_2 = (int *)calloc(10, sizeof(int)); + arr_2 = (char *)calloc(10, sizeof(char)); + printf("calloc test 1st elem: %d, add: %p\n", tab_1[0], tab_1); + printf("calloc test 1st elem: %c, add: %p\n", arr_1[0], arr_1); + printf("calloc real 1st elem: %d, add: %p\n", tab_2[0], tab_2); + printf("calloc real 1st elem: %c, add: %p\n", arr_2[0], arr_2); + free(tab_1); + free(arr_1); + free(tab_2); + free(arr_2); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_isalnum.c b/libft/srcs/libc/ft_isalnum.c new file mode 100644 index 0000000..d3442c7 --- /dev/null +++ b/libft/srcs/libc/ft_isalnum.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:49:53 by tsuchen #+# #+# */ +/* Updated: 2024/05/16 14:39:08 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) + || (c >= 48 && c <= 57)) + return (1); + return (0); +} +/* +#include +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + printf("Input Error\n"); + return (0); + } + int test; + int real; + + test = ft_isalnum(*(av[1])); + real = isalnum(*(av[1])); + if (test == 0) + printf("test = %d, not alnum\n", test); + else + printf("test = %d, is alnum\n", test); + if (real == 0) + printf("real = %d, not alnum\n", real); + else + printf("real = %d, is alnum\n", real); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_isalpha.c b/libft/srcs/libc/ft_isalpha.c new file mode 100644 index 0000000..cd1ef8e --- /dev/null +++ b/libft/srcs/libc/ft_isalpha.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:49:39 by tsuchen #+# #+# */ +/* Updated: 2024/05/16 14:39:29 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) + return (1); + return (0); +} +/* +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + write(1, "Input Error\n", 12); + return (0); + } + int output; + + output = ft_isalpha(*(av[1])); + if (output == 1) + write(1, "1", 1); + else + write(1, "0", 1); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_isascii.c b/libft/srcs/libc/ft_isascii.c new file mode 100644 index 0000000..2d0698e --- /dev/null +++ b/libft/srcs/libc/ft_isascii.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:51:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/15 10:51:26 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} +/* +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + write(1, "Input Error\n", 12); + return (0); + } + int output; + + output = ft_isascii(*(av[1])); + if (output == 1) + write(1, "1", 1); + else + write(1, "0", 1); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_isdigit.c b/libft/srcs/libc/ft_isdigit.c new file mode 100644 index 0000000..761052b --- /dev/null +++ b/libft/srcs/libc/ft_isdigit.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:54:58 by tsuchen #+# #+# */ +/* Updated: 2024/05/15 10:55:01 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= 48 && c <= 57) + return (1); + return (0); +} +/* +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + write(1, "Input Error\n", 12); + return (0); + } + int output; + + output = ft_isdigit(*(av[1])); + if (output == 1) + write(1, "1", 1); + else + write(1, "0", 1); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_isprint.c b/libft/srcs/libc/ft_isprint.c new file mode 100644 index 0000000..7e72f9f --- /dev/null +++ b/libft/srcs/libc/ft_isprint.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 10:57:50 by tsuchen #+# #+# */ +/* Updated: 2024/05/15 10:57:54 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} +/* +#include +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + write(1, "Input Error\n", 12); + return (0); + } + int output; + + output = ft_isprint(*(av[1])); + if (output == 1) + write(1, "1", 1); + else + write(1, "0", 1); + if (isprint(*(av[1])) == 1) + write(1, "\n1", 2); + else + write(1, "\n0", 2); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_memchr.c b/libft/srcs/libc/ft_memchr.c new file mode 100644 index 0000000..4fbea84 --- /dev/null +++ b/libft/srcs/libc/ft_memchr.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 19:27:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 19:02:36 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + const unsigned char *tmp; + + i = 0; + tmp = (unsigned char *)s; + while (i < n) + { + if (tmp[i] == (unsigned char)c) + return ((void *)(tmp + i)); + i++; + } + return (NULL); +} +/* +#include +#include + +int main(void) +{ + int tab[7] = {-49, 49, 1, -1, 0, -2, 2}; + char *ptr; + + ptr = NULL; + printf("%s\n", (char *)ft_memchr(tab, -1, 7)); + printf("%s\n", (char *)memchr(tab, -1, 7)); + printf("%s\n", (char *)ft_memchr(tab, 1, -1)); + printf("%s\n", (char *)memchr(tab, 1, -1)); + printf("check NULL condition\n"); + printf("%s\n", (char *)memchr(ptr, 'A', 0)); + printf("%s\n", (char *)ft_memchr(ptr, 'A', 0)); + //printf("%s\n", (char *)memchr(ptr, 'A', 4)); + //printf("%s\n", (char *)ft_memchr(ptr, 'A', 4)); + return (0); +} +int main(int ac, char *av[]) +{ + if ((ac != 3) || (strlen(av[2]) != 1)) + { + printf("Input Error\n"); + return (0); + } + char *test; + char *real_memchr; + char *real_strchr; + + test = ft_memchr(av[1], *(av[2]), sizeof(av[1])); + real_memchr = memchr(av[1], *(av[2]), sizeof(av[1])); + real_strchr = strchr(av[1], *(av[2])); + printf("test address: %p\n", test); + printf("real_memchr address: %p\n", real_memchr); + printf("real_strchr address: %p\n", real_strchr); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_memcmp.c b/libft/srcs/libc/ft_memcmp.c new file mode 100644 index 0000000..6b10810 --- /dev/null +++ b/libft/srcs/libc/ft_memcmp.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 19:27:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 19:10:46 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + if (n == 0) + return (0); + while (i < (n - 1) && (((unsigned char *)s1)[i] + == ((unsigned char *)s2)[i])) + i++; + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); +} +/* //Add NULL tests +#include +#include +int main(void) +{ + char *ptr; + + ptr = NULL; + printf("Check ptr conditions\n"); + printf("one of s1 s2 is ptr\n"); + printf("%d\n", memcmp(ptr, " ", 0)); + printf("%d\n", ft_memcmp(ptr, " ", 0)); + //printf("%d\n", memcmp(ptr, " ", 2)); + //printf("%d\n", ft_memcmp(ptr, " ", 2)); + printf("both s1 s2 is ptr\n"); + printf("%d\n", memcmp(ptr, ptr, 0)); + printf("%d\n", ft_memcmp(ptr, ptr, 0)); + //printf("%d\n", memcmp(ptr, ptr, 2)); + //printf("%d\n", ft_memcmp(ptr, ptr, 2)); + return (0); +}*/ +/* +int main(int ac, char *av[]) +{ + if (ac != 4) + { + printf("Input Error\n"); + return (0); + } + int test; + int real; + + test = ft_memcmp(av[1], av[2], atoi(av[3])); + real = memcmp(av[1], av[2], atoi(av[3])); + printf("test cmp: %d\n", test); + printf("real cmp: %d\n", real); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_memcpy.c b/libft/srcs/libc/ft_memcpy.c new file mode 100644 index 0000000..6398298 --- /dev/null +++ b/libft/srcs/libc/ft_memcpy.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 14:57:53 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 09:13:21 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + ((unsigned char *)dest)[i] = ((const unsigned char *)src)[i]; + i++; + } + return (dest); +} +/* +#include +#include + +int main(void) +{ + char arr_t[50] = "Standard Deviation"; + char arr_r[50] = "Standard Deviation"; + char arr_c[50] = "Standard Deviation"; + char *test1; + int i = 0; + char *ptr; + char *ptr2; + char *real1, *real2; + + ptr = NULL; + ptr2 = NULL; + printf("Original string :%s\n", arr_t); + test1 = arr_t; + real1 = arr_r; + real2 = arr_c; + ft_memcpy(test1 + 8, test1, 12); + printf("ft_memcpy overlap :%s\n", arr_t + 8); + memcpy(real2 + 8, real2, 12); + printf("memcpy overlap :%s\n", (arr_c + 8)); + memmove(real1 + 8, real1, 12); + printf("memmove overlap :%s\n", arr_r + 8); + printf("My function\n"); + printf("%s\n", (char *)ft_memcpy(ptr2, ptr, 0)); + printf("%s\n", (char *)memcpy(ptr2, ptr, 0)); + printf("%s\n", (char *)ft_memcpy(&i, &i, 1)); + printf("%s\n", (char *)ft_memcpy(&i, "fake", 1)); + printf("%s\n", (char *)ft_memcpy("fake", &i, 1)); + printf("%s\n", (char *)memcpy(&i, &i, 1)); + printf("%s\n", (char *)memcpy(&i, "fake", 1)); + printf("%s\n", (char *)memcpy("fake", &i, 1)); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_memmove.c b/libft/srcs/libc/ft_memmove.c new file mode 100644 index 0000000..23f7df1 --- /dev/null +++ b/libft/srcs/libc/ft_memmove.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 14:57:53 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 18:48:11 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + void *tmp; + + tmp = dest; + if (src == dest) + return (tmp); + if (src > dest) + { + while (n-- > 0) + *(unsigned char *)(dest++) = *(unsigned char *)(src++); + } + else + { + while (n-- > 0) + ((unsigned char *)dest)[n] = ((unsigned char *)src)[n]; + } + return (tmp); +} +/*//malloc version +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned int i; + unsigned char *buf; + + i = 0; + buf = malloc((n + 1) * sizeof(char)); + if (!buf) + return (0); + while (i < n) + { + buf[i] = ((unsigned char *)src)[i]; + i++; + } + buf[i] = '\0'; + i = 0; + while (i < n) + { + ((unsigned char *)dest)[i] = buf[i]; + i++; + } + free(buf); + return (dest); +}*/ +/* Check NULL conditions +#include +#include +int main(void) +{ + char *ptr1; + char *ptr2; + + ptr1 = NULL; + ptr2 = NULL; + printf("case 1: one of src and dest are null\n"); + printf("%s\n", (char *)memmove(ptr1, "SSS", 0)); + printf("%s\n", (char *)ft_memmove(ptr2, "SSS", 0)); + //printf("%s\n", (char *)memmove(ptr1, "SSS", 2)); + //printf("%s\n", (char *)ft_memmove(ptr2, "SSS", 2)); + printf("%s\n", (char *)memmove("SSS", ptr1, 0)); + printf("%s\n", (char *)ft_memmove("SSS", ptr2, 0)); + //printf("%s\n", (char *)memmove("SSS", ptr1, 2)); + //printf("%s\n", (char *)ft_memmove("SSS", ptr2, 2)); + printf("case 2: both src and dest are null\n"); + printf("%s\n", (char *)memmove(ptr1, ptr1, 0)); + printf("%s\n", (char *)memmove(ptr1, ptr1, 0)); + printf("%s\n", (char *)ft_memmove(ptr2, ptr2, 2)); + printf("%s\n", (char *)ft_memmove(ptr2, ptr2, 2)); + return (0); +}*/ +/* +int main(void) +{ + char arr_t[50] = "Standard Deviation"; + char arr_r[50] = "Standard Deviation"; + char arr_c[50] = "Standard Deviation"; + char *test1; + char *real1, *real2; + + printf("Original string :%s\n", arr_t); + test1 = arr_t; + real1 = arr_r; + real2 = arr_c; + ft_memmove(test1 + 8, test1, 12); + printf("ft_memmove overlap :%s\n", arr_t + 8); + memmove(real1 + 8, real1, 12); + printf("memmove overlap :%s\n", arr_r + 8); + memcpy(real2 + 8, real2, 12); + printf("memcpy overlap :%s\n", (arr_c + 8)); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_memset.c b/libft/srcs/libc/ft_memset.c new file mode 100644 index 0000000..ab4fe26 --- /dev/null +++ b/libft/srcs/libc/ft_memset.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 14:57:53 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 17:14:45 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + ((unsigned char *)s)[i++] = (unsigned char)c; + return (s); +} +/* Optimized approach - "Word-Sized Operations" +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char *byte_ptr; + unsigned char byte_value; + size_t *word_ptr; + size_t word_value; + + byte_ptr = (unsigned char *)s; + byte_value = (unsigned char)c; + word_value = byte_value; + word_value |= word_value << 8; + word_value |= word_value << 16; + if (sizeof(size_t) == 8) + word_value |= word_value << 32; + while ((n > 0) && ((size_t)byte_ptr % sizeof(size_t) != 0)) + { + *byte_ptr++ = byte_value; + n--; + } + word_ptr = (size_t *)byte_ptr; + while (n >= sizeof(size_t)) + { + *word_ptr++ = word_value; + n -= sizeof(size_t); + } + byte_ptr = (unsigned char *)word_ptr; + while (n > 0) + { + *byte_ptr++ = byte_value; + n--; + } + return (s); +}*/ +/* +#include +#include +void ft_putchar(char c) +{ + write(1, &c, 1); +} +void ft_putstr(char *str, unsigned int len) +{ + unsigned int i; + + i = 0; + while (i < len) + ft_putchar(str[i++]); +} +int main(void) +{ + char arr_test[9]; + char arr_real[9]; + + ft_memset(arr_test, 48, sizeof(arr_test)); + memset(arr_real, 49, sizeof(arr_real)); + ft_putstr(arr_test, sizeof(arr_test)); + write(1, "\n", 1); + ft_putstr(arr_real, sizeof(arr_real)); + write(1, "\n", 1); + return (0); +} +//memset is used to initialize or zero out an array of memory. +*/ diff --git a/libft/srcs/libc/ft_strchr.c b/libft/srcs/libc/ft_strchr.c new file mode 100644 index 0000000..734a8e4 --- /dev/null +++ b/libft/srcs/libc/ft_strchr.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 19:27:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 19:59:14 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + const char *tmp; + + tmp = s; + while (*tmp) + { + if (*tmp == (unsigned char)c) + return ((char *)tmp); + tmp++; + } + if (*tmp == (unsigned char)c) + return ((char *)tmp); + return (NULL); +} +/* +#include +#include + +int main(void) +{ + char s[] = "tripouille"; + char *test; + char *real; + + test = ft_strchr(s, 't' + 256); + real = strchr(s, 't' + 256); + printf("test :%s\n", test); + printf("real :%s\n", real); + printf("strcmp:%d\n", strcmp(test, real)); + return (0); +}*/ +/* // Int counter method +char *ft_strchr(const char *s, int c) +{ + int len; + char *tmp; + + len = 0; + tmp = 0; + while (s[len]) + len++; + while (len >= 0) + { + if (s[len] == (unsigned char)c) + tmp = (char *)(s + len); + len--; + } + return (tmp); +}*/ +/* +int main(int ac, char *av[]) +{ + if ((ac != 3) || (strlen(av[2]) != 1)) + { + printf("Input Error\n"); + return (0); + } + char *test; + char *real_strrchr; + char *real_strchr; + + test = ft_strchr(av[1], *(av[2])); + real_strchr = strchr(av[1], *(av[2])); + real_strrchr = strrchr(av[1], *(av[2])); + printf("test address : %p\n", test); + printf("real_strchr address : %p\n", real_strchr); + printf("real_strrchr address: %p\n", real_strrchr); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strcmp.c b/libft/srcs/libc/ft_strcmp.c new file mode 100644 index 0000000..f8679b0 --- /dev/null +++ b/libft/srcs/libc/ft_strcmp.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/15 16:56:05 by tsuchen #+# #+# */ +/* Updated: 2024/06/15 16:58:01 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + while ((*s1 || *s2) && (*s1 == *s2)) + { + s1++; + s2++; + } + return (*s1 - *s2); +} diff --git a/libft/srcs/libc/ft_strdup.c b/libft/srcs/libc/ft_strdup.c new file mode 100644 index 0000000..9779725 --- /dev/null +++ b/libft/srcs/libc/ft_strdup.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 12:33:59 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 23:36:03 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *dup; + size_t n; + int i; + + i = 0; + n = ft_strlen(s); + dup = (char *)malloc((n + 1) * sizeof(char)); + if (!dup) + return (0); + while (s[i]) + { + dup[i] = s[i]; + i++; + } + dup[i] = '\0'; + return (dup); +} +/* +#include +#include +int main(void) +{ + char *src = "Source"; + char *dup_t; + char *dup_r; + + dup_t = ft_strdup(src); + if (!dup_t) + return (0); + dup_r = strdup(src); + if (!dup_r) + return (0); + printf("Duplicate test: %s\n", dup_t); + printf("Duplicate real: %s\n", dup_r); + free(dup_t); + free(dup_r); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strlcat.c b/libft/srcs/libc/ft_strlcat.c new file mode 100644 index 0000000..f4fa1ba --- /dev/null +++ b/libft/srcs/libc/ft_strlcat.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 09:34:10 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 17:29:56 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t siz) +{ + size_t i; + size_t dst_len; + size_t src_len; + + src_len = ft_strlen(src); + if (siz == 0) + return (src_len); + dst_len = ft_strlen(dst); + if (siz < dst_len + 1) + return (src_len + siz); + i = 0; + while (src[i] && i < (siz - dst_len - 1)) + { + dst[i + dst_len] = src[i]; + i++; + } + dst[i + dst_len] = '\0'; + return (src_len + dst_len); +} +/* +#include +//#include +#include + +int main(void) +{ + char b[0xF] = "nyan !"; + + printf("%lu\n", strlcat(NULL, "fake", 2)); + printf("%zu\n", ft_strlcat(NULL, "fake", 2)); + return (0); +}*/ +/* +int main(void) +{ + char src[10] = "123456789"; + char dest_test[16] = "hello yo"; + char dest_real[16] = "hello yo"; + + printf("test: %zu\n", ft_strlcat(dest_test, src, 16)); + printf("test text: %s\n", dest_test); + printf("real: %zu\n", strlcat(dest_real, src, 16)); + printf("test real: %s\n", dest_real); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strlcpy.c b/libft/srcs/libc/ft_strlcpy.c new file mode 100644 index 0000000..8f710ef --- /dev/null +++ b/libft/srcs/libc/ft_strlcpy.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 09:33:59 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 23:27:43 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t siz) +{ + size_t src_len; + size_t i; + + src_len = ft_strlen(src); + i = 0; + if (siz > 0) + { + while ((i < (siz - 1)) && (src[i] != '\0')) + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + } + return (src_len); +} +/* +#include +#include + +int main(void) +{ + char *src; + char dest[20]; + char real_dest[20]; + int test; + int real; + + src = "28HLKJHGVLJHBsd"; + printf("str : %s\n", src); + test = ft_strlcpy(dest, src, sizeof(dest)); + real = strlcpy(real_dest, src, sizeof(real_dest)); + printf("dest: %s - %d\n", dest, test); + printf("real: %s - %d\n", real_dest, real); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strlen.c b/libft/srcs/libc/ft_strlen.c new file mode 100644 index 0000000..2c7cd52 --- /dev/null +++ b/libft/srcs/libc/ft_strlen.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 09:33:37 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 19:43:45 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + const char *ptr; + + ptr = s; + while (*ptr) + ptr++; + return (ptr - s); +} +/* //Int counter method +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s[i]) + i++; + return (i); +} +#include +#include + +int main(int ac, char *av[]) +{ + if (ac != 2) + return (0); + printf("%u\n", ft_strlen(av[1])); + printf("%u\n", strlen(av[1]i)); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strncmp.c b/libft/srcs/libc/ft_strncmp.c new file mode 100644 index 0000000..6004960 --- /dev/null +++ b/libft/srcs/libc/ft_strncmp.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 09:39:18 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 23:14:27 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + if (n == 0) + return (0); + while ((i < (n - 1)) && (s1[i] || s2[i]) && (s1[i] == s2[i])) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} +/* +#include +#include + +int main(int ac, char *av[]) +{ + if (ac != 4) + return (0); + printf("test: %d\n",ft_strncmp(av[1], av[2], atoi(av[3]))); + printf("real: %d\n", strncmp(av[1], av[2], atoi(av[3]))); + printf("m test1: %d\n", ft_strncmp("testss", "test1", 5)); + printf("m real1: %d\n", strncmp("testss", "test1", 5)); + printf("m test: %d\n", ft_strncmp("test\200", "test\0", 6)); + printf("m real: %d\n", strncmp("test\200", "test\0", 6)); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strndup.c b/libft/srcs/libc/ft_strndup.c new file mode 100644 index 0000000..1905f14 --- /dev/null +++ b/libft/srcs/libc/ft_strndup.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: okoca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 12:33:59 by tsuchen #+# #+# */ +/* Updated: 2024/07/12 23:51:48 by okoca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strndup(const char *str, int nb) +{ + char *new; + int i; + int len; + + i = 0; + if (!str || nb < 0) + return (NULL); + if ((int)ft_strlen(str) <= nb) + len = ft_strlen(str); + else + len = nb; + new = (char *)malloc(sizeof(char) * (len + 1)); + if (!new) + return (NULL); + while (str[i] && i < nb) + { + new[i] = str[i]; + i++; + } + new[i] = '\0'; + return (new); +} + +/* +#include +#include +int main(void) +{ + char *src = "Source"; + char *dup_t; + char *dup_r; + + dup_t = ft_strdup(src); + if (!dup_t) + return (0); + dup_r = strdup(src); + if (!dup_r) + return (0); + printf("Duplicate test: %s\n", dup_t); + printf("Duplicate real: %s\n", dup_r); + free(dup_t); + free(dup_r); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strnstr.c b/libft/srcs/libc/ft_strnstr.c new file mode 100644 index 0000000..23cfec9 --- /dev/null +++ b/libft/srcs/libc/ft_strnstr.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 09:39:41 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 14:51:21 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + size_t n_len; + + if (!(*little)) + return ((char *)big); + if (len == 0) + return (0); + i = 0; + n_len = ft_strlen(little); + while (big[i] && (i < len)) + { + j = 0; + while ((j < n_len) && (big[i + j] == little[j]) && (i + j < len)) + j++; + if (j == n_len) + return ((char *)(big + i)); + i++; + } + return (0); +} +/* +#include +#include + +int main(void) +{ + printf("%s\n", strnstr(NULL, "fake", 0)); + printf("%s\n", ft_strnstr(NULL, "fake", 0)); + return (0); +}*/ +/* +#include +int main(int ac, char *av[]) +{ + if (ac != 4) + return (0); + printf("Str test: %s\n", ft_strnstr(av[1], av[2], atoi(av[3]))); + printf("Str real: %s\n", strnstr(av[1], av[2], atoi(av[3]))); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_strrchr.c b/libft/srcs/libc/ft_strrchr.c new file mode 100644 index 0000000..7a5f43e --- /dev/null +++ b/libft/srcs/libc/ft_strrchr.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/08 19:27:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/23 23:10:09 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strrchr(const char *s, int c) +{ + const char *tmp; + const char *last; + + tmp = s; + last = NULL; + while (*tmp != '\0') + { + if (*tmp == (unsigned char)c) + last = tmp; + tmp++; + } + if ((unsigned char)c == 0) + return ((char *)tmp); + return ((char *)last); +} +/* //Int Counter Version +char *ft_strrchr(const char *s, int c) +{ + int len; + char *tmp; + + tmp = 0; + len = (int)ft_strlen(s); + while (len >= 0) + { + if (s[len] == (unsigned char)c) + { + tmp = (char *)(s + len); + return (tmp); + } + len--; + } + return (tmp); +}*/ +/* +#include +#include +int main(void) +{ + char s[] = "tripouille"; + char *test; + char *real; + + test = ft_strrchr(s, 'z'); + real = strrchr(s, 'z'); + printf("test :%s\n", test); + printf("real :%s\n", real); + printf("cmp :%d\n", strcmp(test, real)); + return (0); +}*/ +/* +int main(int ac, char *av[]) +{ + if ((ac != 3) || (strlen(av[2]) != 1)) + { + printf("Input Error\n"); + return (0); + } + char *test; + char *real_strrchr; + char *real_strchr; + + test = ft_strrchr(av[1], *(av[2])); + real_strrchr = strrchr(av[1], *(av[2])); + real_strchr = strchr(av[1], *(av[2])); + printf("test address: %p\n", test); + printf("real_strrchr address: %p\n", real_strrchr); + printf("real_strchr address: %p\n", real_strchr); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_tolower.c b/libft/srcs/libc/ft_tolower.c new file mode 100644 index 0000000..4107eba --- /dev/null +++ b/libft/srcs/libc/ft_tolower.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 10:17:21 by tsuchen #+# #+# */ +/* Updated: 2024/05/16 15:40:09 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + return (c + 32); + return (c); +} +/* +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + write (1, "Input Error\n", 12); + return (0); + } + ft_putchar(ft_tolower(*(av[1]))); + write(1, "\n", 1); + ft_putchar(tolower(*(av[1]))); + write(1, "\n", 1); + return (0); +}*/ diff --git a/libft/srcs/libc/ft_toupper.c b/libft/srcs/libc/ft_toupper.c new file mode 100644 index 0000000..0f66900 --- /dev/null +++ b/libft/srcs/libc/ft_toupper.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 10:17:54 by tsuchen #+# #+# */ +/* Updated: 2024/05/16 15:40:29 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + return (c - 32); + return (c); +} +/* +#include +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +int main(int ac, char *av[]) +{ + if ((ac != 2) || (ft_strlen(av[1]) != 1)) + { + write (1, "Input Error\n", 12); + return (0); + } + ft_putchar(ft_toupper(*(av[1]))); + write(1, "\n", 1); + ft_putchar(toupper(*(av[1]))); + write(1, "\n", 1); + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstadd_back_bonus.c b/libft/srcs/list/ft_lstadd_back_bonus.c new file mode 100644 index 0000000..af32a7e --- /dev/null +++ b/libft/srcs/list/ft_lstadd_back_bonus.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 11:39:29 by tsuchen #+# #+# */ +/* Updated: 2024/06/16 16:27:55 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *tmp; + + if (!new) + return ; + if (!(*lst)) + { + *lst = new; + return ; + } + tmp = *lst; + while (tmp->next) + tmp = tmp->next; + new->prev = tmp; + tmp->next = new; +} +/* Note + * 1. if lst (or bgn_lst) is NULL, new will be the 1st lst + */ +/* +#include +void ft_print_result(t_list *elem) +{ + int len; + + len = 0; + while (((char *)elem->content)[len]) + len++; + write(1, elem->content, len); + write(1, "\n", 1); +} + +t_list *ft_lstnewone(void *content) +{ + t_list *elem; + + elem = (t_list *)malloc(sizeof(t_list)); + if (!elem) + return (NULL); + if (!content) + elem->content = NULL; + else + elem->content = content; + elem->next = NULL; + return (elem); +} + +int main(int argc, const char *argv[]) +{ + t_list *begin; + t_list *elem; + t_list *elem2; + t_list *elem3; + t_list *elem4; + char *str = strdup("lorem"); + char *str2 = strdup("ipsum"); + char *str3 = strdup("dolor"); + char *str4 = strdup("sit"); + + elem = ft_lstnewone(str); + elem2 = ft_lstnewone(str2); + elem3 = ft_lstnewone(str3); + elem4 = ft_lstnewone(str4); + alarm(5); + if (argc == 1 || !elem || !elem2 || !elem3 || !elem4) + return (0); + else if (atoi(argv[1]) == 1) + { + begin = NULL; + ft_lstadd_back(&begin, elem); + ft_lstadd_back(&begin, elem2); + ft_lstadd_back(&begin, elem3); + ft_lstadd_back(&begin, elem4); + while (begin) + { + ft_print_result(begin); + begin = begin->next; + } + } + return (0); +}*/ +/* +#include +t_list *ft_lstnew(void *content); +int main(void) +{ + int a = 4; + int b = 3; + t_list *tmp1; + t_list *tmp2; + + t_list *lst_0; + t_list *lst_1; + lst_0 = ft_lstnew(&a); + lst_1 = ft_lstnew(&b); + ft_lstadd_back(&lst_1, lst_0); + tmp1 = lst_1; + printf("Next content of lst_1 is: %d\n", *(int *)(tmp1->next->content)); + tmp2 = lst_1->next; + free(tmp2); + free(lst_1); + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstadd_front_bonus.c b/libft/srcs/list/ft_lstadd_front_bonus.c new file mode 100644 index 0000000..b0fee0e --- /dev/null +++ b/libft/srcs/list/ft_lstadd_front_bonus.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 11:39:29 by tsuchen #+# #+# */ +/* Updated: 2024/06/16 16:21:05 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (!new) + return ; + if (*lst) + (*lst)->prev = new; + new->next = *lst; + *lst = new; +} +/* Note + * 1. if new is NULL, it won't do anything + * 2. if lst (or bgn_lst) is NULL, it will be replaced with new + */ +/* +#include +t_list *ft_lstnew(void *content); +int main(void) +{ + int a = 4; + int b = 3; + t_list *tmp1; + t_list *tmp2; + t_list *lst_0; + t_list *lst_1; + + lst_0 = ft_lstnew(&a); + lst_1 = ft_lstnew(&b); + ft_lstadd_front(&lst_1, lst_0); + tmp1 = lst_1; + printf("Next content of lst_0 is: %d\n", *(int *)(tmp1->next->content)); + tmp2 = lst_1->next; + free(tmp2); + free(lst_1); + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstclear_bonus.c b/libft/srcs/list/ft_lstclear_bonus.c new file mode 100644 index 0000000..bb899c6 --- /dev/null +++ b/libft/srcs/list/ft_lstclear_bonus.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 13:09:48 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 12:04:41 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + while (*lst) + { + tmp = *lst; + *lst = (*lst)->next; + ft_lstdelone(tmp, del); + } +} +/* Note + * 1. if *lst (or bgn_lst) is NULL, do nothing (already cleared) + * 2. if function "del" is NULL, it will crash at lstdelone + */ +/* +#include +t_list *ft_lstnew(void *content); +void ft_lstadd_back(t_list **lst, t_list *new); + +void del_content(void *content) +{ + free(content); +} + +int main(void) +{ + t_list *begin_list; + int a = 3; + int b = 4; + int i = 0; + t_list *tmp; + t_list *tmp2; + t_list *o_next; + int *o_data; + + begin_list = ft_lstnew(ft_lstnew(&a)); + tmp = begin_list; + while (i < 10) + { + ft_lstadd_back(&begin_list, ft_lstnew(ft_lstnew(&b))); + i++; + } + printf("content |current add |next add\n"); + while (tmp) + { + printf("%p |%p |%p\n", ((*tmp).content), tmp, (*tmp).next); + tmp = (*tmp).next; + } + // save original record + o_next = begin_list->next; + o_data = begin_list->content; + // clear the list + tmp2 = begin_list; + ft_lstclear(&begin_list, &del_content); + printf(" ==> after free <==\n"); + printf("bgn_lst %p, %p\n", begin_list, &begin_list); + printf("tmp2 %p, %p\n", tmp2, &tmp2); + printf(" ======================= \n"); + printf("data before: %p; after: %p\n", o_data, tmp2->content); + if (tmp2->content != o_data) + printf("==> list data no longer exists!\n"); + printf("next before: %p; after: %p\n", o_next, tmp2->next); + if (tmp2->next != o_next) + printf("==> list link no longer exists!\n"); + return (0); +} +// The 4 errors are due to tmp2->content and tmp2-> after free +*/ diff --git a/libft/srcs/list/ft_lstdelone_bonus.c b/libft/srcs/list/ft_lstdelone_bonus.c new file mode 100644 index 0000000..c280c50 --- /dev/null +++ b/libft/srcs/list/ft_lstdelone_bonus.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 13:09:48 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 12:04:45 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + del(lst->content); + free(lst); +} +/* Note + * 1. if lst is NULL, it will crash. Make sure the input node is not NULL + * 2. if function "del" is NULL, it will crash as well + */ +/* +#include +t_list *ft_lstnew(void *content); + +void del_content(void *content) +{ + free(content); +} + +int main(void) +{ + int a = 3; + t_list *lst; + t_list *cont; + + cont = ft_lstnew(&a); + lst = ft_lstnew(cont); + printf("Before DEL, add of lst : %p | lst_cont : %p\n", lst, lst->content); + printf("Before DEL, add of cont: %p | cont_cont: %p\n", cont, cont->content); + ft_lstdelone(lst, &del_content); + printf("After DEL, add of lst : %p | lst_cont : %p\n", lst, lst->content); + printf("After DEL, add of cont: %p | cont_cont: %p\n", cont, cont->content); + return (0); +} +// The 2 errors in valgrind are due to trying to access +// the content of lst and cont after free. +*/ diff --git a/libft/srcs/list/ft_lstiter_bonus.c b/libft/srcs/list/ft_lstiter_bonus.c new file mode 100644 index 0000000..11f14f1 --- /dev/null +++ b/libft/srcs/list/ft_lstiter_bonus.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 14:40:23 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 12:13:18 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *tmp; + + tmp = lst; + while (tmp) + { + f(tmp->content); + tmp = tmp->next; + } +} +/* Note + * 1. if lst is NULL, nothing happen + * 2. if funciton "f" is NULL but lst is not NULL, it will crash + */ +/* +#include +void ft_lstadd_back(t_list **lst, t_list *new); +t_list *ft_lstnew(void *content); + +void print_out(void *content) +{ + printf("%d\n", *(int *)content + 1); +} + +int main(void) +{ + int a = 3; + int i = 0; + t_list *lst; + t_list *tmp; + + lst = ft_lstnew(&a); + while (i < 9) + { + ft_lstadd_back(&lst, ft_lstnew(&i)); + i++; + } + ft_lstiter(lst, &print_out); + while (lst) + { + tmp = lst; + lst = lst->next; + free(tmp); + } + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstlast_bonus.c b/libft/srcs/list/ft_lstlast_bonus.c new file mode 100644 index 0000000..e6ca52f --- /dev/null +++ b/libft/srcs/list/ft_lstlast_bonus.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 12:53:24 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 11:45:17 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + t_list *tmp; + + if (!lst) + return (NULL); + tmp = lst; + while (tmp->next) + tmp = tmp->next; + return (tmp); +} +/* Note + * 1. if lst is NULL, it will return NULL + */ +/* +#include +void ft_lstadd_front(t_list **lst, t_list *new); +t_list *ft_lstnew(void *content); + +int main(void) +{ + t_list *lst; + t_list *last; + int a = 3; + int b = 4; + int i = 0; + t_list *tmp; + + lst = ft_lstnew(&a); + while (i < 10) + { + ft_lstadd_front(&lst, ft_lstnew(&b)); + i++; + } + last = ft_lstlast(lst); + printf("value of last list is: %d\n", *(int *)(*last).content); + while (lst) + { + tmp = lst; + lst = lst->next; + free(tmp); + } + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstmap_bonus.c b/libft/srcs/list/ft_lstmap_bonus.c new file mode 100644 index 0000000..f99aa30 --- /dev/null +++ b/libft/srcs/list/ft_lstmap_bonus.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 14:54:24 by tsuchen #+# #+# */ +/* Updated: 2024/05/24 12:10:22 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_lst; + t_list *tmp_lst; + void *tmp_data; + + new_lst = NULL; + while (lst) + { + tmp_data = f(lst->content); + tmp_lst = ft_lstnew(tmp_data); + if (!tmp_lst) + { + del(tmp_data); + ft_lstclear(&new_lst, del); + return (0); + } + ft_lstadd_back(&new_lst, tmp_lst); + lst = lst->next; + } + return (new_lst); +} +/* Note + * 1. if lst is NULL, it will return NULL + * 2. if malloc fail, it will return NULL + * 3. if either function "f" or "del" is NULL, it will crash + */ +/* +#include +void *triple_data(void *data) +{ + void *new; + + new = malloc(sizeof(int)); + if (!new) + return (0); + *(int *)new = *(int *)data * 3; + return (new); +} + +void del_content(void *content) +{ + free(content); +} + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *tmp; + + tmp = lst; + while (tmp) + { + f(tmp->content); + tmp = tmp->next; + } +} + +void print_out(void *content) +{ + printf("%d\n", *(int *)content); +} + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + while (*lst) + { + tmp = *lst; + *lst = (*lst)->next; + del(tmp->content); + free(tmp); + } +} + +int main(void) +{ + int a = 3; + int b = 4; + int i = 0; + t_list *org; + t_list *new; + t_list *tmp1; + + org = ft_lstnew(&a); + while (i++ < 9) + ft_lstadd_back(&org, ft_lstnew(&b)); + printf("original list:\n"); + ft_lstiter(org, &print_out); + new = ft_lstmap(org, &triple_data, &del_content); + printf("mapping list:\n"); + ft_lstiter(new, &print_out); + tmp1 = org; + while (org) + { + tmp1 = org; + org = org->next; + free(tmp1); + } + ft_lstclear(&new, &del_content); + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstnew_bonus.c b/libft/srcs/list/ft_lstnew_bonus.c new file mode 100644 index 0000000..cc74646 --- /dev/null +++ b/libft/srcs/list/ft_lstnew_bonus.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 11:28:30 by tsuchen #+# #+# */ +/* Updated: 2024/06/16 16:18:48 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *lst; + + lst = (t_list *)malloc(sizeof(t_list)); + if (!lst) + return (NULL); + lst->content = content; + lst->next = NULL; + lst->prev = NULL; + return (lst); +} +/* +#include +int main(void) +{ + int a = 7; + t_list *lst; + + lst = ft_lstnew(&a); + printf("val_lst: %d, add_lst: %p\n", *((int *)(lst->content)), lst->content); + printf("val_a : %d, add_a : %p\n", a, &a); + printf("add of nex in lst: %p\n", lst->next); + free(lst); + return (0); +}*/ diff --git a/libft/srcs/list/ft_lstsize_bonus.c b/libft/srcs/list/ft_lstsize_bonus.c new file mode 100644 index 0000000..10d3e50 --- /dev/null +++ b/libft/srcs/list/ft_lstsize_bonus.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 12:42:49 by tsuchen #+# #+# */ +/* Updated: 2024/05/16 10:58:56 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + t_list *tmp; + + tmp = lst; + i = 0; + while (tmp) + { + i++; + tmp = tmp->next; + } + return (i); +} +/* +#include +void ft_lstadd_front(t_list **lst, t_list *new); +t_list *ft_lstnew(void *content); + +int main(void) +{ + t_list *lst; + int a = 3; + int i = 0; + t_list *tmp1; + + lst = ft_lstnew(&a); + while (i < 10) + { + ft_lstadd_front(&lst, ft_lstnew(&a)); + i++; + } + printf("size of list is: %d\n", ft_lstsize(lst)); + while (lst) + { + tmp1 = lst; + lst = lst->next; + free(tmp1); + } + return (0); +}*/