added bgoulard lib + made lib sources in their own directory in build/ for readability

This commit is contained in:
B.Goulard 2024-11-01 00:00:14 +01:00
parent 83cc8419a0
commit 0a390934d6
457 changed files with 21807 additions and 61 deletions

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/10/31 17:19:01 by rparodi ### ########.fr #
# Updated: 2024/10/31 23:54:04 by bgoulard ### ########.fr #
# #
# **************************************************************************** #
@ -24,13 +24,19 @@ RM = rm -rf
# Flags
CFLAGS = -Werror -Wextra -Wall
CFLAGS += -g3 -MMD
#-lm
# CFLAGS += -fsanitize=address
# CFLAGS += -fsanitize=thread
INCLUDES = -I ./includes/ -I ./includes/libft/
# Library flags
LDFLAGS =\
-L./build -lft -lft_personal
# -lm \
SRC = sources/main.c \
sources/error.c \
parsing/arguments.c
@ -52,10 +58,10 @@ END = \033[0m
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) $(LIB_OBJ) footer
bonus: header $(OBJ) footer
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@cc $(CFLAGS) -D BONUS=1 -o $(NAME_BONUS) $(OBJ) $(LIB_OBJ)
@cc $(CFLAGS) -D BONUS=1 -o $(NAME_BONUS) $(OBJ) $(LDFLAGS)
# Clean (make clean)
clean:
@ -73,12 +79,17 @@ fclean: clean
# Restart (make re)
re: header fclean all
build/libft.a:
@make --no-print-directory -C ./libft
build/libft_personal.a:
@make --no-print-directory -C ./libft_personal
# Dependences for all
$(NAME): $(OBJ)
$(NAME): $(OBJ) build/libft.a build/libft_personal.a
@make --no-print-directory -C ./libft lib
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@cc $(CFLAGS) ./build/libft.a -o $(NAME) $(OBJ)
@cc $(CFLAGS) ./build/libft.a -o $(NAME) $(OBJ) $(LDFLAGS)
# Creating the objects
$(OBJDIRNAME)/%.o: %.c

53
libft/.gitignore vendored
View file

@ -1,53 +0,0 @@
to_do*
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

View file

@ -6,7 +6,7 @@
# By: rparodi <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/10/31 18:17:13 by rparodi ### ########.fr #
# Updated: 2024/10/31 23:46:44 by bgoulard ### ########.fr #
# #
# **************************************************************************** #
@ -33,7 +33,7 @@ LDLIBS = -lft
INCLUDES = ./includes/libft/
# Objects
OBJDIRNAME = ../build
OBJDIRNAME = ../build/rparodi
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.c=.o))
SRC = char/ft_isdigit.c \

744
libft_personal/Makefile Normal file
View file

@ -0,0 +1,744 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/05 09:04:05 by bgoulard #+# #+# #
# Updated: 2024/10/31 23:53:29 by bgoulard ### ########.fr #
# #
# **************************************************************************** #
# Colors
GRAY = "\\e[90m"
GREEN = "\\e[42m"
RED = "\\e[41m"
RESET = "\\e[0m"
BOLD = "\\e[1m"
# Commands
CC = clang
NAME = ft_personal
OUTDIR = ../build
TEST_NAME = tests_run
AR = ar
COV = llvm-cov
PRD = llvm-profdata
ECHO = $(shell which echo) -e
PRINTF = $(shell which printf)
# Directories
SRC_DIR = src
BUILD_DIR = ../build/bgoulard
TESTS_DIR = tests
INC_DIR = include
COVERAGE_DIR = coverage
FT_MAP_DIR = ft_map
FT_LIST_DIR = ft_list
FT_STRING_DIR = ft_string
FT_VEC_DIR = ft_vector
FT_OPTIONAL_DIR = ft_optional
FT_ARGS_DIR = ft_args
FT_MATH_DIR = ft_math
FT_PAIR_DIR = ft_pair
# Counpound directories
FT_LIST_LL_DIR = $(FT_LIST_DIR)/ft_ll
FT_LIST_DL_DIR = $(FT_LIST_DIR)/ft_dl
FT_CHR_DIR = $(FT_STRING_DIR)/ft_chr
FT_MEM_DIR = $(FT_STRING_DIR)/ft_mem
FT_STR_DIR = $(FT_STRING_DIR)/ft_str
FT_T_STRING_DIR = $(FT_STRING_DIR)/ft_string
# Compilation flags
##
## To change debug level run make DEBUG_LEVEL=xxx
##
LDFLAGS =
CPPFLAGS = -I$(INC_DIR) -MMD -MP
FFLAGS =\
-fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined \
-fsanitize=leak -fsanitize=pointer-compare \
-fsanitize=pointer-subtract \
-fsanitize-address-use-after-scope -fsanitize=pointer-overflow
CFLAGS =\
-Wall -Wextra $(CPPFLAGS) -Werror -fPIC -fdiagnostics-color
TEST_FLAGS =\
-g2 -DTEST \
-fprofile-instr-generate -ftest-coverage -fcoverage-mapping \
DEBUG_LEVEL =\
0
DEBUG_FLAGS =\
-g2 -DDEBUG $(FFLAGS) -DDEBUG_LEVEL=$(DEBUG_LEVEL)
# Inner variables
MAX_FILE_LEN = 0
TARGET ?= "ALL"
CLOG_FILE = ./compilation.log
# Check for llvm-cov and llvm-profdata
# If not found, use the version 12 if available
# If not found, use the version 14 (latest version)
ifeq (, $(shell which $(COV) 2> /dev/null))
COV = llvm-cov-12
ifeq (, $(shell which $(COV) 2> /dev/null))
COV = llvm-cov-14
endif
endif
ifeq (, $(shell which $(PRD) 2> /dev/null))
PRD = llvm-profdata-12
ifeq (, $(shell which $(PRD) 2> /dev/null))
PRD = llvm-profdata-14
endif
endif
# Sources
FT_PAIR_SRC = \
$(FT_PAIR_DIR)/ft_pair_cmp.c \
$(FT_PAIR_DIR)/ft_pair_destroy.c \
$(FT_PAIR_DIR)/ft_pair_get.c \
$(FT_PAIR_DIR)/ft_pair_new.c \
$(FT_PAIR_DIR)/ft_pair_set.c \
FT_MATH_SRC = \
$(FT_MATH_DIR)/ft_clamp.c \
$(FT_MATH_DIR)/ft_complex.c \
$(FT_MATH_DIR)/ft_intrange.c \
$(FT_MATH_DIR)/ft_log.c \
$(FT_MATH_DIR)/ft_minmax.c \
$(FT_MATH_DIR)/ft_sqrt.c \
$(FT_MATH_DIR)/ft_pow.c \
$(FT_MATH_DIR)/ft_abs.c \
$(FT_MATH_DIR)/ft_align.c \
$(FT_MATH_DIR)/ft_round.c
FT_MAP_SRC = \
$(FT_MAP_DIR)/ft_map_clear.c \
$(FT_MAP_DIR)/ft_map_create.c \
$(FT_MAP_DIR)/ft_map_destroy.c \
$(FT_MAP_DIR)/ft_map_get.c \
$(FT_MAP_DIR)/ft_map_hash.c \
$(FT_MAP_DIR)/ft_map_remove.c \
$(FT_MAP_DIR)/ft_map_set.c
FT_LIST_LL_SRC = \
$(FT_LIST_LL_DIR)/ft_ll_find.c \
$(FT_LIST_LL_DIR)/ft_ll_add.c \
$(FT_LIST_LL_DIR)/ft_ll_clear.c \
$(FT_LIST_LL_DIR)/ft_ll_delete.c \
$(FT_LIST_LL_DIR)/ft_ll_apply.c \
$(FT_LIST_LL_DIR)/ft_ll_iterator.c \
$(FT_LIST_LL_DIR)/ft_ll_map.c \
$(FT_LIST_LL_DIR)/ft_ll_new.c \
$(FT_LIST_LL_DIR)/ft_ll_rev.c \
$(FT_LIST_LL_DIR)/ft_ll_size.c \
$(FT_LIST_LL_DIR)/ft_ll_create.c \
$(FT_LIST_LL_DIR)/ft_ll_getters.c \
$(FT_LIST_LL_DIR)/ft_ll_pushpop.c \
$(FT_LIST_LL_DIR)/ft_ll_sub.c \
FT_LIST_DL_SRC = \
$(FT_LIST_DL_DIR)/ft_dl_apply.c \
$(FT_LIST_DL_DIR)/ft_dl_clear.c \
$(FT_LIST_DL_DIR)/ft_dl_create.c \
$(FT_LIST_DL_DIR)/ft_dl_delete.c \
$(FT_LIST_DL_DIR)/ft_dl_getters.c \
$(FT_LIST_DL_DIR)/ft_dl_iterator.c \
$(FT_LIST_DL_DIR)/ft_dl_pushpop.c \
$(FT_LIST_DL_DIR)/ft_dl_size.c \
$(FT_LIST_DL_DIR)/ft_dl_sub.c \
$(FT_LIST_DL_DIR)/ft_dl_add.c \
$(FT_LIST_DL_DIR)/ft_dl_rev.c \
$(FT_LIST_DL_DIR)/ft_dl_map.c \
$(FT_LIST_DL_DIR)/ft_dl_new.c \
$(FT_LIST_DL_DIR)/ft_dl_find.c \
FT_STR_SRC = \
$(FT_STR_DIR)/ft_atof.c \
$(FT_STR_DIR)/ft_atoi.c \
$(FT_STR_DIR)/ft_atoi_base.c \
$(FT_STR_DIR)/ft_itoa.c \
$(FT_STR_DIR)/ft_itoa_base.c \
$(FT_STR_DIR)/ft_perror.c \
$(FT_STR_DIR)/ft_putendl_fd.c \
$(FT_STR_DIR)/ft_putnbr_fd.c \
$(FT_STR_DIR)/ft_putstr_fd.c \
$(FT_STR_DIR)/ft_shift_args.c \
$(FT_STR_DIR)/ft_split.c \
$(FT_STR_DIR)/ft_splits.c \
$(FT_STR_DIR)/ft_str_isalpha.c \
$(FT_STR_DIR)/ft_str_isalnum.c \
$(FT_STR_DIR)/ft_str_isbool.c \
$(FT_STR_DIR)/ft_str_isdigit.c \
$(FT_STR_DIR)/ft_str_isfloat.c \
$(FT_STR_DIR)/ft_str_ishex.c \
$(FT_STR_DIR)/ft_str_isdouble.c \
$(FT_STR_DIR)/ft_str_isint.c \
$(FT_STR_DIR)/ft_str_islong.c \
$(FT_STR_DIR)/ft_str_isnum.c \
$(FT_STR_DIR)/ft_str_isoct.c \
$(FT_STR_DIR)/ft_str_isvalid.c \
$(FT_STR_DIR)/ft_str_replace.c \
$(FT_STR_DIR)/ft_strappend_c.c \
$(FT_STR_DIR)/ft_strchr.c \
$(FT_STR_DIR)/ft_strclen.c \
$(FT_STR_DIR)/ft_strcmp.c \
$(FT_STR_DIR)/ft_strcnb.c \
$(FT_STR_DIR)/ft_strcspn.c \
$(FT_STR_DIR)/ft_strdup.c \
$(FT_STR_DIR)/ft_strend_with.c \
$(FT_STR_DIR)/ft_strerror.c \
$(FT_STR_DIR)/ft_striteri.c \
$(FT_STR_DIR)/ft_strjoin.c \
$(FT_STR_DIR)/ft_strlcat.c \
$(FT_STR_DIR)/ft_strlcpy.c \
$(FT_STR_DIR)/ft_strlen.c \
$(FT_STR_DIR)/ft_strmapi.c \
$(FT_STR_DIR)/ft_strncmp.c \
$(FT_STR_DIR)/ft_strndup.c \
$(FT_STR_DIR)/ft_strnstr.c \
$(FT_STR_DIR)/ft_strrchr.c \
$(FT_STR_DIR)/ft_strspn.c \
$(FT_STR_DIR)/ft_strstart_with.c \
$(FT_STR_DIR)/ft_strtok.c \
$(FT_STR_DIR)/ft_strtrim.c \
$(FT_STR_DIR)/ft_substr.c \
$(FT_STR_DIR)/ft_utoa.c \
$(FT_STR_DIR)/get_next_line.c \
FT_T_STRING_SRC = \
$(FT_T_STRING_DIR)/ft_string_append.c \
$(FT_T_STRING_DIR)/ft_string_new.c \
$(FT_T_STRING_DIR)/ft_string_put.c \
$(FT_T_STRING_DIR)/ft_string_from.c \
$(FT_T_STRING_DIR)/ft_string_clear.c \
$(FT_T_STRING_DIR)/ft_string_destroy.c \
$(FT_T_STRING_DIR)/ft_string_insert.c \
$(FT_T_STRING_DIR)/ft_string_reserve.c \
$(FT_T_STRING_DIR)/ft_string_resize.c \
$(FT_T_STRING_DIR)/ft_string_shrink.c \
$(FT_T_STRING_DIR)/ft_string_substr.c \
$(FT_T_STRING_DIR)/ft_string_to_str.c \
$(FT_T_STRING_DIR)/ft_string_trim.c \
$(FT_T_STRING_DIR)/ft_string_cmp.c \
$(FT_T_STRING_DIR)/ft_string_get.c \
$(FT_T_STRING_DIR)/ft_string_chr.c \
$(FT_T_STRING_DIR)/ft_string_replace.c \
$(FT_T_STRING_DIR)/ft_string_set.c
FT_MEM_SRC = \
$(FT_MEM_DIR)/ft_apply_2d.c \
$(FT_MEM_DIR)/ft_bzero.c \
$(FT_MEM_DIR)/ft_calloc.c \
$(FT_MEM_DIR)/ft_fd_to_buff.c \
$(FT_MEM_DIR)/ft_free.c \
$(FT_MEM_DIR)/ft_free_2d.c \
$(FT_MEM_DIR)/ft_len_2d.c \
$(FT_MEM_DIR)/ft_malloc.c \
$(FT_MEM_DIR)/ft_memchr.c \
$(FT_MEM_DIR)/ft_memcmp.c \
$(FT_MEM_DIR)/ft_memcpy.c \
$(FT_MEM_DIR)/ft_memmap.c \
$(FT_MEM_DIR)/ft_memmove.c \
$(FT_MEM_DIR)/ft_memset.c \
$(FT_MEM_DIR)/ft_qsort.c \
$(FT_MEM_DIR)/ft_realloc.c \
$(FT_MEM_DIR)/ft_swap.c
FT_CHR_SRC = \
$(FT_CHR_DIR)/ft_isalnum.c \
$(FT_CHR_DIR)/ft_isalpha.c \
$(FT_CHR_DIR)/ft_isascii.c \
$(FT_CHR_DIR)/ft_isdigit.c \
$(FT_CHR_DIR)/ft_ishexdigit.c \
$(FT_CHR_DIR)/ft_islower.c \
$(FT_CHR_DIR)/ft_isoctdigit.c \
$(FT_CHR_DIR)/ft_isprint.c \
$(FT_CHR_DIR)/ft_isspace.c \
$(FT_CHR_DIR)/ft_isupper.c \
$(FT_CHR_DIR)/ft_putchar_fd.c \
$(FT_CHR_DIR)/ft_tolower.c \
$(FT_CHR_DIR)/ft_toupper.c
FT_VEC_SRC = \
$(FT_VEC_DIR)/ft_vec_add.c \
$(FT_VEC_DIR)/ft_vec_apply.c \
$(FT_VEC_DIR)/ft_vec_at.c \
$(FT_VEC_DIR)/ft_vec_cat.c \
$(FT_VEC_DIR)/ft_vec_clear.c \
$(FT_VEC_DIR)/ft_vec_destroy.c \
$(FT_VEC_DIR)/ft_vec_filter.c \
$(FT_VEC_DIR)/ft_vec_get.c \
$(FT_VEC_DIR)/ft_vec_map.c \
$(FT_VEC_DIR)/ft_vec_new.c \
$(FT_VEC_DIR)/ft_vec_pop.c \
$(FT_VEC_DIR)/ft_vec_remove.c \
$(FT_VEC_DIR)/ft_vec_reserve.c \
$(FT_VEC_DIR)/ft_vec_reverse.c \
$(FT_VEC_DIR)/ft_vec_shift.c \
$(FT_VEC_DIR)/ft_vec_shrink.c \
$(FT_VEC_DIR)/ft_vec_sort.c \
$(FT_VEC_DIR)/ft_vec_swap.c \
$(FT_VEC_DIR)/ft_vec_to_array.c
FT_OPTIONAL_SRC = \
$(FT_OPTIONAL_DIR)/ft_optional_chain.c \
$(FT_OPTIONAL_DIR)/ft_optional_copy.c \
$(FT_OPTIONAL_DIR)/ft_optional_destroy.c \
$(FT_OPTIONAL_DIR)/ft_optional_new.c \
$(FT_OPTIONAL_DIR)/ft_optional_unwrap.c
FT_ARGS_SRC = \
$(FT_ARGS_DIR)/ft_arg_custom_checker.c \
$(FT_ARGS_DIR)/ft_parse_args.c \
$(FT_ARGS_DIR)/ft_parse_err.c \
$(FT_ARGS_DIR)/ft_parse_opt.c \
$(FT_ARGS_DIR)/ft_progname.c \
$(FT_ARGS_DIR)/ft_set_opt_args.c \
$(FT_ARGS_DIR)/ft_setup_prog.c \
$(FT_ARGS_DIR)/ft_version.c
# Counpound sources
FT_LIST_SRC = \
$(FT_LIST_LL_SRC) \
$(FT_LIST_DL_SRC) \
FT_STRING_SRC = \
$(FT_CHR_SRC) \
$(FT_MEM_SRC) \
$(FT_STR_SRC) \
$(FT_T_STRING_SRC)
# Tests sources
TESTS_SRC =\
$(TESTS_DIR)/ft_args/tests_custom_checker.c \
$(TESTS_DIR)/ft_args/tests_optlist.c \
$(TESTS_DIR)/ft_args/tests_progname.c \
$(TESTS_DIR)/ft_args/tests_version.c \
$(TESTS_DIR)/ft_args/tests_setup_prog.c \
$(TESTS_DIR)/ft_args/args_tests.c \
\
$(TESTS_DIR)/ft_list/ll_tests/ll_tests_utils.c \
$(TESTS_DIR)/ft_list/ll_tests/ll_list_tests.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_push.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_new.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_map.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_rev.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_sizers.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_apply.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_iterators.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_clear.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_copy.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_create.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_deletors.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_find.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_get.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_subrange.c \
$(TESTS_DIR)/ft_list/ll_tests/tests_list_add.c \
$(TESTS_DIR)/ft_list/dl_tests/dl_tests_utils.c \
$(TESTS_DIR)/ft_list/dl_tests/dl_list_tests.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_add.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_clear.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_copy.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_create.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_delete.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_iterators.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_get.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_subrange.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_map.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_new.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_push.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_rev.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_sizers.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_apply.c \
$(TESTS_DIR)/ft_list/dl_tests/tests_dlist_find.c \
\
$(TESTS_DIR)/ft_map/map_tests.c \
$(TESTS_DIR)/ft_map/tests_map_remove.c \
$(TESTS_DIR)/ft_map/tests_map_hash.c \
$(TESTS_DIR)/ft_map/tests_map_create.c \
$(TESTS_DIR)/ft_map/tests_map_destroy.c \
$(TESTS_DIR)/ft_map/tests_map_set_cmphash.c \
$(TESTS_DIR)/ft_map/tests_map_get.c \
$(TESTS_DIR)/ft_map/tests_map_size.c \
$(TESTS_DIR)/ft_map/tests_map_cappacity.c \
$(TESTS_DIR)/ft_map/tests_map_clear.c \
$(TESTS_DIR)/ft_map/tests_map_set.c \
\
$(TESTS_DIR)/ft_math/tests_abs.c \
$(TESTS_DIR)/ft_math/tests_align.c \
$(TESTS_DIR)/ft_math/tests_clamp.c \
$(TESTS_DIR)/ft_math/tests_complex.c \
$(TESTS_DIR)/ft_math/tests_intrange.c \
$(TESTS_DIR)/ft_math/tests_log.c \
$(TESTS_DIR)/ft_math/tests_minmax.c \
$(TESTS_DIR)/ft_math/tests_pow.c \
$(TESTS_DIR)/ft_math/tests_sqrt.c \
$(TESTS_DIR)/ft_math/tests_round.c \
$(TESTS_DIR)/ft_math/math_tests.c \
\
$(TESTS_DIR)/ft_optional/tests_optional_chain.c \
$(TESTS_DIR)/ft_optional/tests_optional_copy.c \
$(TESTS_DIR)/ft_optional/tests_optional_destroy.c \
$(TESTS_DIR)/ft_optional/tests_optional_dup.c \
$(TESTS_DIR)/ft_optional/tests_optional_from_val.c \
$(TESTS_DIR)/ft_optional/tests_optional_map.c \
$(TESTS_DIR)/ft_optional/tests_optional_new.c \
$(TESTS_DIR)/ft_optional/tests_optional_unwrap.c \
$(TESTS_DIR)/ft_optional/optional_tests.c \
\
$(TESTS_DIR)/ft_pair/tests_pair_cmp.c \
$(TESTS_DIR)/ft_pair/tests_pair_cmp_first.c \
$(TESTS_DIR)/ft_pair/tests_pair_cmp_second.c \
$(TESTS_DIR)/ft_pair/tests_pair_destroy.c \
$(TESTS_DIR)/ft_pair/tests_pair_get_first.c \
$(TESTS_DIR)/ft_pair/tests_pair_get_second.c \
$(TESTS_DIR)/ft_pair/tests_pair_new.c \
$(TESTS_DIR)/ft_pair/tests_pair_set.c \
$(TESTS_DIR)/ft_pair/pair_tests.c \
\
$(TESTS_DIR)/ft_string/ft_char/tests_isalnum.c \
$(TESTS_DIR)/ft_string/ft_char/tests_isalpha.c \
$(TESTS_DIR)/ft_string/ft_char/tests_isascii.c \
$(TESTS_DIR)/ft_string/ft_char/tests_isdigit.c \
$(TESTS_DIR)/ft_string/ft_char/tests_ishexdigit.c \
$(TESTS_DIR)/ft_string/ft_char/tests_isoctdigit.c \
$(TESTS_DIR)/ft_string/ft_char/tests_isspace.c \
$(TESTS_DIR)/ft_string/ft_char/tests_isprint.c \
$(TESTS_DIR)/ft_string/ft_char/tests_puchar.c \
$(TESTS_DIR)/ft_string/ft_char/tests_tolower.c \
$(TESTS_DIR)/ft_string/ft_char/tests_toupper.c \
$(TESTS_DIR)/ft_string/ft_char/ft_char_tests.c \
\
$(TESTS_DIR)/ft_string/ft_mem/tests_apply_2d.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_bzero.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_calloc.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_fd_to_buff.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_free.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_free_2d.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_len_2d.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_memchr.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_memcmp.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_memcpy.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_memmap.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_memmove.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_memset.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_qsort.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_realloc.c \
$(TESTS_DIR)/ft_string/ft_mem/tests_swap.c \
$(TESTS_DIR)/ft_string/ft_mem/mem_tests.c \
\
$(TESTS_DIR)/ft_string/ft_str/test_atoi_base.c \
$(TESTS_DIR)/ft_string/ft_str/test_atoi.c \
$(TESTS_DIR)/ft_string/ft_str/test_atof.c \
$(TESTS_DIR)/ft_string/ft_str/test_gnl.c \
$(TESTS_DIR)/ft_string/ft_str/test_itoa_base.c \
$(TESTS_DIR)/ft_string/ft_str/test_itoa.c \
$(TESTS_DIR)/ft_string/ft_str/test_putendl.c \
$(TESTS_DIR)/ft_string/ft_str/test_putnbr.c \
$(TESTS_DIR)/ft_string/ft_str/test_putstr.c \
$(TESTS_DIR)/ft_string/ft_str/test_shift_args.c \
$(TESTS_DIR)/ft_string/ft_str/test_split.c \
$(TESTS_DIR)/ft_string/ft_str/test_splits.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isalpha.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isalnum.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isdouble.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isdigit.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isbool.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isfloat.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_ishex.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isint.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_islong.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isnum.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isoct.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_isvalid.c \
$(TESTS_DIR)/ft_string/ft_str/test_strchr.c \
$(TESTS_DIR)/ft_string/ft_str/test_strclen.c \
$(TESTS_DIR)/ft_string/ft_str/test_strcmp.c \
$(TESTS_DIR)/ft_string/ft_str/test_strcnb.c \
$(TESTS_DIR)/ft_string/ft_str/test_strcspn.c \
$(TESTS_DIR)/ft_string/ft_str/test_strdup.c \
$(TESTS_DIR)/ft_string/ft_str/test_strend_with.c \
$(TESTS_DIR)/ft_string/ft_str/test_striteri.c \
$(TESTS_DIR)/ft_string/ft_str/test_strjoin.c \
$(TESTS_DIR)/ft_string/ft_str/test_strlcat.c \
$(TESTS_DIR)/ft_string/ft_str/test_strlcpy.c \
$(TESTS_DIR)/ft_string/ft_str/test_strlen.c \
$(TESTS_DIR)/ft_string/ft_str/test_strmapi.c \
$(TESTS_DIR)/ft_string/ft_str/test_strncmp.c \
$(TESTS_DIR)/ft_string/ft_str/test_strndup.c \
$(TESTS_DIR)/ft_string/ft_str/test_strnstr.c \
$(TESTS_DIR)/ft_string/ft_str/test_strrchr.c \
$(TESTS_DIR)/ft_string/ft_str/test_strspn.c \
$(TESTS_DIR)/ft_string/ft_str/test_strstart_with.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_replace.c \
$(TESTS_DIR)/ft_string/ft_str/test_str_replace_chr.c \
$(TESTS_DIR)/ft_string/ft_str/test_strappend_c.c \
$(TESTS_DIR)/ft_string/ft_str/test_strtok.c \
$(TESTS_DIR)/ft_string/ft_str/test_strtrim.c \
$(TESTS_DIR)/ft_string/ft_str/test_substr.c \
$(TESTS_DIR)/ft_string/ft_str/test_utoa.c \
$(TESTS_DIR)/ft_string/ft_str/str_tests.c \
$(TESTS_DIR)/ft_string/string_tests.c \
\
$(TESTS_DIR)/ft_string/ft_string/test_append.c \
$(TESTS_DIR)/ft_string/ft_string/test_append_c.c \
$(TESTS_DIR)/ft_string/ft_string/test_append_n.c \
$(TESTS_DIR)/ft_string/ft_string/test_append_s.c \
$(TESTS_DIR)/ft_string/ft_string/test_append_sn.c \
$(TESTS_DIR)/ft_string/ft_string/test_cap.c \
$(TESTS_DIR)/ft_string/ft_string/test_chr.c \
$(TESTS_DIR)/ft_string/ft_string/test_clear.c \
$(TESTS_DIR)/ft_string/ft_string/test_cmp.c \
$(TESTS_DIR)/ft_string/ft_string/test_cmp_str.c \
$(TESTS_DIR)/ft_string/ft_string/test_destroy.c \
$(TESTS_DIR)/ft_string/ft_string/test_from.c \
$(TESTS_DIR)/ft_string/ft_string/test_from_c.c \
$(TESTS_DIR)/ft_string/ft_string/test_from_n.c \
$(TESTS_DIR)/ft_string/ft_string/test_from_s.c \
$(TESTS_DIR)/ft_string/ft_string/test_from_sn.c \
$(TESTS_DIR)/ft_string/ft_string/test_get.c \
$(TESTS_DIR)/ft_string/ft_string/test_insert.c \
$(TESTS_DIR)/ft_string/ft_string/test_insert_c.c \
$(TESTS_DIR)/ft_string/ft_string/test_insert_n.c \
$(TESTS_DIR)/ft_string/ft_string/test_insert_s.c \
$(TESTS_DIR)/ft_string/ft_string/test_insert_sn.c \
$(TESTS_DIR)/ft_string/ft_string/test_len.c \
$(TESTS_DIR)/ft_string/ft_string/test_ncmp.c \
$(TESTS_DIR)/ft_string/ft_string/test_ncmp_str.c \
$(TESTS_DIR)/ft_string/ft_string/test_new.c \
$(TESTS_DIR)/ft_string/ft_string/test_offset.c \
$(TESTS_DIR)/ft_string/ft_string/test_put.c \
$(TESTS_DIR)/ft_string/ft_string/test_rchr.c \
$(TESTS_DIR)/ft_string/ft_string/test_replace.c \
$(TESTS_DIR)/ft_string/ft_string/test_replace_chr.c \
$(TESTS_DIR)/ft_string/ft_string/test_reserve.c \
$(TESTS_DIR)/ft_string/ft_string/test_resize.c \
$(TESTS_DIR)/ft_string/ft_string/test_roffset.c \
$(TESTS_DIR)/ft_string/ft_string/test_set.c \
$(TESTS_DIR)/ft_string/ft_string/test_set_inplace.c \
$(TESTS_DIR)/ft_string/ft_string/test_set_n.c \
$(TESTS_DIR)/ft_string/ft_string/test_shrink.c \
$(TESTS_DIR)/ft_string/ft_string/test_substr.c \
$(TESTS_DIR)/ft_string/ft_string/test_to_str.c \
$(TESTS_DIR)/ft_string/ft_string/test_trim.c \
$(TESTS_DIR)/ft_string/ft_string/test_trim_chr.c \
$(TESTS_DIR)/ft_string/ft_string/test_trimstr.c \
$(TESTS_DIR)/ft_string/ft_string/t_string_tests.c \
\
$(TESTS_DIR)/ft_vector/tests_vec_add.c \
$(TESTS_DIR)/ft_vector/tests_vec_apply.c \
$(TESTS_DIR)/ft_vector/tests_vec_at.c \
$(TESTS_DIR)/ft_vector/tests_vec_cat.c \
$(TESTS_DIR)/ft_vector/tests_vec_clear.c \
$(TESTS_DIR)/ft_vector/tests_vec_convert_alloc_array.c \
$(TESTS_DIR)/ft_vector/tests_vec_destroy.c \
$(TESTS_DIR)/ft_vector/tests_vec_filter.c \
$(TESTS_DIR)/ft_vector/tests_vec_from_array.c \
$(TESTS_DIR)/ft_vector/tests_vec_from_size.c \
$(TESTS_DIR)/ft_vector/tests_vec_get.c \
$(TESTS_DIR)/ft_vector/tests_vec_map.c \
$(TESTS_DIR)/ft_vector/tests_vec_new.c \
$(TESTS_DIR)/ft_vector/tests_vec_pop.c \
$(TESTS_DIR)/ft_vector/tests_vec_remove.c \
$(TESTS_DIR)/ft_vector/tests_vec_remove_if.c \
$(TESTS_DIR)/ft_vector/tests_vec_reserve.c \
$(TESTS_DIR)/ft_vector/tests_vec_reverse.c \
$(TESTS_DIR)/ft_vector/tests_vec_shift.c \
$(TESTS_DIR)/ft_vector/tests_vec_shrink.c \
$(TESTS_DIR)/ft_vector/tests_vec_sort.c \
$(TESTS_DIR)/ft_vector/tests_vec_swap.c \
$(TESTS_DIR)/ft_vector/tests_vec_to_array.c \
$(TESTS_DIR)/ft_vector/vector_tests.c \
\
$(TESTS_DIR)/main_tests.c \
$(TESTS_DIR)/lambdas_for_tests.c \
$(TESTS_DIR)/tests_utils.c
# Inner variables for targets
STABLE = \
$(FT_MATH_SRC) \
$(FT_LIST_SRC) \
$(FT_VEC_SRC) \
$(FT_STRING_SRC) \
$(FT_MAP_SRC) \
$(FT_OPTIONAL_SRC) \
$(FT_ARGS_SRC) \
$(FT_PAIR_SRC) \
UNSTABLE = \
INNER_SRC = \
$(STABLE)
# Check if user wants to compile unstable sources
# to compile unstable sources run make with TARGET=UNSTABLE
ifeq (UNSTABLE, $(findstring UNSTABLE, $(TARGET)))
INNER_SRC += \
$(UNSTABLE)
endif
# Check if user wants to compile all sources
# to compile all sources run make with TARGET=ALL
ifeq (ALL, $(findstring ALL, $(TARGET)))
INNER_SRC = \
$(STABLE) \
$(UNSTABLE)
endif
# Objects creation
# add prefix to sources to specify the directory src/
SRCS = $(addprefix $(SRC_DIR)/, $(INNER_SRC))
# add prefix to sources to specify the directory build/ for objects
OBJ = $(patsubst %.c, %.o, $(addprefix $(BUILD_DIR)/,$(INNER_SRC)))
# add prefix to sources to specify the directory build/tests/ for test objects
TOBJ = $(patsubst %.c, %.o, $(addprefix $(BUILD_DIR)/$(TESTS_DIR)/,$(INNER_SRC)))
TOBJ += $(patsubst %.c, %.o, $(addprefix $(BUILD_DIR)/,$(TESTS_SRC)))
# Inner variables for rules
# Get the max length of the sources names to align the output
MAX_FILE_LEN = $(shell $(PRINTF) "%s\n" $(SRCS) | \
awk '{print length}' | sort -n | tail -1)
# Rules
# Default rule for objects imported from src/
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@$(ECHO) -n $(GRAY) "building from "
@$(PRINTF) "%*s ... "$(RESET) $(MAX_FILE_LEN) $<
@mkdir -p $(dir $@)
@( $(CC) $(CFLAGS) -c $< -o $@ 2>> $(CLOG_FILE) && \
$(ECHO) $(GREEN) "Success" $(RESET) ) || \
$(ECHO) $(RED) "Failed" $(RESET) \
$(BOLD) "see:" $(CLOG_FILE) $(RESET)
# Rule for tests objects imported from src/
$(BUILD_DIR)/$(TESTS_DIR)/%.o: $(SRC_DIR)/%.c
@$(ECHO) -n $(GRAY) "building from " $< "..." $(RESET)
@mkdir -p $(dir $@)
@( $(CC) $(CFLAGS) $(TEST_FLAGS) -c $< -o $@ \
2>> $(CLOG_FILE) && \
$(ECHO) $(GREEN) "Success" $(RESET) ) || \
$(ECHO) $(RED) "Failed" $(RESET) \
$(BOLD) "see:" $(CLOG_FILE) $(RESET)
# Rule for tests objects imported from tests/
$(BUILD_DIR)/$(TESTS_DIR)/%.o: $(TESTS_DIR)/%.c
@$(ECHO) -n $(GRAY) "building from " $< "..." $(RESET)
@mkdir -p $(dir $@)
@( $(CC) $(CFLAGS) $(TEST_FLAGS) -c $< -o $@ \
2>> $(CLOG_FILE) && \
$(ECHO) $(GREEN) "Success" $(RESET) ) || \
$(ECHO) $(RED) "Failed" $(RESET) \
$(BOLD) "see:" $(CLOG_FILE) $(RESET)
# Default rule
all: $(OUTDIR)/lib$(NAME).a
tmp:
@echo $(SRCS)
so: $(OUTDIR)/lib$(NAME).so
# Rule for shared library
$(OUTDIR)/lib$(NAME).so: $(OBJ)
@$(ECHO) -n $(GRAY) "Making ... " $(RESET) $(BOLD) \
"$(OUTDIR)/lib$(NAME).so" $(RESET) $(GRAY) " ... " $(RESET)
@( $(CC) -shared -o $(OUTDIR)/lib$(NAME).so $(OBJ) 2> /dev/null && \
$(ECHO) $(GREEN) "Success" $(RESET) && $(RM) $(CLOG_FILE) ) || \
$(ECHO) $(RED) "Failed" $(RESET) "see:" $(CLOG_FILE)
# Rule for static library
$(OUTDIR)/lib$(NAME).a: $(OBJ)
@$(ECHO) -n $(GRAY) "Making ... " $(RESET) $(BOLD) \
"$(OUTDIR)/lib$(NAME).a" $(RESET) $(GRAY) " ... " $(RESET)
@( $(AR) -rcs $(OUTDIR)/lib$(NAME).a $(OBJ) 2> /dev/null && \
$(ECHO) $(GREEN) "Success" $(RESET) && $(RM) $(CLOG_FILE) ) || \
$(ECHO) $(RED) "Failed" $(RESET) "see:" $(CLOG_FILE)
# Rule to compile and run tests
$(TEST_NAME): $(TOBJ)
@$(ECHO) -n $(GRAY) "Compiling tests ... " $(RESET)
@$(CC) $(CFLAGS) $(TOBJ) -o $(TEST_NAME) $(TEST_FLAGS) \
$(LDFLAGS) -lgcov \
2>> $(CLOG_FILE) && \
$(ECHO) $(GREEN) "Success" $(RESET) || \
$(ECHO) $(RED) "Failed" $(RESET)
@$(ECHO) -n $(GRAY) "Running tests ... " $(RESET) && \
./$(TEST_NAME) && \
$(ECHO) $(GREEN) "Success" $(RESET) || \
$(ECHO) $(RED) "Failed" $(RESET)
# Rule to generate coverage using llvm
$(COVERAGE_DIR): $(TEST_NAME)
@$(ECHO) -n $(GRAY) "Generating profraw ... " $(RESET) && \
./$(TEST_NAME) && \
$(ECHO) -n $(GRAY) " profdata ... " $(RESET) && \
$(PRD) merge -sparse default.profraw -o \
$(TEST_NAME).profdata && \
$(ECHO) -n $(GRAY) "coverage in html ... " \
$(RESET) && \
$(COV) show -format=html \
-instr-profile=$(TEST_NAME).profdata \
-ignore-filename-regex=$(TESTS_DIR)/* \
--show-branches=count \
./$(TEST_NAME) -output-dir=$(COVERAGE_DIR) > /dev/null && \
$(RM) *.profraw *.profdata && \
$(ECHO) $(GREEN) "Success" $(RESET) || \
$(ECHO) $(RED) "Failed" $(RESET)
# Rule to compile using debug flags
debug:
@$(ECHO) $(GRAY) "Compiling debug, flags are" $(RESET) \
"$(CFLAGS) $(DEBUG_FLAGS)" $(RESET) && \
make --no-print-directory -C . re \
CFLAGS="$(CFLAGS) $(DEBUG_FLAGS)" && \
make --no-print-directory -C . \
so CFLAGS="$(CFLAGS) $(DEBUG_FLAGS)" && \
$(ECHO) $(GREEN) "Success" $(RESET) || \
$(ECHO) $(RED) "Failed" $(RESET)
# Rule to clean objects
clean:
@$(ECHO) -n $(GRAY) "Clean ... " $(RESET) && \
( $(RM) -rf $(BUILD_DIR) $(CLOG_FILE) $(TEST_NAME) *.gcov \
*.gcno *.gcda 2> /dev/null && \
$(ECHO) $(GREEN) "Success" $(RESET) ) || \
$(ECHO) $(RED) "Failed" $(RESET)
# Rule to clean objects and libraries/compiled files
fclean: clean
@$(ECHO) -n $(GRAY) "FClean ... " $(RESET) && \
( $(RM) -rf $(OUTDIR)/lib$(NAME).a l$(OUTDIR)/ib$(NAME).so \
$(COVERAGE_DIR) $(TEST_NAME) \
*.profraw *.profdata Doxygen 2> /dev/null && \
$(ECHO) $(GREEN) "Success" $(RESET) ) || \
$(ECHO) $(RED) "Failed" $(RESET)
# Rule to recompile
re: fclean
@$(ECHO) -ne $(GRAY) "Recompiling ..." $(RESET) "\n" && \
make --no-print-directory all && \
$(ECHO) $(GREEN) "Success" $(RESET) || \
$(ECHO) $(RED) "Failed" $(RESET)
-include $(OBJ:.o=.d)
# rule to force rules to be executed even if files exist
.PHONY: re fclean clean

View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_args.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/13 23:42:28 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:01:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_ARGS_H
# define FT_ARGS_H
/* ************************************************************************** */
/* */
/* Module: FT_ARGS */
/* Prefix: ft_arg */
/* */
/* The Module FT_ARGS provides an easy way to handle task related to the cli */
/* arguments both for manipulation and parsing. */
/* */
/* ************************************************************************** */
// Change version with -DVERSION="x.y.z-W" via Makefile
# ifndef VERSION
# define VERSION "1.0.0"
# endif
# include "ft_defs.h"
# include <sys/types.h>
# include "ft_args_types.h"
/* @file: src/ft_args/ft_arg_custom_checker.c */
void ft_arg_set_custom_checker(t_data_is custom_checker);
t_data_is ft_arg_get_custom_checker(void);
/* @file: src/ft_args/ft_setup_prog.c */
void ft_setup_prog(const char *const *av);
/// @file: src/ft_args/ft_parse_args
int ft_parse_args(const char **argv, void *usr_control_struct);
void ft_set_opt_list(const t_opt *opt_list);
const t_opt *ft_get_opt_list(void);
void ft_set_progname(const char *program_name);
const char *ft_progname(void);
void ft_set_version(const char *version);
const char *ft_progversion(void);
#endif

