+++ /dev/null
-[submodule "Libft"]
- path = Libft
- url = git://ljiriste.work/Libft
+++ /dev/null
-Subproject commit 4afafbba59ad4abf953ea8aa23f1e61fb2c1daea
--- /dev/null
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_access.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/01/17 11:32:45 by ljiriste #+# #+# */
+/* Updated: 2024/01/17 13:03:13 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_append.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_fill.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_free.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_init.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_insert_col.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_insert_row.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mat_zeros.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/04/02 11:00:49 by ljiriste #+# #+# */
+/* Updated: 2024/04/02 11:23:05 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_access.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/12/09 17:14:49 by ljiriste #+# #+# */
+/* Updated: 2024/06/21 11:43:36 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_append.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_contains.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_copy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/28 12:02:22 by ljiriste #+# #+# */
+/* Updated: 2024/07/05 10:26:54 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_enlarge.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/01/12 17:03:50 by ljiriste #+# #+# */
+/* Updated: 2024/01/12 17:06:04 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stdint.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_erase.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_find.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_find_index.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_forget.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_free.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/12/09 17:37:13 by ljiriste #+# #+# */
+/* Updated: 2023/12/11 20:01:30 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stdlib.h>
+
+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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_init.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_insert.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_is_equal.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_is_setequal.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_is_subset.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_reserve.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_setinsert.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isalnum.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isalpha.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isascii.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isdigit.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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');
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isint.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+// 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_islower.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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');
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isprint.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isspace.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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'));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_isupper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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');
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_atoi.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_ctoa.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/09/05 09:55:17 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:44:01 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_conv.h"
+#include <stdlib.h> //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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_itoa.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:58:10 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:44:23 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_conv.h"
+#include <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_itoa_base.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h> //malloc
+#include <stdint.h> //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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_tolower.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_toupper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_uitoa_base.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h> //malloc
+#include <stdint.h> //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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_swap.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* conversion.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h> // NULL, free
+#include <stdarg.h> //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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* formatting.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h> // NULL, free
+#include <limits.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */
+/* Updated: 2024/03/05 09:21:56 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_printf.h"
+#include <stdarg.h> // va_*
+#include <unistd.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdarg.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* padding.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h> //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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parsing.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdarg.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putchar_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:20:18 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:42:41 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_io.h"
+#include <unistd.h>
+
+void ft_putchar_fd(char c, int fd)
+{
+ write(fd, &c, 1);
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putendl_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:25:41 by ljiriste #+# #+# */
+/* Updated: 2024/03/09 00:12:24 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_io.h"
+#include <unistd.h>
+
+void ft_putendl_fd(const char *s, int fd)
+{
+ ft_putstr_fd(s, fd);
+ write(fd, "\n", 1);
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putnbr_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:27:25 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:40:46 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_io.h"
+#include <unistd.h>
+
+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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putstr_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <unistd.h>
+
+void ft_putstr_fd(const char *s, int fd)
+{
+ if (s == NULL)
+ return ;
+ write(fd, s, ft_strlen(s));
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_next_line.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+#include <unistd.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_next_line.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+typedef struct s_file_thread
+{
+ int fd;
+ char *buffer;
+} t_ft;
+
+#endif
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_at.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_find.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 14:47:42 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_foreach_if.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_merge.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_remove_if.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 14:49:53 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_reverse.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 14:22:47 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_sort.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_sorted_insert.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 15:53:26 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_sorted_merge.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstadd_back.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:33:28 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstadd_front.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstclear.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:45:28 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstdelone.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:43:02 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstiter.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstlast.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:31:01 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include "ft_lst.h"
+
+t_list *ft_lstlast(t_list *lst)
+{
+ if (lst == NULL)
+ return (lst);
+ while (lst->next)
+ lst = lst->next;
+ return (lst);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstmap.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:51:57 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstnew.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:23:00 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstsize.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/16 13:28:58 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:22:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include "ft_lst.h"
+
+int ft_lstsize(t_list *lst)
+{
+ int res;
+
+ res = 0;
+ while (lst)
+ {
+ lst = lst->next;
+ ++res;
+ }
+ return (res);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_abs.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_max.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/02/22 14:29:48 by ljiriste #+# #+# */
+/* Updated: 2024/02/23 08:57:26 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_math.h"
+#include <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_min.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/02/23 08:58:39 by ljiriste #+# #+# */
+/* Updated: 2024/02/23 08:59:40 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_math.h"
+#include <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_sgn.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
+*/
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_bzero.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/14 13:38:08 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <sys/types.h>
+#include "ft_mem.h"
+
+void ft_bzero(void *s, size_t n)
+{
+ ft_memset(s, 0, n);
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_calloc.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <errno.h>
+ if (total_bytes / size != nmemb)
+ {
+ errno = ENOMEM;
+ return (NULL);
+ }
+*/
+#include <stdlib.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_memchr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 10:56:53 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include <sys/types.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_memcmp.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 11:06:15 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <sys/types.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_memcpy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/14 13:35:59 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include <sys/types.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_memmove.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/14 13:36:32 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <sys/types.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_memset.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/14 13:13:22 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <sys/types.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* actions.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* add_line.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <sys/types.h>
+#include <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parse.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+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;
+ }
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parse_inner.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parse_tree_free.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parse_tree_print.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parsing_table_free.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parsing_table_init.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parsing_table_load.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parsing_table_load_helpers.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parsing_table_print.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <sys/types.h>
+
+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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* helpers.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* load_rules.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_remove_space.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h>
+
+// 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_split.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/20 11:51:05 by ljiriste #+# #+# */
+/* Updated: 2024/08/02 14:01:23 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strcat_alloc.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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)));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strchr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */
+/* Updated: 2024/07/21 19:02:33 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <stddef.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strcmp.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/01/13 14:11:31 by ljiriste #+# #+# */
+/* Updated: 2024/01/13 15:00:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <sys/types.h>
+
+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]);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strdup.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/17 13:33:49 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:45:31 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <stdlib.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_striteri.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:45:55 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <stddef.h>
+
+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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strjoin.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strlcat.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/12 17:32:33 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#include <sys/types.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strlcpy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/11 17:28:43 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:45:36 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <sys/types.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strlen.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/14 13:40:59 by ljiriste #+# #+# */
+/* Updated: 2024/05/20 20:47:11 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <sys/types.h>
+
+size_t ft_strlen(const char *s)
+{
+ size_t len;
+
+ if (!s)
+ return (0);
+ len = 0;
+ while (s[len] != '\0')
+ ++len;
+ return (len);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strmapi.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strncat_alloc.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdlib.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strncmp.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:44:40 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <sys/types.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strndup.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/24 09:18:49 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:44:47 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <stdlib.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strnstr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/12 17:15:34 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:45:47 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <stddef.h> // For the NULL pointer
+#include <sys/types.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strrchr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */
+/* Updated: 2024/07/21 18:41:05 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strtrim.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:45:03 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include <sys/types.h>
+#include <stdlib.h>
+
+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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_substr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */
+/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stack_free.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stack_init.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <sys/types.h>
+
+t_ft_stat ft_stack_init(t_stack *stack, size_t el_size)
+{
+ return (ft_vec_init(&stack->vec, el_size));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stack_pop.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stack_pop_forget.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stack_push.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stack_top.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+void *ft_stack_top(t_stack *stack)
+{
+ if (stack->vec.size == 0)
+ return (NULL);
+ return (ft_vec_access(&stack->vec, stack->vec.size - 1));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_arr.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+# 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_check.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_conv.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdint.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_gen.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_io.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdarg.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_math.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_mem.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parse.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <sys/types.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_stat.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_str.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stddef.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_struct.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <sys/types.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* libft.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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