From: Lukas Jiriste Date: Sun, 1 Sep 2024 13:25:48 +0000 (+0200) Subject: Absorb Libft into minishell for 42 evaluation X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=refs%2Fheads%2F42;p=42%2Fminishell.git Absorb Libft into minishell for 42 evaluation --- diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 626d139..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "Libft"] - path = Libft - url = git://ljiriste.work/Libft diff --git a/Libft b/Libft deleted file mode 160000 index 4afafbb..0000000 --- a/Libft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4afafbba59ad4abf953ea8aa23f1e61fb2c1daea diff --git a/Libft/Makefile b/Libft/Makefile new file mode 100644 index 0000000..537e92d --- /dev/null +++ b/Libft/Makefile @@ -0,0 +1,181 @@ +CC := cc +CFLAGS := -std=c99 -Wall -Wextra -Werror -Wpedantic + +ifneq ("$(wildcard .debug)","") + CFLAGS += -g +endif + +AR := ar + +RM := rm -f + +INCDIR = ./inc +INCLUDE := $(addprefix -I, $(INCDIR)) + +SRCDIR := ft_gen ft_math ft_str ft_mem ft_io ft_check ft_conv ft_lst ft_arr ft_parse ft_struct + +SRCstruct:= ft_stack_free.c \ + ft_stack_init.c \ + ft_stack_pop.c \ + ft_stack_pop_forget.c \ + ft_stack_push.c \ + ft_stack_top.c \ + +SRCparse:= ft_parse.c \ + ft_parsing_table_init.c \ + ft_parsing_table_load.c \ + ft_parsing_table_load_helpers.c \ + ft_parsing_table_print.c \ + ft_parsing_table_free.c \ + ft_parse_tree_print.c \ + ft_parse_tree_free.c \ + load_rules.c \ + add_line.c \ + actions.c \ + helpers.c \ + +SRCgen := ft_swap.c \ + +SRCmath := ft_abs.c \ + ft_sgn.c \ + ft_max.c \ + ft_min.c \ + +SRCstr := ft_strcat_alloc.c \ + ft_strncat_alloc.c \ + ft_strcmp.c \ + ft_strncmp.c \ + ft_strndup.c \ + ft_strchr.c \ + ft_strtrim.c \ + ft_strlen.c \ + ft_strdup.c \ + ft_strlcpy.c \ + ft_strnstr.c \ + ft_striteri.c \ + ft_strmapi.c \ + ft_strjoin.c \ + ft_remove_space.c \ + ft_split.c \ + ft_substr.c \ + ft_strlcat.c \ + ft_strrchr.c \ + +SRCmem := ft_calloc.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memset.c \ + ft_memmove.c \ + ft_memcpy.c \ + ft_bzero.c \ + +SRCio := ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_printf/conversion.c \ + ft_printf/ft_printf.c \ + ft_printf/padding.c \ + ft_printf/parsing.c \ + ft_printf/formatting.c \ + ft_putchar_fd.c \ + get_next_line.c \ + ft_putstr_fd.c \ + +SRCcheck:= ft_isalnum.c \ + ft_isspace.c \ + ft_isdigit.c \ + ft_isascii.c \ + ft_isalpha.c \ + ft_islower.c \ + ft_isupper.c \ + ft_isprint.c \ + ft_isint.c \ + +SRCconv := ft_itoa_base.c \ + ft_tolower.c \ + ft_toupper.c \ + ft_atoi.c \ + ft_ctoa.c \ + ft_itoa.c \ + ft_uitoa_base.c \ + +SRClst := ft_lst_reverse.c \ + ft_lstdelone.c \ + ft_lst_foreach_if.c \ + ft_lstlast.c \ + ft_lstclear.c \ + ft_lst_at.c \ + ft_lst_sorted_merge.c \ + ft_lst_find.c \ + ft_lstmap.c \ + ft_lst_remove_if.c \ + ft_lstnew.c \ + ft_lst_merge.c \ + ft_lstsize.c \ + ft_lstadd_front.c \ + ft_lstadd_back.c \ + ft_lstiter.c \ + ft_lst_sort.c \ + ft_lst_sorted_insert.c \ + +SRCarr := ft_vec_init.c \ + ft_vec_reserve.c \ + ft_vec_enlarge.c \ + ft_vec_insert.c \ + ft_vec_append.c \ + ft_vec_forget.c \ + ft_vec_erase.c \ + ft_vec_access.c \ + ft_vec_free.c \ + ft_vec_find.c \ + ft_vec_find_index.c \ + ft_vec_copy.c \ + \ + ft_vec_is_equal.c \ + \ + ft_vec_contains.c \ + ft_vec_is_subset.c \ + ft_vec_is_setequal.c \ + \ + ft_vec_setinsert.c \ + \ + ft_mat_init.c \ + ft_mat_append.c \ + ft_mat_insert_col.c \ + ft_mat_insert_row.c \ + ft_mat_zeros.c \ + ft_mat_fill.c \ + ft_mat_access.c \ + ft_mat_free.c \ + +SOURCES := $(foreach dir, $(SRCDIR), $(addprefix $(dir)/, $($(dir:ft_%=SRC%)))) +OBJECTS := $(SOURCES:.c=.o) + +NAME = libft.a + +all : $(NAME) + +.debug : + $(MAKE) fclean + touch $@ + +debug : .debug + $(MAKE) all + +nodebug : + $(RM) .debug + $(MAKE) re + +$(NAME) : $(OBJECTS) + $(AR) rcs $@ $^ + +%.o : %.c + $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) + +clean : + $(RM) $(OBJECTS) + +fclean : clean + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/Libft/ft_arr/ft_mat_access.c b/Libft/ft_arr/ft_mat_access.c new file mode 100644 index 0000000..b8886e9 --- /dev/null +++ b/Libft/ft_arr/ft_mat_access.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_access.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/17 11:32:45 by ljiriste #+# #+# */ +/* Updated: 2024/01/17 13:03:13 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void *ft_mat_access(t_mat *mat, size_t row, size_t col) +{ + if (!mat) + return (NULL); + if (row >= mat->rows || col >= mat->cols) + return (NULL); + return ((char *)mat->vec.vec + (row * mat->cols + col) * mat->vec.el_size); +} diff --git a/Libft/ft_arr/ft_mat_append.c b/Libft/ft_arr/ft_mat_append.c new file mode 100644 index 0000000..50031f8 --- /dev/null +++ b/Libft/ft_arr/ft_mat_append.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_append.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 10:25:34 by ljiriste #+# #+# */ +/* Updated: 2023/12/12 10:28:46 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +t_arr_stat ft_mat_append_row(t_mat *mat, const t_vec *vec) +{ + return (ft_mat_insert_row(mat, vec, mat->rows)); +} + +t_arr_stat ft_mat_append_col(t_mat *mat, const t_vec *vec) +{ + return (ft_mat_insert_col(mat, vec, mat->cols)); +} diff --git a/Libft/ft_arr/ft_mat_fill.c b/Libft/ft_arr/ft_mat_fill.c new file mode 100644 index 0000000..bafe399 --- /dev/null +++ b/Libft/ft_arr/ft_mat_fill.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_fill.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/05 10:00:33 by ljiriste #+# #+# */ +/* Updated: 2024/04/12 16:14:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include "libft.h" + +t_arr_stat ft_mat_fill(t_mat *mat, void *filler) +{ + size_t i; + size_t j; + + i = 0; + while (i < mat->rows) + { + j = 0; + while (j < mat->cols) + { + ft_memcpy(ft_mat_access(mat, i, j), filler, mat->vec.el_size); + ++j; + } + ++i; + } + return (success); +} diff --git a/Libft/ft_arr/ft_mat_free.c b/Libft/ft_arr/ft_mat_free.c new file mode 100644 index 0000000..b166885 --- /dev/null +++ b/Libft/ft_arr/ft_mat_free.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 19:06:07 by ljiriste #+# #+# */ +/* Updated: 2023/12/11 19:08:21 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +void ft_mat_free(t_mat *mat, void (*free_el)(void *)) +{ + ft_vec_free(&(mat->vec), free_el); + mat->cols = 0; + mat->rows = 0; + return ; +} diff --git a/Libft/ft_arr/ft_mat_init.c b/Libft/ft_arr/ft_mat_init.c new file mode 100644 index 0000000..bf70cb8 --- /dev/null +++ b/Libft/ft_arr/ft_mat_init.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 19:03:27 by ljiriste #+# #+# */ +/* Updated: 2023/12/11 19:05:45 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +t_arr_stat ft_mat_init(t_mat *mat, size_t el_size) +{ + mat->rows = 0; + mat->cols = 0; + return (ft_vec_init(&(mat->vec), el_size)); +} diff --git a/Libft/ft_arr/ft_mat_insert_col.c b/Libft/ft_arr/ft_mat_insert_col.c new file mode 100644 index 0000000..d75dae5 --- /dev/null +++ b/Libft/ft_arr/ft_mat_insert_col.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_insert_col.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 08:57:22 by ljiriste #+# #+# */ +/* Updated: 2024/05/06 20:54:19 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include "ft_mem.h" + +// Creates space for the inserted column inside the mat->vec +void reorganize(t_mat *mat, size_t move_index, size_t change) +{ + size_t i; + + i = mat->rows - 1; + if (move_index < mat->cols) + { + ft_memmove((char *)mat->vec.vec + + ((mat->rows - 1) * mat->cols + move_index) * mat->vec.el_size, + (char *)mat->vec.vec + ((mat->rows - 1) + * (mat->cols + change) + move_index) * mat->vec.el_size, + (mat->cols - move_index) * mat->vec.el_size); + } + --i; + while (i + 1 > 0) + { + ft_memmove((char *)mat->vec.vec + + (i * mat->cols + move_index) * mat->vec.el_size, + (char *)mat->vec.vec + + (i * (mat->cols + change) + move_index) * mat->vec.el_size, + mat->cols * mat->vec.el_size); + --i; + } + mat->cols += change; + return ; +} + +static void copy(t_mat *mat, const t_vec *vec, size_t index) +{ + size_t i; + + i = 0; + while (i < mat->rows) + { + ft_memmove((char *)vec->vec + i * vec->el_size, + (char *)mat->vec.vec + (i * mat->cols + index) + * mat->vec.el_size, vec->el_size); + ++i; + } + return ; +} + +static void set_auxiliary_vars(t_mat *mat, size_t index, + size_t *cols_change, size_t *move_index) +{ + if (index > mat->cols) + { + *cols_change = index - mat->cols + 1; + *move_index = mat->cols; + } + else + { + *cols_change = 1; + *move_index = index; + } + return ; +} + +t_arr_stat ft_mat_insert_col(t_mat *mat, const t_vec *vec, size_t index) +{ + size_t cols_change; + size_t move_index; + t_arr_stat res; + + if (!mat || !vec || vec->size == 0) + return (invalid_input); + if (mat->rows == 0) + mat->rows = vec->size; + else if (mat->rows != vec->size) + return (invalid_input); + set_auxiliary_vars(mat, index, &cols_change, &move_index); + while ((mat->cols + cols_change) * mat->rows > mat->vec.capacity) + { + res = ft_vec_enlarge(&(mat->vec)); + if (res != success) + return (res); + } + reorganize(mat, move_index, cols_change); + copy(mat, vec, index); + return (success); +} diff --git a/Libft/ft_arr/ft_mat_insert_row.c b/Libft/ft_arr/ft_mat_insert_row.c new file mode 100644 index 0000000..b64944a --- /dev/null +++ b/Libft/ft_arr/ft_mat_insert_row.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_insert_row.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 19:20:00 by ljiriste #+# #+# */ +/* Updated: 2024/01/17 13:53:42 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +t_arr_stat ft_mat_insert_row(t_mat *mat, const t_vec *vec, size_t index) +{ + size_t res; + + if (!mat || !vec || vec->size == 0) + return (invalid_input); + if (mat->cols == 0) + mat->cols = vec->size; + else if (mat->cols != vec->size) + return (invalid_input); + res = ft_vec_insert_range(&(mat->vec), vec->vec, + mat->cols, index * mat->cols); + if (res != success) + return (res); + if (index < mat->rows) + ++mat->rows; + else + mat->rows += index - mat->rows + 1; + return (success); +} diff --git a/Libft/ft_arr/ft_mat_zeros.c b/Libft/ft_arr/ft_mat_zeros.c new file mode 100644 index 0000000..ca96300 --- /dev/null +++ b/Libft/ft_arr/ft_mat_zeros.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_zeros.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 11:00:49 by ljiriste #+# #+# */ +/* Updated: 2024/04/02 11:23:05 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +t_arr_stat ft_mat_zeros(t_mat *matrix, size_t rows, size_t cols) +{ + t_arr_stat reserve_res; + + if ((rows * cols) / cols != rows) + return (invalid_size); + reserve_res = ft_vec_reserve(&(matrix->vec), rows * cols); + if (reserve_res != success) + return (reserve_res); + matrix->rows = rows; + matrix->cols = cols; + matrix->vec.size = rows * cols; + return (success); +} diff --git a/Libft/ft_arr/ft_vec_access.c b/Libft/ft_arr/ft_vec_access.c new file mode 100644 index 0000000..c60cce9 --- /dev/null +++ b/Libft/ft_arr/ft_vec_access.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_access.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:14:49 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 11:43:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +void *ft_vec_access(t_vec *vec, size_t index) +{ + if (!vec) + return (NULL); + if (index >= vec->size) + return (NULL); + return ((char *)vec->vec + vec->el_size * index); +} + +const void *ft_vec_caccess(const t_vec *vec, size_t index) +{ + if (!vec) + return (NULL); + if (index >= vec->size) + return (NULL); + return ((const char *)vec->vec + vec->el_size * index); +} diff --git a/Libft/ft_arr/ft_vec_append.c b/Libft/ft_arr/ft_vec_append.c new file mode 100644 index 0000000..fb5e0da --- /dev/null +++ b/Libft/ft_arr/ft_vec_append.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_append.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 16:39:20 by ljiriste #+# #+# */ +/* Updated: 2023/12/11 10:21:49 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +t_arr_stat ft_vec_append(t_vec *vec, const void *element) +{ + return (ft_vec_append_range(vec, element, 1)); +} + +t_arr_stat ft_vec_append_range(t_vec *vec, const void *element, size_t count) +{ + return (ft_vec_insert_range(vec, element, count, vec->size)); +} diff --git a/Libft/ft_arr/ft_vec_contains.c b/Libft/ft_arr/ft_vec_contains.c new file mode 100644 index 0000000..6eb51be --- /dev/null +++ b/Libft/ft_arr/ft_vec_contains.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_contains.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 13:27:29 by ljiriste #+# #+# */ +/* Updated: 2024/06/28 13:39:14 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +int ft_vec_contains(const t_vec *vec, const void *wanted, + int (*cmp_elements)(const void *, const void *)) +{ + size_t i; + const void *element; + + i = 0; + while (i < vec->size) + { + element = ft_vec_caccess(vec, i); + if (cmp_elements(wanted, element) == 0) + return (1); + ++i; + } + return (0); +} diff --git a/Libft/ft_arr/ft_vec_copy.c b/Libft/ft_arr/ft_vec_copy.c new file mode 100644 index 0000000..73f6c97 --- /dev/null +++ b/Libft/ft_arr/ft_vec_copy.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 12:02:22 by ljiriste #+# #+# */ +/* Updated: 2024/07/05 10:26:54 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +static t_arr_stat prepare_for_copy(t_vec *dest, const t_vec *src) +{ + t_arr_stat res; + + res = ft_vec_init(dest, src->el_size); + if (res != success) + return (res); + res = ft_vec_reserve(dest, src->capacity); + return (res); +} + +// This function the exact current state of src to dest. +// copy_el function enables deep copy +// free_el function enbales cleaning after itself in a case of error +t_arr_stat ft_vec_copy(t_vec *dest, const t_vec *src, + t_ft_stat (*copy_el)(void *, const void *), void (*free_el)(void *)) +{ + size_t i; + void *tmp; + t_arr_stat res; + + tmp = malloc(src->el_size); + if (!tmp) + return (alloc_fail); + res = prepare_for_copy(dest, src); + if (res != success) + return (res); + i = 0; + while (i < src->size && res == success) + { + res = copy_el(tmp, ft_vec_caccess(src, i)); + if (res != success) + break ; + res = ft_vec_append(dest, tmp); + if (res != success && free_el) + free_el(tmp); + ++i; + } + if (res != success) + ft_vec_free(dest, free_el); + free(tmp); + return (res); +} diff --git a/Libft/ft_arr/ft_vec_enlarge.c b/Libft/ft_arr/ft_vec_enlarge.c new file mode 100644 index 0000000..7778eaf --- /dev/null +++ b/Libft/ft_arr/ft_vec_enlarge.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_enlarge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 17:03:50 by ljiriste #+# #+# */ +/* Updated: 2024/01/12 17:06:04 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +t_arr_stat ft_vec_enlarge(t_vec *vec) +{ + if (vec->capacity == SIZE_MAX) + return (alloc_fail); + if (vec->capacity > SIZE_MAX / 2) + { + if (ft_vec_reserve(vec, SIZE_MAX)) + return (alloc_fail); + } + else if (vec->capacity == 0) + { + if (ft_vec_reserve(vec, V_DEFAULT_CAPACITY)) + return (alloc_fail); + } + else + { + if (ft_vec_reserve(vec, vec->capacity * 2)) + return (alloc_fail); + } + return (success); +} diff --git a/Libft/ft_arr/ft_vec_erase.c b/Libft/ft_arr/ft_vec_erase.c new file mode 100644 index 0000000..c81b95b --- /dev/null +++ b/Libft/ft_arr/ft_vec_erase.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_erase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:10:39 by ljiriste #+# #+# */ +/* Updated: 2024/01/11 10:53:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +t_arr_stat ft_vec_erase(t_vec *vec, size_t index, void (*free_el)(void *)) +{ + return (ft_vec_erase_range(vec, 1, index, free_el)); +} + +t_arr_stat ft_vec_erase_range(t_vec *vec, size_t count, size_t index, + void (*free_el)(void *)) +{ + char *p; + + if (free_el) + { + p = (char *)vec->vec + index * vec->el_size; + while (p < (char *)vec->vec + + (index + count) * vec->el_size) + { + free_el(p); + p += vec->el_size; + } + } + ft_vec_forget_range(vec, count, index); + return (success); +} diff --git a/Libft/ft_arr/ft_vec_find.c b/Libft/ft_arr/ft_vec_find.c new file mode 100644 index 0000000..93564ab --- /dev/null +++ b/Libft/ft_arr/ft_vec_find.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 11:04:05 by ljiriste #+# #+# */ +/* Updated: 2024/01/11 12:27:10 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_vec_find(t_vec *vec, void *wanted) +{ + size_t i; + + if (!vec || !wanted) + return (NULL); + i = 0; + while (i < vec->size) + { + if (!ft_memcmp(ft_vec_access(vec, i), wanted, vec->el_size)) + { + return (ft_vec_access(vec, i)); + } + ++i; + } + return (NULL); +} diff --git a/Libft/ft_arr/ft_vec_find_index.c b/Libft/ft_arr/ft_vec_find_index.c new file mode 100644 index 0000000..988acfc --- /dev/null +++ b/Libft/ft_arr/ft_vec_find_index.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_find_index.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 11:49:41 by ljiriste #+# #+# */ +/* Updated: 2024/01/12 17:10:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index) +{ + if (!vec || !wanted || !index) + return (invalid_input); + wanted = ft_vec_find(vec, wanted); + if (wanted == NULL) + return (non_specific_failure); + *index = (size_t)(((char *)wanted - (char *)vec->vec) / vec->el_size); + return (success); +} diff --git a/Libft/ft_arr/ft_vec_forget.c b/Libft/ft_arr/ft_vec_forget.c new file mode 100644 index 0000000..7a47707 --- /dev/null +++ b/Libft/ft_arr/ft_vec_forget.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_forget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 10:11:45 by ljiriste #+# #+# */ +/* Updated: 2024/02/29 17:20:13 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include "libft.h" + +t_arr_stat ft_vec_forget(t_vec *vec, size_t index) +{ + return (ft_vec_forget_range(vec, 1, index)); +} + +t_arr_stat ft_vec_forget_range(t_vec *vec, size_t count, size_t index) +{ + if (!vec || index + count > vec->size || index > SIZE_MAX - count) + return (invalid_input); + vec->size -= count; + ft_memmove((char *)vec->vec + vec->el_size * index, + (char *)vec->vec + vec->el_size * (index + count), + vec->el_size * (vec->size - index)); + return (success); +} diff --git a/Libft/ft_arr/ft_vec_free.c b/Libft/ft_arr/ft_vec_free.c new file mode 100644 index 0000000..c2cdd80 --- /dev/null +++ b/Libft/ft_arr/ft_vec_free.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:37:13 by ljiriste #+# #+# */ +/* Updated: 2023/12/11 20:01:30 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +void ft_vec_free(t_vec *vec, void (*free_el)(void *)) +{ + size_t i; + + if (!vec) + return ; + i = 0; + if (free_el) + { + while (i < vec->size) + { + free_el((char *)vec->vec + i * vec->el_size); + ++i; + } + } + free(vec->vec); + ft_vec_init(vec, vec->el_size); + return ; +} diff --git a/Libft/ft_arr/ft_vec_init.c b/Libft/ft_arr/ft_vec_init.c new file mode 100644 index 0000000..0e270f7 --- /dev/null +++ b/Libft/ft_arr/ft_vec_init.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 16:04:26 by ljiriste #+# #+# */ +/* Updated: 2024/01/14 16:02:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include "libft.h" +#include + +t_arr_stat ft_vec_init(t_vec *vec, size_t el_size) +{ + if (el_size == 0 || !vec) + return (invalid_input); + vec->capacity = 0; + vec->size = 0; + vec->el_size = el_size; + vec->vec = NULL; + return (success); +} diff --git a/Libft/ft_arr/ft_vec_insert.c b/Libft/ft_arr/ft_vec_insert.c new file mode 100644 index 0000000..9458136 --- /dev/null +++ b/Libft/ft_arr/ft_vec_insert.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 16:50:57 by ljiriste #+# #+# */ +/* Updated: 2024/01/12 17:03:45 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include "libft.h" + +t_arr_stat ft_vec_insert(t_vec *vec, void const *element, size_t index) +{ + return (ft_vec_insert_range(vec, element, 1, index)); +} + +t_arr_stat ft_vec_insert_range(t_vec *vec, void const *element, + size_t count, size_t index) +{ + if (count == 0) + return (success); + if (!element || !vec || index > SIZE_MAX - count) + return (invalid_input); + while (vec->size == vec->capacity || index + count > vec->capacity) + { + if (ft_vec_enlarge(vec)) + return (alloc_fail); + } + if (index < vec->size) + ft_memmove((char *)vec->vec + vec->el_size * (index + count), + (char *)vec->vec + vec->el_size * index, + vec->el_size * (vec->size - index)); + ft_memmove((char *)vec->vec + vec->el_size * index, element, + vec->el_size * count); + if (index > vec->size) + vec->size = index; + vec->size += count; + return (success); +} diff --git a/Libft/ft_arr/ft_vec_is_equal.c b/Libft/ft_arr/ft_vec_is_equal.c new file mode 100644 index 0000000..5b62efb --- /dev/null +++ b/Libft/ft_arr/ft_vec_is_equal.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_is_equal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 14:07:57 by ljiriste #+# #+# */ +/* Updated: 2024/07/04 15:34:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +int ft_vec_is_equal(const t_vec *vec1, const t_vec *vec2, + int (*cmp_elements)(const void *, const void *)) +{ + size_t i; + const void *el1; + const void *el2; + + if (vec1->size != vec2->size) + return (0); + i = 0; + while (i < vec1->size) + { + el1 = ft_vec_caccess(vec1, i); + el2 = ft_vec_caccess(vec2, i); + if (cmp_elements(el1, el2) != 0) + return (0); + ++i; + } + return (1); +} diff --git a/Libft/ft_arr/ft_vec_is_setequal.c b/Libft/ft_arr/ft_vec_is_setequal.c new file mode 100644 index 0000000..34e8662 --- /dev/null +++ b/Libft/ft_arr/ft_vec_is_setequal.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_is_setequal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 13:45:07 by ljiriste #+# #+# */ +/* Updated: 2024/06/28 13:49:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +int ft_vec_is_setequal(const t_vec *first, const t_vec *second, + int (*cmp_elements)(const void *, const void *)) +{ + return (ft_vec_is_subset(first, second, cmp_elements) + && ft_vec_is_subset(second, first, cmp_elements)); +} diff --git a/Libft/ft_arr/ft_vec_is_subset.c b/Libft/ft_arr/ft_vec_is_subset.c new file mode 100644 index 0000000..a6727da --- /dev/null +++ b/Libft/ft_arr/ft_vec_is_subset.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_is_subset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 13:35:18 by ljiriste #+# #+# */ +/* Updated: 2024/06/28 13:50:31 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +int ft_vec_is_subset(const t_vec *subset, const t_vec *set, + int (*cmp_elements)(const void *, const void *)) +{ + size_t i; + const void *element; + + i = 0; + while (i < subset->size) + { + element = ft_vec_caccess(subset, i); + if (!ft_vec_contains(set, element, cmp_elements)) + return (0); + ++i; + } + return (1); +} diff --git a/Libft/ft_arr/ft_vec_reserve.c b/Libft/ft_arr/ft_vec_reserve.c new file mode 100644 index 0000000..1160785 --- /dev/null +++ b/Libft/ft_arr/ft_vec_reserve.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_reserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 16:17:46 by ljiriste #+# #+# */ +/* Updated: 2023/12/11 10:21:49 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include "libft.h" +#include + +t_arr_stat ft_vec_reserve(t_vec *vec, size_t capacity) +{ + void *tmp; + + if (!vec) + return (invalid_input); + if (vec->capacity >= capacity) + return (invalid_input); + tmp = vec->vec; + vec->vec = ft_calloc(capacity, vec->el_size); + if (!vec->vec) + { + vec->vec = tmp; + return (alloc_fail); + } + vec->capacity = capacity; + ft_memcpy(vec->vec, tmp, vec->size * vec->el_size); + free(tmp); + return (success); +} diff --git a/Libft/ft_arr/ft_vec_setinsert.c b/Libft/ft_arr/ft_vec_setinsert.c new file mode 100644 index 0000000..807a700 --- /dev/null +++ b/Libft/ft_arr/ft_vec_setinsert.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_setinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/04 09:52:19 by ljiriste #+# #+# */ +/* Updated: 2024/07/04 10:23:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +t_ft_stat ft_vec_setinsert(t_vec *vec, const void *el, + int (*cmp_elements)(const void *, const void *)) +{ + if (ft_vec_contains(vec, el, cmp_elements)) + return (already_inside); + return (ft_vec_append(vec, el)); +} diff --git a/Libft/ft_check/ft_isalnum.c b/Libft/ft_check/ft_isalnum.c new file mode 100644 index 0000000..d596a2c --- /dev/null +++ b/Libft/ft_check/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:02:16 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isalnum(int c) +{ + return (ft_isdigit(c) || ft_isalpha(c)); +} diff --git a/Libft/ft_check/ft_isalpha.c b/Libft/ft_check/ft_isalpha.c new file mode 100644 index 0000000..af5737f --- /dev/null +++ b/Libft/ft_check/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:32:19 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isalpha(int c) +{ + return (ft_isupper(c) || ft_islower(c)); +} diff --git a/Libft/ft_check/ft_isascii.c b/Libft/ft_check/ft_isascii.c new file mode 100644 index 0000000..f8971c9 --- /dev/null +++ b/Libft/ft_check/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:19:40 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isascii(int c) +{ + return (0x00 <= c && c <= 0x7F); +} diff --git a/Libft/ft_check/ft_isdigit.c b/Libft/ft_check/ft_isdigit.c new file mode 100644 index 0000000..ae10fdc --- /dev/null +++ b/Libft/ft_check/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:32:47 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isdigit(int c) +{ + return ('0' <= c && c <= '9'); +} diff --git a/Libft/ft_check/ft_isint.c b/Libft/ft_check/ft_isint.c new file mode 100644 index 0000000..09afa3a --- /dev/null +++ b/Libft/ft_check/ft_isint.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/10 12:44:56 by ljiriste #+# #+# */ +/* Updated: 2024/04/25 10:26:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" +#include "libft.h" +#include + +// Checks whether str is a "pure" int +// that is whether the string only contains digits +// and whether it fits inside int +int ft_isint(const char *str) +{ + const int num = ft_atoi(str); + char *newstr; + int sign; + int res; + + sign = 1; + newstr = ft_itoa(num); + while (ft_isspace(*str)) + ++str; + while (*str == '+' || *str == '-') + { + if (*str == '-') + sign *= -1; + ++str; + } + while (*str == '0' && *(str + 1)) + ++str; + if (sign == -1) + res = !ft_strcmp(str, newstr + 1); + else + res = !ft_strcmp(str, newstr); + free(newstr); + return (res); +} diff --git a/Libft/ft_check/ft_islower.c b/Libft/ft_check/ft_islower.c new file mode 100644 index 0000000..60078ac --- /dev/null +++ b/Libft/ft_check/ft_islower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_islower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:31:47 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_islower(int c) +{ + return ('a' <= c && c <= 'z'); +} diff --git a/Libft/ft_check/ft_isprint.c b/Libft/ft_check/ft_isprint.c new file mode 100644 index 0000000..9f116ba --- /dev/null +++ b/Libft/ft_check/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:17:32 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isprint(int c) +{ + return (0x20 <= c && c <= 0x7E); +} diff --git a/Libft/ft_check/ft_isspace.c b/Libft/ft_check/ft_isspace.c new file mode 100644 index 0000000..02948c1 --- /dev/null +++ b/Libft/ft_check/ft_isspace.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:29:54 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isspace(int c) +{ + return (c == ' ' || ('\t' <= c && c <= '\r')); +} diff --git a/Libft/ft_check/ft_isupper.c b/Libft/ft_check/ft_isupper.c new file mode 100644 index 0000000..2a426c2 --- /dev/null +++ b/Libft/ft_check/ft_isupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:31:05 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:20:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" + +int ft_isupper(int c) +{ + return ('A' <= c && c <= 'Z'); +} diff --git a/Libft/ft_conv/ft_atoi.c b/Libft/ft_conv/ft_atoi.c new file mode 100644 index 0000000..ec7dbde --- /dev/null +++ b/Libft/ft_conv/ft_atoi.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/13 13:44:39 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:54:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include "libft.h" + +int ft_atoi(const char *nptr) +{ + int res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} + +long ft_atol(const char *nptr) +{ + long res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} + +long long ft_atoll(const char *nptr) +{ + long long res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} diff --git a/Libft/ft_conv/ft_ctoa.c b/Libft/ft_conv/ft_ctoa.c new file mode 100644 index 0000000..e10520f --- /dev/null +++ b/Libft/ft_conv/ft_ctoa.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ctoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:55:17 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:44:01 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include //malloc + +char *ft_ctoa(char c) +{ + char *res; + + res = malloc(2 * sizeof(char)); + if (res == NULL) + return (res); + res[0] = c; + res[1] = '\0'; + return (res); +} diff --git a/Libft/ft_conv/ft_itoa.c b/Libft/ft_conv/ft_itoa.c new file mode 100644 index 0000000..dbf4bf3 --- /dev/null +++ b/Libft/ft_conv/ft_itoa.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:58:10 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:44:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include + +static unsigned int size_needed(int n) +{ + unsigned int res; + + res = 1; + if (n == 0) + return (2); + if (n < 0) + ++res; + while (n != 0) + { + ++res; + n /= 10; + } + return (res); +} + +char *ft_itoa(int n) +{ + unsigned int size; + char *res; + + size = size_needed(n); + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + res[--size] = '\0'; + if (n == 0 && res) + res[0] = '0'; + else if (n < 0 && res) + { + res[0] = '-'; + res[--size] = '0' - (n % 10); + n = -(n / 10); + } + while (n > 0 && res) + { + res[--size] = '0' + (n % 10); + n /= 10; + } + return (res); +} diff --git a/Libft/ft_conv/ft_itoa_base.c b/Libft/ft_conv/ft_itoa_base.c new file mode 100644 index 0000000..935f217 --- /dev/null +++ b/Libft/ft_conv/ft_itoa_base.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:53:11 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include "libft.h" +#include //malloc +#include //intmax_t + +static intmax_t abs_max(intmax_t n) +{ + if (n < 0) + return (-n); + else + return (n); +} + +static int size_needed(intmax_t n, size_t base_len) +{ + int res; + + if (n == 0) + return (2); + res = 1; + if (n < 0) + ++res; + while (n != 0) + { + ++res; + n /= base_len; + } + return (res); +} + +static int valid_base(const char *base) +{ + int i; + int j; + + if (ft_strlen(base) < 2) + return (0); + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + ++i; + } + return (1); +} + +char *ft_itoa_base(intmax_t n, const char *base) +{ + int size; + int base_len; + char *res; + + if (!valid_base(base)) + return (NULL); + base_len = ft_strlen(base); + size = size_needed(n, base_len); + res = malloc(size); + if (res == NULL) + return (res); + if (n < 0) + res[0] = '-'; + res[--size] = '\0'; + if (n == 0) + res[0] = base[0]; + while (n != 0) + { + res[--size] = base[abs_max(n % base_len)]; + n /= base_len; + } + return (res); +} diff --git a/Libft/ft_conv/ft_tolower.c b/Libft/ft_conv/ft_tolower.c new file mode 100644 index 0000000..b8a5b90 --- /dev/null +++ b/Libft/ft_conv/ft_tolower.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:36:54 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:53:39 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include "libft.h" + +int ft_tolower(int c) +{ + if (ft_isupper(c)) + c = c + 'a' - 'A'; + return (c); +} diff --git a/Libft/ft_conv/ft_toupper.c b/Libft/ft_conv/ft_toupper.c new file mode 100644 index 0000000..48f7567 --- /dev/null +++ b/Libft/ft_conv/ft_toupper.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:35:21 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:54:01 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include "libft.h" + +int ft_toupper(int c) +{ + if (ft_islower(c)) + c = c + 'A' - 'a'; + return (c); +} diff --git a/Libft/ft_conv/ft_uitoa_base.c b/Libft/ft_conv/ft_uitoa_base.c new file mode 100644 index 0000000..4b29e05 --- /dev/null +++ b/Libft/ft_conv/ft_uitoa_base.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_uitoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:54:44 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_conv.h" +#include "libft.h" +#include //malloc +#include //intmax_t + +static int size_needed(uintmax_t n, size_t base_len) +{ + int res; + + if (n == 0) + return (2); + res = 1; + while (n != 0) + { + ++res; + n /= base_len; + } + return (res); +} + +static int valid_base(const char *base) +{ + int i; + int j; + + if (ft_strlen(base) < 2) + return (0); + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + ++i; + } + return (1); +} + +char *ft_uitoa_base(uintmax_t n, const char *base) +{ + int size; + int base_len; + char *res; + + if (!valid_base(base)) + return (NULL); + base_len = ft_strlen(base); + size = size_needed(n, base_len); + res = malloc(size); + if (res == NULL) + return (res); + res[--size] = '\0'; + if (n == 0) + res[0] = base[0]; + while (n != 0) + { + res[--size] = base[n % base_len]; + n /= base_len; + } + return (res); +} diff --git a/Libft/ft_gen/ft_swap.c b/Libft/ft_gen/ft_swap.c new file mode 100644 index 0000000..0270d42 --- /dev/null +++ b/Libft/ft_gen/ft_swap.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 13:59:28 by ljiriste #+# #+# */ +/* Updated: 2024/07/21 21:36:34 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_gen.h" + +void ft_swap_int(int *a, int *b) +{ + int tmp; + + tmp = *a; + *a = *b; + *b = tmp; + return ; +} + +void ft_swap(void **a, void **b) +{ + void *tmp; + + tmp = *a; + *a = *b; + *b = tmp; + return ; +} diff --git a/Libft/ft_io/ft_printf/conversion.c b/Libft/ft_io/ft_printf/conversion.c new file mode 100644 index 0000000..3b7b082 --- /dev/null +++ b/Libft/ft_io/ft_printf/conversion.c @@ -0,0 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* conversion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 11:30:56 by ljiriste #+# #+# */ +/* Updated: 2024/03/05 09:16:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "libft.h" +#include // NULL, free +#include //va* + +static char *base_str_constr(char type, va_list *args) +{ + char *res; + + res = NULL; + if (type == 'd' || type == 'i') + res = ft_itoa(va_arg(*args, int)); + else if (type == 'o') + res = ft_uitoa_base(va_arg(*args, unsigned int), "01234567"); + else if (type == 'u') + res = ft_uitoa_base(va_arg(*args, unsigned int), "0123456789"); + else if (type == 'x') + res = ft_uitoa_base(va_arg(*args, unsigned int), "0123456789abcdef"); + else if (type == 'X') + res = ft_uitoa_base(va_arg(*args, unsigned int), "0123456789ABCDEF"); + else if (type == 'c') + res = ft_ctoa(va_arg(*args, int)); + else if (type == 's') + res = ft_strdup(va_arg(*args, char *)); + else if (type == 'p') + res = ft_uitoa_base((uintptr_t)va_arg(*args, void *), + "0123456789abcdef"); + return (res); +} + +static int valid_toprint(t_to_print tp) +{ + return (tp.left_pad && tp.zero_pad && tp.main_part && tp.right_pad); +} + +static size_t to_print_len(t_to_print *tp) +{ + size_t len; + + len = 0; + len += ft_strlen(tp->left_pad); + len += (tp->sign != '\0'); + len += ft_strlen(tp->alt); + len += ft_strlen(tp->zero_pad); + len += ft_strlen(tp->main_part); + len += ft_strlen(tp->right_pad); + return (len); +} + +static int tp_print_and_free_members(int fd, t_to_print *tp, t_conv conv) +{ + int len; + + len = to_print_len(tp); + ft_putstr_fd(tp->left_pad, fd); + if (tp->sign != '\0') + ft_putchar_fd(tp->sign, fd); + ft_putstr_fd(tp->alt, fd); + ft_putstr_fd(tp->zero_pad, fd); + if (*(tp->main_part) == '\0' && conv.type == 'c') + { + ft_putchar_fd('\0', fd); + ++len; + } + else + ft_putstr_fd(tp->main_part, fd); + ft_putstr_fd(tp->right_pad, fd); + free(tp->left_pad); + free(tp->alt); + free(tp->zero_pad); + free(tp->main_part); + free(tp->right_pad); + return (len); +} + +int handle_conversion(int fd, const char **format, va_list *args) +{ + t_conv conv; + char *temp; + t_to_print to_print; + + ++(*format); + conv = parse_format(format, args); + if (conv.type == '%') + { + ft_putchar_fd('%', 1); + return (1); + } + temp = base_str_constr(conv.type, args); + to_print = formatting(&temp, conv); + free(temp); + if (!valid_toprint(to_print)) + return (-1); + return (tp_print_and_free_members(fd, &to_print, conv)); +} diff --git a/Libft/ft_io/ft_printf/formatting.c b/Libft/ft_io/ft_printf/formatting.c new file mode 100644 index 0000000..8c60dcb --- /dev/null +++ b/Libft/ft_io/ft_printf/formatting.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* formatting.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 11:28:21 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:51:30 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "libft.h" +#include // NULL, free +#include + +static int starts_with_zero(t_to_print *tp, t_conv conv) +{ + return ((intmax_t)ft_strlen(tp->main_part) < conv.prec); +} + +static void create_alt(t_to_print *tp, t_conv conv) +{ + if (conv.type == 'p' && *(tp->main_part) != '(') + tp->alt = ft_strdup("0x"); + else if (conv.type == 's' || conv.type == 'c' + || ft_strncmp(tp->main_part, "0", 2) == 0 + || ((*(tp->main_part) == '\0' || !conv.flags.alt_mode) + && conv.type != 'o')) + tp->alt = ft_strdup(""); + else if (conv.flags.alt_mode) + { + if (conv.type == 'x') + tp->alt = ft_strdup("0x"); + else if (conv.type == 'X') + tp->alt = ft_strdup("0X"); + else if (conv.type == 'o' && !starts_with_zero(tp, conv)) + tp->alt = ft_strdup("0"); + else + tp->alt = ft_strdup(""); + } + else + tp->alt = ft_strdup(""); + return ; +} + +static void create_main(char *str, t_to_print *tp, t_conv conv) +{ + if (conv.type != 's' && conv.type != 'c' + && ft_strncmp(str, "0", 2) == 0 && conv.prec == 0) + tp->main_part = ft_strdup(""); + else if (conv.type == 'd' || conv.type == 'i') + { + if (*str == '-') + { + tp->sign = '-'; + tp->main_part = ft_strdup(str + 1); + } + else + { + tp->main_part = ft_strdup(str); + if (conv.flags.sign_show) + tp->sign = '+'; + else if (conv.flags.sign_allign) + tp->sign = ' '; + } + } + else if (conv.type == 's' + && (size_t)conv.prec < ft_strlen(str) && conv.prec >= 0) + tp->main_part = ft_strndup(str, conv.prec); + else + tp->main_part = ft_strdup(str); + return ; +} + +static void init_printed(t_to_print *tp) +{ + tp->left_pad = NULL; + tp->sign = '\0'; + tp->zero_pad = NULL; + tp->main_part = NULL; + tp->right_pad = NULL; + return ; +} + +t_to_print formatting(char **str, t_conv conv) +{ + t_to_print res; + + if ((conv.type == 'p' && ft_strncmp(*str, "0", 2) == 0)) + { + free (*str); + *str = ft_strdup("(nil)"); + } + else if (conv.type == 's' && *str == NULL + && (conv.prec >= 6 || conv.prec < 0)) + *str = ft_strdup("(null)"); + else if (conv.type == 's' && *str == NULL) + *str = ft_strdup(""); + init_printed(&res); + create_main(*str, &res, conv); + create_alt(&res, conv); + create_padding(&res, conv); + return (res); +} diff --git a/Libft/ft_io/ft_printf/ft_printf.c b/Libft/ft_io/ft_printf/ft_printf.c new file mode 100644 index 0000000..d131df1 --- /dev/null +++ b/Libft/ft_io/ft_printf/ft_printf.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */ +/* Updated: 2024/03/05 09:21:56 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include // va_* +#include // write + +static int print_ordinary(int fd, const char **s) +{ + int len; + + len = 0; + while ((*s)[len] && (*s)[len] != '%') + ++len; + write(fd, *s, len); + *s += len; + return (len); +} + +int ft_vdprintf(int fd, const char *format, va_list *args) +{ + int res; + + if (fd < 0 || write(fd, NULL, 0) == -1) + return (0); + res = 0; + while (*format) + { + res += print_ordinary(fd, &format); + if (!*format) + break ; + res += handle_conversion(fd, &format, args); + } + return (res); +} + +int ft_dprintf(int fd, const char *format, ...) +{ + va_list args; + int res; + + va_start(args, format); + res = ft_vdprintf(fd, format, &args); + va_end(args); + return (res); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int res; + + va_start(args, format); + res = ft_vdprintf(STDOUT_FILENO, format, &args); + va_end(args); + return (res); +} diff --git a/Libft/ft_io/ft_printf/ft_printf.h b/Libft/ft_io/ft_printf/ft_printf.h new file mode 100644 index 0000000..cf63696 --- /dev/null +++ b/Libft/ft_io/ft_printf/ft_printf.h @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 12:00:16 by ljiriste #+# #+# */ +/* Updated: 2024/03/05 09:15:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include + +typedef struct s_flags +{ + int alt_mode; + int zero_pad; + int left_adjust; + int sign_allign; + int sign_show; +} t_flags; + +typedef struct s_conv +{ + t_flags flags; + int minwidth; + int prec; + char type; +} t_conv; + +typedef struct s_to_print +{ + char *left_pad; + char sign; + char *alt; + char *zero_pad; + char *main_part; + char *right_pad; +} t_to_print; + +int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); +int ft_vdprintf(int fd, const char *format, va_list *args); +int handle_conversion(int fd, const char **format, va_list *args); +t_to_print formatting(char **str, t_conv conv); +t_conv parse_format(const char **format, va_list *args); +void create_padding(t_to_print *tp, t_conv conv); + +#endif diff --git a/Libft/ft_io/ft_printf/padding.c b/Libft/ft_io/ft_printf/padding.c new file mode 100644 index 0000000..057cd14 --- /dev/null +++ b/Libft/ft_io/ft_printf/padding.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* padding.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 11:25:46 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:50:15 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "libft.h" +#include //malloc + +static char *repeat_char(char c, int n) +{ + char *res; + + if (n < 0) + n = 0; + res = malloc(++n); + if (res == NULL) + return (res); + res[--n] = '\0'; + while (n > 0) + res[--n] = c; + return (res); +} + +static void lengthen_by_zeros(char **str, int n) +{ + char *temp; + size_t size; + + if (n <= 0) + return ; + temp = *str; + size = ft_strlen(temp) + n + 1; + *str = malloc(size); + if (*str == NULL) + { + free(temp); + return ; + } + ft_strlcpy(*str, temp, size); + free(temp); + while (n > 0) + (*str)[size - (n--) - 1] = '0'; + (*str)[size - 1] = '\0'; + return ; +} + +void create_padding(t_to_print *tp, t_conv cv) +{ + size_t len; + + len = ft_strlen(tp->main_part) + (!tp->main_part[0] && cv.type == 'c'); + if (cv.type == 's' || cv.type == 'c') + tp->zero_pad = ft_strdup(""); + else + tp->zero_pad = repeat_char('0', cv.prec - len); + len += ft_strlen(tp->zero_pad) + ft_strlen(tp->alt) + (tp->sign != '\0'); + if (cv.flags.left_adjust) + { + tp->right_pad = repeat_char(' ', cv.minwidth - len); + tp->left_pad = ft_strdup(""); + } + else + { + tp->right_pad = ft_strdup(""); + if (cv.flags.zero_pad && cv.prec < 0 && ft_strchr("diouxX", cv.type)) + { + tp->left_pad = ft_strdup(""); + lengthen_by_zeros(&(tp->zero_pad), cv.minwidth - len); + } + else + tp->left_pad = repeat_char(' ', cv.minwidth - len); + } + return ; +} diff --git a/Libft/ft_io/ft_printf/parsing.c b/Libft/ft_io/ft_printf/parsing.c new file mode 100644 index 0000000..660fcc0 --- /dev/null +++ b/Libft/ft_io/ft_printf/parsing.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 10:50:54 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:50:30 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "libft.h" +#include // va_* + +static t_flags parse_flags(const char **format) +{ + t_flags flags; + + flags.alt_mode = 0; + flags.zero_pad = 0; + flags.left_adjust = 0; + flags.sign_allign = 0; + flags.sign_show = 0; + while (1) + { + if (**format == '#') + flags.alt_mode = 1; + else if (**format == '0') + flags.zero_pad = 1; + else if (**format == '-') + flags.left_adjust = 1; + else if (**format == ' ') + flags.sign_allign = 1; + else if (**format == '+') + flags.sign_show = 1; + else + return (flags); + ++(*format); + } +} + +static int parse_int(const char **format, va_list *args) +{ + int res; + + res = 0; + if (**format == '*') + { + res = va_arg(*args, int); + ++(*format); + } + else if (**format == '-' || ft_isdigit(**format)) + { + res = ft_atoi(*format); + while (**format == '-' || ft_isdigit(**format)) + ++(*format); + } + return (res); +} + +t_conv parse_format(const char **format, va_list *args) +{ + t_conv conv; + + conv.flags = parse_flags(format); + conv.minwidth = parse_int(format, args); + if (**format == '.') + { + ++(*format); + conv.prec = parse_int(format, args); + } + else + conv.prec = -1; + conv.type = *((*format)++); + return (conv); +} diff --git a/Libft/ft_io/ft_putchar_fd.c b/Libft/ft_io/ft_putchar_fd.c new file mode 100644 index 0000000..dadcedf --- /dev/null +++ b/Libft/ft_io/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:20:18 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:42:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_io.h" +#include + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); + return ; +} diff --git a/Libft/ft_io/ft_putendl_fd.c b/Libft/ft_io/ft_putendl_fd.c new file mode 100644 index 0000000..23fa343 --- /dev/null +++ b/Libft/ft_io/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:25:41 by ljiriste #+# #+# */ +/* Updated: 2024/03/09 00:12:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_io.h" +#include + +void ft_putendl_fd(const char *s, int fd) +{ + ft_putstr_fd(s, fd); + write(fd, "\n", 1); + return ; +} diff --git a/Libft/ft_io/ft_putnbr_fd.c b/Libft/ft_io/ft_putnbr_fd.c new file mode 100644 index 0000000..1b510dd --- /dev/null +++ b/Libft/ft_io/ft_putnbr_fd.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:27:25 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:40:46 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_io.h" +#include + +void ft_putnbr_fd(int n, int fd) +{ + char digit; + + if (n < 0) + { + write(fd, "-", 1); + digit = '0' - (n % 10); + if (n / 10 != 0) + ft_putnbr_fd(-(n / 10), fd); + } + else if (n < 10) + digit = '0' + n; + else + { + digit = '0' + n % 10; + ft_putnbr_fd(n / 10, fd); + } + write(fd, &digit, 1); + return ; +} diff --git a/Libft/ft_io/ft_putstr_fd.c b/Libft/ft_io/ft_putstr_fd.c new file mode 100644 index 0000000..9158b12 --- /dev/null +++ b/Libft/ft_io/ft_putstr_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:23:36 by ljiriste #+# #+# */ +/* Updated: 2024/03/08 22:29:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_io.h" +#include "libft.h" +#include + +void ft_putstr_fd(const char *s, int fd) +{ + if (s == NULL) + return ; + write(fd, s, ft_strlen(s)); + return ; +} diff --git a/Libft/ft_io/get_next_line.c b/Libft/ft_io/get_next_line.c new file mode 100644 index 0000000..1523988 --- /dev/null +++ b/Libft/ft_io/get_next_line.c @@ -0,0 +1,145 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/28 00:01:15 by ljiriste #+# #+# */ +/* Updated: 2024/02/17 00:05:19 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include "ft_io.h" +#include "libft.h" +#include +#include + +t_list *new_ft_node(int fd) +{ + t_list *node; + t_ft *thread; + + thread = malloc(sizeof(t_ft)); + if (thread == NULL) + return (NULL); + node = ft_lstnew(thread); + if (node == NULL) + { + free(thread); + return (NULL); + } + thread->buffer = malloc((BUFFER_SIZE + 1) * sizeof(char)); + if (thread->buffer == NULL) + { + free(thread); + free(node); + return (NULL); + } + (thread->buffer)[0] = '\0'; + thread->fd = fd; + return (node); +} + +static void delete_file_node(int fd, t_list **list) +{ + t_list *cur; + t_list *prev; + + prev = NULL; + cur = *list; + while (fd >= 0 && ((t_ft *)(cur->content))->fd != fd) + { + prev = cur; + cur = cur->next; + } + if (prev == NULL) + *list = cur->next; + else + prev->next = cur->next; + free(((t_ft *)(cur->content))->buffer); + free(cur->content); + free(cur); + return ; +} + +static char *select_file_buffer(int fd, t_list **list) +{ + t_list *local_head; + + if (fd < 0) + { + while (*list != NULL) + delete_file_node(-1, list); + return (NULL); + } + if (*list == NULL) + { + *list = new_ft_node(fd); + if (list == NULL) + return (NULL); + return (((t_ft *)((*list)->content))->buffer); + } + local_head = *list; + while (local_head->next && ((t_ft *)(local_head->content))->fd != fd) + local_head = local_head->next; + if (((t_ft *)(local_head->content))->fd == fd) + return (((t_ft *)(local_head->content))->buffer); + local_head->next = new_ft_node(fd); + if (local_head->next == NULL) + return (NULL); + return (((t_ft *)(local_head->next->content))->buffer); +} + +/* Concatenates res and buffer up to newline. + Shifts buffer by the transfered characters. + Returns 0 when buffer is empty (and needs filling with read). + Also returns 1 when newline is transfered + unless buffer empty (to delete node). + Otherwise returns true. +*/ +static int transfer_string(char **res, char *buffer) +{ + int i; + int nl; + + i = 0; + while (buffer[i] && buffer[i] != '\n') + ++i; + nl = (buffer[i] == '\n'); + i += nl; + ft_strncat_alloc(res, buffer, i); + ft_memmove(buffer, buffer + i, BUFFER_SIZE + 1 - i); + return (buffer[0] || nl); +} + +// Passing negative fd causes every buffer to close +char *get_next_line(int fd) +{ + static t_list *list; + char *buffer; + char *res; + int i; + + res = NULL; + buffer = select_file_buffer(fd, &list); + while (buffer && !transfer_string(&res, buffer)) + { + if (buffer[0] == '\0') + { + i = read(fd, buffer, BUFFER_SIZE); + if (i <= 0) + delete_file_node(fd, &list); + if (i < 0) + { + free(res); + return (NULL); + } + if (i == 0) + return (res); + buffer[i] = '\0'; + } + } + return (res); +} diff --git a/Libft/ft_io/get_next_line.h b/Libft/ft_io/get_next_line.h new file mode 100644 index 0000000..2ddfa98 --- /dev/null +++ b/Libft/ft_io/get_next_line.h @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/28 11:24:52 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 80 +# endif + +# include + +typedef struct s_file_thread +{ + int fd; + char *buffer; +} t_ft; + +#endif diff --git a/Libft/ft_lst/ft_lst_at.c b/Libft/ft_lst/ft_lst_at.c new file mode 100644 index 0000000..08836f3 --- /dev/null +++ b/Libft/ft_lst/ft_lst_at.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_at.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:19:21 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" + +t_list *ft_lst_at(t_list *lst, unsigned int i) +{ + unsigned int j; + + j = 0; + while (j < i && lst) + { + lst = lst->next; + ++j; + } + return (lst); +} diff --git a/Libft/ft_lst/ft_lst_find.c b/Libft/ft_lst/ft_lst_find.c new file mode 100644 index 0000000..86a8ca0 --- /dev/null +++ b/Libft/ft_lst/ft_lst_find.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:47:42 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +t_list *ft_lst_find(t_list *lst, void *content_ref, int (*cmp)()) +{ + while (lst) + { + if (cmp(lst->content, content_ref) == 0) + return (lst); + lst = lst->next; + } + return (NULL); +} diff --git a/Libft/ft_lst/ft_lst_foreach_if.c b/Libft/ft_lst/ft_lst_foreach_if.c new file mode 100644 index 0000000..d333fe2 --- /dev/null +++ b/Libft/ft_lst/ft_lst_foreach_if.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_foreach_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:42:33 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" + +void ft_lst_foreach_if(t_list *lst, void (*f)(void *), + void *content_ref, int (*cmp)()) +{ + while (lst) + { + if (cmp(lst->content, content_ref) == 0) + f(lst->content); + lst = lst->next; + } + return ; +} diff --git a/Libft/ft_lst/ft_lst_merge.c b/Libft/ft_lst/ft_lst_merge.c new file mode 100644 index 0000000..f79a004 --- /dev/null +++ b/Libft/ft_lst/ft_lst_merge.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:09:00 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" + +void ft_lst_merge(t_list **lst1, t_list *lst2) +{ + t_list *last; + + if (!*lst1) + { + *lst1 = lst2; + return ; + } + last = ft_lstlast(*lst1); + last->next = lst2; + return ; +} diff --git a/Libft/ft_lst/ft_lst_remove_if.c b/Libft/ft_lst/ft_lst_remove_if.c new file mode 100644 index 0000000..64bd2bd --- /dev/null +++ b/Libft/ft_lst/ft_lst_remove_if.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_remove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:49:53 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +static void remove_next_el(t_list *prev, t_list **cur, void (*free_fct)(void *)) +{ + prev->next = (*cur)->next; + free_fct((*cur)->content); + free(*cur); + *cur = prev->next; + return ; +} + +void ft_lst_remove_if(t_list **lst, void *content_ref, + int (*cmp)(), void (*free_fct)(void *)) +{ + t_list *prev; + + prev = NULL; + while (*lst) + { + if (cmp((*lst)->content, content_ref) == 0) + remove_next_el(prev, lst, free_fct); + else + *lst = (*lst)->next; + } + return ; +} diff --git a/Libft/ft_lst/ft_lst_reverse.c b/Libft/ft_lst/ft_lst_reverse.c new file mode 100644 index 0000000..a77dad8 --- /dev/null +++ b/Libft/ft_lst/ft_lst_reverse.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:22:47 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +void ft_lst_reverse(t_list **lst) +{ + t_list *prev; + t_list *current; + t_list *next; + + prev = NULL; + current = *lst; + while (current) + { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + *lst = prev; + return ; +} diff --git a/Libft/ft_lst/ft_lst_sort.c b/Libft/ft_lst/ft_lst_sort.c new file mode 100644 index 0000000..40f3499 --- /dev/null +++ b/Libft/ft_lst/ft_lst_sort.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:13:29 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:55:45 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" +#include "libft.h" + +static void ft_max_to_end(t_list *lst, int (*cmp)(), int size) +{ + int i; + + i = 0; + while (i < size - 1) + { + if (cmp(lst->content, lst->next->content) > 0) + ft_swap(&(lst->content), &(lst->next->content)); + lst = lst->next; + } + return ; +} + +// Bubble sort +void ft_lst_sort(t_list **lst, int (*cmp)()) +{ + int size; + int i; + + size = ft_lstsize(*lst); + i = 0; + while (i < size) + { + ft_max_to_end(*lst, cmp, size - i); + ++i; + } + return ; +} diff --git a/Libft/ft_lst/ft_lst_sorted_insert.c b/Libft/ft_lst/ft_lst_sorted_insert.c new file mode 100644 index 0000000..5705a3f --- /dev/null +++ b/Libft/ft_lst/ft_lst_sorted_insert.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sorted_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:53:26 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +static void insert_after(t_list *prev, t_list *new) +{ + if (!new) + return ; + new->next = prev->next; + prev->next = new; + return ; +} + +void ft_lst_sorted_insert(t_list **lst, t_list *new, int (*cmp)()) +{ + t_list *prev; + t_list *cur; + + if (!lst || !new) + return ; + if (!*lst) + { + *lst = new; + return ; + } + prev = NULL; + cur = *lst; + while (cur) + { + if (cmp(new->content, cur->content) <= 0) + break ; + prev = cur; + cur = cur->next; + } + if (prev) + insert_after(prev, new); + else + ft_lstadd_front(lst, new); + return ; +} diff --git a/Libft/ft_lst/ft_lst_sorted_merge.c b/Libft/ft_lst/ft_lst_sorted_merge.c new file mode 100644 index 0000000..f80caa7 --- /dev/null +++ b/Libft/ft_lst/ft_lst_sorted_merge.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sorted_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 17:13:15 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:55:15 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" +#include "libft.h" + +void ft_lst_sorted_merge(t_list **lst1, t_list *lst2, int (*cmp)()) +{ + t_list *main; + + if (cmp((*lst1)->content, lst2->content) > 0) + { + main = lst2; + lst2 = *lst1; + *lst1 = main; + } + else + main = *lst1; + while (main->next) + { + if (cmp(main->next->content, lst2->content) > 0) + ft_swap((void **)&main, (void **)&lst2); + main = main->next; + } + main->next = lst2; + return ; +} diff --git a/Libft/ft_lst/ft_lstadd_back.c b/Libft/ft_lst/ft_lstadd_back.c new file mode 100644 index 0000000..f127224 --- /dev/null +++ b/Libft/ft_lst/ft_lstadd_back.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:33:28 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + if (!new) + return ; + if (*lst == NULL) + { + *lst = new; + return ; + } + ft_lstlast(*lst)->next = new; + return ; +} diff --git a/Libft/ft_lst/ft_lstadd_front.c b/Libft/ft_lst/ft_lstadd_front.c new file mode 100644 index 0000000..55ca676 --- /dev/null +++ b/Libft/ft_lst/ft_lstadd_front.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:26:20 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (!new) + return ; + new->next = *lst; + *lst = new; + return ; +} diff --git a/Libft/ft_lst/ft_lstclear.c b/Libft/ft_lst/ft_lstclear.c new file mode 100644 index 0000000..a3b83a6 --- /dev/null +++ b/Libft/ft_lst/ft_lstclear.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:45:28 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *temp; + + if (lst == NULL || del == NULL) + return ; + while (*lst) + { + temp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = temp; + } + return ; +} diff --git a/Libft/ft_lst/ft_lstdelone.c b/Libft/ft_lst/ft_lstdelone.c new file mode 100644 index 0000000..35289e8 --- /dev/null +++ b/Libft/ft_lst/ft_lstdelone.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:43:02 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (lst == NULL || del == NULL) + return ; + del(lst->content); + free(lst); + return ; +} diff --git a/Libft/ft_lst/ft_lstiter.c b/Libft/ft_lst/ft_lstiter.c new file mode 100644 index 0000000..9960fe5 --- /dev/null +++ b/Libft/ft_lst/ft_lstiter.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:50:31 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_lst.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + f(lst->content); + lst = lst->next; + } + return ; +} diff --git a/Libft/ft_lst/ft_lstlast.c b/Libft/ft_lst/ft_lstlast.c new file mode 100644 index 0000000..d827d38 --- /dev/null +++ b/Libft/ft_lst/ft_lstlast.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:31:01 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (lst); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/Libft/ft_lst/ft_lstmap.c b/Libft/ft_lst/ft_lstmap.c new file mode 100644 index 0000000..15efc8e --- /dev/null +++ b/Libft/ft_lst/ft_lstmap.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:51:57 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +static t_list *first_el(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + void *cont; + + if (lst == NULL) + return (NULL); + cont = f(lst->content); + res = ft_lstnew(cont); + if (res == NULL) + del(cont); + return (res); +} + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + t_list *new_node; + void *new_cont; + + res = first_el(lst, f, del); + if (res == NULL) + return (res); + new_node = res; + lst = lst->next; + while (lst && res) + { + new_cont = f(lst->content); + new_node->next = ft_lstnew(new_cont); + new_node = new_node->next; + if (new_node == NULL) + { + del(new_cont); + ft_lstclear(&res, del); + } + lst = lst->next; + } + return (res); +} diff --git a/Libft/ft_lst/ft_lstnew.c b/Libft/ft_lst/ft_lstnew.c new file mode 100644 index 0000000..b902847 --- /dev/null +++ b/Libft/ft_lst/ft_lstnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:23:00 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +t_list *ft_lstnew(void *content) +{ + t_list *res; + + res = malloc(sizeof(t_list)); + if (res) + { + res->content = content; + res->next = NULL; + } + return (res); +} diff --git a/Libft/ft_lst/ft_lstsize.c b/Libft/ft_lst/ft_lstsize.c new file mode 100644 index 0000000..106602e --- /dev/null +++ b/Libft/ft_lst/ft_lstsize.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:28:58 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lst.h" + +int ft_lstsize(t_list *lst) +{ + int res; + + res = 0; + while (lst) + { + lst = lst->next; + ++res; + } + return (res); +} diff --git a/Libft/ft_math/ft_abs.c b/Libft/ft_math/ft_abs.c new file mode 100644 index 0000000..5fc5552 --- /dev/null +++ b/Libft/ft_math/ft_abs.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:48:21 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:23:57 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int ft_abs(int n) +{ + if (n < 0) + return (-n); + else + return (n); +} diff --git a/Libft/ft_math/ft_max.c b/Libft/ft_math/ft_max.c new file mode 100644 index 0000000..076c6e8 --- /dev/null +++ b/Libft/ft_math/ft_max.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_max.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/22 14:29:48 by ljiriste #+# #+# */ +/* Updated: 2024/02/23 08:57:26 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" +#include + +int ft_max(int a, int b) +{ + if (a > b) + return (a); + return (b); +} + +size_t ft_maxs(size_t a, size_t b) +{ + if (a > b) + return (a); + return (b); +} diff --git a/Libft/ft_math/ft_min.c b/Libft/ft_math/ft_min.c new file mode 100644 index 0000000..d09f05e --- /dev/null +++ b/Libft/ft_math/ft_min.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_min.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/23 08:58:39 by ljiriste #+# #+# */ +/* Updated: 2024/02/23 08:59:40 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" +#include + +int ft_min(int a, int b) +{ + if (a < b) + return (a); + return (b); +} + +size_t ft_mins(size_t a, size_t b) +{ + if (a < b) + return (a); + return (b); +} diff --git a/Libft/ft_math/ft_sgn.c b/Libft/ft_math/ft_sgn.c new file mode 100644 index 0000000..da3b0ae --- /dev/null +++ b/Libft/ft_math/ft_sgn.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sgn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/18 09:41:56 by ljiriste #+# #+# */ +/* Updated: 2024/01/30 18:09:32 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int ft_sgn(int n) +{ + return ((n > 0) - (n < 0)); +} + +/* +int ft_sgn(int n) +{ + if (n > 0) + return (1); + if (n < 0) + return (-1); + return (0); +} +*/ diff --git a/Libft/ft_mem/ft_bzero.c b/Libft/ft_mem/ft_bzero.c new file mode 100644 index 0000000..048354d --- /dev/null +++ b/Libft/ft_mem/ft_bzero.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:38:08 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_mem.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); + return ; +} diff --git a/Libft/ft_mem/ft_calloc.c b/Libft/ft_mem/ft_calloc.c new file mode 100644 index 0000000..6e4a0fa --- /dev/null +++ b/Libft/ft_mem/ft_calloc.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* This should not be commented as to emulate builtin calloc + but errno calls function which is deemed forbidden by Moulinette +#include + if (total_bytes / size != nmemb) + { + errno = ENOMEM; + return (NULL); + } +*/ +#include // for malloc +#include "ft_mem.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + size_t total_bytes; + void *res; + + if (nmemb == 0 || size == 0) + return (malloc(0)); + total_bytes = nmemb * size; + if (total_bytes / size != nmemb) + return (NULL); + res = malloc(total_bytes); + if (res == NULL) + return (NULL); + ft_memset(res, 0, total_bytes); + return (res); +} diff --git a/Libft/ft_mem/ft_memchr.c b/Libft/ft_mem/ft_memchr.c new file mode 100644 index 0000000..ec84ed5 --- /dev/null +++ b/Libft/ft_mem/ft_memchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:56:53 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_mem.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (*((unsigned char *)s + i) == (unsigned char)c) + return ((unsigned char *)s + i); + ++i; + } + return (NULL); +} diff --git a/Libft/ft_mem/ft_memcmp.c b/Libft/ft_mem/ft_memcmp.c new file mode 100644 index 0000000..f51279b --- /dev/null +++ b/Libft/ft_mem/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:06:15 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_mem.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + while (*((unsigned char *)s1 + i) == *((unsigned char *)s2 + i) + && i + 1 < n) + ++i; + if (n > 0) + return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); + else + return (0); +} diff --git a/Libft/ft_mem/ft_memcpy.c b/Libft/ft_mem/ft_memcpy.c new file mode 100644 index 0000000..fd58af4 --- /dev/null +++ b/Libft/ft_mem/ft_memcpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:35:59 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_mem.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + if (dest == NULL && src == NULL) + return (NULL); + while (n > 0) + { + --n; + *((unsigned char *)dest + n) = *((unsigned char *)src + n); + } + return (dest); +} diff --git a/Libft/ft_mem/ft_memmove.c b/Libft/ft_mem/ft_memmove.c new file mode 100644 index 0000000..ffd024b --- /dev/null +++ b/Libft/ft_mem/ft_memmove.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:36:32 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_mem.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t i; + + if (dest > src) + { + i = n; + while (i > 0) + { + --i; + *((unsigned char *)dest + i) = *((unsigned char *)src + i); + } + } + else if (dest < src) + { + i = 0; + while (i < n) + { + *((unsigned char *)dest + i) = *((unsigned char *)src + i); + ++i; + } + } + return (dest); +} diff --git a/Libft/ft_mem/ft_memset.c b/Libft/ft_mem/ft_memset.c new file mode 100644 index 0000000..30ce07a --- /dev/null +++ b/Libft/ft_mem/ft_memset.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:13:22 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_mem.h" + +void *ft_memset(void *s, int c, size_t n) +{ + while (n > 0) + *((unsigned char *)s + (--n)) = (unsigned char)c; + return (s); +} diff --git a/Libft/ft_parse/actions.c b/Libft/ft_parse/actions.c new file mode 100644 index 0000000..accc57c --- /dev/null +++ b/Libft/ft_parse/actions.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* actions.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 15:34:14 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:46:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "ft_parse.h" +#include "libft.h" +#include + +int push_state(t_stack *stack, size_t state_num, t_token token) +{ + t_parser_stack_element element; + + element.node = malloc(sizeof(*element.node)); + if (!element.node) + return (1); + ft_vec_init(&element.node->children, sizeof(t_parse_tree_node)); + element.node->token = ft_token_dup(token); + if (!element.node->token.type) + return (1); + element.state_num = state_num; + ft_stack_push(stack, &element); + return (0); +} + +static ssize_t goto_state(const t_parser_stack_element *el, + const t_parsing_table *table, t_token token) +{ + const t_parser_state *state; + ssize_t column; + + state = ft_vec_caccess(&table->states, el->state_num); + column = find_token_index(token, &table->tokens); + return (*(ssize_t *)ft_vec_caccess(&state->gotos, + column - (table->terminal_tokens_num + 1))); +} + +static int hang_top_from_tree(t_stack *stack, t_parse_tree_node *tree, + const t_token *constituent_token) +{ + t_parse_tree_node *node; + + node = ((t_parser_stack_element *)ft_stack_pop(stack, NULL))->node; + if (ft_strcmp(node->token.type, constituent_token->type) + || ft_vec_insert(&tree->children, node, 0) != success) + { + ft_parse_tree_free(node); + return (1); + } + free(node); + return (0); +} + +int follow_rule(t_stack *stack, size_t rule_num, const t_parsing_table *table) +{ + const t_grammar_rule *rule; + t_parser_stack_element element; + size_t i; + + element.node = malloc(sizeof(*element.node)); + if (!element.node) + return (1); + ft_vec_init(&element.node->children, sizeof(t_parse_tree_node)); + rule = ft_vec_caccess(&table->rules, rule_num); + element.node->token = ft_token_dup(rule->result); + i = rule->constituents.size; + while (i > 0) + { + --i; + if (hang_top_from_tree(stack, element.node, + ft_vec_caccess(&rule->constituents, i))) + { + ft_parse_tree_free(element.node); + return (1); + } + } + element.state_num = goto_state(ft_stack_top(stack), table, rule->result); + return (ft_stack_push(stack, &element) != success || element.state_num < 0); +} diff --git a/Libft/ft_parse/add_line.c b/Libft/ft_parse/add_line.c new file mode 100644 index 0000000..23dc2de --- /dev/null +++ b/Libft/ft_parse/add_line.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 13:31:48 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 13:51:01 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "libft.h" +#include +#include + +static t_vec parse_lookahead(const char *line, size_t lookahead_size) +{ + t_parser_action action; + t_vec lookahead; + + ft_vec_init(&lookahead, sizeof(t_parser_action)); + while (lookahead_size > 0) + { + while (*line && *line != ';') + ++line; + if (!*line) + break ; + ++line; + action.number = ft_atoi(line + 1); + if (*line == 'r') + action.type = parser_reduce; + else if (*line == 's') + action.type = parser_shift; + else if (!ft_strncmp(line, "acc", 3)) + action.type = parser_accept; + else + action.type = parser_refuse; + ft_vec_append(&lookahead, &action); + --lookahead_size; + } + return (lookahead); +} + +static t_vec parse_goto(const char *line) +{ + ssize_t goto_rule; + t_vec gotos; + + ft_vec_init(&gotos, sizeof(ssize_t)); + while (*line) + { + while (*line && *line != ';') + ++line; + if (!*line) + break ; + ++line; + if (!*line) + break ; + else if (*line == ';') + goto_rule = -1; + else + goto_rule = ft_atoi(line); + ft_vec_append(&gotos, &goto_rule); + } + return (gotos); +} + +int add_line(t_vec *states, const char *line, size_t lookahead_size) +{ + t_parser_state state; + char *condensed_line; + size_t i; + + condensed_line = ft_remove_space(line); + state.lookahead = parse_lookahead(condensed_line, lookahead_size); + i = 0; + while (lookahead_size > 0) + { + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + if (condensed_line[i]) + ++i; + --lookahead_size; + } + state.gotos = parse_goto(condensed_line + i); + free(condensed_line); + ft_vec_append(states, &state); + return (0); +} diff --git a/Libft/ft_parse/ft_parse.c b/Libft/ft_parse/ft_parse.c new file mode 100644 index 0000000..3fbcdcc --- /dev/null +++ b/Libft/ft_parse/ft_parse.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 20:51:36 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:45:13 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "ft_parse.h" +#include "libft.h" +#include + +static void initialize_parser_stack(t_stack *stack) +{ + t_parser_stack_element zero; + + zero.state_num = 0; + zero.node = NULL; + ft_stack_init(stack, sizeof(t_parser_stack_element)); + ft_stack_push(stack, &zero); + return ; +} + +static const t_parser_action *get_action(t_stack *stack, t_token token, + const t_parsing_table *table) +{ + const t_parser_state *state; + ssize_t column; + + state = ft_vec_caccess(&table->states, + ((t_parser_stack_element *)ft_stack_top(stack))->state_num); + column = find_token_index(token, &table->tokens); + if (column < 0 || (size_t)column > table->terminal_tokens_num) + { + ft_stack_free(stack, ft_free_stack_element); + return (NULL); + } + return (ft_vec_caccess(&state->lookahead, column)); +} + +static t_token get_token(const t_vec *tokens, size_t index) +{ + static char end_token[] = "$"; + + if (index < tokens->size) + return (*(t_token *)ft_vec_caccess(tokens, index)); + else + return ((t_token){.type = end_token, .str = NULL}); +} + +static t_parse_tree_node *handle_accept(t_stack *stack) +{ + t_parse_tree_node *root; + + root = ((t_parser_stack_element *)ft_stack_top(stack))->node; + ft_stack_free(stack, NULL); + return (root); +} + +t_parse_tree_node *ft_parse(const t_vec *tokens, const t_parsing_table *table) +{ + t_stack stack; + size_t i; + t_token token; + const t_parser_action *action; + + initialize_parser_stack(&stack); + i = 0; + while (1) + { + token = get_token(tokens, i); + action = get_action(&stack, token, table); + if (!action || action->type == parser_refuse + || (action->type == parser_reduce + && follow_rule(&stack, action->number, table)) + || (action->type == parser_shift + && push_state(&stack, action->number, token))) + { + ft_stack_free(&stack, ft_free_stack_element); + return (NULL); + } + else if (action->type == parser_accept) + return (handle_accept(&stack)); + if (action->type == parser_shift) + ++i; + } +} diff --git a/Libft/ft_parse/ft_parse_inner.h b/Libft/ft_parse/ft_parse_inner.h new file mode 100644 index 0000000..3d5cea9 --- /dev/null +++ b/Libft/ft_parse/ft_parse_inner.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_inner.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 13:23:20 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 14:21:48 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PARSE_INNER_H +# define FT_PARSE_INNER_H + +# include "libft.h" + +void ft_free_token(void *v_token); +void ft_free_rule(void *v_rule); +void ft_free_state(void *v_state); +void ft_free_stack_element(void *v_el); + +t_token ft_token_dup(t_token token); +ssize_t find_token_index(t_token token, const t_vec *tokens); + +t_ft_stat load_rules_fd(t_vec *rules, int fd); +t_ft_stat load_rules_str(t_vec *rules, const char *rules_str); +int add_line(t_vec *states, const char *line, size_t lookahead_size); +int follow_rule(t_stack *stack, size_t rule_num, + const t_parsing_table *table); +int push_state(t_stack *stack, size_t state_num, t_token token); + +int is_consistent(t_parsing_table *table); +size_t get_terminal_tokens_num(t_vec *tokens); +t_vec parse_header(const char *header); +#endif //FT_PARSE_INNER_H diff --git a/Libft/ft_parse/ft_parse_tree_free.c b/Libft/ft_parse/ft_parse_tree_free.c new file mode 100644 index 0000000..d8b3b67 --- /dev/null +++ b/Libft/ft_parse/ft_parse_tree_free.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_tree_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 15:26:34 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:45:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "ft_parse.h" +#include + +static void parse_tree_free_children(void *v_node) +{ + t_parse_tree_node *node; + + node = v_node; + if (!node) + return ; + ft_vec_free(&node->children, parse_tree_free_children); + ft_free_token(&node->token); +} + +void ft_parse_tree_free(void *node) +{ + parse_tree_free_children(node); + free(node); + return ; +} diff --git a/Libft/ft_parse/ft_parse_tree_print.c b/Libft/ft_parse/ft_parse_tree_print.c new file mode 100644 index 0000000..bb46813 --- /dev/null +++ b/Libft/ft_parse/ft_parse_tree_print.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_tree_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 09:51:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 13:09:00 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse.h" +#include "libft.h" + +#define MAX_WIDTH 80 + +static void print_last_at_depth(t_parse_tree_node *node, + size_t depth, char branches[MAX_WIDTH]); +static void print_node_at_depth(t_parse_tree_node *node, + size_t depth, char branches[MAX_WIDTH]); + +static void print_children_at_depth(t_parse_tree_node *node, + size_t depth, char branches[MAX_WIDTH]) +{ + size_t i; + + if (!node) + { + ft_printf("%s\n", node); + return ; + } + i = 0; + while (i + 1 < node->children.size) + { + print_node_at_depth(ft_vec_access(&node->children, i), + depth + 1, branches); + ++i; + } + if (i < node->children.size) + print_last_at_depth(ft_vec_access(&node->children, i), + depth + 1, branches); + if (depth > 0) + branches[ft_strlen(branches) - 1] = '\0'; + branches[ft_strlen(branches) - 1] = '\0'; + return ; +} + +static void print_last_at_depth(t_parse_tree_node *node, + size_t depth, char branches[MAX_WIDTH]) +{ + if (!node) + { + ft_printf("%s\n", node); + return ; + } + ft_printf("%s-%s - %s\n", branches, node->token.type, node->token.str); + branches[ft_strlen(branches) - 1] = ' '; + ft_strlcat(branches, " |", MAX_WIDTH); + print_children_at_depth(node, depth, branches); +} + +static void print_node_at_depth(t_parse_tree_node *node, + size_t depth, char branches[MAX_WIDTH]) +{ + if (!node) + { + ft_printf("%s\n", node); + return ; + } + if (depth == 0) + { + ft_printf("%s - %s\n", node->token.type, node->token.str); + ft_strlcat(branches, "|", MAX_WIDTH); + } + else + { + ft_printf("%s-%s - %s\n", branches, node->token.type, node->token.str); + ft_strlcat(branches, " |", MAX_WIDTH); + } + print_children_at_depth(node, depth, branches); +} + +void ft_parse_tree_print(t_parse_tree_node *root) +{ + char branches[MAX_WIDTH]; + + branches[0] = '\0'; + print_node_at_depth(root, 0, branches); + return ; +} diff --git a/Libft/ft_parse/ft_parsing_table_free.c b/Libft/ft_parse/ft_parsing_table_free.c new file mode 100644 index 0000000..bf16334 --- /dev/null +++ b/Libft/ft_parse/ft_parsing_table_free.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_table_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 13:21:26 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:56:50 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "ft_parse.h" +#include + +void ft_free_token(void *v_token) +{ + t_token *token; + + token = v_token; + free(token->type); + free(token->str); + return ; +} + +void ft_free_rule(void *v_rule) +{ + t_grammar_rule *rule; + + rule = v_rule; + ft_free_token(&rule->result); + ft_vec_free(&rule->constituents, ft_free_token); + return ; +} + +void ft_free_state(void *v_state) +{ + t_parser_state *state; + + state = v_state; + ft_vec_free(&state->lookahead, NULL); + ft_vec_free(&state->gotos, NULL); + return ; +} + +void ft_parsing_table_free(t_parsing_table *table) +{ + ft_vec_free(&table->rules, ft_free_rule); + ft_vec_free(&table->states, ft_free_state); + ft_vec_free(&table->tokens, ft_free_token); + return ; +} diff --git a/Libft/ft_parse/ft_parsing_table_init.c b/Libft/ft_parse/ft_parsing_table_init.c new file mode 100644 index 0000000..e13ff8c --- /dev/null +++ b/Libft/ft_parse/ft_parsing_table_init.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_table_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 13:28:20 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 13:28:46 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse.h" + +t_ft_stat ft_parsing_table_init(t_parsing_table *table) +{ + t_ft_stat res; + + res = ft_vec_init(&table->rules, sizeof(t_grammar_rule)); + if (res != success) + return (res); + res = ft_vec_init(&table->states, sizeof(t_parser_state)); + if (res != success) + return (res); + res = ft_vec_init(&table->tokens, sizeof(t_token)); + return (res); +} diff --git a/Libft/ft_parse/ft_parsing_table_load.c b/Libft/ft_parse/ft_parsing_table_load.c new file mode 100644 index 0000000..7f916ba --- /dev/null +++ b/Libft/ft_parse/ft_parsing_table_load.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_table_load.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 12:34:17 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 14:13:13 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "ft_parse.h" +#include "libft.h" +#include +#include +#include + +t_ft_stat ft_parsing_table_load_str(t_parsing_table *table, + const char *table_str, const char *rules_str) +{ + char **table_split; + size_t i; + + load_rules_str(&table->rules, rules_str); + table_split = ft_split(table_str, "\n"); + if (!table_split) + return (alloc_fail); + table->tokens = parse_header(table_split[0]); + table->terminal_tokens_num = get_terminal_tokens_num(&table->tokens); + i = 1; + while (table_split[i]) + { + add_line(&table->states, table_split[i], + table->terminal_tokens_num + 1); + ++i; + } + ft_free_split(table_split); + if (is_consistent(table)) + return (success); + return (non_specific_failure); +} + +t_ft_stat ft_parsing_table_load_fd(t_parsing_table *table, + int table_fd, int rules_fd) +{ + char *line; + + load_rules_fd(&table->rules, rules_fd); + line = get_next_line(table_fd); + table->tokens = parse_header(line); + table->terminal_tokens_num = get_terminal_tokens_num(&table->tokens); + free(line); + line = get_next_line(table_fd); + while (line) + { + add_line(&table->states, line, table->terminal_tokens_num + 1); + free(line); + line = get_next_line(table_fd); + } + if (is_consistent(table)) + return (success); + return (non_specific_failure); +} + +t_ft_stat ft_parsing_table_load_name(t_parsing_table *table, + const char *filename, + const char *rules_filename) +{ + int rules_fd; + int table_fd; + t_ft_stat res; + + rules_fd = open(rules_filename, O_RDONLY); + if (rules_fd < 0) + return (file_error); + table_fd = open(filename, O_RDONLY); + if (table_fd < 0) + { + close(rules_fd); + return (file_error); + } + res = ft_parsing_table_load_fd(table, table_fd, rules_fd); + close(table_fd); + close(rules_fd); + return (res); +} diff --git a/Libft/ft_parse/ft_parsing_table_load_helpers.c b/Libft/ft_parse/ft_parsing_table_load_helpers.c new file mode 100644 index 0000000..8436368 --- /dev/null +++ b/Libft/ft_parse/ft_parsing_table_load_helpers.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_table_load_helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 14:09:21 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 14:21:37 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "libft.h" +#include +#include +#include + +static char *get_token_type(const char *line) +{ + size_t i; + char *type; + + i = 0; + while (line[i] && line[i] != ';') + ++i; + type = ft_strndup(line, i); + return (type); +} + +t_vec parse_header(const char *header) +{ + t_vec tokens; + t_token token; + char *condensed_line; + size_t i; + + condensed_line = ft_remove_space(header); + ft_vec_init(&tokens, sizeof(t_token)); + token.str = NULL; + i = 0; + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + while (condensed_line[i]) + { + ++i; + token.type = get_token_type(condensed_line + i); + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + ft_vec_append(&tokens, &token); + } + free(condensed_line); + return (tokens); +} + +size_t get_terminal_tokens_num(t_vec *tokens) +{ + size_t i; + t_token *token; + + i = 0; + while (i < tokens->size) + { + token = (t_token *)ft_vec_access(tokens, i); + if (!ft_strcmp(token->type, "$")) + return (i); + ++i; + } + return (0); +} + +int is_consistent(__attribute__((unused)) t_parsing_table *table) +{ + return (1); +} diff --git a/Libft/ft_parse/ft_parsing_table_print.c b/Libft/ft_parse/ft_parsing_table_print.c new file mode 100644 index 0000000..2f6dd0e --- /dev/null +++ b/Libft/ft_parse/ft_parsing_table_print.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_table_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/16 07:19:50 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 12:20:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse.h" +#include "libft.h" +#include + +static void print_rule(t_grammar_rule *rule, unsigned int column_width) +{ + size_t i; + + ft_printf("%-*s -> ", column_width - 1, rule->result.type); + i = 0; + while (i < rule->constituents.size) + ft_printf("%-*s ", column_width - 1, + ((t_token *)ft_vec_access(&rule->constituents, i++))->type); + ft_printf("\n"); + return ; +} + +static void print_action(t_parser_action *action, unsigned int column_width) +{ + if (action->type == parser_reduce) + ft_printf("r%-*u ", column_width - 2, action->number); + else if (action->type == parser_shift) + ft_printf("s%-*u ", column_width - 2, action->number); + else if (action->type == parser_accept) + ft_printf("%-*s ", column_width - 1, "acc"); + else + ft_printf("%*s", column_width, ""); + return ; +} + +static void print_state(t_parser_state *state, + unsigned int column_width, size_t state_num) +{ + size_t i; + t_vec *vec; + ssize_t *gt; + + ft_printf("%-5u ", state_num); + vec = &state->lookahead; + i = 0; + while (i < vec->size) + print_action(ft_vec_access(vec, i++), column_width); + vec = &state->gotos; + i = 0; + while (i < vec->size) + { + gt = ft_vec_access(vec, i++); + if (*gt < 0) + ft_printf("%*s", column_width, ""); + else + ft_printf("%-*u ", column_width - 1, *gt); + } + ft_printf("\n"); + return ; +} + +void ft_parsing_table_print(t_parsing_table *table, + unsigned int column_width) +{ + size_t i; + + if (column_width < 3) + column_width = 3; + i = 0; + while (i < table->rules.size) + print_rule(ft_vec_access(&table->rules, i++), column_width); + i = 0; + ft_printf("\n%-5s ", "State"); + while (i < table->tokens.size) + ft_printf("%-*.*s ", column_width - 1, column_width - 1, + ((t_token *)ft_vec_access(&table->tokens, i++))->type); + ft_printf("\n"); + i = 0; + while (i < table->states.size) + { + print_state(ft_vec_access(&table->states, i), column_width, i); + ++i; + } + return ; +} diff --git a/Libft/ft_parse/helpers.c b/Libft/ft_parse/helpers.c new file mode 100644 index 0000000..14b1719 --- /dev/null +++ b/Libft/ft_parse/helpers.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 15:30:29 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 16:52:09 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "ft_parse.h" +#include "libft.h" + +void ft_free_stack_element(void *v_el) +{ + t_parser_stack_element *el; + + el = v_el; + if (!el) + return ; + ft_parse_tree_free(el->node); + return ; +} + +t_token ft_token_dup(t_token token) +{ + t_token res; + + res.type = ft_strdup(token.type); + res.str = ft_strdup(token.str); + return (res); +} + +ssize_t find_token_index(t_token token, const t_vec *tokens) +{ + size_t i; + + if (!token.type) + return (-1); + i = 0; + while (i < tokens->size) + { + if (!ft_strcmp(token.type, + ((t_token *)ft_vec_caccess(tokens, i))->type)) + return (i); + ++i; + } + return (-1); +} diff --git a/Libft/ft_parse/load_rules.c b/Libft/ft_parse/load_rules.c new file mode 100644 index 0000000..162df5f --- /dev/null +++ b/Libft/ft_parse/load_rules.c @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* load_rules.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 13:29:48 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 14:24:01 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "libft.h" +#include +#include +#include + +static void parse_constituents(t_vec *constituents, const char *line) +{ + size_t i; + size_t j; + t_token token; + + token.str = NULL; + i = 0; + while (line[i]) + { + while (ft_isspace(line[i])) + ++i; + j = i; + while (!ft_isspace(line[i]) && line[i]) + ++i; + if (j == i) + break ; + token.type = ft_strndup(line + j, i - j); + ft_vec_append(constituents, &token); + } +} + +static t_grammar_rule parse_rule(const char *line) +{ + t_grammar_rule rule; + t_token token; + size_t i; + size_t j; + + token.str = NULL; + ft_vec_init(&rule.constituents, sizeof(t_token)); + i = 0; + while (ft_isspace(line[i])) + ++i; + j = i; + while (!ft_isspace(line[i])) + ++i; + token.type = ft_strndup(line + j, i - j); + rule.result = token; + while (ft_isspace(line[i])) + ++i; + if (!(line[i++] == '-' && line[i++] == '>')) + return (rule); + parse_constituents(&rule.constituents, line + i); + return (rule); +} + +static int is_valid_rule(t_grammar_rule *rule) +{ + size_t i; + + if (!rule->result.type) + return (0); + i = 0; + while (i < rule->constituents.size) + { + if (!((t_token *)ft_vec_access(&rule->constituents, i))->type) + return (0); + ++i; + } + return (1); +} + +t_ft_stat load_rules_str(t_vec *rules, const char *rules_str) +{ + char **rules_split; + size_t i; + t_grammar_rule rule; + + rules_split = ft_split(rules_str, "\n"); + if (!rules_split) + return (alloc_fail); + i = 0; + while (rules_split[i]) + { + rule = parse_rule(rules_split[i]); + if (!is_valid_rule(&rule) || ft_vec_append(rules, &rule) != success) + { + ft_vec_free(rules, ft_free_rule); + return (non_specific_failure); + } + ++i; + } + ft_free_split(rules_split); + return (success); +} + +t_ft_stat load_rules_fd(t_vec *rules, int fd) +{ + char *line; + t_grammar_rule rule; + + line = get_next_line(fd); + while (line) + { + rule = parse_rule(line); + if (!is_valid_rule(&rule) || ft_vec_append(rules, &rule) != success) + { + ft_vec_free(rules, ft_free_rule); + return (non_specific_failure); + } + free(line); + line = get_next_line(fd); + } + close(fd); + return (success); +} diff --git a/Libft/ft_str/ft_remove_space.c b/Libft/ft_str/ft_remove_space.c new file mode 100644 index 0000000..931cfa5 --- /dev/null +++ b/Libft/ft_str/ft_remove_space.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_remove_space.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/15 08:27:22 by ljiriste #+# #+# */ +/* Updated: 2024/06/15 08:36:48 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include "libft.h" +#include + +// This function could be improved not to overallocate +char *ft_remove_space(const char *str) +{ + char *res; + size_t i; + size_t j; + + res = malloc(ft_strlen(str) + 1); + if (!res) + return (NULL); + i = 0; + j = 0; + while (str[i]) + { + if (!ft_isspace(str[i])) + res[j++] = str[i]; + ++i; + } + res[j] = '\0'; + return (res); +} diff --git a/Libft/ft_str/ft_split.c b/Libft/ft_str/ft_split.c new file mode 100644 index 0000000..c9994d4 --- /dev/null +++ b/Libft/ft_str/ft_split.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/20 11:51:05 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 14:01:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +static size_t substr_len(const char *str, const char *seps) +{ + size_t len; + + len = 0; + while (!ft_strchr(seps, str[len])) + ++len; + return (len); +} + +static size_t count_valid_strs(const char *str, const char *seps) +{ + size_t i; + size_t str_count; + + str_count = 0; + i = 0; + while (str[i]) + { + while (ft_strchr(seps, str[i]) && str[i]) + ++i; + ++str_count; + while (!ft_strchr(seps, str[i]) && str[i]) + ++i; + } + return (str_count); +} + +static char *extract_substr(const char *str, const char *seps) +{ + char *res; + size_t i; + + res = malloc((substr_len(str, seps) + 1) * sizeof(char)); + if (!res) + return (NULL); + i = 0; + while (!ft_strchr(seps, str[i])) + { + res[i] = str[i]; + ++i; + } + res[i] = '\0'; + return (res); +} + +void ft_free_split(char **split) +{ + size_t i; + + i = 0; + while (split[i]) + { + free(split[i]); + ++i; + } + free(split); + return ; +} + +char **ft_split(const char *str, const char *seps) +{ + char **res; + size_t i; + size_t str_num; + size_t str_count; + + str_count = count_valid_strs(str, seps); + res = malloc((str_count + 1) * sizeof(char *)); + i = 0; + str_num = 0; + while (str[i]) + { + if (!ft_strchr(seps, str[i])) + { + res[str_num] = extract_substr(&str[i], seps); + if (!res[str_num++]) + { + ft_free_split(res); + return (NULL); + } + i += substr_len(&str[i], seps) - 1; + } + ++i; + } + res[str_num] = NULL; + return (res); +} diff --git a/Libft/ft_str/ft_strcat_alloc.c b/Libft/ft_str/ft_strcat_alloc.c new file mode 100644 index 0000000..150470f --- /dev/null +++ b/Libft/ft_str/ft_strcat_alloc.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat_alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/02 10:49:25 by ljiriste #+# #+# */ +/* Updated: 2024/05/02 11:23:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" + +char *ft_strcat_alloc(char **dest, const char *src) +{ + return (ft_strncat_alloc(dest, src, ft_strlen(src))); +} diff --git a/Libft/ft_str/ft_strchr.c b/Libft/ft_str/ft_strchr.c new file mode 100644 index 0000000..182001e --- /dev/null +++ b/Libft/ft_str/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */ +/* Updated: 2024/07/21 19:02:33 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +char *ft_strchr(const char *s, int c) +{ + if (!s) + return (NULL); + while (*s) + { + if (*s == (char)c) + return ((char *)s); + ++s; + } + if (c == '\0') + return ((char *)s); + return (NULL); +} diff --git a/Libft/ft_str/ft_strcmp.c b/Libft/ft_str/ft_strcmp.c new file mode 100644 index 0000000..e20ddb5 --- /dev/null +++ b/Libft/ft_str/ft_strcmp.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/13 14:11:31 by ljiriste #+# #+# */ +/* Updated: 2024/01/13 15:00:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +int ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] && s1[i] && s2[i]) + ++i; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/Libft/ft_str/ft_strdup.c b/Libft/ft_str/ft_strdup.c new file mode 100644 index 0000000..6c725f4 --- /dev/null +++ b/Libft/ft_str/ft_strdup.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/17 13:33:49 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:45:31 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include // For the malloc function + +char *ft_strdup(const char *s) +{ + char *dest; + size_t s_len; + + if (s == NULL) + return (NULL); + s_len = ft_strlen(s); + dest = malloc((s_len + 1) * sizeof(char)); + if (dest) + ft_strlcpy(dest, s, s_len + 1); + return (dest); +} diff --git a/Libft/ft_str/ft_striteri.c b/Libft/ft_str/ft_striteri.c new file mode 100644 index 0000000..ac2bce2 --- /dev/null +++ b/Libft/ft_str/ft_striteri.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:45:55 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + if (s == NULL || f == NULL) + return ; + i = 0; + while (s[i]) + { + f(i, s + i); + ++i; + } + return ; +} diff --git a/Libft/ft_str/ft_strjoin.c b/Libft/ft_str/ft_strjoin.c new file mode 100644 index 0000000..39ff7e9 --- /dev/null +++ b/Libft/ft_str/ft_strjoin.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_str.h" + +char *ft_strjoin(const char *s1, const char *s2) +{ + char *res; + size_t size; + + if (s1 == NULL || s2 == NULL) + return (NULL); + size = ft_strlen(s1) + ft_strlen(s2) + 1; + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + res[0] = '\0'; + ft_strlcat(res, s1, size); + ft_strlcat(res, s2, size); + return (res); +} diff --git a/Libft/ft_str/ft_strlcat.c b/Libft/ft_str/ft_strlcat.c new file mode 100644 index 0000000..ba6de01 --- /dev/null +++ b/Libft/ft_str/ft_strlcat.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:32:33 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_str.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t length; + + if ((dst == NULL && src == NULL) || size == 0) + return (0); + length = 0; + while (*dst && size > length) + { + ++dst; + ++length; + } + while (*src && size > length + 1) + { + *dst = *src; + ++src; + ++dst; + ++length; + } + if (size > length) + *dst = '\0'; + while (*src) + { + ++length; + ++src; + } + return (length); +} diff --git a/Libft/ft_str/ft_strlcpy.c b/Libft/ft_str/ft_strlcpy.c new file mode 100644 index 0000000..cae2832 --- /dev/null +++ b/Libft/ft_str/ft_strlcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/11 17:28:43 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:45:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + while (src[i] && i + 1 < size) + { + dst[i] = src[i]; + ++i; + } + if (size > 0) + dst[i] = '\0'; + while (src[i]) + ++i; + return (i); +} diff --git a/Libft/ft_str/ft_strlen.c b/Libft/ft_str/ft_strlen.c new file mode 100644 index 0000000..e014d7e --- /dev/null +++ b/Libft/ft_str/ft_strlen.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:40:59 by ljiriste #+# #+# */ +/* Updated: 2024/05/20 20:47:11 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +size_t ft_strlen(const char *s) +{ + size_t len; + + if (!s) + return (0); + len = 0; + while (s[len] != '\0') + ++len; + return (len); +} diff --git a/Libft/ft_str/ft_strmapi.c b/Libft/ft_str/ft_strmapi.c new file mode 100644 index 0000000..cb44e80 --- /dev/null +++ b/Libft/ft_str/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_str.h" + +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) +{ + char *res; + unsigned int i; + + if (s == NULL || f == NULL) + return (NULL); + res = malloc((ft_strlen(s) + 1) * sizeof(char)); + if (res == NULL) + return (NULL); + i = 0; + while (s[i]) + { + res[i] = f(i, s[i]); + ++i; + } + res[i] = '\0'; + return (res); +} diff --git a/Libft/ft_str/ft_strncat_alloc.c b/Libft/ft_str/ft_strncat_alloc.c new file mode 100644 index 0000000..45d2cfa --- /dev/null +++ b/Libft/ft_str/ft_strncat_alloc.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat_alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/11 13:36:53 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:48:57 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include "libft.h" +#include // malloc etc. + +/* Functions like ft_strncat but reallocates dest to + make sure it is large enough */ +char *ft_strncat_alloc(char **dest, const char *src, size_t n) +{ + int size; + char *temp; + + if (n == 0) + return (*dest); + temp = *dest; + size = 0; + if (temp) + while (temp[size]) + ++size; + size += n + 1; + *dest = malloc(size); + if (*dest == NULL) + { + free(temp); + return (NULL); + } + ft_memmove(*dest, temp, size - n - 1); + ft_memmove(*dest + size - n - 1, src, n); + (*dest)[size - 1] = '\0'; + free(temp); + return (*dest); +} diff --git a/Libft/ft_str/ft_strncmp.c b/Libft/ft_str/ft_strncmp.c new file mode 100644 index 0000000..ad5f582 --- /dev/null +++ b/Libft/ft_str/ft_strncmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:44:40 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] && s1[i] && s2[i] && i + 1 < n) + ++i; + if (n > 0) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + else + return (0); +} diff --git a/Libft/ft_str/ft_strndup.c b/Libft/ft_str/ft_strndup.c new file mode 100644 index 0000000..03608ca --- /dev/null +++ b/Libft/ft_str/ft_strndup.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/24 09:18:49 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:44:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include // For the malloc function + +char *ft_strndup(const char *s, size_t n) +{ + char *dest; + size_t s_len; + + s_len = ft_strlen(s); + if (s_len < n) + n = s_len; + dest = malloc((n + 1) * sizeof(char)); + if (dest == NULL) + return (dest); + ft_strlcpy(dest, s, n + 1); + return (dest); +} diff --git a/Libft/ft_str/ft_strnstr.c b/Libft/ft_str/ft_strnstr.c new file mode 100644 index 0000000..afb9326 --- /dev/null +++ b/Libft/ft_str/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:15:34 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:45:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include // For the NULL pointer +#include + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t little_len; + size_t i; + + if (little == NULL && len == 0) + return ((char *)big); + if (*little == '\0') + return ((char *)big); + if (big == NULL && len == 0) + return (NULL); + little_len = ft_strlen(little); + i = 0; + while (big[i] && i + little_len <= len) + { + if (!(ft_strncmp(big + i, little, little_len))) + return ((char *)big + i); + ++i; + } + return (NULL); +} diff --git a/Libft/ft_str/ft_strrchr.c b/Libft/ft_str/ft_strrchr.c new file mode 100644 index 0000000..8aef14e --- /dev/null +++ b/Libft/ft_str/ft_strrchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */ +/* Updated: 2024/07/21 18:41:05 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_str.h" + +char *ft_strrchr(const char *s, int c) +{ + char *result; + + if (!s) + return (NULL); + result = NULL; + while (*s) + { + if (*s == (char)c) + result = (char *)s; + ++s; + } + if (c == '\0') + return ((char *)s); + return (result); +} diff --git a/Libft/ft_str/ft_strtrim.c b/Libft/ft_str/ft_strtrim.c new file mode 100644 index 0000000..6494bd3 --- /dev/null +++ b/Libft/ft_str/ft_strtrim.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:45:03 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_str.h" +#include +#include + +static size_t size_needed(const char *s1, const char *set) +{ + size_t at_start; + size_t at_end; + size_t whole; + + whole = ft_strlen(s1); + at_start = 0; + at_end = 0; + while (ft_strchr(set, *s1) && *s1) + { + ++s1; + ++at_start; + } + if (*s1 == '\0') + return (1); + while (*s1) + ++s1; + --s1; + while (ft_strchr(set, *s1)) + { + --s1; + ++at_end; + } + return (whole - at_start - at_end + 1); +} + +char *ft_strtrim(const char *s1, const char *set) +{ + size_t size; + char *res; + + if (s1 == NULL) + return (NULL); + size = size_needed(s1, set); + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + while (ft_strchr(set, *s1) && *s1) + ++s1; + ft_strlcpy(res, s1, size); + return (res); +} diff --git a/Libft/ft_str/ft_substr.c b/Libft/ft_str/ft_substr.c new file mode 100644 index 0000000..ededd6a --- /dev/null +++ b/Libft/ft_str/ft_substr.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_str.h" + +char *ft_substr(const char *s, unsigned int start, size_t len) +{ + char *res; + size_t size; + + if (s == NULL) + return (NULL); + size = ft_strlen(s) + 1; + if (start > size) + { + res = malloc(1 * sizeof(char)); + if (res == NULL) + return (NULL); + *res = '\0'; + return (res); + } + else + size -= start; + if (size > len + 1) + size = len + 1; + res = malloc(size * sizeof(char)); + if (res == NULL) + return (NULL); + ft_strlcpy(res, s + start, size); + return (res); +} diff --git a/Libft/ft_struct/ft_stack_free.c b/Libft/ft_struct/ft_stack_free.c new file mode 100644 index 0000000..66148f5 --- /dev/null +++ b/Libft/ft_struct/ft_stack_free.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:16:56 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:18:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +void ft_stack_free(t_stack *stack, void (*free_el)(void *)) +{ + ft_vec_free(&stack->vec, free_el); + return ; +} diff --git a/Libft/ft_struct/ft_stack_init.c b/Libft/ft_struct/ft_stack_init.c new file mode 100644 index 0000000..6e8e0d5 --- /dev/null +++ b/Libft/ft_struct/ft_stack_init.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:04:10 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:14 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" +#include + +t_ft_stat ft_stack_init(t_stack *stack, size_t el_size) +{ + return (ft_vec_init(&stack->vec, el_size)); +} diff --git a/Libft/ft_struct/ft_stack_pop.c b/Libft/ft_struct/ft_stack_pop.c new file mode 100644 index 0000000..1fd7db6 --- /dev/null +++ b/Libft/ft_struct/ft_stack_pop.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_pop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:06:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:27 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +void *ft_stack_pop(t_stack *stack, void (*free_el)(void *)) +{ + void *res; + + res = ft_stack_top(stack); + if (res) + ft_vec_erase(&stack->vec, stack->vec.size - 1, free_el); + return (res); +} diff --git a/Libft/ft_struct/ft_stack_pop_forget.c b/Libft/ft_struct/ft_stack_pop_forget.c new file mode 100644 index 0000000..b0b0095 --- /dev/null +++ b/Libft/ft_struct/ft_stack_pop_forget.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_pop_forget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:15:41 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +void *ft_stack_pop_forget(t_stack *stack) +{ + void *res; + + res = ft_stack_top(stack); + if (res) + ft_vec_forget(&stack->vec, stack->vec.size - 1); + return (res); +} diff --git a/Libft/ft_struct/ft_stack_push.c b/Libft/ft_struct/ft_stack_push.c new file mode 100644 index 0000000..9ad3319 --- /dev/null +++ b/Libft/ft_struct/ft_stack_push.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:05:28 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:51 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +t_ft_stat ft_stack_push(t_stack *stack, void *element) +{ + return (ft_vec_append(&stack->vec, element)); +} diff --git a/Libft/ft_struct/ft_stack_top.c b/Libft/ft_struct/ft_stack_top.c new file mode 100644 index 0000000..36dd701 --- /dev/null +++ b/Libft/ft_struct/ft_stack_top.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_top.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:11:34 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:13:19 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" +#include + +void *ft_stack_top(t_stack *stack) +{ + if (stack->vec.size == 0) + return (NULL); + return (ft_vec_access(&stack->vec, stack->vec.size - 1)); +} diff --git a/Libft/inc/ft_arr.h b/Libft/inc/ft_arr.h new file mode 100644 index 0000000..6a103f5 --- /dev/null +++ b/Libft/inc/ft_arr.h @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 13:58:15 by ljiriste #+# #+# */ +/* Updated: 2024/07/04 10:20:25 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_ARR_H +# define FT_ARR_H + +# include "ft_stat.h" +# include + +# ifndef V_DEFAULT_CAPACITY +# define V_DEFAULT_CAPACITY 8 +# endif + +typedef t_ft_stat t_arr_stat; + +// It should be possible to remove el_size with the use of macros? +typedef struct s_vec +{ + size_t capacity; + size_t size; + size_t el_size; + void *vec; +} t_vec; + +typedef struct s_mat +{ + size_t rows; + size_t cols; + t_vec vec; +} t_mat; + +t_arr_stat ft_vec_reserve(t_vec *vec, size_t capacity); +t_arr_stat ft_vec_enlarge(t_vec *vec); +t_arr_stat ft_vec_insert(t_vec *vec, const void *element, size_t index); +t_arr_stat ft_vec_append(t_vec *vec, const void *element); +t_arr_stat ft_vec_forget(t_vec *vec, size_t index); +t_arr_stat ft_vec_erase(t_vec *vec, size_t index, void (*free_el)(void *)); + +t_arr_stat ft_vec_insert_range(t_vec *vec, const void *element, + size_t count, size_t index); +t_arr_stat ft_vec_append_range(t_vec *vec, const void *element, size_t count); +t_arr_stat ft_vec_forget_range(t_vec *vec, size_t index, size_t count); +t_arr_stat ft_vec_erase_range(t_vec *vec, size_t count, size_t index, + void (*free_el)(void *)); + +// This macro should have been used for static t_vec initialization +// as I see no other way. +// But the Norm forbids macro functions, so just use the literal itself +/* +# define VEC_INIT(x) \ + (t_vec){.capacity = 0, .size = 0, .el_size = (x), .vec = NULL} \ +*/ + +t_arr_stat ft_vec_init(t_vec *vec, size_t el_size); +void ft_vec_free(t_vec *vec, void (*free_el)(void *)); +void *ft_vec_access(t_vec *vec, size_t index); +const void *ft_vec_caccess(const t_vec *vec, size_t index); + +/* It is probably better to use (type *)vec->vec + index for pointer + * or ((type *)vec->vec)[index] for value + * insted of (type *)ft_vec_access(vec, index) + * because it is smaller, more readable. But it does not range-check! + * Also implement access function that casts to desired type + * eg. int *int_access(t_vec *vec, size_t ind){return ((int *)vec->vec + ind);} + */ +t_arr_stat ft_vec_copy(t_vec *dest, const t_vec *src, + t_ft_stat (*copy_el)(void *, const void *), + void (*free_el)(void *)); + +void *ft_vec_find(t_vec *vec, void *wanted); +t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index); + +int ft_vec_is_equal(const t_vec *vec1, const t_vec *vec2, + int (*cmp_elements)(const void *, const void *)); + +// The following are functions that would operate on sets. +// I have not implemented set structure as it would be just a wrapper +// on t_vec with worsened access to the elements. +// The cmp_elements should return 0 when elements are equal (like strcmp) +int ft_vec_contains(const t_vec *vec, const void *wanted, + int (*cmp_elements)(const void *, const void *)); +int ft_vec_is_subset(const t_vec *subset, const t_vec *set, + int (*cmp_elements)(const void *, const void *)); +int ft_vec_is_setequal(const t_vec *first, const t_vec *second, + int (*cmp_elements)(const void *, const void *)); + +t_ft_stat ft_vec_setinsert(t_vec *vec, const void *el, + int (*cmp_el)(const void *, const void *)); + +t_arr_stat ft_mat_init(t_mat *mat, size_t el_size); +void ft_mat_free(t_mat *mat, void (*free_el)(void *)); +t_arr_stat ft_mat_resize(t_mat *mat, size_t rows, size_t cols); +t_arr_stat ft_mat_insert_row(t_mat *mat, const t_vec *vec, size_t index); +t_arr_stat ft_mat_insert_col(t_mat *mat, const t_vec *vec, size_t index); +t_arr_stat ft_mat_append_row(t_mat *mat, const t_vec *vec); +t_arr_stat ft_mat_append_col(t_mat *mat, const t_vec *vec); +t_arr_stat ft_mat_erase_rows(t_mat *mat, size_t count, + size_t index, void (*free_el)(void *)); +t_arr_stat ft_mat_erase_cols(t_mat *mat, size_t count, + size_t index, void (*free_el)(void *)); +void *ft_mat_access(t_mat *mat, size_t row, size_t col); +t_arr_stat ft_mat_zeros(t_mat *matrix, size_t rows, size_t cols); +t_arr_stat ft_mat_fill(t_mat *mat, void *filler); + +#endif diff --git a/Libft/inc/ft_check.h b/Libft/inc/ft_check.h new file mode 100644 index 0000000..e640d93 --- /dev/null +++ b/Libft/inc/ft_check.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 11:55:15 by ljiriste #+# #+# */ +/* Updated: 2024/04/10 12:54:40 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_CHECK_H +# define FT_CHECK_H + +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_islower(int c); +int ft_isprint(int c); +int ft_isspace(int c); +int ft_isupper(int c); +int ft_isascii(int c); +int ft_isint(const char *str); + +#endif diff --git a/Libft/inc/ft_conv.h b/Libft/inc/ft_conv.h new file mode 100644 index 0000000..421b1f0 --- /dev/null +++ b/Libft/inc/ft_conv.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_conv.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 11:53:15 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_CONV_H +# define FT_CONV_H + +# include + +int ft_toupper(int c); +int ft_tolower(int c); + +int ft_atoi(const char *nptr); +long ft_atol(const char *nptr); +long long ft_atoll(const char *nptr); +char *ft_itoa(int n); +char *ft_itoa_base(intmax_t n, const char *base); +char *ft_uitoa_base(uintmax_t n, const char *base); +char *ft_ctoa(char c); + +#endif diff --git a/Libft/inc/ft_gen.h b/Libft/inc/ft_gen.h new file mode 100644 index 0000000..0781078 --- /dev/null +++ b/Libft/inc/ft_gen.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_gen.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 12:21:15 by ljiriste #+# #+# */ +/* Updated: 2024/07/21 21:36:48 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_GEN_H +# define FT_GEN_H + +void ft_swap_int(int *a, int *b); +void ft_swap(void **a, void **b); + +#endif diff --git a/Libft/inc/ft_io.h b/Libft/inc/ft_io.h new file mode 100644 index 0000000..7542179 --- /dev/null +++ b/Libft/inc/ft_io.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_io.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 11:38:28 by ljiriste #+# #+# */ +/* Updated: 2024/03/09 00:00:58 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_IO_H +# define FT_IO_H + +# include + +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(const char *s, int fd); +void ft_putendl_fd(const char *s, int fd); +void ft_putnbr_fd(int n, int fd); +int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); +int ft_vdprintf(int fd, const char *format, va_list *args); +char *get_next_line(int fd); + +#endif diff --git a/Libft/inc/ft_lst.h b/Libft/inc/ft_lst.h new file mode 100644 index 0000000..7143526 --- /dev/null +++ b/Libft/inc/ft_lst.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 11:42:15 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:13:44 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LST_H +# define FT_LST_H + +typedef struct s_list +{ + struct s_list *next; + void *content; +} t_list; + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +t_list *ft_lst_at(t_list *lst, unsigned int i); +t_list *ft_lst_find(t_list *lst, void *content_red, int (*cmp)()); +void ft_lst_foreach_if(t_list *lst, void (*f)(void *), + void *content_ref, int (*cmp)()); +void ft_lst_merge(t_list **lst1, t_list *lst2); +void ft_lst_remove_if(t_list **lst, void *content_ref, + int (*cmp)(), void (*free_fct)(void *)); +void ft_lst_reverse(t_list **lst); +void ft_lst_sort(t_list **lst, int (*cmp)()); +void ft_lst_sorted_insert(t_list **lst, t_list *new, int (*cmp)()); +void ft_lst_sorted_merge(t_list **lst1, t_list *lst2, int (*cmp)()); + +#endif diff --git a/Libft/inc/ft_math.h b/Libft/inc/ft_math.h new file mode 100644 index 0000000..6c272f4 --- /dev/null +++ b/Libft/inc/ft_math.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_math.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 12:19:15 by ljiriste #+# #+# */ +/* Updated: 2024/02/23 09:01:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MATH_H +# define FT_MATH_H + +# include + +int ft_abs(int n); +int ft_sgn(int n); + +int ft_max(int a, int b); +size_t ft_maxs(size_t a, size_t b); + +int ft_min(int a, int b); +size_t ft_mins(size_t a, size_t b); + +#endif diff --git a/Libft/inc/ft_mem.h b/Libft/inc/ft_mem.h new file mode 100644 index 0000000..073d9af --- /dev/null +++ b/Libft/inc/ft_mem.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mem.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 12:45:15 by ljiriste #+# #+# */ +/* Updated: 2023/12/09 15:13:44 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MEM_H +# define FT_MEM_H + +# include + +void *ft_memset(void *s, int c, 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_memchr(const void *s, int c, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +void ft_bzero(void *s, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); + +#endif diff --git a/Libft/inc/ft_parse.h b/Libft/inc/ft_parse.h new file mode 100644 index 0000000..6c3d05f --- /dev/null +++ b/Libft/inc/ft_parse.h @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/27 21:21:54 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 13:57:10 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PARSE_H +# define FT_PARSE_H + +# include "ft_arr.h" +# include + +typedef struct s_token +{ + char *type; + char *str; +} t_token; + +typedef struct s_grammar_rule +{ + t_token result; + t_vec constituents; +} t_grammar_rule; + +enum e_parser_action_type +{ + parser_accept, + parser_refuse, + parser_reduce, + parser_shift, +}; + +typedef struct s_parser_action +{ + enum e_parser_action_type type; + size_t number; +} t_parser_action; + +typedef struct s_parser_state +{ + t_vec lookahead; + t_vec gotos; +} t_parser_state; + +typedef struct s_parsing_table +{ + t_vec rules; + t_vec states; + t_vec tokens; + size_t terminal_tokens_num; +} t_parsing_table; + +typedef struct s_parse_tree_node +{ + t_token token; + t_vec children; +} t_parse_tree_node; + +typedef struct s_parser_stack_element +{ + ssize_t state_num; + t_parse_tree_node *node; +} t_parser_stack_element; + +// The parsing table has the following form: +// +// State token[i] token[i+n] +// j states[j].lookahead[i] states[0].goto[i] +// +// The first row contains all the n terminal tokens first followed by +// $ (the end token) and after them all the non-terminal tokens. +// Every other row contains the (unique) state number and the reduce/shift +// rule for the appropriate token. +// +// The whitespace is not significant and ; should be used as separator. +// For ease of parsing the "end of input" token $ should be the last +// lookahead token. Additionally the states should be consecutive +// increasing integers starting at 0. +// Do not use non-ASCII whitespace! +// +// The table containing rules should have the form +// +// token[i_1] -> [ token[j_1] [ token[k_1] ... ]] +// token[i_2] -> [ token[j_2] [ token[k_2] ... ]] +// +// Tokens should not contain whitespace as it is used as separator + +t_ft_stat ft_parsing_table_init(t_parsing_table *table); +t_ft_stat ft_parsing_table_load_name(t_parsing_table *table, + const char *filename, + const char *rules_filename); +t_ft_stat ft_parsing_table_load_fd(t_parsing_table *table, + int table_fd, int rules_fd); +t_ft_stat ft_parsing_table_load_str(t_parsing_table *table, + const char *table_str, const char *rules_str); +t_parse_tree_node *ft_parse(const t_vec *tokens, + const t_parsing_table *table); + +void ft_parsing_table_print(t_parsing_table *table, + unsigned int column_width); +void ft_parse_tree_print(t_parse_tree_node *root); + +void ft_parse_tree_free(void *node); +void ft_parsing_table_free(t_parsing_table *table); + +#endif // FT_PARSE_H diff --git a/Libft/inc/ft_stat.h b/Libft/inc/ft_stat.h new file mode 100644 index 0000000..bf78c89 --- /dev/null +++ b/Libft/inc/ft_stat.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stat.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 12:40:28 by ljiriste #+# #+# */ +/* Updated: 2024/07/04 10:14:10 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STAT_H +# define FT_STAT_H + +typedef enum e_ft_stat +{ + success, + already_inside, + alloc_fail, + invalid_size, + invalid_input, + file_error, + non_specific_failure, +} t_ft_stat; + +#endif //FT_STAT_H diff --git a/Libft/inc/ft_str.h b/Libft/inc/ft_str.h new file mode 100644 index 0000000..56d743b --- /dev/null +++ b/Libft/inc/ft_str.h @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 11:50:15 by ljiriste #+# #+# */ +/* Updated: 2024/08/02 14:02:44 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STR_H +# define FT_STR_H + +# include + +size_t ft_strlen(const char *s); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strcat_alloc(char **dest, const char *src); +char *ft_strncat_alloc(char **dest, const char *src, size_t n); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strdup(const char *s); +char *ft_strndup(const char *s, size_t n); +char *ft_substr(const char *s, unsigned int start, size_t len); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strtrim(const char *s1, const char *set); +char *ft_remove_space(const char *str); +char **ft_split(const char *str, const char *seps); +void ft_free_split(char **split); +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); + +#endif diff --git a/Libft/inc/ft_struct.h b/Libft/inc/ft_struct.h new file mode 100644 index 0000000..bbcc14f --- /dev/null +++ b/Libft/inc/ft_struct.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_struct.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 16:59:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 15:24:34 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STRUCT_H +# define FT_STRUCT_H + +# include "ft_arr.h" +# include + +typedef struct s_stack +{ + t_vec vec; +} t_stack; + +t_ft_stat ft_stack_init(t_stack *stack, size_t el_size); +t_ft_stat ft_stack_push(t_stack *stack, void *element); +void *ft_stack_top(t_stack *stack); +void *ft_stack_pop(t_stack *stack, void (*free_el)(void *)); +void *ft_stack_pop_forget(t_stack *stack); +void ft_stack_free(t_stack *stack, void (*free_el)(void *)); + +#endif //FT_STRUCT_H diff --git a/Libft/inc/libft.h b/Libft/inc/libft.h new file mode 100644 index 0000000..283f702 --- /dev/null +++ b/Libft/inc/libft.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:21:06 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include "ft_gen.h" +# include "ft_math.h" +# include "ft_check.h" +# include "ft_conv.h" +# include "ft_str.h" +# include "ft_mem.h" +# include "ft_io.h" +# include "ft_lst.h" +# include "ft_arr.h" +# include "ft_struct.h" +# include "ft_parse.h" + +# include "ft_stat.h" + +#endif