View file

@ -0,0 +1,146 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_args_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/13 23:42:58 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:25:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_ARGS_TYPES_H
# define FT_ARGS_TYPES_H
# define ARG_MASK_ATYPE ~3
# define ARG_MASK_ANY_ARG 0x1
/*
OPT_ARG = 1,// technically the inverse of OPT_NOARG so mask it
OPT_EQSIGN = 2,// if it has an arg and not eqsign,
it will be the next arg aka space separated
OPT_OTHER = 4,// custom type, see set_custom_checker
*
*/
/// @brief Enum to define the type of the option
/// @details The type of the option is defined by the flags that are set in the
/// t_opt structure. The flags are defined in the enum e_opt_type.
/// The flags are defined as follows:
/// - OPT_ARG: The option has an argument
/// - OPT_EQSIGN: The option has an argument and the argument is separated by an
/// equal sign '=' (e.g. --option=arg). If the flag is not set, the argument is
/// considered to be in the next argument (e.g. --option arg).
/// - OPT_OTHER: The argument of the option is of another custom type (e.g. a
/// structure or an array or file with a custom extension etc.)
/// - OPT_INT: The argument of the option is an integer
/// - OPT_STRING: The argument of the option is a string
/// - OPT_BOOL: The argument of the option is a boolean (0 | 1 | true | false)
/// - OPT_FLOAT: The argument of the option is a float
/// - OPT_LONG: The argument of the option is a long
/// - OPT_DOUBLE: The argument of the option is a double
/// - OPT_ALPHANUM: The argument of the option is an alphanumeric string
/// - OPT_HEX: The argument of the option is a hexadecimal number
/// - OPT_OCT: The argument of the option is an octal number
/// @details The flags can be combined with the bitwise OR operator '|'.
/// @details The flags OPT_INT, OPT_STRING, OPT_BOOL, OPT_FLOAT, OPT_LONG,
/// OPT_DOUBLE, OPT_ALPHANUM, OPT_HEX, OPT_OCT are the standard types that can
/// be used to define the type of the argument of the option. The flag OPT_OTHER
/// is a custom type that can be used to define the type of the argument of the
/// option. The custom type can be defined by setting a custom checker with the
/// function set_custom_checker. The custom checker will be called to check the
/// argument of the option. The custom checker should return 0 if the argument
/// is valid and -1 if the argument is invalid. The custom checker should be
/// defined as follows:
/// @code
/// int custom_checker(char *arg)
/// {
/// // check the argument
/// return (0 or -1);
/// }
/// @endcode
/// @see: set_custom_checker to set a custom checker for the argument of type
/// OPT_OTHER.
/// @warning: System limitations: Currently you cannot set an option which uses
/// more than one argument. You can set an option which uses a custom type
/// (OPT_OTHER) but the custom type should be defined as a single argument.
/// @see: t_opt
/// @see: set_custom_checker
///
typedef enum e_opt_type
{
OPT_ARG = 1,
OPT_EQSIGN = 2,
OPT_OTHER = 4,
OPT_INT = 8,
OPT_STRING = 12,
OPT_BOOL = 16,
OPT_FLOAT = 20,
OPT_LONG = 24,
OPT_DOUBLE = 28,
OPT_ALPHANUM = 32,
OPT_HEX = 36,
OPT_OCT = 40,
} t_opt_type;
/// @brief Structure to define the options
/// @param opt_long_name: The long name of the option "name" (without the "--")
/// @param opt_short_name: The short name of the option 'n' (without the "-")
/// @param opt_type: The type of the option
/// @param opt_func: The function to call when the option is found casted as
/// void * and later casted depending on the flags
/// @details An example of how to use the structure:
/// @code
/// #include "ft_args.h"
/// #include "ft_args_types.h"
/// #include "ft_string.h"
/// #include <stdio.h>
///
/// typedef struct s_control {
/// int n;
/// char *name;
/// float f;
/// } t_control;
///
/// void set_n(void *control_struct, char *arg) {
/// printf("set_n\n");
/// ((t_control *)control_struct)->n = ft_atoi(arg);
/// }
/// void set_name(void *control_struct, char *arg) {
/// printf("set_name\n");
/// ((t_control *)control_struct)->name = arg;
/// }
/// void set_f(void *control_struct, char *arg) {
/// printf("set_f\n");
/// ((t_control *)control_struct)->f = ft_atof(arg);
/// }
/// int main(int argc, char **argv) {
/// t_control control;
/// t_opt opt_list[] = {
/// {"nbr", 'n', set_n, OPT_INT | OPT_EQSIGN},
/// {"name", 'a', set_name, OPT_STRING | OPT_EQSIGN},
/// {"float", 'f', set_f, OPT_FLOAT | OPT_EQSIGN},
/// {NULL, 0, NULL, 0}
/// };
///
/// ft_bzero(&control, sizeof(t_control));
/// ft_set_opt_list(opt_list);
/// ft_parse_args(argv, &control);
/// printf("n = %d\nname = %s\nf = %f\n", control.n, control.name,
/// control.f);
/// return (0);
/// }
/// @endcode
/// @details The previous code will parse the arguments and set the values of
/// the control structure according to the options found in the arguments.
/// @details the function set_n will be called when the option --nbr or -n is
/// found in the arguments, and will be casted as a void (*) (void *, char *).
///
typedef struct s_opt
{
char *long_name;
char short_name;
void *func;
t_opt_type type;
} t_opt;
#endif

View file

@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_char.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 16:59:44 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:49:37 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_CHAR_H
# define FT_CHAR_H
/* ************************************************************************** */
/* */
/* Module: FT_CHAR */
/* Prefix: ft_* */
/* */
/* The module ft_char - (a sub module of the module string) provides an easy */
/* way to handle task or queries related purely to chars. It was */
/* separated from module ft_string to lighten and clarify it. */
/* */
/* ************************************************************************** */
# include <stdbool.h>
/// @file: src/ft_string/ft_char/ft_isoctdigit.c
int ft_isoctdigit(int c);
/// @file: src/ft_string/ft_char/ft_ishexdigit.c
int ft_ishexdigit(int c);
/// @brief check if the char is a space character
/// @param c char to check
/// @return 1 if the char is a space character, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isspace.c
int ft_isspace(int c);
/// @brief check if the char is a lower case character
/// @param c char to check
/// @return 1 if the char is a lower case character, 0 otherwise
/// @file: src/ft_string/ft_char/ft_islower.c
int ft_islower(int c);
/// @brief check if the char is an ascii character
/// @param c char to check
/// @return 1 if the char is an ascii character, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isascii.c
int ft_isascii(int c);
/// @brief check if the char is an upper case character
/// @param c char to check
/// @return 1 if the char is an upper case character, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isupper.c
int ft_isupper(int c);
/// @brief check if the char is an alphanumeric character
/// @param c char to check
/// @return 1 if the char is an alphanumeric character, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isalnum.c
int ft_isalnum(int c);
/// @brief check if the char is a digit
/// @param c char to check
/// @return 1 if the char is a digit, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isdigit.c
int ft_isdigit(int c);
/// @brief check if the char is a letter
/// @param c char to check
/// @return 1 if the char is a letter, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isalpha.c
int ft_isalpha(int c);
/// @brief pass a char to lower case
/// @param c char to pass to lower case
/// @return the char in lower case
/// @file: src/ft_string/ft_char/ft_tolower.c
int ft_tolower(int c);
/// @brief check if the char is printable
/// @param c char to check
/// @return 1 if the char is printable, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isprint.c
int ft_isprint(int c);
/// @brief print the char on the specified file descriptor
/// @param c char to print
/// @param fd file descriptor to print on
/// @return 1 if the char was printed, -1 otherwise
int ft_putchar_fd(char c, int fd);
/// @brief pass a char to lower case
/// @param c char to pass to lower case
/// @return the char in lower case
/// @file: src/ft_string/ft_char/ft_toupper.c
int ft_toupper(int c);
#endif /* FT_CHAR_H */

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_colors.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/20 15:01:55 by bgoulard #+# #+# */
/* Updated: 2024/08/20 15:04:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_COLORS_H
# define FT_COLORS_H
# define FT_C_BOLD "\033[1m"
# define FT_C_DIM "\033[2m"
# define FT_C_ITALIC "\033[3m"
# define FT_C_UNDERLINED "\033[4m"
# define FT_C_BLINK "\033[5m"
# define FT_C_BLACK "\033[30m"
# define FT_C_RED "\033[31m"
# define FT_C_GREEN "\033[32m"
# define FT_C_YELLOW "\033[33m"
# define FT_C_BLUE "\033[34m"
# define FT_C_MAGENTA "\033[35m"
# define FT_C_CYAN "\033[36m"
# define FT_C_WHITE "\033[37m"
# define FT_C_BG_BLACK "\033[40m"
# define FT_C_BG_RED "\033[41m"
# define FT_C_BG_GREEN "\033[42m"
# define FT_C_BG_YELLOW "\033[43m"
# define FT_C_BG_BLUE "\033[44m"
# define FT_C_BG_MAGENTA "\033[45m"
# define FT_C_BG_CYAN "\033[46m"
# define FT_C_BG_WHITE "\033[47m"
# define FT_C_RESET "\033[0m"
#endif

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_defs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/21 14:55:34 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:00:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_DEFS_H
# define FT_DEFS_H
# include <stdbool.h>
# include <stddef.h>
/// @brief Type of function to compare two elements
/// @param A The first element to compare
/// @param B The second element to compare
/// @return Standard cmp c function ( < 0, 0, > 0 for a < b, a == b, a > b)
typedef int (*t_data_cmp)(const void *, const void *);
/// @brief Type of function to apply on a node data
/// @param Data The data to apply the function on
/// @return Void
typedef void (*t_data_apply)(void *);
/// @brief Type of function to see if a node data is something
/// @return True if the data is what we are looking for, false otherwise
typedef bool (*t_data_is)(const void *);
/// @brief Type of function to transform a data into something else
/// @return The new data
typedef void *(*t_data_tr)(const void *);
/// @brief Type of function to transform inplace a data into some other data
/// @return pointer to the data (think of strcat or memcpy, returns a pointer
/// to dst)
typedef void *(*t_data_tr_i)(void *);
#endif /* FT_DEFS_H */

View file

@ -0,0 +1,415 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_errno.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 14:10:11 by bgoulard #+# #+# */
/* Updated: 2024/07/08 14:24:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_ERRNO_H
# define FT_ERRNO_H
/* ************************************************************************** */
/* */
/* Module: ft_string */
/* */
/* Prefix: */
/* */
/* Description: */
/* - This file is part of the ft_string module. Was separated for */
/* clarity. */
/* */
/* ************************************************************************** */
# define EPERM 1
// EPERM 'Operation not permitted'
# define ENOENT 2
// ENOENT 'No such file or directory'
# define ESRCH 3
// ESRCH 'No such process'
# define EINTR 4
// EINTR 'Interrupted system call'
# define EIO 5
// EIO 'I/O error'
# define ENXIO 6
// ENXIO 'No such device or address'
# define E2BIG 7
// E2BIG 'Argument list too long'
# define ENOEXEC 8
// ENOEXEC 'Exec format error'
# define EBADF 9
// EBADF 'Bad file number'
# define ECHILD 10
// ECHILD 'No child processes'
# define EAGAIN 11
// EAGAIN 'Try again'
# define ENOMEM 12
// ENOMEM 'Out of memory'
# define EACCES 13
// EACCES 'Permission denied'
# define EFAULT 14
// EFAULT 'Bad address'
# define ENOTBLK 15
// ENOTBLK 'Block device required'
# define EBUSY 16
// EBUSY 'Device or resource busy'
# define EEXIST 17
// EEXIST 'File exists'
# define EXDEV 18
// EXDEV 'Cross-device link'
# define ENODEV 19
// ENODEV 'No such device'
# define ENOTDIR 20
// ENOTDIR 'Not a directory'
# define EISDIR 21
// EISDIR 'Is a directory'
# define EINVAL 22
// EINVAL 'Invalid argument'
# define ENFILE 23
// ENFILE 'File table overflow'
# define EMFILE 24
// EMFILE 'Too many open files'
# define ENOTTY 25
// ENOTTY 'Not a typewriter'
# define ETXTBSY 26
// ETXTBSY 'Text file busy'
# define EFBIG 27
// EFBIG 'File too large'
# define ENOSPC 28
// ENOSPC 'No space left on device'
# define ESPIPE 29
// ESPIPE 'Illegal seek'
# define EROFS 30
// EROFS 'Read-only file system'
# define EMLINK 31
// EMLINK 'Too many links'
# define EPIPE 32
// EPIPE 'Broken pipe'
# define EDOM 33
// EDOM 'Math argument out of domain of func'
# define ERANGE 34
// ERANGE 'Math result not representable'
# define EDEADLK 35
// EDEADLK 'Resource deadlock would occur'
# define ENAMETOOLONG 36
// ENAMETOOLONG 'File name too long'
# define ENOLCK 37
// ENOLCK 'No record locks available'
# define ENOSYS 38
// ENOSYS 'Function not implemented'
# define ENOTEMPTY 39
// ENOTEMPTY 'Directory not empty'
# define ELOOP 40
// ELOOP 'Too many symbolic links encountered'
# define ENOMSG 42
// ENOMSG 'No message of desired type'
# define EIDRM 43
// EIDRM 'Identifier removed'
# define ECHRNG 44
// ECHRNG 'Channel number out of range'
# define EL2NSYNC 45
// EL2NSYNC 'Level 2 not synchronized'
# define EL3HLT 46
// EL3HLT 'Level 3 halted'
# define EL3RST 47
// EL3RST 'Level 3 reset'
# define ELNRNG 48
// ELNRNG 'Link number out of range'
# define EUNATCH 49
// EUNATCH 'Protocol driver not attached'
# define ENOCSI 50
// ENOCSI 'No CSI structure available'
# define EL2HLT 51
// EL2HLT 'Level 2 halted'
# define EBADE 52
// EBADE 'Invalid exchange'
# define EBADR 53
// EBADR 'Invalid request descriptor'
# define EXFULL 54
// EXFULL 'Exchange full'
# define ENOANO 55
// ENOANO 'No anode'
# define EBADRQC 56
// EBADRQC 'Invalid request code'
# define EBADSLT 57
// EBADSLT 'Invalid slot'
# define EBFONT 59
// EBFONT 'Bad font file format'
# define ENOSTR 60
// ENOSTR 'Device not a stream'
# define ENODATA 61
// ENODATA 'No data available'
# define ETIME 62
// ETIME 'Timer expired'
# define ENOSR 63
// ENOSR 'Out of streams resources'
# define ENONET 64
// ENONET 'Machine is not on the network'
# define ENOPKG 65
// ENOPKG 'Package not installed'
# define EREMOTE 66
// EREMOTE 'Object is remote'
# define ENOLINK 67
// ENOLINK 'Link has been severed'
# define EADV 68
// EADV 'Advertise error'
# define ESRMNT 69
// ESRMNT 'Srmount error'
# define ECOMM 70
// ECOMM 'Communication error on send'
# define EPROTO 71
// EPROTO 'Protocol error'
# define EMULTIHOP 72
// EMULTIHOP 'Multihop attempted'
# define EDOTDOT 73
// EDOTDOT 'RFS specific error'
# define EBADMSG 74
// EBADMSG 'Not a data message'
# define EOVERFLOW 75
// EOVERFLOW 'Value too large for defined data type'
# define ENOTUNIQ 76
// ENOTUNIQ 'Name not unique on network'
# define EBADFD 77
// EBADFD 'File descriptor in bad state'
# define EREMCHG 78
// EREMCHG 'Remote address changed'
# define ELIBACC 79
// ELIBACC 'Can not access a needed shared library'
# define ELIBBAD 80
// ELIBBAD 'Accessing a corrupted shared library'
# define ELIBSCN 81
// ELIBSCN '.lib section in a.out corrupted'
# define ELIBMAX 82
// ELIBMAX 'Attempting to link in too many shared libraries'
# define ELIBEXEC 83
// ELIBEXEC 'Cannot exec a shared library directly'
# define EILSEQ 84
// EILSEQ 'Illegal byte sequence'
# define ERESTART 85
// ERESTART 'Interrupted system call should be restarted'
# define ESTRPIPE 86
// ESTRPIPE 'Streams pipe error'
# define EUSERS 87
// EUSERS 'Too many users'
# define ENOTSOCK 88
// ENOTSOCK 'Socket operation on non-socket'
# define EDESTADDRREQ 89
// EDESTADDRREQ 'Destination address required'
# define EMSGSIZE 90
// EMSGSIZE 'Message too long'
# define EPROTOTYPE 91
// EPROTOTYPE 'Protocol wrong type for socket'
# define ENOPROTOOPT 92
// ENOPROTOOPT 'Protocol not available'
# define EPROTONOSUPPORT 93
// EPROTONOSUPPORT 'Protocol not supported'
# define ESOCKTNOSUPPORT 94
// ESOCKTNOSUPPORT 'Socket type not supported'
# define EOPNOTSUPP 95
// EOPNOTSUPP 'Operation not supported on transport endpoint'
# define EPFNOSUPPORT 96
// EPFNOSUPPORT 'Protocol family not supported'
# define EAFNOSUPPORT 97
// EAFNOSUPPORT 'Address family not supported by protocol'
# define EADDRINUSE 98
// EADDRINUSE 'Address already in use'
# define EADDRNOTAVAIL 99
// EADDRNOTAVAIL 'Cannot assign requested address'
# define ENETDOWN 100
// ENETDOWN 'Network is down'
# define ENETUNREACH 101
// ENETUNREACH 'Network is unreachable'
# define ENETRESET 102
// ENETRESET 'Network dropped connection because of reset'
# define ECONNABORTED 103
// ECONNABORTED 'Software caused connection abort'
# define ECONNRESET 104
// ECONNRESET 'Connection reset by peer'
# define ENOBUFS 105
// ENOBUFS 'No buffer space available'
# define EISCONN 106
// EISCONN 'Transport endpoint is already connected'
# define ENOTCONN 107
// ENOTCONN 'Transport endpoint is not connected'
# define ESHUTDOWN 108
// ESHUTDOWN 'Cannot send after transport endpoint shutdown'
# define ETOOMANYREFS 109
// ETOOMANYREFS 'Too many references: cannot splice'
# define ETIMEDOUT 110
// ETIMEDOUT 'Connection timed out'
# define ECONNREFUSED 111
// ECONNREFUSED 'Connection refused'
# define EHOSTDOWN 112
// EHOSTDOWN 'Host is down'
# define EHOSTUNREACH 113
// EHOSTUNREACH 'No route to host'
# define EALREADY 114
// EALREADY 'Operation already in progress'
# define EINPROGRESS 115
// EINPROGRESS 'Operation now in progress'
# define ESTALE 116
// ESTALE 'Stale NFS file handle'
# define EUCLEAN 117
// EUCLEAN 'Structure needs cleaning'
# define ENOTNAM 118
// ENOTNAM 'Not a XENIX named type file'
# define ENAVAIL 119
// ENAVAIL 'No XENIX semaphores available'
# define EISNAM 120
// EISNAM 'Is a named type file'
# define EREMOTEIO 121
// EREMOTEIO 'Remote I/O error'
# define EDQUOT 122
// EDQUOT 'Quota exceeded'
# define ENOMEDIUM 123
// ENOMEDIUM 'No medium found'
# define EMEDIUMTYPE 124
// EMEDIUMTYPE 'Wrong medium type'
# define ECANCELED 125
// ECANCELED 'Operation Canceled'
# define ENOKEY 126
// ENOKEY 'Required key not available'
# define EKEYEXPIRED 127
// EKEYEXPIRED 'Key has expired'
# define EKEYREVOKED 128
// EKEYREVOKED 'Key has been revoked'
# define EKEYREJECTED 129
// EKEYREJECTED 'Key was rejected by service'
# define EOWNERDEAD 130
// EOWNERDEAD 'Owner died'
# define ENOTRECOVERABLE 131
// ENOTRECOVERABLE 'State not recoverable'
#endif /* FT_ERRNO_H */

View file

@ -0,0 +1,450 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 11:40:02 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:04:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_LIST_H
# define FT_LIST_H
/* ************************************************************************** */
/* */
/* Module: FT_LIST */
/* Prefix: ft_dl, ft_ll */
/* */
/* The module FT_LIST provides a way to handle task related to linked */
/* lists whether the list happens to be simply linked, doubly linked. */
/* Due to the nature of the module (handling different types of the same */
/* variety of struct the prototypes are arranged using functionality */
/* instead of type as would be common. */
/* Currently supported types: */
/* -simply_linked, */
/* -doubly_linked. */
/* */
/* ************************************************************************** */
# include "ft_list_types.h"
# include "ft_defs.h"
/* ************************************************************************** */
/* ADD */
/* ************************************************************************** */
/// @brief Add a node at the end of the list
/// @param head The head of the list
/// @param added The node to add
void ft_dl_add_back(t_dlist **head, t_dlist *const added);
/// @brief Add a node at the beginning of the list
/// @param head The head of the list
/// @param added The node to add
void ft_dl_add_front(t_dlist **head, t_dlist *const added);
/// @brief Add a node at the begining of the list
/// @param head The head of the list
/// @param added The node to add
/// @return void
void ft_ll_add_front(t_list **lst, t_list *const new_node);
/// @brief Add a node at the end of the list
/// @param head The head of the list
/// @param added The node to add
/// @return void
void ft_ll_add_back(t_list **lst, t_list *const new_node);
/* ************************************************************************** */
/* APPLY */
/* ************************************************************************** */
/// @brief Apply a function on every node of the list
/// @param start The start of the list
/// @param applied The function to apply
/// @return The number of nodes applied
size_t ft_dl_apply(const t_dlist *start, t_data_apply applied);
/// @brief Apply a function on every node of the list until the end
/// @param start The start of the list
/// @param end The end of the list
/// @param applied The function to apply
/// @return The number of nodes applied
size_t ft_dl_apply_range(const t_dlist *start, const t_dlist *end,
t_data_apply applied);
/// @brief Apply a function on every node of the list until the end
/// @param start The start of the list
/// @param end The end of the list
/// @param applied The function to apply
/// @return The number of nodes applied
size_t ft_dl_apply_range_node(const t_dlist *start, const t_dlist *end,
t_dnode_apply applied);
/// @brief Apply a function on every node of the list
/// @param lst The list
/// @param f The function to apply
/// @return void
void ft_ll_apply(const t_list *lst, t_data_apply f);
/// @brief Apply a function on every node of the list until the element end
/// @param lst The list
/// @param end The end of the list
/// @param f The function to apply
/// @return void
void ft_ll_apply_range(const t_list *lst, const t_list *end,
t_data_apply f);
/// @brief Apply a function on every node of the list until the element end
/// @param lst The list
/// @param end The end of the list
/// @param f The function to apply
/// @return void
void ft_ll_apply_range_node(const t_list *lst, const t_list *end,
t_lnode_apply f);
/* ************************************************************************** */
/* CLEAR */
/* ************************************************************************** */
/// @brief Clear a list
/// @param head The adress of head of the list to clear
/// @param del The function to delete the data
/// @return The number of nodes deleted
/// @note The head is set to NULL
size_t ft_dl_clear(t_dlist **head, t_data_apply del);
/// @brief Clear a list until the end
/// @param start The start of the list
/// @param end The end of the list
/// @param del The function to delete the data
/// @return The number of nodes deleted
size_t ft_dl_clear_range(t_dlist *start, t_dlist *end, t_data_apply del);
/// @brief Clear a list
/// @param lst The list
/// @param del The function to delete the data
/// @return void
void ft_ll_clear(t_list **lst, t_data_apply del);
// TODO: implement clear range for ll
/* ************************************************************************** */
/* CREATE & COPY */
/* ************************************************************************** */
/// @brief Create a new node
/// @param data The data of the node
/// @return The new node
t_dlist *ft_dl_create(const void *data);
/// @brief Copy a node
/// @param other The node to copy
/// @return The new node
/// @note The node is a copy of the original node but does not copy the data
/// in a new ptr
t_dlist *ft_dl_copy_node(const t_dlist *const other);
/// @brief Copy a node
/// @param other The node to copy
/// @return The new node
/// @note The node is a copy of the original node but does not copy the data
/// in a new ptr. This is wgy it doesn't need a delete function.
t_dlist *ft_dl_copy_list(const t_dlist *const other);
/// @brief Create a new node
/// @param data The data of the node
/// @return The new node
t_list *ft_ll_create(const void *const data);
/// @brief Copy a node
/// @param other The node to copy
/// @return The new node
t_list *ft_ll_copy_node(const t_list *const other);
/// @brief Copy a list
/// @param other The list to copy
/// @return The new list
t_list *ft_ll_copy_list(const t_list *const other);
/* ************************************************************************** */
/* DELETE */
/* ************************************************************************** */
/// @brief Delete a node
/// @param node The node to delete
/// @param del The function to delete the data
/// @return The number of nodes deleted
int ft_dl_delete_self(t_dlist *node, t_data_apply del);
/// @brief Delete a node
/// @param start The node from which to delete
/// @param end The node until which to delete
/// @return The number of nodes deleted
size_t ft_dl_delete_range(t_dlist *start, const t_dlist *target,
t_data_apply del);
/// @brief Delete a doubly linked list entirely
/// @param head The head of the list
/// @return The number of nodes deleted
size_t ft_dl_delete(t_dlist **head, t_data_apply del);
// TODO: implement delete dup for dl
// not currently implemented - idea of the function:
// /// @brief Delete duplicates node
// /// @param head The head of the list
// /// @param cmp The compare function
// /// @param del The function to delete the data
// /// @return The number of nodes deleted
// size_t ft_dl_delete_dup(t_dlist **src, t_data_cmp cmp,
// t_data_apply del);
/// @brief Delete a node
/// @param node The node to delete
/// @param del The function to delete the data
/// @return void
/// @note this is a useles function, it is here for compatibility with the
/// libft project.
void ft_ll_delone(t_list *lst, t_data_apply del);
/// @brief Delete a node
/// @param lst The node from which to delete
/// @param target The node until which to delete
/// @param del The function to delete the data
/// @return void
size_t ft_ll_delete_range(t_list *lst, const t_list *end, t_data_apply del);
// TODO: implement delete for ll
// delete should delete the whole list
// TODO: implement delete dup for ll
// not currently implemented - idea of the function:
// /// @brief Delete duplicates node
// /// @param head The head of the list
// /// @param cmp The compare function
// /// @param del The function to delete the data
// /// @return The number of nodes deleted
// size_t ft_listdelete_dup(t_list **src, t_data_cmp cmp,
// t_data_apply del);
/* ************************************************************************** */
/* FIND */
/* ************************************************************************** */
/// @brief Find a node in a list
/// @param head The head of the list
/// @param data The data to find
/// @param cmp The compare function
/// @return The node found or NULL
t_dlist *ft_dl_find(const t_dlist *head, const void *data, t_data_cmp cmp);
/// @brief Find a node in a list
/// @param list The list
/// @param data The data to find
/// @param cmp The compare function
/// @return The node found or NULL
void *ft_ll_find(const t_list *list, const void *data, t_data_cmp cmp);
/* ************************************************************************** */
/* GETTERS */
/* ************************************************************************** */
/// @brief Get the datas of a list
/// @param src The list
/// @return The datas of the list
/// @note The datas are in the same order as the nodes and the pointers to the
/// datas need to be freed
void **ft_dl_get_datas(const t_dlist *src);
/// @brief Get the nodes of a list
/// @param src The list
/// @return The nodes of the list
/// @note The nodes are in the same order as the datas and the pointers to the
/// nodes need to be freed
t_dlist **ft_dl_get_nodes(const t_dlist *src);
/// @brief Get the datas of a list
/// @param src The list
/// @return The datas of the list
void **ft_ll_get_datas(const t_list *src);
/// @brief Get the nodes of a list
/// @param src The list
/// @return The nodes of the list
t_list **ft_ll_get_nodes(const t_list *src);
/* ************************************************************************** */
/* ITERATORS */
/* ************************************************************************** */
/// @brief Get the last node of a list
/// @param head The head of the list
/// @param index The index of the node
/// @return The node at index or NULL
t_dlist *ft_dl_at(const t_dlist *head, size_t index);
/// @brief Get the last node of a list
/// @param head The head of the list
/// @return The last node of the list
t_dlist *ft_dl_end(const t_dlist *head);
/// @brief Get the first node of a list
/// @param head The head of the list
/// @return The first node of the list
t_dlist *ft_dl_begin(const t_dlist *head);
/// @brief Get the last node of a list
/// @param lst The list
/// @return The last node of the list
t_list *ft_ll_end(const t_list *lst);
/// @brief Get the node of a list at position index
/// @param lst The list
/// @param index The index of the node
/// @return The node at index or NULL
t_list *ft_ll_at(const t_list *lst, size_t index);
/* ************************************************************************** */
/* MAP */
/* ************************************************************************** */
/// @brief Apply a function on every node of the list
/// @param lst The list
/// @param f The function to apply
/// @param del The function to delete the data if allocation fails
/// @return The new list
t_dlist *ft_dl_map(const t_dlist *lst, t_data_tr f, t_data_apply del);
/// @brief Apply a function on every node of the list
/// @param lst The list
/// @param f The function to apply
/// @param del The function to delete the data if allocation fails
/// @return The new list
t_list *ft_ll_map(const t_list *lst, t_data_tr f, t_data_apply del);
/* ************************************************************************** */
/* NEW */
/* ************************************************************************** */
/// @brief Create a new node
/// @return The new node
t_dlist *ft_dl_new(void);
/// @brief Create a new node
/// @return The new node
t_list *ft_ll_new(void);
/* ************************************************************************** */
/* PUSH & POP */
/* ************************************************************************** */
/// @brief Push a node at the beginning of the list
/// @param node The head of the list
/// @param data The data of the node
/// @return The new head of the list
t_dlist *ft_dl_push(t_dlist **node, const void *data);
/// @brief Push a node at the end of the list
/// @param node The head of the list
/// @param data The data of the node
/// @return The new head of the list
t_dlist *ft_dl_push_back(t_dlist **node, const void *data);
/// @brief Pop a node at the beginning of the list
/// @param node The head of the list
/// @return The data of the node
void *ft_dl_pop(t_dlist **node);
/// @brief Pop a node at the end of the list
/// @param node The head of the list
/// @return The data of the node
void *ft_dl_pop_back(t_dlist **node);
/// @brief Push a node at the beginning of the list
/// @param lst The head of the list
/// @param data The data of the node
/// @return The new head of the list
t_list *ft_ll_push(t_list **lst, const void *data);
/// @brief Push a node at the end of the list
/// @param lst The head of the list
/// @param data The data of the node
/// @return The new head of the list
t_list *ft_ll_push_back(t_list **lst, const void *data);
/// @brief Pop a node at the beginning of the list
/// @param lst The head of the list
/// @return The data of the node
void *ft_ll_pop(t_list **lst);
/// @brief Pop a node at the end of the list
/// @param lst The head of the list
/// @return The data of the node
void *ft_ll_pop_back(t_list **lst);
/* ************************************************************************** */
/* REVERSE */
/* ************************************************************************** */
/// @brief Reverse a list
/// @param head The head of the list
/// @return the new head of the list
t_dlist *ft_dl_rev(t_dlist **head);
/// @brief Reverse a list
/// @param head The head of the list
/// @return the new head of the list
t_list *ft_ll_rev(t_list **head);
/* ************************************************************************** */
/* SIZE */
/* ************************************************************************** */
/// @brief Get the size of a list
/// @param head The head of the list
/// @return The size of the list
size_t ft_dl_size(const t_dlist *head);
/// @brief Get the size of a list
/// @param head The head of the list
/// @param function The function to check if the data is something
/// @return The size of the list
size_t ft_dl_size_of_data(const t_dlist *head, t_data_is function);
/// @brief Get the size of a list
/// @param lst The list
/// @return The size of the list
size_t ft_ll_size(const t_list *lst);
/// @brief Get the size of a list
/// @param lst The list
/// @param function The function to check if the data is something
/// @return The size of the list
size_t ft_ll_size_match(const t_list *lst, t_data_is function);
// todo: rename either ll_size_of_data or dl_size_match
/* ************************************************************************** */
/* SUB */
/* ************************************************************************** */
/// @brief Get a sublist of a list
/// @param src The list
/// @param to The node until which to get the sublist
/// @return The sublist
/// @note The sublist is a copy of the original list but does not copy
/// the data in a new ptr. This is why it doesn't need a delete function.
t_dlist *ft_dl_subrange(const t_dlist *src, const t_dlist *to);
/// @brief Get a sublist of a list
/// @param src The list
/// @param to The node until which to get the sublist
/// @note The sublist is a copy of the original list but does not copy
/// the data in a new ptr. This is why it doesn't need a delete function.
/// @return The sublist
t_list *ft_ll_subrange(const t_list *lst, const t_list *end);
#endif /* FT_LIST_H */

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 10:37:44 by bgoulard #+# #+# */
/* Updated: 2024/05/19 17:48:07 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_LIST_TYPES_H
# define FT_LIST_TYPES_H
# include <stdbool.h>
# define FTLIST_SUCCESS 0
# define FTLIST_FAILURE 1
/// @brief Structure representing a node in a list
/// @param data The data of the node
/// @param next The next node
typedef struct s_list
{
void *data;
struct s_list *next;
} t_list;
/// @brief Structure representing a node in a doubly linked list
/// @param data The data of the node
/// @param next The next node
/// @param prev The previous node
typedef struct s_dl_list
{
struct s_dl_list *next;
struct s_dl_list *prev;
void *data;
} t_dlist;
/// @brief Type of function to apply on a doubly linked list node
typedef void (*t_dnode_apply)(t_dlist *);
/// @brief Type of function to apply on a simply linked list node
typedef void (*t_lnode_apply)(t_list *);
#endif /* FT_LIST_TYPES_H */

View file

@ -0,0 +1,144 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 16:08:04 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:05:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_MAP_H
# define FT_MAP_H
/* ************************************************************************** */
/* */
/* Module: FT_MAP */
/* Prefix: ft_map */
/* */
/* The module FT_MAP provides a way to handle hash maps and function */
/* related to hashmaps. */
/* */
/* ************************************************************************** */
# include "ft_defs.h"
# include "ft_map_types.h"
/// @brief Create a new map
/// @param capacity possible number of elements in the map
/// @return a pointer to the new map
/// @file ft_map/ft_map_create.c
t_map *ft_map_create(size_t capacity);
/// @brief Destroy a map and free the nodes
/// @param map map to destroy
/// @file ft_map/ft_map_destroy.c
/// @return void
void ft_map_destroy(t_map *map);
/// @brief Destroy a map and free the nodes and the user data in them using a
/// function passed as an argument
/// @param map map to destroy
/// @param free_data function to free the data in the map
/// @file ft_map/ft_map_destroy.c
/// @return void
void ft_map_destroy_free(t_map *map, t_data_apply free_data);
/// @brief Clears a map
/// @param map map to clear
/// @file ft_map/ft_map_clear.c
/// @return void
void ft_map_clear(t_map *map);
/// @brief Set a value in a map
/// @param map map to set the value in
/// @param key key of the value
/// @param value value to set
/// @param size size of the key
/// @file ft_map/ft_map_set.c
/// @return true if the value was set, otherwise if key creation failed false
bool ft_map_set(t_map *map, const void *key, const void *value,
size_t size);
/// @brief Set the compare function of a map
/// @param map map to set the compare function
/// @param cmp compare function newly set
/// @file ft_map/ft_map_set.c
/// @return void
void ft_map_set_cmp(t_map *map, t_data_cmp cmp);
/// @brief Set the hash function of a map
/// @param map map to set the hash function
/// @param hash hash function newly set
/// @file ft_map/ft_map_set.c
/// @return void
void ft_map_set_hash(t_map *map, t_memhash hash);
/// @brief Get a node from a map
/// @param map map to get the node from
/// @param key key of the node
/// @param size size of the key
/// @file ft_map/ft_map_get.c
t_map_node *ft_map_get_node(t_map *map, const void *key, size_t size);
/// @brief Get a value from a map
/// @param map map to get the value from
/// @param key key of the value
/// @param key_size size of the key
/// @file ft_map/ft_map_get.c
/// @return a pointer to the user data or NULL if the key is not found
void *ft_map_get(t_map *map, const void *key, size_t key_size);
/// @brief Get the number of elements in a map
/// @param map map to get the size from
/// @file ft_map/ft_map_size.c
/// @return the number of elements in the map
size_t ft_map_size(const t_map *map);
/// @brief Get the capacity of a map
/// @param map map to get the capacity from
/// @file ft_map/ft_map_get.c
/// @return the capacity of the map
size_t ft_map_capacity(const t_map *map);
/// @brief Remove a value from a map
/// @param map map to remove the value from
/// @param key key of the value
/// @param size size of the key
/// @file ft_map/ft_map_remove.c
/// @return the value removed or NULL if the key is not found
void *ft_map_remove(t_map *map, const void *key, size_t size);
/// @brief Hash a key
/// @param key key to hash
/// @param size size of the key
/// @file ft_map/ft_map_hash.c
/// @return the hash of the key
size_t ft_hash_djb2(const void *key, size_t size);
/// @brief Hash a key
/// @param key key to hash
/// @param size size of the key
/// @file ft_map/ft_map_hash.c
/// @return the hash of the key
size_t ft_hash_sdbm(const void *key, size_t size);
/// @brief Hash a key
/// @param key key to hash
/// @param size size of the key
/// @return the hash of the key
size_t ft_hash_fnv1a(const void *key, size_t size);
/// @brief Hash a key
/// @param key key to hash
/// @param size size of the key
/// @file ft_map/ft_map_hash.c
/// @return the hash of the key
/// @note this hash function is not very good, full of collisions
/// but it's very fast, easy to understand and never overflows.
/// use it ONLY for TESTing purposes
size_t ft_hash_dummy(const void *key, size_t size);
#endif /* FT_MAP_H */

View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 13:02:13 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:14:26 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_MAP_TYPES_H
# define FT_MAP_TYPES_H
# include "ft_list_types.h"
# include "ft_defs.h"
# include "ft_vector_types.h"
# include <stdbool.h>
# include <stddef.h>
/// @brief Structure representing a node in a map
/// @param data The data of the node
/// @param key The key of the node
/// @param used Whether the node is used or not
typedef struct s_map_node
{
void *data;
const void *key;
size_t hash;
} t_map_node;
typedef size_t (*t_memhash)(const void *data, size_t data_len);
/// @brief Structure representing a map
/// @param capacity The capacity of the map
/// @param size The size of the map
/// @param nodes The nodes of the map (array of lists distributed by hash)
/// @param cmp The compare function of the map
/// @param hash The hash function of the map
/// @code
/// #include "ft_map.h"
/// #include "ft_string.h"
/// #include <stdio.h>
///
/// static void goodbye(void *data)
/// {
/// printf("Goodbye %s\n", (char *)data);
/// free(data);
/// }
///
/// int main()
/// {
/// t_map *map;
/// char *key = "key";
/// char *value = ft_strdup("value");
/// char *ptr;
///
/// map = ft_map_create(5);
///
/// ft_map_set(map, key, value, ft_strlen(key));
/// ft_map_set(map, "key2", ft_strdup("val32"), ft_strlen("key2"));
///
/// ptr = ft_map_remove(map, "key2", ft_strlen("key2"));
/// goodbye(ptr);
///
/// ft_map_set(map, "key3", ft_strdup("val3"), ft_strlen("key3"));
///
/// printf("---\n");
/// ft_map_destroy_free(map, goodbye);
/// return (0);
/// }
/// @endcode
/// @see ft_map_create
typedef struct s_map
{
size_t capacity;
size_t *weights;
size_t w_total;
t_list **nodes;
t_data_cmp cmp;
t_memhash hash;
t_vector *reserved_nodes;
} t_map;
#endif /* FT_MAP_TYPES_H */

View file

@ -0,0 +1,187 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_math.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 18:59:37 by bgoulard #+# #+# */
/* Updated: 2024/08/21 21:38:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_MATH_H
# define FT_MATH_H
/* ************************************************************************** */
/* */
/* Module: FT_MATH */
/* Prefix: ft_* */
/* */
/* The FT_MATH module provides a way to handle task or queries related */
/* to arithmetic operations. */
/* */
/* ************************************************************************** */
# include "ft_math_types.h"
# include <stddef.h>
/// @brief Return the nearest aligned value of size on the alignment
/// @param size The size to align
/// @param alignment The alignment to use
/// @return The aligned value of size on the alignment
/// @note Optimized for power of 2 alignment
size_t ft_align_2(size_t size, size_t alignment);
/// @brief Return the nearest aligned value of size on the alignment
/// @param size The size to align
/// @param alignment The alignment to use
/// @return The aligned value of size on the alignment
/// @note See: ft_align_2 for an optimized version for power of 2
size_t ft_align(size_t size, size_t alignment);
/// @brief return the logaritm of the number in the specified base
/// @param nbr number to get the logaritm
/// @param base base of the logaritm
/// @return the logaritm of the number in the specified base. in case of error
/// return -1
int ft_llogof(long long nbr, int base);
/// @brief return the logaritm of the number in the specified base
/// @param nbr number to get the logaritm
/// @param base base of the logaritm
/// @return the logaritm of the number in the specified base. in case of error
/// return -1
int ft_ullogof(unsigned long long nbr, int base);
/// @brief return the logaritm of the number in the specified base
/// @param nbr number to get the logaritm
/// @param base base of the logaritm
/// @return the logaritm of the number in the specified base. in case of error
/// return -1
int ft_logof(int nbr, int base);
/// @brief return the logaritm of the number in the specified base
/// @param nbr number to get the logaritm
/// @param base base of the logaritm
/// @return the logaritm of the number in the specified base. in case of error
/// return -1
int ft_log(int nbr);
/// @brief returns the minimum of a and b
/// @param a first number
/// @param b second number
/// @return the smallest between a and b
int ft_min(int a, int b);
/// @brief returns the maximum of a and b
/// @param a first number
/// @param b second number
/// @return the biggest between a and b
int ft_max(int a, int b);
/// @brief Clamp a value between a minimum and a maximum
/// @param value The value to clamp
/// @param min The minimum value
/// @param max The maximum value
/// @return The clamped value between or at the minimum or maximum
/// @note This is a clamp function aka inferior and superior to min and max
/// are set to min and max
/// @file ft_clamp.c
int ft_clamp(int value, int min, int max);
/// @brief Clamp a value between a minimum and a maximum
/// @param value The value to clamp
/// @param min The minimum value
/// @param max The maximum value
/// @return The clamped value between or at the minimum or maximum
/// @note See ft_clamp.
/// @file ft_clamp.c
float ft_clamp_f(float value, float min, float max);
/// @brief Clamp a value between a minimum and a maximum
/// @param value The value to clamp
/// @param min The minimum value
/// @param max The maximum value
/// @return The clamped value between or at the minimum or maximum
/// @note See ft_clamp.
/// @file ft_clamp.c
double ft_clamp_d(double value, double min, double max);
/// @brief Take a value in a range and puts it in another range of 1 to new_max
/// @param value The value to range
/// @param min The minimum value of the range
/// @param max The maximum value of the range
/// @param new_max The maximum value of the new range
/// @return The value ranged between 1 and new_max
int ft_range(int value, int min, int max, int new_max);
/// @brief Take a value in a range and puts it in another range of 1 to new_max
/// @param value The value to range
/// @param min The minimum value of the range
/// @param max The maximum value of the range
/// @param new_max The maximum value of the new range
/// @return The value ranged between 1 and new_max
/// @note See ft_range.
float ft_range_f(float value, float min, float max, float new_max);
/// @brief Take a value in a range and puts it in another range of 1 to new_max
/// @param value The value to range
/// @param min The minimum value of the range
/// @param max The maximum value of the range
/// @param new_max The maximum value of the new range
/// @return The value ranged between 1 and new_max
/// @note See ft_range.
double ft_range_d(double value, double min, double max, double new_max);
/// @brief Return the absolute value of a
/// @param a The value to get the absolute value
/// @return The absolute value of a
int ft_abs(int a);
/// @brief Return the rounded value of x
/// @param x The value to round
/// @return The rounded value of x
/// @note This function round the value to the nearest integer
double ft_round(double x);
/// @brief Return the power of a number x to the power of y
/// @param x The number to power
/// @param y The power
/// @return The result of x to the power of y
size_t ft_pow(size_t x, size_t y);
/// @brief Return the root square of a number
/// @param nb The number to get the root square
/// @return The root square of nb
/// @note This function use the newton's method to get the root square
/// @note If nb is negative, return -1
/// @file ft_sqrt.c
double ft_sqrt(double nb);
/// @brief Return the absolute value of a
/// @param a The value to get the absolute value
/// @return The absolute value of a
/// @note See ft_abs.
double ft_complex_abs(t_complex nb);
/// @brief Add a long to a complex number
/// @param nb The complex number to add the factor
/// @param factor The factor to add to the complex number
/// @return The complex number with the factor added
/// @note See ft_complex_addl.
t_complex ft_complex_addl(t_complex nb, long factor);
/// @brief Multiply a complex number by a long
/// @param nb The complex number to multiply
/// @param factor The factor to multiply the complex number
/// @return The complex number multiplied by the factor
t_complex ft_complex_mull(t_complex nb, long factor);
/// @brief Multiply a complex number by a double
/// @param nb The complex number to multiply
/// @param factor The factor to multiply the complex number
/// @return The complex number multiplied by the factor
t_complex ft_complex_muld(t_complex nb, double factor);
#endif

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_math_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 19:08:43 by bgoulard #+# #+# */
/* Updated: 2024/05/18 19:09:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_MATH_TYPES_H
# define FT_MATH_TYPES_H
typedef struct s_complex
{
double real;
double imaginary;
} t_complex;
typedef struct s_vec2
{
double x;
double y;
} t_vec2;
typedef struct s_vec3
{
double x;
double y;
double z;
} t_vec3;
typedef struct s_vec4
{
double x;
double y;
double z;
double w;
} t_vec4;
#endif

View file

@ -0,0 +1,145 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:07:13 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:08:15 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_OPTIONAL_H
# define FT_OPTIONAL_H
/* ************************************************************************** */
/* */
/* Module: FT_OPTIONAL */
/* Prefix: ft_optional */
/* */
/* The FT_OPTIONAL module provides a way to add the optional type to the */
/* project, this type is used to handle the case where a function can */
/* return a value or nothing, or to chain functions on a pointer */
/* (creating a 'pipeline' for the data to follow). */
/* */
/* ************************************************************************** */
# include "ft_defs.h"
# include "ft_optional_types.h"
# include <stdbool.h>
/// @brief Create a new empty optional
/// @return The new optional
/// @note prefer creating from stack
/// @note WARNING: you must free the optional after use
t_optional *ft_optional_new(void);
/// @brief Create a new optional with a value
/// @param ptr The value to store in the optional
/// @return The new optional
/// @note prefer creating from stack
/// @note WARNING: you must free the optional after use
t_optional *ft_optional_from_val(void *ptr);
/// @brief Copies the value of an optional into another
/// @param dest The destination optional
/// @param src The source optional
void ft_optional_copy(t_optional *dest, t_optional *src);
/// @brief Chain functions calls on an optional until either there are no more
/// functions to call or one of the functions returns NULL
/// @param opt The optional to chain
/// @param f The functions to call
/// @return true if all functions were called, false otherwise
/// @note This function was made to emulate object programming pipelines
/// @note WARNING: The function will return false if the optional is empty
/// or if the function list is NULL
/// @code
/// #include "ft_optional.h"
/// #include "ft_optional_types.h"
/// #include "ft_string.h"
/// #include "ft_defs.h"
/// #include <fcntl.h>
/// #include <stdio.h>
///
/// char *ft_fd_to_buff(int fd);
///
/// void *check_file_ext(void *filename) {
/// if (!filename)
/// return (NULL);
/// if (ft_strend_with(filename, ".txt"))
/// return (filename);
/// return (printf("Error: file must be a .txt\n"), free(filename), NULL);
/// }
///
/// void *load_file_to_buff(void *filename) {
/// int fd;
/// char *buff;
///
/// fd = open(filename, O_RDONLY);
/// if (fd < 0)
/// {
/// printf("Error: could not open file\n");
/// return (free(filename), NULL);
/// }
/// buff = ft_fd_to_buff(fd);
/// return (close(fd), free(filename), buff);
/// }
///
/// int main(int ac, char **av) {
/// t_optional opt = {OPT_NONE, NULL};
/// const t_data_tr_i function_list[] = {
/// check_file_ext,
/// load_file_to_buff,
/// NULL};
///
/// if (ac != 2)
/// return (printf("Usage: %s <file.txt>\n", av[0]), 1);
/// opt.pres = OPT_SOME;
/// opt.val = ft_strdup(av[1]);
/// if (!ft_optional_chain(&opt, function_list))
/// return (printf("Error: could not load file\n"), 1);
/// // Do something with the file
/// printf("File content: %s\n", (char *)opt.val);
/// return (free(opt.val), ft_optional_destroy(&opt), 0);
/// }
///
/// @endcode
/// @note Demonstrated above, we crate a list of operations to perform on a file
/// name, we then chain the operations on the optional until either:
/// 1) an operation returns NULL (we return false)
/// -or-
/// 2) there are no more operations to perform (we return true)
bool ft_optional_chain(t_optional *opt, const t_data_tr_i *f);
/// @brief Map a result of a function on an optional
/// @param opt The optional with the value to map
/// @param f The function to call
/// @return The new optional with the result of the functions.
/// @note WARNING: The function will return an empty optional if the result
/// of the function is NULL.
t_optional ft_optional_map(t_optional *opt, void *(**f)(void *));
/// @brief Destroy an optional
/// @param opt The optional to destroy
/// @return true if the optional was destroyed, false otherwise
/// @note WARNING: The optional must be empty before destroying it
/// otherwise the function will return false
bool ft_optional_destroy(t_optional *opt);
/// @brief Duplicate an optional
/// @param org The optional to duplicate
/// @return The new optional
/// @note prefer creating from stack
/// @note WARNING: you must free the optional after use
t_optional *ft_optional_dup(t_optional *org);
/// @brief Return the value contained in an optional
/// @param opt The optional to unwrap
/// @return The value contained in the optional
/// @note WARNING: The optional must not be empty otherwise the function will
/// crash
void *ft_optional_unwrap(t_optional opt);
#endif /* FT_OPTIONAL_H */

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:07:23 by bgoulard #+# #+# */
/* Updated: 2024/05/19 17:47:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_OPTIONAL_TYPES_H
# define FT_OPTIONAL_TYPES_H
# include <stddef.h>
typedef enum e_optional_type
{
OPT_NONE,
OPT_SOME,
} t_optional_type;
typedef struct s_optional
{
t_optional_type pres;
void *val;
} t_optional;
#endif /* FT_OPTIONAL_TYPES_H */

View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:14:08 by bgoulard #+# #+# */
/* Updated: 2024/07/06 17:12:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PAIR_H
# define FT_PAIR_H
/* ************************************************************************** */
/* */
/* Module: FT_PAIR */
/* Prefix: ft_pair */
/* */
/* This module provide a pair structure and a simple way to interact with it. */
/* */
/* ************************************************************************** */
# include "ft_defs.h"
# include "ft_pair_types.h"
/// @brief Create a new pair
/// @param first The first element of the pair
/// @param second The second element of the pair
/// @return The new pair allocated
/// @note avoid this function if you can, use ft_pair_set instead with a
/// stack allocated pair.
/// @note Free with basic free call.
t_pair *ft_pair_new(void *first, void *second);
/// @brief Set the first and second element of the pair
/// @param pair The pointer to the pair to set
/// @param first The first element of the pair
/// @param second The second element of the pair
/// @return void
void ft_pair_set(t_pair *pair, void *first, void *second);
/// @brief Get the first element of the pair
/// @param pair The pointer to the pair
/// @return The first element of the pair
/// @note Null safe.
void *ft_pair_first(t_pair *pair);
/// @brief Get the second element of the pair
/// @param pair The pointer to the pair
/// @return The second element of the pair
/// @note Null safe.
void *ft_pair_second(t_pair *pair);
/// @brief Frees the pair
/// @param pair The pointer to the pair to free.
/// @param del_f The function to apply to the first elements of the pair before
/// freeing the pair.
/// @param del_s The function to apply to the second elements of the pair before
/// freeing the pair.
/// @note The pair pointer is set to NULL.
void ft_pair_destroy(t_pair **pair, t_data_apply del_f, t_data_apply del_s);
/// @brief Compare two pairs
/// @param pair1 The first pair to compare
/// @param pair2 The second pair to compare
/// @param cmp The comparison function to use
/// @return The result of the comparison
/// @note The comparison function should conform to the c standard,
/// aka < 0, 0 or > 0 for pair1 < pair2, pair1 == pair2 or pair1 > pair2
/// respectively.
/// @note If no comparison function is provided, the comparison will be done
/// on the pointer value.
int ft_pair_cmp(t_pair *pair1, t_pair *pair2, t_data_cmp cmp);
/// @brief Compare two pairs
/// @param pair1 The first pair to compare
/// @param pair2 The second pair to compare
/// @param cmp The comparison function to use
/// @return The result of the comparison
/// @note Same as ft_pair_cmp but compare the first element of the pair
int ft_pair_cmp_first(t_pair *pair1, t_pair *pair2, t_data_cmp cmp);
/// @brief Compare two pairs
/// @param pair1 The first pair to compare
/// @param pair2 The second pair to compare
/// @param cmp The comparison function to use
/// @return The result of the comparison
/// @note Same as ft_pair_cmp but compare the second element of the pair
int ft_pair_cmp_second(t_pair *pair1, t_pair *pair2, t_data_cmp cmp);
#endif

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:14:56 by bgoulard #+# #+# */
/* Updated: 2024/06/23 22:15:51 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PAIR_TYPES_H
# define FT_PAIR_TYPES_H
typedef struct s_pair
{
void *first;
void *second;
} t_pair;
#endif

View file

@ -0,0 +1,925 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:25:27 by bgoulard #+# #+# */
/* Updated: 2024/08/21 21:42:42 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_STRING_H
# define FT_STRING_H
/* ************************************************************************** */
/* */
/* Module: FT_STRING */
/* Prefix: ft_, ft_string (t_string) */
/* */
/* This module provides a usefull c function for manipulationg string and */
/* manipulating memory. It also includes the type t_string a string that */
/* caches it's malloc and allocates by chunks instead of 'JIT' in the */
/* code. */
/* */
/* ************************************************************************** */
// sys types
# include <stddef.h>
# include <stdbool.h>
// self types
# include "ft_defs.h"
# include "ft_string_types.h"
// malloc + free
# include <stdlib.h>
# include <unistd.h>
// errno
# include <errno.h>
/* ************************************************************************** */
/* ** FT_MEM SUB MODULE ** */
/* ************************************************************************** */
/// @brief apply the function f on each byte of the memory
/// @param s start of the memory
/// @param n size of the memory
/// @param f function to apply
/// @return void
/// @note the memory is modified in place
void ft_apply_2d(void **array, t_data_apply f);
/// @brief fill the memory with 0
/// @param s start of the memory
/// @param n size of the memory
/// @return void
void ft_bzero(void *s, size_t n);
/// @brief allocate memory
/// @param size size of the memory to allocate
/// @return pointer to the allocated memory otherwise NULL
void *ft_malloc(size_t size);
/// @brief allocate memory and fill it with 0
/// @param nmemb number of elements
/// @param weight size of each element
/// @return pointer to the allocated memory
void *ft_calloc(size_t nmemb, size_t weight);
/// @brief load the content of the file descriptor into a string
/// @param fd file descriptor to read from
/// @return pointer to the string otherwise NULL
/// @note You must free the returned string
char *ft_fd_to_buff(int fd);
/// @brief allocate memory and copy the content of the source memory
/// @param ptr pointer to the source memory.
/// @param sizeNew size of the destination memory
/// @param sizeOld size of the source memory
/// @return pointer to a larger chunk of allocated memory otherwise NULL
/// @note WARNING: the pointer is FREE'ed after the copy in case of success
void *ft_realloc(void *ptr, size_t sizeNew, size_t sizeOld);
/// @brief free the memory
/// @param ptr pointer to the memory to free (set to NULL after)
/// @return void
void ft_free(void **ptr);
/// @brief free the memory
/// @param arr pointer to the 2d array to free.
/// @return void
/// @note the array is not set to NULL after as memory is freed and no longer
/// valid to write to.
void ft_free_2d(void **arr);
/// @brief return the length of the 2d array
/// @param array pointer to the 2d array
/// @return the length of the 2d array
/// @note the array must be NULL terminated
size_t ft_len_2d(const void *const *array);
/// @brief search for the first occurence of c in the memory
/// @param s start of the memory
/// @param c char to search
/// @param n size of the memory
/// @return pointer to the first occurence of c in the memory otherwise NULL
void *ft_memchr(const void *s, int c, size_t n);
/// @brief compare the memory
/// @param s1 start of the first memory
/// @param s2 start of the second memory
/// @param n size of the memory to compare
/// @return 0 if the memory are identical, otherwise the difference between the
/// first different char
int ft_memcmp(const void *s1, const void *s2, size_t n);
/// @brief copy the memory
/// @param dest start of the destination memory
/// @param src start of the source memory
/// @param n size of the memory to copy
/// @return pointer to the destination memory
void *ft_memcpy(void *dest, const void *src, size_t n);
/// @brief map memory region src to new region using f
/// @param src start of the source memory
/// @param nb_e number of elements
/// @param f function to apply
/// @return pointer to the new memory or NULL
void **ft_memmap(void **src, size_t nb_e, t_data_tr f);
/// @brief copy the memory
/// @param dest start of the destination memory
/// @param src start of the source memory
/// @param n size of the memory to copy
/// @return pointer to the destination memory
void *ft_memmove(void *dest, const void *src, size_t n);
/// @brief set the memory with c
/// @param s start of the memory
/// @param c char to set
/// @param n size of the memory
/// @return pointer to the memory
void *ft_memset(void *s, int c, size_t n);
/// @brief swap the memory
/// @param a first memory
/// @param b second memory
/// @return void
void ft_swap(void **a, void **b);
/// @brief sort the memory with the cmp function by chunks of size
/// @param array pointer to the memory
/// @param nmb number of chunks
/// @param size size of each chunk
/// @param cmp comparison function
/// @return void
/// @WARNING Do not use. Not implemented fully.
void ft_qsort(void *array, size_t nmb, size_t size, t_data_cmp cmp);
/* ************************************************************************** */
/* ** FT_PUT SUB MODULE ** */
/* ************************************************************************** */
/// @brief print the string on the specified file descriptor followed by a new
/// line
/// @param s string to print
/// @param fd file descriptor to print on
/// @return void
int ft_putendl_fd(const char *s, int fd);
/// @brief print the string on the specified file descriptor
/// @param nbr string to print
/// @param fd file descriptor to print on
/// @return void
int ft_putnbr_fd(int nbr, int fd);
/// @brief print the string on the specified file descriptor
/// @param s string to print
/// @param fd file descriptor to print on
/// @return void
int ft_putstr_fd(const char *s, int fd);
/* ************************************************************************** */
/* ** FT_STR MAIN MODULE ** */
/* ************************************************************************** */
/// @brief convert the string to a float
/// @param str string to convert
/// @return the float converted from the string
double ft_atof(const char *str);
/// @brief convert the string to an int
/// @param str string to convert
/// @return the int converted from the string
int ft_atoi(const char *str);
/// @brief convert the string to an int in the specified base
/// @param str string to convert
/// @param base base of the string
/// @return the int converted from the string
int ft_atoi_base(const char *str, const char *base);
/// @brief convert a string with the int converted in ascii chars
/// @param nbr number to convert
/// @return pointer to the string.
/// @note You must free the returned string
char *ft_itoa(int nbr);
/// @brief convert the int to a string in the specified base
/// @param nbr number to convert
/// @param base base of the string to return.
/// @return pointer to the string.
/// @note You must free the returned string
char *ft_itoa_base(int nbr, const char *base);
/// @brief convert the unsigned int to a string
/// @param nbr number to convert
/// @return pointer to the string.
/// @note You must free the returned string
char *ft_utoa(unsigned int nbr);
/// @brief Return a pointer to the first occurrence of any character contained
/// in the string delims in the string str
/// @param str_init string to search into WARNING: the string is modified by
/// this function. DO NOT PASS A CONST STRING.
/// @param delims string of delimiters
/// @return a pointer to the first occurrence of any character contained in the
/// string delims in the string str otherwise NULL
char *ft_strtok(char *str_init, const char *delims);
/// @brief Split the string str with the specified delimiter
/// @param str String to split
/// @param delim Char to split the string
/// @return A pointer to the array of string
/// @note You must free the returned array of string and its content
char **ft_split(const char *str, char delim);
/// @brief Same as ft_split but with multiple delimiters
/// @param str string to split
/// @param delims delimiters to split the string
/// @return a pointer to the array of string
/// @note You must free the returned array of string and its content
char **ft_splits(const char *str, const char *delims);
/// @brief Search for the first occurence of the char c in the string str
/// @param str string to search into
/// @param c char to search
/// @return a pointer to the first occurence of c in the string str otherwise
/// NULL
/// @note The returned pointer is from str and has the same constness as str
/// it was left as non-const to align with glibc's strchr
char *ft_strchr(const char *str, int c);
/// @brief duplicate the string src into a new allocated string
/// @param strsrc string to copy from
/// @return The copy of string src
/// @note You must free the returned string
char *ft_strdup(const char *strsrc);
/// @brief Iterate over the string s and execute the function f on each char
/// @param s String to iterate over
/// @param f Function to execute on each char
/// @return void
/// @note The first parameter of the function f is the index of the char in the
/// string s.
void ft_striteri(char *s, void (*f)(unsigned int, char *));
/// @brief Add up two strings s1 and s2 and return the result
/// @param s1 String to add
/// @param s2 String to add
/// @return The result of the addition of the two strings
/// @note You must free the returned string
char *ft_strjoin(char const *s1, char const *s2);
/// @brief Add up a char c at the end of a pre-existing and allocated string
/// @param str String to append to
/// @param c Char to append
/// @return The new memory segment pointed by *str or null if re-allocation
/// failed
/// @note You must free the returned string
/// @note Do no use this method to add two string, please use strjoin instead
char *ft_strappend_c(char **str, char c);
/// @brief copy up to size - 1 characters from the NULL-terminated string src
/// to dst, NULL-terminating the result.
/// Puts the result after the characters already in dst.
/// @param dst string to copy to
/// @param src string to copy from
/// @param size maximum number of characters in destination string, including
/// the terminating null character.
/// @return returns the total length of the string they tried to create.
size_t ft_strlcat(char *dst, const char *src, size_t size);
/// @brief Copies up to size - 1 characters from the NULL-terminated string src
/// to dst, NULL-terminating the result.
/// Puts the result directly at dst.
/// @param dst string to copy to
/// @param src string to copy from
/// @param size maximum number of characters in destination string, including
/// the terminating null character.
/// @return returns the total length of the string they tried to create.
/// @note If the length of src is greater than or equal to size, the string
/// will be truncated.
size_t ft_strlcpy(char *dst, const char *src, size_t size);
/// @brief Get the length of the string str
/// @param str String to get the length of
/// @return the length of the string str
size_t ft_strlen(const char *str);
/// @brief Get the length of the string str up to the first occurence of c
/// @param str String to get the length of
/// @param c Char to search
/// @return the length of the string str up to the first c, if c is not found
/// the length of the string str
size_t ft_strclen(char *str, char c);
/// @brief Get the number of occurance of char c in string str
/// @param str String to search from
/// @param c Char to search
/// @return the number of occurance of char c in string str
size_t ft_strcnb(char *str, char c);
/// @brief Calculate the length of the starting segment of str that contain
/// char from the accept string
/// @param str String to search from
/// @param accept String of the valid char
/// @return The calculated length.
size_t ft_strspn(const char *str, const char *accept);
/// @brief Calculate the length of the starting segment of str that don't
/// contain chars from the exclude string
/// @param str String to search from
/// @param exclude String of char to exclude
/// @return The calculated length.
size_t ft_strcspn(const char *str, const char *exclude);
/// @brief Execute the function f on each char of the string s
/// @param s String to iterate over
/// @param f function to execute on each char
/// @return an allocated string with the result of the function f on each char
/// of the string s otherwise NULL
/// @note You must free the returned string
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
/// @brief Compare the two strings s1 and s2
/// @param s1 String to compare s1
/// @param s2 String to compare s2
/// @return 0 if the strings are identical, otherwise the difference between the
/// first different char (s1 - s2)
int ft_strcmp(const char *s1, const char *s2);
/// @brief Compare the two strings s1 and s2 up to n characters or until a `\0'
/// @param s1 String to compare s1
/// @param s2 String to compare s2
/// @param n number of chars to compare maximum
/// @return 0 if the strings are identical, otherwise the difference between the
/// first different char (s1 - s2)
int ft_strncmp(const char *s1, const char *s2, size_t n);
/// @brief duplicate no more than n character of the string src into a
// new allocated string
/// @param str string to copy from
/// @param n number of chars to copy
/// @return A copy of the string src
/// @note You must free the returned string
char *ft_strndup(const char *str, size_t n);
/// @brief duplicate the string src into a new allocated string
/// @param src string to copy from
/// @return A copy of the string src
/// @note You must free the returned string
char *ft_strdup(const char *src);
/// @brief search for the first occurence of the string small in the string big
/// @param big string to search into
/// @param small string to search
/// @param n minimum number of characters to search
/// @return returns a pointer to the first occurrence of the string small in the
/// string big, where not more than n characters are searched. Characters that
/// appear after a `\0' character are not searched.
/// @note The returned pointer is from str and has the same constness as str
/// it was left as non-const to align with glibc's strnstr
char *ft_strnstr(const char *big, const char *small, size_t n);
/// @brief search for the last occurence of c in the string
/// @param str string to search from
/// @param c char to search
/// @return pointer to the last occurence of c in the string otherwise NULL
/// @note The returned pointer is from str and has the same constness as str
/// it was left as non-const to align with glibc's strrchr
char *ft_strrchr(const char *str, int c);
/// @brief remove the specified chars from the string s1
/// @param s1 string to trim
/// @param set characters to remove from s1
/// @return allocated string without the specified chars otherwise NULL
/// @note You must free the returned string
char *ft_strtrim(char const *s1, char const *set);
/// @brief return a substring of the string s from the specified position
/// @param s string to get the substring
/// @param start position of the substring
/// @param len length of the substring
/// @return allocated string with the substring otherwise NULL
/// @note You must free the returned string
char *ft_substr(char const *s, unsigned int start, size_t len);
/// @brief search and replace the string to_replace in the string str
/// by the string replace_by
/// @param str String to in which the string will be searched and replaced
/// @param to_replace string to search and replace
/// @param replace_by string to replace with
/// @return the string with the modified chars otherwise NULL
/// @note to_replace and replace_by must not be NULL
/// @note You must free the returned string !
char *ft_str_replace(const char *str, const char *to_replace,
const char *replace_by);
/// @brief search if the string str ends with the string end
/// @param str string to search from
/// @param end string to search
/// @return 1 if the string str ends with the string end, 0 otherwise
int ft_strend_with(const char *str, const char *end);
/// @brief search if the string str starts with the string start
/// @param str string to search from
/// @param start string to search
/// @return 1 if the string str starts with the string start, 0 otherwise
int ft_strstart_with(const char *str, const char *start);
/// @brief replace the chars to_replace in the string by the char replace_by
/// @param str string to in which the char will be replaced
/// @param to_replace char to replace
/// @param replace_by char to replace by
/// @return A pointer to the string str
/// @note The string is modified in place
char *ft_str_replace_chr(char *str, char to_replace, char replace_by);
/// @brief Returns a pointer to the first string pointed to by args
/// @param args Pointer to a list of const char pointer
/// @param index Number of elements in Args
/// @return if index is negative or the first string of args
/// is null fails and return null otherwise return the first
/// const char pointed to by args
const char *ft_shift_args(const char **args[], int *index);
/// @brief Checks if the string str is composed only of alphabetical characters
/// @param str string to check
/// @return true if the string is composed only of alphabetical characters,
/// false otherwise
bool ft_str_isalpha(const char *str);
/// @brief Checks if the string str is composed only of alphabetical and
/// numerical characters
/// @param str string to check
/// @return true if the string is composed only of alphabetical and numerical
/// characters, false otherwise
bool ft_str_isalnum(const char *str);
/// @brief Checks if the string str is comprised of only numbers.
/// @param str string to check
/// @return true if the string is composed only of numerical characters, false
/// otherwise
/// @note This function is not the same as ft_str_isdigit, as it checks for
/// and accepts negative symbols.
bool ft_str_isnum(const char *str);
/// @brief Checks if the string str is a valid boolean value ("false" ||
/// "true" || "0" || "1")
/// @param str string to check
/// @return true if it ;atches with any of the following: "false" "0" "true" "1"
/// false otherwise
bool ft_str_isbool(const char *str);
/// @brief Checks if the string str is composed only of numerical characters
/// @param str string to check
/// @return true if the string is composed only of numerical characters, false
/// otherwise
/// @note This function is not the same as ft_str_isnum, as it does not accept
/// ANYTHING other than numerical characters.
bool ft_str_isdigit(const char *str);
/// @brief Check if the string is a float
/// @param str string to check
/// @return 1 if the string is a float, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isfloat.c
bool ft_str_isfloat(const char *str);
/// @brief Check if the string is a float
/// @param str string to check
/// @return 1 if the string is a float, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isdouble.c
bool ft_str_isdouble(const char *str);
/// @brief Check if the string is an int valid value
/// @param str string to check
/// @return 1 if the string is an int, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isint.c
bool ft_str_isint(const char *str);
/// @brief Check if the string is a long
/// @param str string to check
/// @return 1 if the string is a number, 0 otherwise
/// @file: src/ft_string/ft_char/ft_islong.c
bool ft_str_islong(const char *str);
/// @brief check if the string is a hex character
/// @param c char to check
/// @return 1 if the char is a hex character, 0 otherwise
/// @file: src/ft_string/ft_char/ft_ishex.c
bool ft_str_ishex(const char *str);
/// @brief check if the string is an octal number
/// @param str string to check
/// @return 1 if the string is an octal number, 0 otherwise
bool ft_str_isoct(const char *str);
/// @brief Check if the string is valid using a function pointer
/// @param str string to check
/// @param f function pointer to check the string (takes a char c as int
/// and returns 0 if the char is invalid)
/// @return 1 if the string is valid, 0 otherwise
/// @file: src/ft_string/ft_char/ft_isvalid.c
bool ft_str_isvalid(const char *str, int (*f)(int));
/// @brief Return a pointer to a constant string describing the error code
/// @param errnum Error code
/// @return A pointer to a constant string describing the error code
/// @note The returned pointer can be null if errnum is out of range (0 - 133)
const char *ft_strerror(int errnum);
/// @brief Print the string s if present to STDERR followed by the error code
/// as a string
/// @param s String to print before the error code
void ft_perror(const char *s);
/* ************************************************************************** */
/* FT_GNL SUB MODULE */
/* ************************************************************************** */
/// @brief Read the next line from the file descriptor
/// @param fd file descriptor to read from
/// @return the next line from the file descriptor otherwise NULL
/// @note You must free the returned string
/// @note You can see the number of supported file descriptor in the macro
/// MAX_FD
char *get_next_line(int fd);
// printf
// missing ... todo
/* ************************************************************************** */
/* FT_STRING SUB MODULE */
/* ************************************************************************** */
/* ************************************************************************** */
/* ** ft_string_new ** */
/* ************************************************************************** */
/// @brief create a new t_string
/// @param capacity initial capacity of the string
/// @return a pointer to the new t_string
/// @note You must free the returned string with ft_string_destroy
t_string *ft_string_new(size_t capacity);
/* ************************************************************************** */
/* ** ft_string_from ** */
/* ************************************************************************** */
/// @brief create a new t_string from the string
/// @param str string to copy from
/// @return a pointer to the new t_string
/// @note You must free the returned string with ft_string_destroy
/// @note This function does NOT take ownership of the passed string.
t_string *ft_string_from(const char *str);
/// @brief create a new t_string from the string with at most n chars
/// @param str string to copy from
/// @param n number of chars to copy (including the '\0') "1234" with n = 3
/// -> "123"
/// @return a pointer to the new t_string
/// @note You must free the returned string with ft_string_destroy
t_string *ft_string_from_n(const char *str, size_t n);
/// @brief create a new t_string from the char c
/// @param c char to copy from
/// @return a pointer to the new t_string
/// @note You must free the returned string with ft_string_destroy
t_string *ft_string_from_c(char c);
/// @brief create a new t_string from the t_string str
/// @param str t_string to copy from
/// @return a pointer to the new t_string
/// @note You must free the returned string with ft_string_destroy
t_string *ft_string_from_s(const t_string *str);
/// @brief create a new t_string from the t_string str using at most n chars
/// @param str t_string to copy from
/// @param n number of chars to copy
/// @return a pointer to the new t_string created
/// @note You must free the returned string with ft_string_destroy
t_string *ft_string_from_s_n(const t_string *str, size_t n);
/* ************************************************************************** */
/* ** ft_string_put ** */
/* ************************************************************************** */
/// @brief write the string on the specified file descriptor
/// @param str t_string to write
/// @param fd file descriptor to write on
/// @return the return of write if the fd and str are valid otherwise -1
int ft_string_put(t_string *str, int fd);
/* ************************************************************************** */
/* ** ft_string_append ** */
/* ************************************************************************** */
/// @brief append the string src to the string str
/// @param str t_string to modify
/// @param src string to append
/// @return 1 if the string has been appended otherwise 0
int ft_string_append(t_string *str, const char *src);
/// @brief append at most n chars of the string src to the string str
/// @param str t_string to modify
/// @param src string to append
/// @param n number of chars to append
/// @return 1 if the string has been appended otherwise 0
int ft_string_append_n(t_string *str, const char *src, size_t n);
/// @brief append the char c to the string str
/// @param str t_string to modify
/// @param c char to append
/// @return 1 if the string has been appended otherwise 0
int ft_string_append_c(t_string *str, char c);
/// @brief append the string src to the string str
/// @param str t_string to modify
/// @param src string to append
/// @return 1 if the string has been appended otherwise 0
int ft_string_append_s(t_string *str, t_string *src);
/// @brief append at most n chars of the string src to the string str
/// @param str t_string to modify
/// @param src string to append
/// @param n number of chars to append
/// @return 1 if the string has been appended otherwise 0
int ft_string_append_s_n(t_string *str, t_string *src, size_t n);
/* ************************************************************************** */
/* ** ft_string_clear ** */
/* ************************************************************************** */
/// @brief clear the string
/// @param str t_string to clear
/// @return void
/// @note the string is not freed but the content is cleared
void ft_string_clear(t_string *str);
/* ************************************************************************** */
/* ** ft_string_destroy ** */
/* ************************************************************************** */
/// @brief free the string
/// @param str pointer to the t_string to free
/// @return void
void ft_string_destroy(t_string **str);
/* ************************************************************************** */
/* ** ft_string_insert ** */
/* ************************************************************************** */
/// @brief insert the string src in the string str at the specified position
/// @param str t_string to modify
/// @param src string to insert
/// @param pos position to insert the string
/// @return 1 if the string has been inserted otherwise 0
int ft_string_insert(t_string *str, const char *src, size_t pos);
/// @brief insert at most n chars of the string src in the string str at the
/// specified position
/// @param str t_string to modify
/// @param src string to insert
/// @param pos position to insert the string
/// @param n number of chars to insert
/// @return 1 if the string has been inserted otherwise 0
int ft_string_insert_n(t_string *str, const char *src, size_t pos, \
size_t n);
/// @brief insert the char c in the string str at the specified position
/// @param str t_string to modify
/// @param c char to insert
/// @param pos position to insert the char
/// @return 1 if the string has been inserted otherwise 0
int ft_string_insert_c(t_string *str, char c, size_t pos);
/// @brief insert the string src in the string str at the specified position
/// @param str t_string to modify
/// @param src string to insert
/// @param pos position to insert the string
/// @return 1 if the string has been inserted otherwise 0
int ft_string_insert_s(t_string *str, t_string *src, size_t pos);
/// @brief insert at most n chars of the string src in the string str at the
/// specified position
/// @param str t_string to modify
/// @param src string to insert
/// @param pos position to insert the string
/// @param n number of chars to insert
/// @return 1 if the string has been inserted otherwise 0
int ft_string_insert_s_n(t_string *str, t_string *src, size_t pos,
size_t n);
/* ************************************************************************** */
/* ** ft_string_reserve ** */
/* ************************************************************************** */
/// @brief reserve the specified capacity for the string
/// @param str t_string to modify
/// @param capacity capacity to reserve
/// @return 1 if the string has been reserved otherwise 0
int ft_string_reserve(t_string *str, size_t capacity);
/* ************************************************************************** */
/* ** ft_string_resize ** */
/* ************************************************************************** */
/// @brief resize the string to the specified size
/// @param str t_string to modify
/// @param size size to resize
/// @return 1 if the string has been resized otherwise -1
/// @note if the size is smaller than the current size, the string is truncated
int ft_string_resize(t_string *str, size_t size);
/* ************************************************************************** */
/* ** ft_string_shrink ** */
/* ************************************************************************** */
/// @brief shrink the string to the minimum size
/// @param str t_string to modify
/// @return 1 if the string has been shrinked otherwise 0
int ft_string_shrink(t_string *str);
/* ************************************************************************** */
/* ** ft_string_substr ** */
/* ************************************************************************** */
/// @brief return a substring of the string str from the specified position
/// @param str t_string to get the substring
/// @param start position of the substring
/// @param len length of the substring
/// @return allocated string with the substring otherwise NULL
/// @note You must free the returned string. use ft_string_destroy.
t_string *ft_string_substr(t_string *str, size_t start, size_t len);
/* ************************************************************************** */
/* ** ft_string_to_str ** */
/* ************************************************************************** */
/// @brief convert the t_string to a string
/// @param str t_string to convert
/// @return allocated string with the content of the t_string otherwise NULL
/// @note You must free the returned string.
/// @note The t_string is not freed or modified.
char *ft_string_to_str(t_string *str);
/* ************************************************************************** */
/* ** ft_string_trim ** */
/* ************************************************************************** */
/// @brief trim the characters ' ' from the string
/// @param str t_string to trim
/// @return void
/// @note the inner string is not freed but the content modified.
void ft_string_trim(t_string *str);
/// @brief trim the specified char from the string
/// @param str t_string to trim
/// @param c char to trim
/// @return void
/// @note the inner string is not freed but the content modified.
void ft_string_trim_chr(t_string *str, char c);
/// @brief trim the specified chars from the string
/// @param str t_string to trim
/// @param to_trim chars to trim
/// @return void
/// @note the inner string is not freed but the content modified.
void ft_string_trimstr(t_string *str, const char *to_trim);
/* ************************************************************************** */
/* ** ft_string_cmp ** */
/* ************************************************************************** */
/// @brief compare the string with the string cmp
/// @param str t_string to compare
/// @param cmp string to compare
/// @return 0 if the strings are identical, otherwise the difference between the
/// first different char (s1 - s2)
int ft_string_cmp(const t_string *str, const char *cmp);
/// @brief compare the string with the string cmp up to n chars
/// @param str t_string to compare
/// @param cmp string to compare
/// @param n number of chars to compare
/// @return 0 if the strings are identical, otherwise the difference between the
/// first different char (s1 - s2)
int ft_string_ncmp(const t_string *str, const char *cmp, size_t n);
/// @brief compare the string with the string cmp
/// @param str t_string to compare
/// @param cmp string to compare
/// @return 0 if the strings are identical, otherwise the difference between the
/// first different char (s1 - s2)
int ft_string_cmpstr(const t_string *str, const t_string *cmp);
/// @brief compare the string with the string cmp up to n chars
/// @param str t_string to compare
/// @param cmp string to compare
/// @param n number of chars to compare
/// @return 0 if the strings are identical, otherwise the difference between the
/// first different char (s1 - s2)
int ft_string_ncmpstr(const t_string *str, const t_string *cmp, size_t \
n);
/* ************************************************************************** */
/* ** ft_string_get ** */
/* ************************************************************************** */
/// @brief get the length of the string
/// @param str t_string to get the length
/// @return the length of the string
size_t ft_string_len(const t_string *str);
/// @brief get the capacity of the string
/// @param str t_string to get the capacity
/// @return the capacity of the string
size_t ft_string_cap(const t_string *str);
/// @brief get the content of the string
/// @param str t_string to get the content
/// @return the content of the string
const char *ft_string_get(const t_string *str);
/* ************************************************************************** */
/* ** ft_string_set ** */
/* ************************************************************************** */
/// @brief replace the content of the string with the new string src
/// @param str t_string to modify
/// @param src string to copy from
/// @return 1 if the string has been set otherwise 0
int ft_string_set(t_string *str, const char *src);
/// @brief replace the content of the string with at most n chars of the new
/// string src
/// @param str t_string to modify
/// @param src string to copy from
/// @param n number of chars to set (including the '\0') "1234" with n = 3
/// -> "123"
/// @return 1 if the string has been set otherwise 0
int ft_string_set_n(t_string *str, const char *src, size_t n);
/// @brief replace the content of the string with the new string src
/// @param str t_string to modify
/// @param src string to set
/// @return 1 if the string has been set otherwise 0
/// @note This function takes ownership of the string src and does no alloc.
int ft_string_set_inplace(t_string *str, char *src);
/* ************************************************************************** */
/* ** ft_string_chr ** */
/* ************************************************************************** */
/// @brief search for the first occurence of c in the string
/// @param str t_string to search from
/// @param c char to search
/// @return an offset to the first occurence of c in the string otherwise -1
ssize_t ft_string_offset(const t_string *str, char c);
/// @brief search for the last occurence of c in the string
/// @param str t_string to search from
/// @param c char to search
/// @return an offset to the last occurence of c in the string otherwise -1
ssize_t ft_string_roffset(const t_string *str, char c);
/// @brief search for the first occurence of c in the string
/// @param str t_string to search from
/// @param c char to search
/// @return a pointer to the first occurence of c in the string otherwise NULL
char *ft_string_chr(const t_string *str, char c);
/// @brief search for the last occurence of c in the string
/// @param str t_string to search from
/// @param c char to search
/// @return a pointer to the last occurence of c in the string otherwise NULL
char *ft_string_rchr(const t_string *str, char c);
/* ************************************************************************** */
/* ** ft_string_replace ** */
/* ************************************************************************** */
/// @brief search and replace the string to_replace in the string str
/// by the string replace_by
/// @param str t_string to in which the string will be searched and replaced
/// @param to_replace string to search and replace
/// @param replace_by string to replace with
/// @return 1 if the string has been replaced otherwise 0
int ft_string_replace(t_string *str, const char *to_replace,
const char *replace_by);
/// @brief search and replace the string to_replace in the string str
/// by the string replace_by
/// @param str t_string to in which the string will be searched and replaced
/// @param to_replace string to search and replace
/// @param replace_by string to replace with
/// @return 1 if the string has been replaced otherwise 0
int ft_string_replace_chr(t_string *str, char to_replace,
char replace_by);
#endif // FT_STRING_H

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 16:54:48 by bgoulard #+# #+# */
/* Updated: 2024/06/02 10:19:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_STRING_TYPES_H
# define FT_STRING_TYPES_H
# include <stddef.h>
// maximum number of file descriptors
// - get_next_line
# ifndef MAX_FD
# ifdef TEST
# define MAX_FD 5
# else
# define MAX_FD 1024
# endif
# endif
// default allocation size for t_strings
// - ft_string_new
# ifndef T_STRING_BUFF
# ifdef TEST
# define T_STRING_BUFF 5
# else
# define T_STRING_BUFF 4096
# endif
# endif
// buffer size for temporary read buffers
// - get_next_line
// - ft_fd_to_buff
# ifndef BUFFER_SIZE
# ifdef TEST
# define BUFFER_SIZE 5
# else
# define BUFFER_SIZE 4096
# endif
# endif
/// @brief Structure representing a string
/// @param str The string
/// @param length The length of the string
/// @param capacity The capacity of the string
typedef struct s_string
{
char *str;
size_t length;
size_t capacity;
} t_string;
#endif /* FT_STRING_TYPES_H */

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 23:11:19 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:12:08 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_TYPES_H
# define FT_TYPES_H
/* ************************************************************************** */
/* */
/* Useful in early development of projects where you might no know what you */
/* want from the ft_personal lib - otherwise if you already know what */
/* you use / are at the end of a project remove this to lighten the .h */
/* step of compilation */
/* */
/* ************************************************************************** */
// true - false
# include <stdbool.h>
// size_t - ssize_t - ptrdiff_t ... NULL
# include <stddef.h>
// int8_t - int16_t - uint32_t ... INT_MAX - INT_MIN
# include <stdint.h>
# include "ft_args_types.h"
# include "ft_list_types.h"
# include "ft_map_types.h"
# include "ft_math_types.h"
# include "ft_optional_types.h"
# include "ft_pair_types.h"
# include "ft_string_types.h"
# include "ft_vector_types.h"
#endif

View file

@ -0,0 +1,218 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vector.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 13:37:46 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:09:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_VECTOR_H
# define FT_VECTOR_H
/* ************************************************************************** */
/* */
/* Module: FT_VECTOR */
/* Prefix: ft_vec_ */
/* */
/* This module provides the vector type, a way to create array that are chunk */
/* allocated and allows for easy manipulations. */
/* */
/* ************************************************************************** */
# include "ft_defs.h"
# include "ft_vector_types.h"
// ft_vector/ft_vec_add.c
/// @brief Add an element to the end of the vector
/// @param vec pointer to the vector
/// @param data data to add
/// @return true if the element was added, false otherwise
bool ft_vec_add(t_vector **vec, void *data);
// ft_vector/ft_vec_apply.c
/// @brief Apply a function to all the elements of the vector
/// @param vec pointer to the vector
/// @param func function to apply
/// @return void
void ft_vec_apply(t_vector *vec, t_data_apply func);
// ft_vector/ft_vec_at.c
/// @brief Get an element from the vector
/// @param vec pointer to the vector
/// @param n index of the element
/// @return pointer to the element
void *ft_vec_at(t_vector *vec, size_t n);
// ft_vector/ft_vec_cat.c
/// @brief Concatenate two vectors
/// @param vec_a pointer to the first vector
/// @param vec_b pointer to the second vector
/// @return true if the vectors were concatenated, false otherwise
/// @note the second vector is not modified therefore it is not cleared.
/// Be careful with double free.
bool ft_vec_cat(t_vector **vec_a, const t_vector *vec_b);
// ft_vector/ft_vec_clear.c
/// @brief Clear a vector by setting its size to 0 and all elements to NULL
/// @param vec pointer to the vector cleared
/// @return void
/// @note the data is not freed, cappacity is not changed. if you want to free
// the data / reduce the cappacity use ft_vec_clear, then ft_vec_shrink.
void ft_vec_clear(t_vector *vec);
// ft_vector/ft_vec_destroy.c
/// @brief Destroy a vector
/// @param vec pointer to the vector
/// @return true if the vector was destroyed, false otherwise
bool ft_vec_destroy(t_vector **vec);
// ft_vector/ft_vec_filter.c
/// @brief Filter a vector
/// @param vec pointer to the vector
/// @param func function to filter the vector
/// @param del function to delete the elements filtered
/// @return void
void ft_vec_filter(t_vector *vec, t_data_is func, t_data_apply del);
// ft_vector/ft_vec_get.c
/// @brief Get an element from the vector
/// @param vector vector to get the element from
/// @param key key to search for using the cmp function
/// @param cmp function to compare the elements where the first argument is the
/// key and the second is the element of the vector
/// @return pointer to the element or NULL if not found
/// @note If you want to get an element by index, use ft_vec_at
void *ft_vec_get(t_vector *vector, const void *key, t_data_cmp cmp);
// ft_vector/ft_vec_map.c
/// @brief Map a vector
/// @param vec pointer to the vector
/// @param func function to map the vector
/// @return pointer to the new vector
/// @note the new vector is allocated and must be freed
/// @note as vec is an array under the hood,
// the new vector will be a collection of adress.
/// therefore, if your function changes the value of the data,
// the new vector will be a collection of pointers to the
// data of the old vector. If you want to do that and change the
// value of the data, use ft_vec_apply instead.
t_vector *ft_vec_map(t_vector *vec, t_data_tr func);
// ft_vector/ft_vec_new.c
/// @brief Create a new vector
/// @return pointer to the new vector
t_vector *ft_vec_new(void);
// ft_vector/ft_vec_pop.c
/// @brief Pop an element from the vector's end
/// @param vec pointer to the vector
/// @return pointer to the element
void *ft_vec_pop(t_vector *vec);
/// @brief Create a new vector with a given capacity
/// @param n capacity of the new vector
/// @return pointer to the new vector
t_vector *ft_vec_from_size(size_t n);
/// @brief Create a new vector from an array
/// @param data data to add to the vector
/// @param count count of the data
/// @return pointer to the new vector
t_vector *ft_vec_from_array(void **data, size_t count);
/// @brief Create a new vector from an array and steals ownership of the array
/// @param data data to create the vector from
/// @param count count of the data array
/// @return pointer to the new vector
t_vector *ft_vec_convert_alloccarray(void **data, size_t count);
// ft_vector/ft_vec_remove.c
/// @brief Remove an element from the vector
/// @param vector vector to remove the element from
/// @param n index of the element to remove
/// @param del function to delete the elements data
void ft_vec_remove(t_vector *vector, size_t n, t_data_apply del);
/// @brief Remove an element from the vector
/// @param vector vector to remove the element from
/// @param func function to remove the element
/// @param del function to delete the elements data
/// @return void
/// @note Similar to ft_vec_filter
void ft_vec_remove_if(t_vector *vector, t_data_is func,
t_data_apply del);
// ft_vector/ft_vec_reserve.c
/// @brief Reserve a given size for the vector
/// @param vec pointer to the vector
/// @param size size to reserve
/// @return true if the vector was reserved or size is smaller
/// than the current cappacity, false otherwise
bool ft_vec_reserve(t_vector **vec, size_t size);
// ft_vector/ft_vec_reverse.c
/// @brief Reverse a vector
/// @param vector vector to reverse
/// @return void
void ft_vec_reverse(t_vector *vector);
// ft_vector/ft_vec_shift.c
/// @brief Shift a vector
/// @param vec pointer to the vector
/// @param start index to start the shift
/// @param shift number of elements to shift
/// @return void
void ft_vec_shift(t_vector *vec, size_t start, size_t shift);
// ft_vector/ft_vec_sort.c
/// @brief Sort a vector
/// @param vec pointer to the vector
/// @param cmp_f function to compare the elements
/// @return void
void ft_vec_sort(t_vector *vec, t_data_cmp cmp_f);
// ft_vector/ft_vec_shrink.c
/// @brief Shrink a vector
/// @param vec pointer to the vector
/// @return true if the vector was shrunk, false otherwise
bool ft_vec_shrink(t_vector *vec);
// ft_vector/ft_vec_swap.c
/// @brief Swap two elements of a vector
/// @param vec pointer to the vector
/// @param a index of the first element
/// @param b index of the second element
/// @return void
void ft_vec_swap(t_vector *vec, size_t a, size_t b);
// ft_vector/ft_vec_to_array.c
/// @brief Convert a vector to an array
/// @param vec pointer to the vector
/// @return pointer to the array
/// @note the array is allocated and must be freed but the vector is freed
void **ft_vec_to_array(t_vector **vec);
#endif /* FT_VECTOR_H */

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vector_types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 15:13:10 by bgoulard #+# #+# */
/* Updated: 2023/12/30 13:20:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_VECTOR_TYPES_H
# define FT_VECTOR_TYPES_H
# ifndef FT_VECTOR_BASE_LEN
# ifdef TEST
# define FT_VECTOR_BASE_LEN 5
# else
# define FT_VECTOR_BASE_LEN 4096
# endif
# endif
# include <stdbool.h>
# include <stddef.h>
/// @brief vector structure
/// @param datas array of pointers to the elements
/// @param count number of elements in the vector
/// @param cappacity number of elements that can be stored in the vector
typedef struct s_vector
{
void **datas;
size_t count;
size_t cappacity;
} t_vector;
#endif /* FT_VECTOR_TYPES_H */

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* args_helper.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/31 17:47:15 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:48:21 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARGS_HELPER_H
# define ARGS_HELPER_H
# include "ft_args_types.h"
# include <stddef.h>
# include <sys/types.h>
// Parser opt
//
// Parse long option
ssize_t parse_long_opt(const char *str_op, const t_opt *opt_list);
// Parse short option
ssize_t parse_short_opt(const char *str_op, const t_opt *opt_list);
// Checker
//
// Check if the argument is valid
int checker_arg(t_opt_type type, const char *arg);
// Run
//
// Run the function associated with the option
int run_opt_func(const t_opt opt, void *usr_control_struct, \
const char **arg, int *i);
// Error
//
// Print error message for option
int arg_opt_err(const char *opt);
// Print error message for argument type
int arg_type_err(const t_opt opt, const char *arg);
#endif

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* args_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 22:39:19 by bgoulard #+# #+# */
/* Updated: 2024/05/31 18:33:02 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARGS_TESTS_H
# define ARGS_TESTS_H
/* @file: tests/ft_args/args_tests.c */
int parse_args_test(void);
int tests_args(void);
/* @file: tests/ft_args/tests_custom_checker.c */
int getset_custom_checker_test(void);
/* @file: tests/ft_args/tests_optlist.c */
int getset_opt_list_test(void);
/* @file: tests/ft_args/tests_setup_prog.c */
int tests_setup_prog(void);
/* @file: tests/ft_args/tests_version.c */
int getset_version_test(void);
/* @file: tests/ft_args/tests_progname.c */
int getset_program_name_test(void);
#endif /* ARGS_TESTS_H */

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* char_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 22:39:35 by bgoulard #+# #+# */
/* Updated: 2024/05/26 12:28:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHAR_TESTS_H
# define CHAR_TESTS_H
/* @file: tests/ft_string/ft_char/test_tolower.c */
int test_ft_tolower(void);
/* @file: tests/ft_string/ft_char/test_isalpha.c */
int test_ft_isalpha(void);
/* @file: tests/ft_string/ft_char/test_isalnum.c */
int test_ft_isalnum(void);
/* @file: tests/ft_string/ft_char/test_puchar.c */
int test_ft_putchar(void);
/* @file: tests/ft_string/ft_char/test_ishexdigit.c */
int test_ft_ishexdigit(void);
/* @file: tests/ft_string/ft_char/test_isspace.c */
int test_ft_isspace(void);
/* @file: tests/ft_string/ft_char/test_isoctdigit.c */
int test_ft_isoctdigit(void);
/* @file: tests/ft_string/ft_char/test_isprint.c */
int test_ft_isprint(void);
/* @file: tests/ft_string/ft_char/test_toupper.c */
int test_ft_toupper(void);
/* @file: tests/ft_string/ft_char/test_isascii.c */
int test_ft_isascii(void);
/* @file: tests/ft_string/ft_char/ft_char_tests.c */
int char_tests(void);
/* @file: tests/ft_string/ft_char/test_isdigit.c */
int test_ft_isdigit(void);
#endif /* CHAR_TESTS_H */

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dl_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 16:51:11 by bgoulard #+# #+# */
/* Updated: 2024/05/19 16:52:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DL_TESTS_H
# define DL_TESTS_H
int t_dl_add_front(void);
int t_dl_add_back(void);
int t_dl_apply(void);
int t_dl_apply_range(void);
int t_dl_apply_range_node(void);
int t_dl_clear(void);
int t_dl_clear_range(void);
int t_dl_create(void);
int t_dl_copy_node(void);
int t_dl_copy_list(void);
int t_dl_delete_self(void);
int t_dl_delete_range(void);
int t_dl_delete(void);
int t_dl_find(void);
int t_dl_get_datas(void);
int t_dl_get_nodes(void);
int t_dl_at(void);
int t_dl_begin(void);
int t_dl_end(void);
int t_dl_map(void);
int t_dl_new(void);
int t_dl_pop(void);
int t_dl_pop_back(void);
int t_dl_push(void);
int t_dl_push_back(void);
int t_dl_rev(void);
int t_dl_size(void);
int t_dl_size_of_data(void);
int t_dl_subrange(void);
#endif /* DL_TESTS_H */

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lists_test_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 13:30:26 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:42:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LISTS_TEST_UTILS_H
# define LISTS_TEST_UTILS_H
# include "ft_list_types.h"
# include <stdbool.h>
void create_2elem_dlist(t_dlist **list, void **data1, void **data2);
void create_2elem_list(t_list **list, void **data1, void **data2);
#endif

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ll_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 16:10:07 by bgoulard #+# #+# */
/* Updated: 2024/05/19 16:11:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LL_TESTS_H
# define LL_TESTS_H
int t_ll_add_front(void);
int t_ll_add_back(void);
int t_ll_apply(void);
int t_ll_apply_range(void);
int t_ll_apply_range_node(void);
int t_ll_clear(void);
int t_ll_create(void);
int t_ll_copy_node(void);
int t_ll_copy_list(void);
int t_ll_delone(void);
int t_ll_delete_range(void);
int t_ll_find(void);
int t_ll_get_datas(void);
int t_ll_get_nodes(void);
int t_ll_end(void);
int t_ll_at(void);
int t_ll_map(void);
int t_ll_new(void);
int t_ll_push(void);
int t_ll_push_back(void);
int t_ll_pop(void);
int t_ll_pop_back(void);
int t_ll_rev(void);
int t_ll_size(void);
int t_ll_size_match(void);
int t_ll_subrange(void);
#endif /* LL_TESTS_H */

View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:23:29 by bgoulard #+# #+# */
/* Updated: 2024/05/19 18:24:01 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_TESTS_H
# define MAP_TESTS_H
// File: tests/ft_map/tests_map_cappacity.c
int test_map_capacity(void);
// File: tests/ft_map/tests_map_clear.c
int test_map_clear(void);
// File: tests/ft_map/tests_map_create.c
int test_map_create(void);
// File: tests/ft_map/tests_map_destroy.c
int test_map_destroy(void);
int test_map_destroy_free(void);
// File: tests/ft_map/tests_map_get.c
int test_map_get(void);
// File: tests/ft_map/tests_map_hash.c
int test_map_hash(void);
// File: tests/ft_map/tests_map_remove.c
int test_map_remove(void);
// File: tests/ft_map/tests_map_set.c
int test_map_set(void);
// File: tests/ft_map/tests_map_set_cmphash.c
int test_map_set_cmp(void);
int test_map_set_hash(void);
// File: tests/ft_map/tests_map_size.c
int test_map_size(void);
#endif /* MAP_TESTS_H */

View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* math_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:17:37 by bgoulard #+# #+# */
/* Updated: 2024/06/26 20:51:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MATH_TESTS_H
# define MATH_TESTS_H
/* @file: tests/ft_math/tests_complex.c */
int test_ft_complex_abs(void);
int test_ft_complex_add(void);
int test_ft_complex_addl(void);
int test_ft_complex_mull(void);
int test_ft_complex_muld(void);
/* @file: tests/ft_math/tests_log.c */
int test_ft_log(void);
int test_ft_llogof(void);
int test_ft_ullogof(void);
int test_ft_logof(void);
/* @file: tests/ft_math/tests_intrange.c */
int test_ft_range(void);
int test_ft_range_f(void);
int test_ft_range_d(void);
/* @file: tests/ft_math/tests_sqrt.c */
int test_ft_sqrt(void);
/* @file: tests/ft_math/tests_minmax.c */
int test_ft_min(void);
int test_ft_max(void);
/* @file: tests/ft_math/tests_pow.c */
int test_ft_pow(void);
/* @file: tests/ft_math/tests_round.c */
int test_ft_round(void);
/* @file: tests/ft_math/tests_clamp.c */
int test_ft_clamp(void);
int test_ft_clamp_f(void);
int test_ft_clamp_d(void);
/* @file: tests/ft_math/tests_abs.c */
int test_ft_abs(void);
/* @file: tests/ft_math/tests_align.c */
int test_ft_align_2(void);
int test_ft_align(void);
#endif

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* optional_test.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 22:39:46 by bgoulard #+# #+# */
/* Updated: 2024/05/30 12:08:37 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OPTIONAL_TEST_H
# define OPTIONAL_TEST_H
/* @file: tests/ft_optional/test_optional_new.c */
int test_optional_new(void);
/* @file: tests/ft_optional/test_optional_dup.c */
int test_optional_dup(void);
/* @file: tests/ft_optional/test_optional_copy.c */
int test_optional_copy(void);
/* @file: tests/ft_optional/test_optional_unwrap.c */
int test_optional_unwrap(void);
/* @file: tests/ft_optional/test_optional_from_val.c */
int test_optional_from_val(void);
/* @file: tests/ft_optional/test_optional_chain.c */
int test_optional_chain(void);
/* @file: tests/ft_optional/optional_tests.c */
void *add_4(void *val);
int tests_optional(void);
/* @file: tests/ft_optional/test_optional_map.c */
int test_optional_map(void);
/* @file: tests/ft_optional/test_optional_destroy.c */
int test_optional_destroy(void);
#endif /* INCLUDE/TESTS/OPTIONAL_TEST_H */

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pair_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 00:30:00 by bgoulard #+# #+# */
/* Updated: 2024/07/06 17:11:02 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PAIR_TESTS_H
# define PAIR_TESTS_H
int tests_pair_destroy(void);
int test_pair_set(void);
int test_pair_new(void);
int test_pair_second(void);
int test_pair_first(void);
int test_pair_destroy(void);
int test_pair_cmp(void);
int test_pair_cmp_first(void);
int test_pair_cmp_second(void);
#endif /* PAIR_TESTS_H */

View file

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str__mem_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 12:26:09 by bgoulard #+# #+# */
/* Updated: 2024/05/30 12:40:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STR__MEM_TESTS_H
# define STR__MEM_TESTS_H
/// @file: tests/ft_string/ft_mem/tests_apply_2d
int test_apply_2d(void);
/// @file: tests/ft_string/ft_mem/tests_bzero.c
int test_bzero(void);
/// @file: tests/ft_string/ft_mem/tests_calloc.c
int test_calloc(void);
/// @file: tests/ft_string/ft_mem/tests_fd_to_buff.c
int test_fd_to_buff(void);
/// @file: tests/ft_string/ft_mem/tests_free.c
int test_free(void);
/// @file: tests/ft_string/ft_mem/tests_free_2d.c
int test_free_2d(void);
/// @file: tests/ft_string/ft_mem/tests_len_2d.c
int test_len_2d(void);
/// @file: tests/ft_string/ft_mem/tests_memchr.c
int test_memchr(void);
/// @file: tests/ft_string/ft_mem/tests_memcmp.c
int test_memcmp(void);
/// @file: tests/ft_string/ft_mem/tests_memcpy.c
int test_memcpy(void);
/// @file: tests/ft_string/ft_mem/tests_memmap.c
int test_memmap(void);
/// @file: tests/ft_string/ft_mem/tests_memmove.c
int test_memmove(void);
/// @file: tests/ft_string/ft_mem/tests_memset.c
int test_memset(void);
/// @file: tests/ft_string/ft_mem/tests_qsort.c
int test_qsort(void);
/// @file: tests/ft_string/ft_mem/tests_realloc.c
int test_realloc(void);
/// @file: tests/ft_string/ft_mem/tests_swap.c
int test_swap(void);
#endif /* STR__MEM_TESTS_H */

View file

@ -0,0 +1,169 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str__str_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:28:40 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:42:41 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STR__STR_TESTS_H
# define STR__STR_TESTS_H
/// @brief test_striteri
/// @file: tests/ft_string/ft_str/test_striteri.c
int test_striteri(void);
/// @brief test_utoa
/// @file: tests/ft_string/ft_str/test_utoa.c
int test_utoa(void);
/// @brief test_strdup
/// @file: tests/ft_string/ft_str/test_strdup.c
int test_strdup(void);
/// @brief test_strnstr
/// @file: tests/ft_string/ft_str/test_strnstr.c
int test_strnstr(void);
/// @brief test_strrchr
/// @file: tests/ft_string/ft_str/test_strrchr.c
int test_strrchr(void);
/// @brief test_strtrim
/// @file: tests/ft_string/ft_str/test_strtrim.c
int test_strtrim(void);
/// @brief test_atoi
/// @file: tests/ft_string/ft_str/test_atoi.c
int test_atoi(void);
/// @brief test_strlcpy
/// @file: tests/ft_string/ft_str/test_strlcpy.c
int test_strlcpy(void);
/// @brief test_shift_args
/// @file: tests/ft_string/ft_str/test_shift_args.c
int test_shift_args(void);
/// @brief str_tests
/// @file: tests/ft_string/ft_str/str_tests.c
int str_tests(void);
/// @brief test_itoa_base
/// @file: tests/ft_string/ft_str/test_itoa_base.c
int test_itoa_base(void);
/// @brief test_putendl
/// @file: tests/ft_string/ft_str/test_putendl.c
int test_putendl(void);
/// @brief test_strtok
/// @file: tests/ft_string/ft_str/test_strtok.c
int test_strtok(void);
/// @brief test_strmapi
/// @file: tests/ft_string/ft_str/test_strmapi.c
int test_strmapi(void);
/// @brief test_strndup
/// @file: tests/ft_string/ft_str/test_strndup.c
int test_strndup(void);
/// @brief test_putstr
/// @file: tests/ft_string/ft_str/test_putstr.c
int test_putstr(void);
/// @brief test_strncmp
/// @file: tests/ft_string/ft_str/test_strncmp.c
int test_strncmp(void);
/// @brief test_strlcat
/// @file: tests/ft_string/ft_str/test_strlcat.c
int test_strlcat(void);
/// @brief test_atof
/// @file: tests/ft_string/ft_str/test_atof.c
int test_atof(void);
/// @brief test_itoa
/// @file: tests/ft_string/ft_str/test_itoa.c
int test_itoa(void);
/// @brief tests_splits
/// @file: tests/ft_string/ft_str/test_splits.c
int tests_splits(void);
/// @brief test_str_replace
/// @file: tests/ft_string/ft_str/test_str_replace.c
int test_str_replace(void);
/// @brief test_atoi_base
/// @file: tests/ft_string/ft_str/test_atoi_base.c
int test_atoi_base(void);
/// @brief test_str_replace_chr
/// @file: tests/ft_string/ft_str/test_str_replace_chr.c
int test_str_replace_chr(void);
/// @brief test_putnbr
/// @file: tests/ft_string/ft_str/test_putnbr.c
int test_putnbr(void);
/// @brief test_strjoin
/// @file: tests/ft_string/ft_str/test_strjoin.c
int test_strjoin(void);
/// @brief test_strcmp
/// @file: tests/ft_string/ft_str/test_strcmp.c
int test_strcmp(void);
/// @brief test_substr
/// @file: tests/ft_string/ft_str/test_substr.c
int test_substr(void);
/// @brief test_gnl
/// @file: tests/ft_string/ft_str/test_gnl.c
int test_gnl(void);
/// @brief test_strlen
/// @file: tests/ft_string/ft_str/test_strlen.c
int test_strlen(void);
/// @brief test_split
/// @file: tests/ft_string/ft_str/test_split.c
int test_split(void);
/// @brief test_strchr
/// @file: tests/ft_string/ft_str/test_strchr.c
int test_strchr(void);
/// @brief test_str_isbool
/// @file: tests/ft_string/ft_str/test_str_isbool.c
int test_str_isbool(void);
/// @brief test_str_isalpha
/// @file: tests/ft_string/ft_str/test_str_isalpha.c
int test_str_isalpha(void);
int test_str_isdouble(void);
int test_str_isalnum(void);
int test_str_isdigit(void);
int test_str_ishex(void);
int test_str_isfloat(void);
int test_str_isint(void);
int test_str_islong(void);
int test_str_isnum(void);
int test_str_isoct(void);
int test_str_isvalid(void);
int test_strclen(void);
int test_strcnb(void);
int test_strcspn(void);
int test_strspn(void);
int test_strend_with(void);
int test_strstart_with(void);
int test_strappend_c(void);
#endif /* STR__STR_TESTS_H */

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str__t_str_test.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 17:42:08 by bgoulard #+# #+# */
/* Updated: 2024/05/31 13:33:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STR__T_STR_TEST_H
# define STR__T_STR_TEST_H
int test_string_append_c(void);
int test_string_append_n(void);
int test_string_append_sn(void);
int test_string_append_s(void);
int test_string_append(void);
int test_string_cap(void);
int test_string_chr(void);
int test_string_clear(void);
int test_string_cmp_str(void);
int test_string_cmp(void);
int test_string_destroy(void);
int test_string_from_c(void);
int test_string_from_n(void);
int test_string_from_sn(void);
int test_string_from_s(void);
int test_string_from(void);
int test_string_get(void);
int test_string_insert_c(void);
int test_string_insert_n(void);
int test_string_insert_sn(void);
int test_string_insert_s(void);
int test_string_insert(void);
int test_string_len(void);
int test_string_ncmp_str(void);
int test_string_ncmp(void);
int test_string_new(void);
int test_string_offset(void);
int test_string_put(void);
int test_string_rchr(void);
int test_string_replace_chr(void);
int test_string_replace(void);
int test_string_reserve(void);
int test_string_resize(void);
int test_string_roffset(void);
int test_string_set_inplace(void);
int test_string_set_n(void);
int test_string_set(void);
int test_string_shrink(void);
int test_string_substr(void);
int test_string_to_str(void);
int test_string_trim_chr(void);
int test_string_trimstr(void);
int test_string_trim(void);
#endif /* STR__T_STR_TEST_H */

View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/29 11:15:14 by bgoulard #+# #+# */
/* Updated: 2024/07/06 16:23:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TESTS_H
# define TESTS_H
# include <fcntl.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
# define TESTS_FPREFIX "build/test_"
# ifndef FORK_TESTS
# define FORK_TESTS 1
# endif
typedef struct s_test
{
char *name;
int (*test)(void);
} t_test;
int run_test(const t_test *test, int *collect);
int open_test_file(char **func_to_test);
void destroy_test_file(int fd, const char *file);
int tests_args(void);
int tests_doubly_linked_list_all(void);
int tests_linked_list_all(void);
int tests_map(void);
int tests_math(void);
int tests_optional(void);
int tests_pair(void);
int tests_string(void);
int tests_vector(void);
#endif /* TESTS_H */

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_lambda_functions.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:41:41 by bgoulard #+# #+# */
/* Updated: 2024/05/24 14:05:47 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TESTS_LAMBDA_FUNCTIONS_H
# define TESTS_LAMBDA_FUNCTIONS_H
# include <stdbool.h>
// File: tests/lambda_functions.h
bool is42(const void *data);
void add42(void *data);
void *add42_ret(const void *data);
int cmp_int(const void *a, const void *b);
void **creat_tb(void);
#endif

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector_tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:10:06 by bgoulard #+# #+# */
/* Updated: 2024/06/02 11:33:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VECTOR_TESTS_H
# define VECTOR_TESTS_H
// File: tests/ft_vector/vector_tests.c
int test_vec_add(void);
int test_vec_apply(void);
int test_vec_at(void);
int test_vec_cat(void);
int test_vec_clear(void);
int test_vec_convert_alloc_array(void);
int test_vec_destroy(void);
int test_vec_filter(void);
int test_vec_from_array(void);
int test_vec_from_size(void);
int test_vec_get(void);
int test_vec_map(void);
int test_vec_new(void);
int test_vec_pop(void);
int test_vec_remove(void);
int test_vec_remove_if(void);
int test_vec_reserve(void);
int test_vec_reverse(void);
int test_vec_shift(void);
int test_vec_shrink(void);
int test_vec_sort(void);
int test_vec_swap(void);
int test_vec_to_array(void);
int tests_vector(void);
#endif /* VECTOR_TESTS_H */

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arg_custom_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 14:13:26 by bgoulard #+# #+# */
/* Updated: 2024/06/01 14:10:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include <stddef.h>
static t_data_is singleton_custom_checker(t_data_is custom_checker)
{
static t_data_is custom_checker_ptr = NULL;
if (custom_checker)
custom_checker_ptr = custom_checker;
return (custom_checker_ptr);
}
void ft_arg_set_custom_checker(t_data_is custom_checker)
{
singleton_custom_checker(custom_checker);
}
t_data_is ft_arg_get_custom_checker(void)
{
return (singleton_custom_checker(NULL));
}

View file

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 01:10:29 by bgoulard #+# #+# */
/* Updated: 2024/08/21 10:28:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include "ft_args_types.h"
#include "ft_string.h"
#include "internal/args_helper.h"
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
int run_opt_func(const t_opt opt, void *usr_control_struct, const char **arg, \
int *i)
{
const char *arg_ptr;
void (*func_arg)(void *, const char *);
void (*func_no_arg)(void *);
func_arg = opt.func;
func_no_arg = opt.func;
if (opt.type & OPT_ARG && opt.type & OPT_EQSIGN)
arg_ptr = ft_strchr(arg[*i], '=') + 1;
else if (opt.type & OPT_ARG)
arg_ptr = arg[*i + 1];
else
arg_ptr = NULL;
if (checker_arg(opt.type, arg_ptr) != 0)
return (arg_type_err(opt, arg_ptr));
if (opt.type & OPT_ARG)
func_arg(usr_control_struct, arg_ptr);
else
func_no_arg(usr_control_struct);
*i += 1 + ((opt.type != 0) && (opt.type & OPT_EQSIGN) == 0);
return (EXIT_SUCCESS);
}
int ft_parse_args(const char **argv, void *usr_control_struct)
{
const t_opt *opt;
ssize_t opt_index;
int i;
opt = ft_get_opt_list();
i = 1;
while (argv[i] && argv[i][0] == '-')
{
if (argv[i][1] == '-' && argv[i][2] == '\0')
return (i + 1);
else if (argv[i][1] == '-')
opt_index = parse_long_opt(argv[i] + 2, opt);
else
opt_index = parse_short_opt(argv[i] + 1, opt);
if (opt_index == -1)
return (arg_opt_err(argv[i]));
if (run_opt_func(opt[opt_index], usr_control_struct, argv, &i) != 0)
return (-1);
}
return (i);
}

View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_err.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:23:49 by bgoulard #+# #+# */
/* Updated: 2024/05/31 18:22:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args_types.h"
#include "ft_args.h"
#include "ft_char.h"
#include "ft_string.h"
#include <unistd.h>
#include <stdlib.h>
// 0 success, !0 failure
int checker_arg(t_opt_type type, const char *arg)
{
if (!arg || arg == (char *)0x1)
return (type);
if ((type & ARG_MASK_ATYPE) == OPT_INT)
return (!ft_str_isint(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_STRING)
return (EXIT_SUCCESS);
else if ((type & ARG_MASK_ATYPE) == OPT_BOOL)
return (!ft_str_isbool(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_FLOAT)
return (!ft_str_isfloat(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_LONG)
return (!ft_str_islong(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_DOUBLE)
return (!ft_str_isdouble(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_ALPHANUM)
return (!ft_str_isvalid(arg, ft_isalnum));
else if ((type & ARG_MASK_ATYPE) == OPT_HEX)
return (!ft_str_ishex(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_OCT)
return (!ft_str_isoct(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_OTHER
&& ft_arg_get_custom_checker())
return (!ft_arg_get_custom_checker()(arg));
return (EXIT_SUCCESS);
}
int arg_type_err(t_opt opt, const char *arg)
{
const char *progname;
progname = ft_progname();
if (progname)
{
ft_putstr_fd(progname, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
}
ft_putstr_fd("Error: invalid argument for option: ", STDERR_FILENO);
if (opt.long_name)
ft_putstr_fd(opt.long_name, STDERR_FILENO);
else
ft_putchar_fd(opt.short_name, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
if (!arg || arg == (char *)1)
ft_putstr_fd("NULL", STDERR_FILENO);
else
ft_putstr_fd(arg, STDERR_FILENO);
ft_putstr_fd("\n", STDERR_FILENO);
return (EXIT_FAILURE);
}
int arg_opt_err(const char *arg)
{
const char *progname;
progname = ft_progname();
if (progname)
{
ft_putstr_fd(progname, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
}
ft_putstr_fd("Error: invalid option: ", STDERR_FILENO);
ft_putstr_fd(arg, STDERR_FILENO);
ft_putstr_fd("\n", STDERR_FILENO);
return (EXIT_FAILURE);
}

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_opt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 14:06:00 by bgoulard #+# #+# */
/* Updated: 2024/05/31 17:57:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args_types.h"
#include "ft_math.h"
#include "ft_string.h"
#include <sys/types.h>
ssize_t parse_short_opt(char *str_op, const t_opt *opt_list)
{
ssize_t i;
i = 0;
while (opt_list[i].func)
{
if (opt_list[i].short_name && \
opt_list[i].short_name == str_op[0])
return (i);
i++;
}
return (-1);
}
ssize_t parse_long_opt(char *str_op, const t_opt *opt_list)
{
ssize_t i;
ssize_t offset;
i = 0;
offset = 0;
if (ft_strchr(str_op, '='))
offset = ft_strchr(str_op, '=') - str_op;
while (opt_list[i].func)
{
if (opt_list[i].long_name && ft_strncmp(opt_list[i].long_name, str_op, \
ft_max(offset, ft_strlen(opt_list[i].long_name))) == 0)
return (i);
i++;
}
return (-1);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_progname.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 00:00:49 by bgoulard #+# #+# */
/* Updated: 2024/04/21 14:44:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include <stddef.h>
static const char *singleton_progname(const char *progname)
{
static const char *singleton_progname = NULL;
if (progname)
singleton_progname = progname;
return (singleton_progname);
}
void ft_set_progname(const char *progname)
{
if (progname)
singleton_progname(progname);
else
singleton_progname("a.out");
}
const char *ft_progname(void)
{
return (singleton_progname(NULL));
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_set_opt_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 00:01:33 by bgoulard #+# #+# */
/* Updated: 2024/04/21 14:44:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include "ft_args_types.h"
#include <stddef.h>
static const t_opt *singleton_opt_list(const t_opt *opt_list)
{
static const t_opt *singleton_opt_list = NULL;
if (opt_list)
singleton_opt_list = opt_list;
return (singleton_opt_list);
}
void ft_set_opt_list(const t_opt *opt_list)
{
if (opt_list)
singleton_opt_list(opt_list);
}
const t_opt *ft_get_opt_list(void)
{
return (singleton_opt_list(NULL));
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_setup_prog.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 11:31:28 by bgoulard #+# #+# */
/* Updated: 2024/05/30 00:47:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
void ft_setup_prog(const char *const *av)
{
ft_set_progname(av[0]);
ft_set_version(VERSION);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_version.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/13 23:57:19 by bgoulard #+# #+# */
/* Updated: 2024/04/21 14:45:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include <stddef.h>
static const char *singleton_version(const char *version)
{
static const char *singleton_version = NULL;
if (version)
singleton_version = version;
return (singleton_version);
}
void ft_set_version(const char *version)
{
if (version)
singleton_version(version);
else
singleton_version(VERSION);
return ;
}
const char *ft_progversion(void)
{
return (singleton_version(NULL));
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:17:48 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void ft_dl_add_back(t_dlist **head, t_dlist *const added)
{
t_dlist *last;
if (!head || !added)
return ;
if (!*head)
return (ft_dl_add_front(head, added));
last = ft_dl_end(*head);
last->next = added;
added->prev = last;
}
void ft_dl_add_front(t_dlist **head, t_dlist *const added)
{
if (!head || !added)
return ;
if (!*head)
return ((void)(*head = added));
added->next = *head;
(*head)->prev = added;
*head = added;
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_apply.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 18:59:42 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:59:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_dl_apply(const t_dlist *start, t_data_apply applied)
{
return (ft_dl_apply_range(start, NULL, applied));
}
size_t ft_dl_apply_range(const t_dlist *start, const t_dlist *end,
t_data_apply applied)
{
size_t i;
t_dlist *it;
it = (t_dlist *)start;
i = 0;
while (it != end)
{
applied(it->data);
it = it->next;
i++;
}
return (i);
}
size_t ft_dl_apply_range_node(const t_dlist *start, const t_dlist *end,
t_dnode_apply applied)
{
size_t i;
t_dlist *it;
it = (t_dlist *)start;
i = 0;
while (it != end)
{
applied(it);
it = it->next;
i++;
}
return (i);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:33:59 by iron #+# #+# */
/* Updated: 2024/06/23 18:59:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_dl_clear(t_dlist **head, t_data_apply del)
{
size_t ret;
ret = ft_dl_delete_range(*head, NULL, del);
*head = NULL;
return (ret);
}
size_t ft_dl_clear_range(t_dlist *start, t_dlist *end, t_data_apply del)
{
size_t i;
i = 0;
if (!start)
return (0);
while (start != end)
{
if (del)
del(start->data);
start->data = NULL;
start = start->next;
i++;
}
return (i);
}

View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:05 by iron #+# #+# */
/* Updated: 2024/06/23 18:59:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_create(const void *data)
{
t_dlist *ret;
ret = ft_dl_new();
if (!ret)
return (ret);
ret->data = (void *)data;
return (ret);
}
t_dlist *ft_dl_copy_node(const t_dlist *const other)
{
t_dlist *ret;
ret = ft_dl_new();
if (!ret)
return (ret);
ret->data = (void *)other->data;
ret->next = (t_dlist *)other->next;
ret->prev = (t_dlist *)other->prev;
return (ret);
}
t_dlist *ft_dl_copy_list(const t_dlist *const other)
{
t_dlist *node;
t_dlist *head;
t_dlist *prev;
t_dlist *it;
it = (t_dlist *)other;
node = NULL;
head = NULL;
prev = NULL;
while (it)
{
node = ft_dl_copy_node(it);
if (!node)
return (ft_dl_delete(&head, NULL), head);
node->prev = prev;
if (prev)
prev->next = node;
else
head = node;
if (it->next)
prev = node;
it = it->next;
}
return (head);
}

View file

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_delete.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:10 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:01 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
int ft_dl_delete_self(t_dlist *node, t_data_apply del)
{
if (!node)
return (FTLIST_FAILURE);
if (del)
del(node->data);
if (node->prev)
node->prev->next = node->next;
if (node->next)
node->next->prev = node->prev;
free(node);
return (FTLIST_SUCCESS);
}
size_t ft_dl_delete_range(t_dlist *start, const t_dlist *target,
t_data_apply del)
{
t_dlist *next;
t_dlist *prev;
size_t i;
i = 0;
next = NULL;
prev = NULL;
if (start && start->prev)
prev = start->prev;
while (start != target)
{
next = start->next;
if (del)
del(start->data);
free(start);
start = next;
i++;
}
if (prev)
prev->next = next;
if (next)
next->prev = prev;
return (i);
}
size_t ft_dl_delete(t_dlist **head, t_data_apply del)
{
size_t i;
t_dlist *firs_elem;
if (!head)
return (0);
firs_elem = ft_dl_begin(*head);
i = ft_dl_delete_range(firs_elem, NULL, del);
*head = NULL;
return (i);
}
/*
size_t ft_dl_delete_dup(t_dlist **src)
{
t_dlist **node_ptrs;
size_t nb_del;
size_t length;
node_ptrs = NULL;
nb_del = 0;
length = 0;
if (!*src)
return (0);
node_ptrs = ft_dl_get_nodes(*src);
if (!node_ptrs)
return (0);
length = ft_dl_count(*src);
for (size_t j = 0; j + 1 != length; j++) {
for (size_t k = j + 1; k != length; k++) {
if (!node_ptrs[j] || !node_ptrs[k])
continue ;
if (node_ptrs[j]->data == node_ptrs[k]->data) {
ft_dl_delete_self(node_ptrs[k]);
node_ptrs[k] = NULL;
nb_del++;
j++;
}
}
}
free(node_ptrs);
return (nb_del);
}
*/

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_find.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 13:05:32 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_find(const t_dlist *head, const void *data,
int (*cmp)(const void *, const void *))
{
t_dlist *it;
it = (t_dlist *)head;
while (it)
{
if (data == it->data || (cmp && !cmp(it->data, data)))
return (it);
it = it->next;
}
return (NULL);
}

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_getters.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:21 by iron #+# #+# */
/* Updated: 2024/07/19 17:36:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
#include <stdlib.h>
void **ft_dl_get_datas(const t_dlist *src)
{
size_t size;
void **ret;
if (!src)
return (NULL);
size = ft_dl_size(src);
ret = ft_calloc(sizeof(void *), (size + 1));
if (!ret)
return (NULL);
ret[size] = NULL;
size = 0;
while (src)
{
ret[size++] = (void *)src->data;
src = src->next;
}
return (ret);
}
t_dlist **ft_dl_get_nodes(const t_dlist *src)
{
size_t size;
t_dlist **ret;
size = ft_dl_size(src);
ret = NULL;
if (!src)
return (NULL);
ret = ft_calloc(sizeof(t_dlist *), (size + 1));
if (!ret)
return (ret);
ret[size] = NULL;
size = 0;
while (src)
{
ret[size++] = (t_dlist *)src;
src = src->next;
}
return (ret);
}

View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_iterator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:27 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_at(const t_dlist *head, size_t index)
{
size_t i;
t_dlist *it;
it = (t_dlist *)head;
i = 0;
while (i != index)
{
i++;
it = it->next;
}
return (it);
}
t_dlist *ft_dl_end(const t_dlist *head)
{
t_dlist *it;
it = (t_dlist *)head;
while (it->next)
it = it->next;
return (it);
}
t_dlist *ft_dl_begin(const t_dlist *head)
{
t_dlist *it;
it = (t_dlist *)head;
while (it->prev)
it = it->prev;
return (it);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:21:43 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_map(const t_dlist *lst, t_data_tr f, t_data_apply del)
{
t_dlist *ret;
t_dlist *tmp;
if (!lst || !f)
return (NULL);
ret = NULL;
while (lst)
{
tmp = ft_dl_create(f(lst->data));
if (!tmp)
return (ft_dl_clear(&ret, del), NULL);
ft_dl_add_back(&ret, tmp);
lst = lst->next;
}
return (ret);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:42:00 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
t_dlist *ft_dl_new(void)
{
t_dlist *ret;
ret = ft_calloc(1, sizeof(t_dlist));
if (!ret)
return (ret);
ret->data = NULL;
ret->prev = NULL;
ret->next = NULL;
return (ret);
}

View file

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_pushpop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:38 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
t_dlist *ft_dl_push(t_dlist **node, const void *data)
{
t_dlist *added;
added = ft_dl_create(data);
added->next = *node;
if (*node)
(*node)->prev = added;
*node = added;
return (added);
}
t_dlist *ft_dl_push_back(t_dlist **node, const void *data)
{
t_dlist *added;
t_dlist *it;
it = *node;
if (!*node)
return (ft_dl_push(node, data));
added = ft_dl_create(data);
while (it->next)
it = it->next;
it->next = added;
added->prev = it;
return (*node);
}
void *ft_dl_pop(t_dlist **node)
{
void *data;
t_dlist *it;
data = NULL;
if (!node || !*node)
return (NULL);
it = *node;
*node = (*node)->next;
data = it->data;
free(it);
if (*node)
(*node)->prev = NULL;
return (data);
}
void *ft_dl_pop_back(t_dlist **node)
{
void *data;
t_dlist *it;
t_dlist *prev;
data = NULL;
prev = NULL;
if (!node || !*node)
return (NULL);
it = ft_dl_end(*node);
data = it->data;
prev = it->prev;
if (prev)
prev->next = NULL;
else
*node = NULL;
free(it);
return (data);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_rev.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iron <iron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:20:27 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_rev(t_dlist **head)
{
t_dlist *it;
t_dlist *tmp;
if (!head || !*head)
return (NULL);
it = *head;
while (it)
{
tmp = it->next;
it->next = it->prev;
it->prev = tmp;
if (!tmp)
*head = it;
it = tmp;
}
return (*head);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:50 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_dl_size(const t_dlist *head)
{
t_dlist *it;
size_t i;
i = 0;
it = (t_dlist *)head;
while (it)
{
it = it->next;
i++;
}
return (i);
}
size_t ft_dl_size_of_data(const t_dlist *head, t_data_is function)
{
t_dlist *it;
size_t i;
it = (t_dlist *)head;
i = 0;
while (it)
{
if (function(it->data))
i++;
it = it->next;
}
return (i);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_sub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:45 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_subrange(const t_dlist *src, const t_dlist *to)
{
t_dlist *ret;
t_dlist *prev;
t_dlist *new_node;
t_dlist *it;
ret = NULL;
if (!src)
return (ret);
ret = ft_dl_create(src->data);
if (src == to)
return (ret);
prev = ret;
it = src->next;
while (it != to)
{
new_node = ft_dl_create(it->data);
if (!new_node)
return (ft_dl_delete(&prev, NULL), NULL);
new_node->prev = prev;
new_node->next = NULL;
prev->next = new_node;
prev = new_node;
it = it->next;
}
return (ret);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:48:35 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:50 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void ft_ll_add_front(t_list **lst, t_list *new)
{
if (!lst)
return ;
new->next = *lst;
*lst = new;
}
void ft_ll_add_back(t_list **lst, t_list *new)
{
if (!*lst)
*lst = new;
else
ft_ll_end(*lst)->next = new;
}

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_apply.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:01:53 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:54 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void ft_ll_apply(const t_list *lst, t_data_apply f)
{
t_list *it;
if (!f)
return ;
it = (t_list *)lst;
while (it)
{
f(it->data);
it = it->next;
}
}
void ft_ll_apply_range(const t_list *lst, const t_list *end, t_data_apply f)
{
t_list *it;
if (!f)
return ;
it = (t_list *)lst;
while (it && it != end)
{
f(it->data);
it = it->next;
}
}
// nxt = lst->next;
// f may modify lst->next, we dont know
void ft_ll_apply_range_node(const t_list *lst, const t_list *end,
t_lnode_apply f)
{
t_list *nxt;
t_list *it;
if (!f)
return ;
it = (t_list *)lst;
while (it && it != end)
{
nxt = it->next;
f(it);
it = nxt;
}
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 03:12:14 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
void ft_ll_clear(t_list **lst, t_data_apply del)
{
t_list *runner;
t_list *next;
if (!lst)
return ;
runner = *lst;
while (runner)
{
next = runner->next;
if (del)
del(runner->data);
runner->data = NULL;
free(runner);
runner = next;
}
*lst = NULL;
}

View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 11:40:49 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_create(const void *const data)
{
t_list *ret;
ret = ft_ll_new();
if (!ret)
return (ret);
ret->data = (void *)data;
return (ret);
}
t_list *ft_ll_copy_node(const t_list *const other)
{
t_list *ret;
ret = ft_ll_new();
if (!ret)
return (ret);
ret->data = (void *)other->data;
ret->next = (t_list *)other->next;
return (ret);
}
t_list *ft_ll_copy_list(const t_list *const other)
{
t_list *node;
t_list *head;
t_list *prev;
t_list *it;
it = (t_list *)other;
head = ft_ll_copy_node(it);
if (!head)
return (head);
prev = head;
it = (t_list *)it->next;
while (it)
{
node = ft_ll_copy_node(it);
if (!node)
return (ft_ll_clear(&head, NULL), NULL);
prev->next = node;
prev = node;
it = (t_list *)it->next;
}
return (head);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_delete.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 03:09:13 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
void ft_ll_delone(t_list *lst, t_data_apply del)
{
if (!lst)
return ;
if (del)
del(lst->data);
free(lst);
}
size_t ft_ll_delete_range(t_list *lst, const t_list *end, t_data_apply del)
{
t_list *tmp;
size_t i;
i = 0;
if (!lst || !del)
return (i);
while (lst && lst != end)
{
i++;
tmp = lst->next;
del(lst->data);
free(lst);
lst = tmp;
}
return (i);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_find.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 11:12:59 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void *ft_ll_find(const t_list *list, const void *data,
int (*cmp)(const void *, const void *))
{
t_list *it;
it = (t_list *)list;
while (it)
{
if (data == it->data || (cmp && cmp(it->data, data) == 0))
return (it->data);
it = it->next;
}
return (NULL);
}

View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_getters.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 11:54:40 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
void **ft_ll_get_datas(const t_list *src)
{
void **datas;
size_t i;
if (!src)
return (NULL);
datas = ft_calloc(sizeof(void *), ft_ll_size(src) + 1);
if (!datas)
return (NULL);
i = 0;
while (src)
{
datas[i] = src->data;
src = src->next;
i++;
}
return (datas);
}
t_list **ft_ll_get_nodes(const t_list *src)
{
t_list **nodes;
size_t i;
if (!src)
return (NULL);
nodes = ft_calloc(sizeof(t_list *), ft_ll_size(src) + 1);
if (!nodes)
return (NULL);
i = 0;
while (src)
{
nodes[i] = (t_list *)src;
src = src->next;
i++;
}
return (nodes);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_iterator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:43:27 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_end(const t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return ((t_list *)lst);
}
t_list *ft_ll_at(const t_list *lst, size_t index)
{
size_t i;
i = 0;
while (lst && i < index)
{
lst = lst->next;
i++;
}
if (i != index)
return (NULL);
return ((t_list *)lst);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:04:38 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_map(const t_list *lst, t_data_tr f, t_data_apply del)
{
t_list *ret;
t_list *node;
ret = NULL;
if (!f || !del || !lst)
return (NULL);
while (lst)
{
node = ft_ll_create(f(lst->data));
if (!node)
return (ft_ll_clear(&ret, del), NULL);
ft_ll_add_back(&ret, node);
lst = lst->next;
}
return (ret);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:30:32 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
t_list *ft_ll_new(void)
{
t_list *elem;
elem = ft_calloc(sizeof(t_list), 1);
return (elem);
}

View file

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_pushpop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:02:47 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
t_list *ft_ll_push(t_list **lst, const void *data)
{
t_list *new;
if (!lst)
return (NULL);
new = ft_ll_create(data);
if (!new)
return (NULL);
new->next = *lst;
*lst = new;
return (*lst);
}
t_list *ft_ll_push_back(t_list **lst, const void *data)
{
t_list *added;
if (!lst)
return (NULL);
added = ft_ll_create(data);
if (!added)
return (NULL);
if (!*lst)
*lst = added;
else
ft_ll_end(*lst)->next = added;
return (*lst);
}
void *ft_ll_pop(t_list **lst)
{
t_list *tmp;
void *data;
if (!lst || !*lst)
return (NULL);
tmp = *lst;
data = (*lst)->data;
*lst = (*lst)->next;
ft_free((void **)&tmp);
return (data);
}
void *ft_ll_pop_back(t_list **lst)
{
t_list *pre_last;
void *data;
if (!lst || !*lst)
return (NULL);
if (!(*lst)->next)
return (ft_ll_pop(lst));
pre_last = *lst;
while (pre_last->next->next)
pre_last = pre_last->next;
data = pre_last->next->data;
free(pre_last->next);
pre_last->next = NULL;
return (data);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_rev.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iron <iron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/21 15:22:54 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_rev(t_list **head)
{
t_list *next;
t_list *it;
t_list *prev;
if (!head || !*head)
return (NULL);
it = *head;
prev = NULL;
next = it->next;
while (next)
{
next = it->next;
it->next = prev;
prev = it;
it = next;
}
*head = prev;
return (*head);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:38:32 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_ll_size(const t_list *lst)
{
t_list *it;
size_t i;
i = 0;
it = (t_list *)lst;
while (it)
{
it = it->next;
i++;
}
return (i);
}
size_t ft_ll_size_match(const t_list *lst, t_data_is function)
{
size_t i;
t_list *it;
i = 0;
it = (t_list *)lst;
while (it)
{
if (function(it->data))
i++;
it = it->next;
}
return (i);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_sub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:10:57 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_subrange(const t_list *lst, const t_list *end)
{
t_list *sub;
sub = NULL;
if (!lst)
return (sub);
if (lst == end)
return (ft_ll_create(lst->data));
while (lst && lst != end)
{
ft_ll_push_back(&sub, lst->data);
lst = lst->next;
}
return (sub);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:29:23 by bgoulard #+# #+# */
/* Updated: 2024/05/31 23:30:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_vector.h"
void ft_map_clear(t_map *map)
{
size_t i;
t_list *cur;
i = 0;
while (i < map->capacity)
{
while (map->nodes[i])
{
cur = map->nodes[i];
map->nodes[i] = map->nodes[i]->next;
ft_vec_add(&map->reserved_nodes, cur);
}
map->weights[i] = 0;
i++;
}
map->w_total = 0;
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 16:11:41 by bgoulard #+# #+# */
/* Updated: 2024/05/31 23:12:34 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_string.h"
#include "ft_vector.h"
t_map *ft_map_create(size_t size)
{
t_map *map;
map = ft_calloc(1, sizeof(t_map));
if (!map)
return (0);
map->capacity = size;
map->w_total = 0;
map->weights = ft_calloc(size, sizeof(size_t));
if (!map->weights)
return (free(map), NULL);
map->nodes = ft_calloc(size, sizeof(t_list *));
if (!map->nodes)
return (free(map->weights), free(map), NULL);
map->hash = &ft_hash_djb2;
map->cmp = (int (*)(const void *, const void *)) & ft_strcmp;
map->reserved_nodes = ft_vec_new();
if (!map->reserved_nodes)
return (free(map->weights), free(map->nodes), free(map), NULL);
return (map);
}

View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 17:32:38 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:26:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include "ft_list.h"
#include "ft_map.h"
#include "ft_vector.h"
#include <stdlib.h>
static t_data_apply singleton_custom_destroy(const t_data_apply custom_destroy,
bool set)
{
static t_data_apply f_ptr = NULL;
if (set == true)
f_ptr = custom_destroy;
return (f_ptr);
}
static void wrapper_destroy(void *restrict data)
{
t_data_apply destroy;
t_map_node *map_node;
destroy = singleton_custom_destroy(NULL, false);
map_node = (t_map_node *)data;
if (destroy)
(*destroy)(map_node->data);
free(map_node);
}
void ft_map_destroy(t_map *map)
{
ft_map_destroy_free(map, NULL);
}
static void rsv_del(void *data)
{
t_list *node;
node = (t_list *)data;
if (node && node->data)
wrapper_destroy(node->data);
free(node);
}
void ft_map_destroy_free(t_map *map, t_data_apply free_fun)
{
size_t i;
singleton_custom_destroy(free_fun, true);
i = 0;
while (i < map->capacity)
{
ft_ll_clear(&map->nodes[i++], wrapper_destroy);
}
free(map->nodes);
free(map->weights);
ft_vec_apply(map->reserved_nodes, rsv_del);
ft_vec_destroy(&map->reserved_nodes);
free(map);
singleton_custom_destroy(NULL, true);
}

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:05:52 by bgoulard #+# #+# */
/* Updated: 2024/06/01 11:54:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include <stdio.h>
t_map_node *ft_map_get_node(t_map *map, const void *key, size_t size)
{
t_list *bucket;
t_map_node *map_node;
bucket = map->nodes[map->hash(key, size) % map->capacity];
map_node = NULL;
while (bucket)
{
map_node = bucket->data;
if (map->cmp(map_node->key, key) == 0)
break ;
bucket = bucket->next;
}
if (map_node && map->cmp(map_node->key, key) == 0)
return (map_node);
return (NULL);
}
void *ft_map_get(t_map *map, const void *key, size_t size)
{
t_map_node *map_node;
map_node = ft_map_get_node(map, key, size);
if (!map_node)
return (NULL);
return (map_node->data);
}
size_t ft_map_size(const t_map *map)
{
return (map->w_total);
}
size_t ft_map_capacity(const t_map *map)
{
return (map->capacity);
}

View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:07:04 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:18:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
// Default hash function provided by map
// Overflows are possible
// Default hash function for ft_map
// Very fast, but not very good
// see http://www.cse.yorku.ca/~oz/hash.html
// See ft_hash_sdbm or ft_hash_fnv1a for less collisions prone hash functions
size_t ft_hash_djb2(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
hash = 5381;
str = (unsigned char *)key;
while (size--)
hash = (hash << 5) + hash + str[size];
return (hash);
}
// Better hash function
// Slower, but better
// see http://www.cse.yorku.ca/~oz/hash.html
// overal better than djb2 but default hash function stays djb2 because it's
// faster and easier to understand, plus it's not that bad... right ?
size_t ft_hash_sdbm(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
hash = 0;
str = (unsigned char *)key;
while (size--)
hash = *str++ + (hash << 6) + (hash << 16) - hash;
return (hash);
}
// Another hash function
// Slower, but better
// see
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
//
// 64 bits version check is just that the size of size_t impact the starting
// hash value to lower the number of collisions otherwise we use the 32 bits
// version.
// lets hope that 128 bits computers will not be a thing...
//
// note: djb2 and sdbm are 32 bits hash functions and are not impacted by
// the size of size_t
size_t ft_hash_fnv1a(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
if (sizeof(size_t) == 8)
hash = 0xcbf29ce484222325;
else
hash = 0x811c9dc5;
str = (unsigned char *)key;
while (size--)
hash = (*str++ ^ hash) * 0x01000193;
return (hash);
}
// dumbest hash function ever
// just for testing purposes when you don't care about collisions
// or when you want to test your map with a hash function that doesn't
// overflow at all ever
//
size_t ft_hash_dummy(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
str = (unsigned char *)key;
hash = 0;
while (size--)
hash += *str++;
return (hash);
}

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_remove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:08:04 by bgoulard #+# #+# */
/* Updated: 2024/06/01 11:56:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_vector.h"
#include "ft_list_types.h"
#include <stdio.h>
void *ft_map_remove(t_map *map, const void *key, size_t size)
{
size_t hash;
t_list *prev;
t_list *cur;
hash = map->hash(key, size) % map->capacity;
prev = NULL;
cur = map->nodes[hash];
while (cur)
{
if (map->cmp(((t_map_node *)cur->data)->key, key) == 0)
break ;
prev = cur;
cur = cur->next;
}
if (!cur)
return (NULL);
if (!prev)
map->nodes[hash] = cur->next;
else
prev->next = cur->next;
cur->next = NULL;
ft_vec_add(&map->reserved_nodes, cur);
map->weights[hash]--;
map->w_total--;
return (((t_map_node *)cur->data)->data);
}

View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 17:36:14 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:16:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "ft_list.h"
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_string.h"
#include "ft_vector.h"
static void setup_map_node(t_map_node *map_node, const void *key,
const void *value)
{
map_node->data = (void *)value;
map_node->key = key;
map_node->hash = 0;
}
// works but dog shit doesnt check all alloc faillure cases
//
bool ft_map_set(t_map *map, const void *key, const void *value, size_t size)
{
t_map_node *map_node;
t_list *reuse_node;
map_node = ft_map_get_node(map, key, size);
reuse_node = NULL;
if (map_node)
return (map_node->data = (void *)value, true);
if (map->reserved_nodes->count)
reuse_node = ft_vec_pop(map->reserved_nodes);
else
map_node = ft_malloc(sizeof(t_map_node));
if (!map_node && !reuse_node)
return (false);
if (reuse_node)
map_node = reuse_node->data;
else
reuse_node = ft_ll_create(map_node);
setup_map_node(map_node, key, value);
map_node->hash = map->hash(key, size);
ft_ll_add_front(&map->nodes[map_node->hash % map->capacity], reuse_node);
map->weights[map_node->hash % map->capacity]++;
map->w_total++;
return (true);
}
void ft_map_set_cmp(t_map *map, t_data_cmp cmp)
{
map->cmp = cmp;
}
void ft_map_set_hash(t_map *map, t_memhash hash)
{
map->hash = hash;
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_abs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:10:52 by bgoulard #+# #+# */
/* Updated: 2024/05/26 10:21:20 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_abs(int x)
{
if (x < 0)
return (-x);
return (x);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_align.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/26 19:51:21 by bgoulard #+# #+# */
/* Updated: 2024/06/26 20:46:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_align_2(size_t size, size_t alignment)
{
return ((size + alignment - 1) & ~(alignment - 1));
}
size_t ft_align(size_t size, size_t alignment)
{
if (alignment < 2)
return (size);
if (size % alignment == 0)
return (size);
size += alignment - size % alignment;
return (size);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_torange.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/16 14:51:08 by bgoulard #+# #+# */
/* Updated: 2024/05/16 15:04:23 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_clamp(int value, int min, int max)
{
if (value < min)
return (min);
else if (value > max)
return (max);
return (value);
}
float ft_clamp_f(float value, float min, float max)
{
if (value < min)
return (min);
else if (value > max)
return (max);
return (value);
}
double ft_clamp_d(double value, double min, double max)
{
if (value < min)
return (min);
else if (value > max)
return (max);
return (value);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_complex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/01 08:11:12 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:39:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_math.h"
double ft_complex_abs(t_complex nb)
{
return (ft_sqrt(nb.real * nb.real + nb.imaginary * nb.imaginary));
}
t_complex ft_complex_addl(t_complex nb, long factor)
{
return ((t_complex){.real = nb.real + (double)factor,
.imaginary = nb.imaginary + (double)factor});
}
t_complex ft_complex_mull(t_complex nb, long factor)
{
return (ft_complex_muld(nb, (double)factor));
}
t_complex ft_complex_muld(t_complex nb, double factor)
{
return ((t_complex){.real = nb.real * factor,
.imaginary = nb.imaginary * factor});
}

View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_intrange.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 16:22:01 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:41:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_math.h"
int ft_range(int x, int min, int max, int new_max)
{
int res;
if (max == min || new_max == 0 || min >= max)
return (0);
if (x <= min)
return (0);
if (x >= max)
return (new_max);
res = x - min;
max -= min;
return ((int)(((double)res / max) * new_max));
}
float ft_range_f(float x, float min, float max, float new_max)
{
return ((float)ft_range_d((double)x, (double)min, (double)max, \
(double)new_max));
}
double ft_range_d(double x, double min, double max, double new_max)
{
double res;
if (max == min || new_max == 0 || min >= max)
return (0);
if (x <= min)
return (0);
if (x >= max)
return (new_max);
res = x - min;
max -= min;
return ((res / max) * new_max);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_log.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 10:09:57 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:27:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_ullogof(unsigned long long nbr, int base)
{
const unsigned long long bul = (unsigned long long)base;
int pow;
pow = -1;
while (nbr)
{
nbr /= bul;
pow++;
}
return (pow);
}
int ft_llogof(long long nbr, int base)
{
if (nbr <= 0)
return (-1);
return (ft_ullogof((unsigned long long)nbr, base));
}
int ft_logof(int nbr, int base)
{
if (nbr <= 0)
return (-1);
return (ft_llogof((long long)nbr, base));
}
int ft_log(int nbr)
{
return (ft_logof(nbr, 10));
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_minmax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 10:23:07 by bgoulard #+# #+# */
/* Updated: 2023/11/23 10:35:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_min(int a, int b)
{
if (a < b)
return (a);
return (b);
}
int ft_max(int a, int b)
{
if (a > b)
return (a);
return (b);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pow.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:04:24 by bgoulard #+# #+# */
/* Updated: 2024/05/24 01:01:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_pow(size_t x, size_t y)
{
size_t res;
res = 1;
if (y == 0)
return (1);
if (x == 0)
return (0);
while (y > 0)
{
res *= x;
y--;
}
return (res);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_round.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:10:18 by bgoulard #+# #+# */
/* Updated: 2024/05/23 23:10:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
double ft_round(double x)
{
double dec;
double round;
dec = x - (int)x;
if (dec >= 0.5)
round = (int)x + 1;
else
round = (int)x;
return (round);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:00:06 by bgoulard #+# #+# */
/* Updated: 2024/05/24 09:33:30 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
// using newton's method
double ft_sqrt(double nb)
{
double x;
double y;
x = nb;
if (nb <= 0)
return (-1);
y = 1;
while (x - y > 0.0000001)
{
x = (x + y) / 2;
y = nb / x;
}
return (x);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_chain.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:26 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:04:29 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
#include <stdbool.h>
bool ft_optional_chain(t_optional *opt, const t_data_tr_i *f)
{
size_t i;
if (!f)
return (false);
i = 0;
while (f[i] && opt->pres != OPT_NONE)
{
opt->val = f[i](opt->val);
if (!opt->val)
opt->pres = OPT_NONE;
i++;
}
if (opt->pres == OPT_NONE)
return (false);
return (true);
}
t_optional ft_optional_map(t_optional *opt, void *(**f)(void *))
{
t_optional ret;
ft_optional_copy(&ret, opt);
if (ret.pres == OPT_NONE || !f)
return (ret);
ft_optional_chain(&ret, f);
return (ret);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_copy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:40 by bgoulard #+# #+# */
/* Updated: 2024/05/18 15:39:27 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
void ft_optional_copy(t_optional *dest, t_optional *src)
{
dest->pres = src->pres;
dest->val = src->val;
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:49 by bgoulard #+# #+# */
/* Updated: 2024/05/18 15:39:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
#include <stdbool.h>
#include <stdlib.h>
bool ft_optional_destroy(t_optional *opt)
{
if (!opt)
return (false);
if (opt->pres == OPT_NONE)
return (free(opt), true);
return (false);
}

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:55 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:15:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
#include "ft_string.h"
#include <stdlib.h>
t_optional *ft_optional_new(void)
{
t_optional *elem;
elem = ft_malloc(sizeof(*elem));
if (!elem)
return (NULL);
elem->pres = OPT_NONE;
elem->val = NULL;
return (elem);
}
t_optional *ft_optional_from_val(void *ptr)
{
t_optional *elem;
elem = ft_optional_new();
if (!elem)
return (elem);
elem->pres = OPT_SOME;
elem->val = ptr;
return (elem);
}
// As this function can fail it should never be called,
// the type t_optional is supposed to be ultra reliable.
// Carefull use is preconized.
t_optional *ft_optional_dup(t_optional *org)
{
t_optional *ret;
ret = ft_optional_new();
if (!ret)
return (NULL);
ft_optional_copy(ret, org);
return (ret);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_unwrap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:07:00 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:28:55 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
void *ft_optional_unwrap(t_optional opt)
{
if (opt.pres != OPT_NONE)
return (opt.val);
return (NULL);
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_cmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:58:24 by bgoulard #+# #+# */
/* Updated: 2024/07/06 16:52:21 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pair.h"
int ft_pair_cmp(t_pair *pair1, t_pair *pair2, t_data_cmp cmp)
{
if (!pair1 && !pair2)
return (0);
if (!pair1)
return (-(pair2 != NULL));
if (!pair2)
return (pair1 != NULL);
if (cmp)
return (cmp(pair1, pair2));
return (pair1 - pair2);
}
int ft_pair_cmp_first(t_pair *pair1, t_pair *pair2, t_data_cmp cmp)
{
if (!pair1 && !pair2)
return (0);
if (!pair1)
return (-(pair2->first != NULL));
if (!pair2)
return (pair1->first != NULL);
if (cmp)
return (cmp(pair1->first, pair2->first));
return (pair1->first - pair2->first);
}
int ft_pair_cmp_second(t_pair *pair1, t_pair *pair2, t_data_cmp cmp)
{
if (!pair1 && !pair2)
return (0);
if (!pair1)
return (-(pair2->second != NULL));
if (!pair2)
return (pair1->second != NULL);
if (cmp)
return (cmp(pair1->second, pair2->second));
return (pair1->second - pair2->second);
}

Some files were not shown because too many files have changed in this diff Show more