diff options
Diffstat (limited to 'libft')
173 files changed, 5595 insertions, 0 deletions
diff --git a/libft b/libft deleted file mode 160000 -Subproject 39951f08a2938683d800c677c3a244e9ff8dbe1 diff --git a/libft/.gitignore b/libft/.gitignore new file mode 100644 index 0000000..cf870d3 --- /dev/null +++ b/libft/.gitignore @@ -0,0 +1,13 @@ +*.o +*.so +*.a +*.ghc +*.dSYM +a.out +test/libft_test +obj/* +rendu.makefile +doc/* +tmp/* +*.gz +vgcore.* diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..aa03c59 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,98 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <marvin@42.fr> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/10/08 15:45:53 by cacharle #+# #+# # +# Updated: 2020/10/11 14:51:18 by cacharle ### ########.fr # +# # +# **************************************************************************** # + +LIB = ar rcs +RM = rm -f +NORM = norminette +MAKE = make +MAKE_ARGS = --no-print-directory +DOXYGEN = doxygen +DOXYGEN_FILE = Doxyfile + +SRC_DIR = src +INCLUDE_DIR = include +OBJ_DIR = obj +SCRIPT_DIR = script +TEST_DIR = test +DOC_DIR = doc + +INCLUDE_DIR = include + + +CC = gcc +OFLAG ?= -g -O1 +CCFLAGS = $(OFLAG) -I$(INCLUDE_DIR) -Wall -Wextra -Werror +ifeq ($(TRAVIS_COMPILER),gcc) +CCFLAGS += -Wno-unused-result +endif + +IGNORE_FILE = .libftignore +IGNORE_DEFAULT = ft_printf + +NAME = libft.a + +SRC = $(shell find $(SRC_DIR) -type f -name '*.c') +OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +INCLUDE = $(shell find $(INCLUDE_DIR) -name "*.h") + +all: prebuild + @$(MAKE) -j$(JOBS) allnopre + +allnopre: $(NAME) + +.PHONY: test +test: all + @echo "Testing" + @$(MAKE) -C $(TEST_DIR) run + +norm: + @if [ `command -v $(NORM)` ]; \ + then echo "Running norminette"; \ + $(NORM) $(SRC) $(INCLUDE); \ + else echo "$(NORM) not installed"; fi + +prebuild: + @for dir in $$(find $(SRC_DIR)/* $(FIND_ARGS) -type d | \ + sed 's_$(SRC_DIR)/_$(OBJ_DIR)/_g'); \ + do \ + if [ ! -d "$$dir" ]; then \ + mkdir -p $$dir; echo "Making build dir: $$dir"; fi \ + done + +$(NAME): $(OBJ) $(INCLUDE) + @echo "Linking: $@" + @$(LIB) $@ $(OBJ) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INCLUDE) + @echo "Compiling: $@" + @$(CC) $(CCFLAGS) -c -o $@ $< + +clean: + @echo "Removing objects" + @$(RM) -r $(OBJ_DIR) + +fclean: clean + @echo "Removing $(NAME)" + @$(RM) $(NAME) + +re: fclean all + +.PHONY: doc +doc: + mkdir -p tmp + for f in $(SRC) $(INCLUDE); do mkdir -p tmp/`dirname $$f` && sed 's_^/\*$$_/**_' $$f > tmp/$$f; done + $(DOXYGEN) $(DOXYGEN_FILE) + +doc_clean: + $(RM) -r $(DOC_DIR) + diff --git a/libft/include/libft.h b/libft/include/libft.h new file mode 100644 index 0000000..e982608 --- /dev/null +++ b/libft/include/libft.h @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */ +/* Updated: 2020/04/03 06:32:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include <unistd.h> +# include <stdarg.h> +# include <stdlib.h> +# include <stddef.h> +# include <stdbool.h> +# include <limits.h> +# include <errno.h> + +# ifdef __linux__ +# include <stdio.h> +# define OPEN_MAX FOPEN_MAX +# endif + +# include "libft_io.h" +# include "libft_mem.h" +# include "libft_str.h" +# include "libft_ctype.h" + +#endif diff --git a/libft/include/libft_algo.h b/libft/include/libft_algo.h new file mode 100644 index 0000000..e859de5 --- /dev/null +++ b/libft/include/libft_algo.h @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_algo.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 07:22:57 by cacharle #+# #+# */ +/* Updated: 2020/04/04 23:33:51 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** \file libft_algo.h +** \brief Algorithms +*/ + +#ifndef LIBFT_ALGO_H +# define LIBFT_ALGO_H + +# include <stdlib.h> +# include "libft_mem.h" +# include "libft_def.h" +# include "libft_str.h" + +/* +** \brief Range struct +** \param lo Lower bound +** \param hi Upper bound +*/ + +typedef struct +{ + int lo; + int hi; +} t_ftrange; + +/* +** \brief Merge sort consts struct +** \param base Array to sort +** \param left Left subarray +** \param right Right subarray +** \note Only used internaly by ft_mergesort +*/ + +struct s_merge_sorted_arrays +{ + void *base; + void *left; + void *right; +}; + +/* +** remove this horror +*/ + +typedef struct s_ft_search_const +{ + const void *key; + t_ftcompar_func compar; +} t_ftsearch_const; + +bool ft_is_set(void *base, size_t nel, size_t width, + t_ftcompar_func compar); +int ft_compar_int(const void *a, const void *b); +int ft_compar_str(const void *s1_p, const void *s2_p); +void ft_qsort(void *base, size_t nel, size_t width, + t_ftcompar_func compar); +int ft_mergesort(void *base, size_t nel, size_t width, + int (*compar)(const void *, const void *)); +int ft_heapsort(void *base, size_t nel, size_t width, + int (*compar)(const void *, const void *)); +void ft_reverse(void *base, size_t nel, size_t width); +void *ft_bsearch(const void *base, size_t nel, size_t width, + t_ftsearch_const *consts); +void *ft_lfind(const void *base, size_t *nelp, size_t width, + t_ftsearch_const *consts); +void *ft_lsearch(const void *base, size_t *nelp, size_t width, + t_ftsearch_const *consts); + +#endif diff --git a/libft/include/libft_bt.h b/libft/include/libft_bt.h new file mode 100644 index 0000000..6e2cc91 --- /dev/null +++ b/libft/include/libft_bt.h @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_bt.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/07 21:26:34 by cacharle #+# #+# */ +/* Updated: 2020/02/07 21:34:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_BT_H +# define LIBFT_BT_H + +# include <stdlib.h> + +typedef struct s_ftbtree +{ + void *data; + struct s_ftbtree *left; + struct s_ftbtree *right; +} t_ftbtree; + +t_ftbtree *ft_btnew(void *data); +void ft_btdestroy(t_ftbtree *tree, void (*del)(void *data)); + +#endif diff --git a/libft/include/libft_ctype.h b/libft/include/libft_ctype.h new file mode 100644 index 0000000..61f4534 --- /dev/null +++ b/libft/include/libft_ctype.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_ctype.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:35:31 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:08:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_CTYPE_H +# define LIBFT_CTYPE_H + +/* +** assertion +*/ + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_isspace(int c); +int ft_isblank(int c); + +/* +** conversion +*/ + +int ft_toupper(int c); +int ft_tolower(int c); +int ft_todigit(int c); + +#endif diff --git a/libft/include/libft_def.h b/libft/include/libft_def.h new file mode 100644 index 0000000..7406959 --- /dev/null +++ b/libft/include/libft_def.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_def.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:36:56 by cacharle #+# #+# */ +/* Updated: 2020/04/04 21:40:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** \file libft_def.h +** \brief Type and constant definition +*/ + +#ifndef LIBFT_DEF_H +# define LIBFT_DEF_H + +# include <stddef.h> +# include <stdbool.h> + +# define TRUE 1 +# define FALSE 0 + +typedef unsigned char t_ftbyte; +typedef int t_ftbool; +typedef unsigned int t_ftsize; + +typedef char t_ftchar; +typedef unsigned char t_ftuchar; +typedef int t_ftint; +typedef unsigned int t_ftuint; +typedef long int t_ftlong; +typedef unsigned long int t_ftulong; + +/* +** \brief Standard delete function +** \param x Resource to delete +*/ + +typedef void (*t_ftdel_func)(void *x); + +/* +** \brief Standard comparison function +** \param x1 Resource 1 +** \param x2 Resource 2 +** \return negative number if x1 < x2, +** 0 if x1 == x2, +** positive number if x1 > x2 +*/ + +typedef int (*t_ftcompar_func)(const void *x1, const void *x2); + +#endif diff --git a/libft/include/libft_dlst.h b/libft/include/libft_dlst.h new file mode 100644 index 0000000..9870ea7 --- /dev/null +++ b/libft/include/libft_dlst.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_dlst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:15:04 by charles #+# #+# */ +/* Updated: 2020/04/03 15:44:32 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_DLST_H +# define LIBFT_DLST_H + +# include <stdlib.h> +# include "libft_def.h" + +typedef struct s_ftdlst +{ + struct s_ftdlst *prev; + struct s_ftdlst *next; + void *data; +} t_ftdlst; + +t_ftdlst *ft_dlstnew(void *data); +void ft_dlstdestroy(t_ftdlst *dlst, t_ftdel_func del); +void ft_dlstdelone(t_ftdlst *dlst, t_ftdel_func del); + +#endif diff --git a/libft/include/libft_dstr.h b/libft/include/libft_dstr.h new file mode 100644 index 0000000..d482f73 --- /dev/null +++ b/libft/include/libft_dstr.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_dstr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 10:39:51 by charles #+# #+# */ +/* Updated: 2020/06/09 17:35:47 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_DSTR_H +# define LIBFT_DSTR_H + +# include <stdlib.h> +# include "libft_def.h" +# include "libft_str.h" +# include "libft_mem.h" + +/* +** \brief Dynamic string struct +** \param str Underlying null-terminated character array +** \param length Number of character (not including the '\0') +** \param capacity Maximum length - 1 of the current string +*/ + +typedef struct s_ftdstr +{ + char *str; + size_t length; + size_t capacity; +} t_ftdstr; + +t_ftdstr *ft_dstrnew(char *from); +void ft_dstrdestroy(t_ftdstr *dstr); +t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least); +t_ftdstr *ft_dstrwrap(char *str); +char *ft_dstrunwrap(t_ftdstr *dstr); +t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i); +void ft_dstrerase(t_ftdstr *dstr, size_t start, size_t len); +t_ftdstr *ft_dstrsubstitute(t_ftdstr *dstr, char *replacement, + size_t start, size_t end); + +#endif diff --git a/libft/include/libft_ht.h b/libft/include/libft_ht.h new file mode 100644 index 0000000..91aa153 --- /dev/null +++ b/libft/include/libft_ht.h @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_ht.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:36:09 by cacharle #+# #+# */ +/* Updated: 2020/09/09 15:47:22 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_HT_H +# define LIBFT_HT_H + +/* +** \file libft_ht.h +** \brief Hash table manipulation +*/ + +# include "libft.h" +# include "libft_def.h" +# include "libft_lst.h" +# include "libft_mem.h" + +/* +** \brief Hash table entry, key/value pair +** \param key String key +** \param value Pointer to data +*/ + +typedef struct s_ftht_entry +{ + char *key; + void *value; +} t_ftht_entry; + +/* +** \brief Hash table struct +** \param size Number of buckets +** \param buckets Bucket array, each bucket is a linked list +*/ + +typedef struct s_ftht +{ + size_t size; + t_ftlst **buckets; +} t_ftht; + +size_t ft_hthash(t_ftht *ht, char *key); + +t_ftht *ft_htnew(size_t size); +void ft_htdestroy(t_ftht *ht, t_ftdel_func del); +void *ft_htget(t_ftht *ht, char *key); +t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value, + t_ftdel_func del); +t_ftht_entry *ft_htset_safe(t_ftht *ht, char *key, void *value, + t_ftdel_func del); +void ft_htdelone(t_ftht *ht, char *key, t_ftdel_func del); +t_ftht_entry *ft_htentry_new(char *key, void *value); +void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*)); + +/* +** internals +*/ + +int ft_inter_htkey_cmp(const void *ref_key, const void *content); +void ft_inter_htdel_first_order(t_ftht_entry *entry); +void ft_inter_htdel_first_order_setup(t_ftdel_func del); +void ft_inter_htdel_first_order_teardown(void); + +#endif diff --git a/libft/include/libft_io.h b/libft/include/libft_io.h new file mode 100644 index 0000000..1294abc --- /dev/null +++ b/libft/include/libft_io.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_io.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:35:43 by cacharle #+# #+# */ +/* Updated: 2020/06/19 17:45:34 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_IO_H +# define LIBFT_IO_H + +# include <unistd.h> +# include <stdlib.h> +# include <limits.h> +# include "libft.h" + +void ft_putendl(char *s); +void ft_putchar(char c); +void ft_putstr(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); + +char ft_getchar(void); + +# ifndef FTGL_BUFFER_SIZE +# define FTGL_BUFFER_SIZE 32 +# endif + +# define FTGL_OK 1 +# define FTGL_EOF 0 +# define FTGL_ERROR -1 + +/* +** ft_getline.c (get_next_line) +*/ + +int ft_getline(int fd, char **line); + +#endif diff --git a/libft/include/libft_lst.h b/libft/include/libft_lst.h new file mode 100644 index 0000000..0dc1e7c --- /dev/null +++ b/libft/include/libft_lst.h @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_lst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:36:39 by cacharle #+# #+# */ +/* Updated: 2020/08/20 14:40:01 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_LST_H +# define LIBFT_LST_H + +/* +** \file libft_lst.h +** \brief Linked list Manipulation +*/ + +# include <stdlib.h> +# include "libft_def.h" +# include "libft_algo.h" + +/* +** \brief List struct +** \param data Pointer to node data +** \param next Pointer to next node or NULL if last node +*/ + +typedef struct s_ftlst +{ + void *data; + struct s_ftlst *next; +} t_ftlst; + +typedef void (*t_ftdel_func)(void *); + +t_ftlst *ft_lstnew(void const *data); +int ft_lstsize(t_ftlst *lst); +void ft_lstpush_front(t_ftlst **alst, t_ftlst *new); +t_ftlst *ft_lstpush_front_node(t_ftlst **alst, void *data); +void ft_lstpush_back(t_ftlst **alst, t_ftlst *new); +void ft_lstpop_front(t_ftlst **lst, void (*del)(void *)); +void ft_lstpop_back(t_ftlst **lst, void (*del)(void *)); +t_ftlst *ft_lstlast(t_ftlst *lst); +void ft_lstdelone(t_ftlst *lst, void (*del)(void *)); +void *ft_lstdestroy(t_ftlst **lst, void (*del)(void *)); +void ft_lstiter(t_ftlst *lst, void (*f)(void *)); +t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), + t_ftdel_func del); +t_ftlst *ft_lstreverse_ret(t_ftlst *lst); +void ft_lstreverse(t_ftlst **lst); +void ft_lstremove_if(t_ftlst **lst, + t_ftcompar_func cmp, const void *ref, + t_ftdel_func del); +void ft_lstinsert(t_ftlst **lst, t_ftlst *insert, size_t i); +t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftcompar_func cmp, + const void *ref); +t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftcompar_func cmp, + const void *ref); +t_ftlst *ft_lstlfind(t_ftlst *lst, t_ftcompar_func cmp, + const void *ref); +void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp); +t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, + t_ftcompar_func cmp); + +#endif diff --git a/libft/include/libft_mem.h b/libft/include/libft_mem.h new file mode 100644 index 0000000..26b4ccd --- /dev/null +++ b/libft/include/libft_mem.h @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_mem.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:35:57 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:17:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_MEM_H +# define LIBFT_MEM_H + +# include <stddef.h> +# include "libft_def.h" + +void ft_bzero(void *s, size_t n); +void *ft_memset(void *s, int c, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memccpy(void *dest, const void *src, int c, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_calloc(size_t count, size_t size); +void ft_memswap(void *a, void *b, size_t size); +void *ft_memmem(const void *big, size_t big_len, + const void *little, size_t little_len); +void ft_memset_pattern4(void *b, const void *pattern4, + size_t len); + +/* +** bloat ? +*/ + +void ft_memdel(void **ap); + +#endif diff --git a/libft/include/libft_printf.h b/libft/include/libft_printf.h new file mode 100644 index 0000000..8e64208 --- /dev/null +++ b/libft/include/libft_printf.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:36:47 by cacharle #+# #+# */ +/* Updated: 2020/01/31 10:36:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_PRINTF_H +# define LIBFT_PRINTF_H + +int ft_printf(const char *format, ...); +int ft_sprintf(char *str, const char *format, ...); +int ft_snprintf(char *str, size_t size, const char *format, ...); +int ft_asprintf(char **ret, const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); +int ft_vprintf(const char *format, va_list ap); +int ft_vsprintf(char *str, const char *format, va_list ap); +int ft_vsnprintf(char *str, size_t size, const char *format, va_list ap); +int ft_vasprintf(char **ret, const char *format, va_list ap); +int ft_vdprintf(int fd, const char *format, va_list ap); + +#endif diff --git a/libft/include/libft_str.h b/libft/include/libft_str.h new file mode 100644 index 0000000..3a52a2f --- /dev/null +++ b/libft/include/libft_str.h @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:39:22 by cacharle #+# #+# */ +/* Updated: 2020/10/08 09:40:44 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_STR_H +# define LIBFT_STR_H + +# include <stddef.h> +# include <stdbool.h> +# include "libft_ctype.h" +# include "libft_util.h" + +typedef enum +{ + FT_STRJOINF_FST, + FT_STRJOINF_SND, + FT_STRJOINF_ALL +} t_ftstrjoinf_tag; + +/* +** std +*/ + +size_t ft_strlen(const char *s); +char *ft_strcpy(char *dest, const char *src); +char *ft_strncpy(char *dest, const char *src, size_t n); +char *ft_strdup(const char *s); +char *ft_strndup(const char *s1, size_t n); +char *ft_strcat(char *dest, const char *src); +char *ft_strncat(char *dest, const char *src, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *haystack, const char *needle); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *nptr); + +/* +** extra +*/ + +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strsub(char const *s, size_t start, size_t len); +char *ft_strsubf(char const *s, size_t start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strjoin3(char const *s1, char const *s2, char const *s3); +char *ft_strjoinf(char const *s1, char const *s2, t_ftstrjoinf_tag tag); +char *ft_strjoinf_fst(char const *s1, char const *s2); +char *ft_strjoinf_snd(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char **ft_splitf(char *s, char c); +int ft_strcount(char *str, char c); +char *ft_itoa(int n); +char *ft_itoa_cpy(char *dst, int n); +int ft_atoi_strict(const char *s); +long ft_strtol(const char *s, char **endptr, int base); +int ft_strcasecmp(const char *s1, const char *s2); +int ft_strncasecmp(const char *s1, const char *s2, size_t n); +size_t ft_strspn(const char *s, const char *charset); +size_t ft_strcspn(const char *s, const char *charset); +char *ft_strpbrk(const char *s, const char *charset); +char *ft_strsep(char **stringp, const char *delim); +size_t ft_strnlen(const char *s, size_t maxlen); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strtolower(char *s); +char *ft_strtoupper(char *s); +char *ft_strcat3(char *dest, const char *src1, const char *src2); + +/* +** glob +*/ + +bool ft_fnmatch(const char *pattern, const char *string); + +/* +** NULL terminated string array +*/ + +char *ft_strsjoin(char **strs, char *delim); +char *ft_strsjoinf(char **strs, char *delim); + +/* +** bloat ? +*/ + +/* +** char *ft_strnew(size_t size); +** void ft_strdel(char **as); +** void ft_strclr(char *s); +*/ + +#endif diff --git a/libft/include/libft_util.h b/libft/include/libft_util.h new file mode 100644 index 0000000..7a9e056 --- /dev/null +++ b/libft/include/libft_util.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_util.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 17:51:36 by cacharle #+# #+# */ +/* Updated: 2020/02/27 17:52:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_UTIL_H +# define LIBFT_UTIL_H + +# include <stdlib.h> + +void *ft_split_destroy(char **strs); + +#endif diff --git a/libft/include/libft_vec.h b/libft/include/libft_vec.h new file mode 100644 index 0000000..287ce6f --- /dev/null +++ b/libft/include/libft_vec.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_vec.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:57:16 by charles #+# #+# */ +/* Updated: 2020/10/11 14:04:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_VEC_H +# define LIBFT_VEC_H + +/* +** \file libft_vec.h +** \brief Vector manipulation +*/ + +# include <stdlib.h> +# include "libft_def.h" +# include "libft_mem.h" +# include "libft_algo.h" +# include "libft_lst.h" + +/* +** \brief Vector struct +** \param data Underlying array +** \param capacity Size of the underlying array +** \param size Number of element in the vector +*/ + +typedef struct s_ftvec +{ + void **data; + size_t capacity; + size_t size; +} t_ftvec; + +t_ftvec *ft_vecnew(size_t capacity); +void *ft_vecdestroy(t_ftvec *vec, void (*del)(void *elem)); +t_ftvec *ft_vecgrow(t_ftvec *vec); +t_ftvec *ft_vecreserve(t_ftvec *vec, size_t capacity); +t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed); +t_ftvec *ft_vecpush_safe(t_ftvec *vec, void *pushed); +void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)); +void ft_veciter(t_ftvec *vec, void (*f)(void *elem)); +void ft_veciter_addr(t_ftvec *vec, void (*f)(void **addr)); +void *ft_vectake(t_ftvec *vec, size_t i); +void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)); +t_ftvec *ft_vecinsert(t_ftvec *vec, size_t i, void *elem); +t_ftvec *ft_vecinsert_safe(t_ftvec *vec, size_t i, void *elem); +void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp); +void **ft_vecunwrap(t_ftvec *vec); +t_ftvec *ft_vecfrom_lst(t_ftlst *lst); +t_ftvec *ft_vecswallow_at( + t_ftvec *vec, size_t index, t_ftvec *swallowed); + +#endif diff --git a/libft/src/algo/ft_bsearch.c b/libft/src/algo/ft_bsearch.c new file mode 100644 index 0000000..5132fa2 --- /dev/null +++ b/libft/src/algo/ft_bsearch.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 05:29:05 by cacharle #+# #+# */ +/* Updated: 2020/02/13 23:14:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +void *ft_bsearch(const void *base, size_t nel, size_t width, + t_ftsearch_const *consts) +{ + int res; + size_t mid; + + if (nel < 1) + return (NULL); + mid = nel / 2; + res = (consts->compar)(consts->key, base + mid * width); + if (res == 0) + return ((void*)base + mid * width); + if (res < 0) + return (ft_bsearch(base, mid, width, consts)); + else + return (ft_bsearch(base + (mid + 1) * width, nel - mid - 1, + width, consts)); +} diff --git a/libft/src/algo/ft_compar_int.c b/libft/src/algo/ft_compar_int.c new file mode 100644 index 0000000..ab057bf --- /dev/null +++ b/libft/src/algo/ft_compar_int.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_compar_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 08:24:43 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:30:09 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief Comparison function for 2 ints +** \param a Pointer to first int +** \param b Pointer to first int +** \return The difference between a and b +*/ + +int ft_compar_int(const void *a, const void *b) +{ + return (*(int*)a - *(int*)b); +} diff --git a/libft/src/algo/ft_compar_str.c b/libft/src/algo/ft_compar_str.c new file mode 100644 index 0000000..6749ab0 --- /dev/null +++ b/libft/src/algo/ft_compar_str.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_compar_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 15:44:24 by charles #+# #+# */ +/* Updated: 2020/04/04 22:31:15 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +/* +** \brief String comparison function +** \param s1_p Pointer to first string +** \param s2_p Pointer to second string +** \return `strcmp` of both strings +*/ + +int ft_compar_str(const void *s1_p, const void *s2_p) +{ + return (ft_strcmp(*(const char**)s1_p, *(const char**)s2_p)); +} diff --git a/libft/src/algo/ft_is_set.c b/libft/src/algo/ft_is_set.c new file mode 100644 index 0000000..fd26a88 --- /dev/null +++ b/libft/src/algo/ft_is_set.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_is_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:33:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +/* +** \brief Test whether array only contains unique elements +** \param base Array to test +** \param nel Number of element in the array +** \param width Size of an element +** \param compar Comparison function to test if 2 elements are equal +*/ + +bool ft_is_set(void *base, size_t nel, size_t width, + t_ftcompar_func compar) +{ + size_t i; + + if (nel < 2) + return (TRUE); + ft_qsort(base, nel, width, compar); + i = 0; + while (i < nel - 1) + { + if (compar(base + (i * width), base + ((i + 1) * width)) == 0) + return (FALSE); + i++; + } + return (TRUE); +} diff --git a/libft/src/algo/ft_lfind.c b/libft/src/algo/ft_lfind.c new file mode 100644 index 0000000..8538f50 --- /dev/null +++ b/libft/src/algo/ft_lfind.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lfind.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 05:49:19 by cacharle #+# #+# */ +/* Updated: 2020/02/10 05:58:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +void *ft_lfind(const void *base, size_t *nelp, size_t width, + t_ftsearch_const *consts) +{ + size_t i; + + i = 0; + while (i < *nelp) + { + if ((consts->compar)(consts->key, base + i * width) == 0) + return ((void*)base + i * width); + i++; + } + return (NULL); +} diff --git a/libft/src/algo/ft_lsearch.c b/libft/src/algo/ft_lsearch.c new file mode 100644 index 0000000..4c77bca --- /dev/null +++ b/libft/src/algo/ft_lsearch.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 05:53:57 by cacharle #+# #+# */ +/* Updated: 2020/02/10 05:59:33 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +void *ft_lsearch(const void *base, size_t *nelp, size_t width, + t_ftsearch_const *consts) +{ + void *found; + + if ((found = ft_lfind(base, nelp, width, consts)) != NULL) + return (found); + return (ft_memcpy((void*)base + (*nelp)++ * width, consts->key, width)); +} diff --git a/libft/src/algo/ft_mergesort.c b/libft/src/algo/ft_mergesort.c new file mode 100644 index 0000000..8556145 --- /dev/null +++ b/libft/src/algo/ft_mergesort.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mergesort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 02:26:41 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:40:55 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +/* +** \brief Helper function to return in case of error +** \param left Left subarray +** \param right Right subarray +** \return -1 to indicate error +*/ + +static int st_mergesort_error(void *left, void *right) +{ + free(left); + free(right); + return (-1); +} + +/* +** \brief Merge 2 sorted arrays +** \param arrays Struct containing the arrays (base, left, right) +** \param nel Number of element in base +** \param compar Comparison function +*/ + +static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, + size_t width, int (*compar)(const void *, const void *)) +{ + size_t bi; + size_t li; + size_t ri; + size_t mid; + + mid = nel / 2; + bi = 0; + li = 0; + ri = 0; + while (li < mid && ri < nel - mid) + { + if (compar(arrays->left + li * width, arrays->right + ri * width) < 0) + ft_memcpy(arrays->base + bi * width, + arrays->left + li++ * width, width); + else + ft_memcpy(arrays->base + bi * width, + arrays->right + ri++ * width, width); + bi++; + } + while (li < mid) + ft_memcpy(arrays->base + bi++ * width, + arrays->left + li++ * width, width); + while (ri < nel - mid) + ft_memcpy(arrays->base + bi++ * width, + arrays->right + ri++ * width, width); +} + +/* +** \brief Sort an array using the merge sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \return 0 on success, -1 on error +*/ + +int ft_mergesort(void *base, size_t nel, size_t width, + int (*compar)(const void *, const void *)) +{ + size_t mid; + struct s_merge_sorted_arrays arrays; + + if (nel < 2) + return (0); + mid = nel / 2; + if ((arrays.left = malloc(mid * width)) == NULL) + return (-1); + if ((arrays.right = malloc((nel - mid) * width)) == NULL) + return (st_mergesort_error(arrays.left, NULL)); + ft_memcpy(arrays.left, base, mid * width); + ft_memcpy(arrays.right, base + mid * width, (nel - mid) * width); + if (ft_mergesort(arrays.left, mid, width, compar) == -1) + return (st_mergesort_error(arrays.left, arrays.right)); + if (ft_mergesort(arrays.right, nel - mid, width, compar) == -1) + return (st_mergesort_error(arrays.left, arrays.right)); + arrays.base = base; + st_merge_sorted(&arrays, nel, width, compar); + free(arrays.left); + free(arrays.right); + return (0); +} diff --git a/libft/src/algo/ft_qsort.c b/libft/src/algo/ft_qsort.c new file mode 100644 index 0000000..0c1c0f7 --- /dev/null +++ b/libft/src/algo/ft_qsort.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_qsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 07:25:51 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:10:29 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +/* +** \brief Helper to create a new range +** \param lo Lower bound +** \param hi Higher bound +** \return Range struct (not allocated) +*/ + +static t_ftrange ft_range_new(int lo, int hi) +{ + t_ftrange range; + + range.lo = lo; + range.hi = hi; + return (range); +} + +/* +** \brief Array partitionning, +** takes a pivot and place every element lower than it before +** and every element greater after +** \param base Partitioned array +** \param range Lower and upper bound +** \param width Single element size +** \param compar Comparison function +*/ + +static int ft_qsort_partition(void *base, t_ftrange range, + size_t width, t_ftcompar_func compar) +{ + void *pivot; + int p; + int i; + + pivot = base + (range.hi * width); + p = range.lo; + i = range.lo - 1; + while (++i < range.hi) + { + if (compar(base + (i * width), pivot) < 0) + { + ft_memswap(base + (i * width), base + (p * width), width); + p++; + } + } + ft_memswap(pivot, base + (p * width), width); + return (p); +} + +/* +** \brief Qsort recursion function +** \param base Array to sort +** \param range Lower and upper bound which define the area to sort +** \param width Single element size +** \param compar Comparision function +*/ + +static void ft_qsort_rec(void *base, t_ftrange range, + size_t width, t_ftcompar_func compar) +{ + int pivot; + + if (range.lo >= range.hi) + return ; + pivot = ft_qsort_partition(base, range, width, compar); + ft_qsort_rec(base, ft_range_new(range.lo, pivot - 1), width, compar); + ft_qsort_rec(base, ft_range_new(pivot + 1, range.hi), width, compar); +} + +/* +** \brief Sort an array using the Quick sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \param compar Comparision function +*/ + +void ft_qsort(void *base, size_t nel, size_t width, + t_ftcompar_func compar) +{ + if (width == 0 || nel < 2) + return ; + ft_qsort_rec(base, ft_range_new(0, nel - 1), width, compar); +} diff --git a/libft/src/algo/ft_reverse.c b/libft/src/algo/ft_reverse.c new file mode 100644 index 0000000..c617338 --- /dev/null +++ b/libft/src/algo/ft_reverse.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 05:07:13 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:36:23 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +/* +** \brief Reverse an array +** \param base Array to reverse +** \param nel Number of element in the array +** \param width Size of each elements +*/ + +void ft_reverse(void *base, size_t nel, size_t width) +{ + size_t i; + + i = 0; + nel--; + while (i < nel) + { + ft_memswap(base + i * width, base + nel * width, width); + i++; + nel--; + } +} diff --git a/libft/src/bt/ft_btdestroy.c b/libft/src/bt/ft_btdestroy.c new file mode 100644 index 0000000..c802db0 --- /dev/null +++ b/libft/src/bt/ft_btdestroy.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_btdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/07 21:30:53 by cacharle #+# #+# */ +/* Updated: 2020/02/07 21:35:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_bt.h" + +void ft_btdestroy(t_ftbtree *tree, void (*del)(void *data)) +{ + if (tree == NULL) + return ; + ft_btdestroy(tree->left, del); + ft_btdestroy(tree->right, del); + (*del)(tree->data); + free(tree); +} diff --git a/libft/src/bt/ft_btnew.c b/libft/src/bt/ft_btnew.c new file mode 100644 index 0000000..973e1a4 --- /dev/null +++ b/libft/src/bt/ft_btnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_btnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/07 21:33:16 by cacharle #+# #+# */ +/* Updated: 2020/02/07 21:34:35 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_bt.h" + +t_ftbtree *ft_btnew(void *data) +{ + t_ftbtree *tree; + + if ((tree = (t_ftbtree*)malloc(sizeof(t_ftbtree))) == NULL) + return (NULL); + tree->data = data; + tree->left = NULL; + tree->right = NULL; + return (tree); +} diff --git a/libft/src/ctype/ft_isalnum.c b/libft/src/ctype/ft_isalnum.c new file mode 100644 index 0000000..1ee1e0f --- /dev/null +++ b/libft/src/ctype/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:41:40 by cacharle #+# #+# */ +/* Updated: 2019/10/07 09:41:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft/src/ctype/ft_isalpha.c b/libft/src/ctype/ft_isalpha.c new file mode 100644 index 0000000..6f155b4 --- /dev/null +++ b/libft/src/ctype/ft_isalpha.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:54:52 by cacharle #+# #+# */ +/* Updated: 2019/10/20 13:01:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); +} diff --git a/libft/src/ctype/ft_isascii.c b/libft/src/ctype/ft_isascii.c new file mode 100644 index 0000000..12b6849 --- /dev/null +++ b/libft/src/ctype/ft_isascii.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:54:30 by cacharle #+# #+# */ +/* Updated: 2020/02/12 23:01:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + return (c >= 00 && c <= 0177); +} diff --git a/libft/src/ctype/ft_isblank.c b/libft/src/ctype/ft_isblank.c new file mode 100644 index 0000000..def106b --- /dev/null +++ b/libft/src/ctype/ft_isblank.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isblank.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 05:17:45 by cacharle #+# #+# */ +/* Updated: 2020/02/10 05:18:34 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isblank(int c) +{ + return (c == ' ' || c == '\t'); +} diff --git a/libft/src/ctype/ft_isdigit.c b/libft/src/ctype/ft_isdigit.c new file mode 100644 index 0000000..f8a5850 --- /dev/null +++ b/libft/src/ctype/ft_isdigit.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:41:20 by cacharle #+# #+# */ +/* Updated: 2019/10/07 10:41:25 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft/src/ctype/ft_isprint.c b/libft/src/ctype/ft_isprint.c new file mode 100644 index 0000000..c311709 --- /dev/null +++ b/libft/src/ctype/ft_isprint.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:52:09 by cacharle #+# #+# */ +/* Updated: 2019/10/20 13:03:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + return (c >= ' ' && c <= '~'); +} diff --git a/libft/src/ctype/ft_isspace.c b/libft/src/ctype/ft_isspace.c new file mode 100644 index 0000000..18b6dba --- /dev/null +++ b/libft/src/ctype/ft_isspace.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 11:33:36 by cacharle #+# #+# */ +/* Updated: 2020/01/15 11:35:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int c) +{ + return (c == ' ' || c == '\t' || c == '\n' + || c == '\v' || c == '\f' || c == '\r'); +} diff --git a/libft/src/ctype/ft_todigit.c b/libft/src/ctype/ft_todigit.c new file mode 100644 index 0000000..f201470 --- /dev/null +++ b/libft/src/ctype/ft_todigit.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_todigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 10:37:57 by cacharle #+# #+# */ +/* Updated: 2020/02/12 23:11:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_todigit(int c) +{ + if (!ft_isdigit(c)) + return (-1); + return (c & 0x0F); +} diff --git a/libft/src/ctype/ft_tolower.c b/libft/src/ctype/ft_tolower.c new file mode 100644 index 0000000..919469f --- /dev/null +++ b/libft/src/ctype/ft_tolower.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:14:26 by cacharle #+# #+# */ +/* Updated: 2019/11/20 01:04:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** E: 0100 0101 +** e: 0110 0101 +** ^ +*/ + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c | 0b00100000); + return (c); +} diff --git a/libft/src/ctype/ft_toupper.c b/libft/src/ctype/ft_toupper.c new file mode 100644 index 0000000..8579b91 --- /dev/null +++ b/libft/src/ctype/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:14:10 by cacharle #+# #+# */ +/* Updated: 2019/11/20 01:04:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c ^ 0b00100000); + return (c); +} diff --git a/libft/src/dlst/ft_dlstdelone.c b/libft/src/dlst/ft_dlstdelone.c new file mode 100644 index 0000000..7f826f5 --- /dev/null +++ b/libft/src/dlst/ft_dlstdelone.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:28:47 by charles #+# #+# */ +/* Updated: 2020/04/03 15:40:32 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dlst.h" + +void ft_dlstdelone(t_ftdlst *dlst, t_ftdel_func del) +{ + if (dlst == NULL) + return ; + if (dlst->prev != NULL) + dlst->prev->next = dlst->next; + if (dlst->next != NULL) + dlst->next->prev = dlst->prev; + if (del != NULL) + del(dlst->data); + free(dlst); +} diff --git a/libft/src/dlst/ft_dlstdestroy.c b/libft/src/dlst/ft_dlstdestroy.c new file mode 100644 index 0000000..6ce0d98 --- /dev/null +++ b/libft/src/dlst/ft_dlstdestroy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:22:51 by charles #+# #+# */ +/* Updated: 2020/04/03 15:44:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dlst.h" + +void ft_dlstdestroy(t_ftdlst *dlst, t_ftdel_func del) +{ + if (dlst == NULL) + return ; + if (dlst->prev != NULL) + { + dlst->prev->next = NULL; + ft_dlstdestroy(dlst->prev, del); + } + if (dlst->next != NULL) + { + dlst->next->prev = NULL; + ft_dlstdestroy(dlst->next, del); + } + if (del != NULL) + del(dlst->data); + free(dlst); +} diff --git a/libft/src/dlst/ft_dlstnew.c b/libft/src/dlst/ft_dlstnew.c new file mode 100644 index 0000000..6eb9346 --- /dev/null +++ b/libft/src/dlst/ft_dlstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:21:38 by charles #+# #+# */ +/* Updated: 2020/04/03 15:22:45 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dlst.h" + +t_ftdlst *ft_dlstnew(void *data) +{ + t_ftdlst *dlst; + + if ((dlst = (t_ftdlst*)malloc(sizeof(t_ftdlst))) == NULL) + return (NULL); + dlst->prev = NULL; + dlst->next = NULL; + dlst->data = data; + return (dlst); +} diff --git a/libft/src/dstr/ft_dstrdestroy.c b/libft/src/dstr/ft_dstrdestroy.c new file mode 100644 index 0000000..c76b692 --- /dev/null +++ b/libft/src/dstr/ft_dstrdestroy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:58:46 by charles #+# #+# */ +/* Updated: 2020/04/04 19:51:28 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Destroy a dynamic string +** \param dstr Dynamic string to destroy +*/ + +void ft_dstrdestroy(t_ftdstr *dstr) +{ + if (dstr == NULL) + return ; + free(dstr->str); + free(dstr); +} diff --git a/libft/src/dstr/ft_dstrerase.c b/libft/src/dstr/ft_dstrerase.c new file mode 100644 index 0000000..b758d3a --- /dev/null +++ b/libft/src/dstr/ft_dstrerase.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrerase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 00:39:12 by charles #+# #+# */ +/* Updated: 2020/10/11 14:00:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Erase part of a dynamic string +** \param dstr Dynamic string to erase from +** \param start Erase starting index +** \param len Number of character to erase +*/ + +void ft_dstrerase(t_ftdstr *dstr, size_t start, size_t len) +{ + if (start > dstr->length) + return ; + if (start + len > dstr->length) + len = dstr->length - start; + ft_memmove( + dstr->str + start, + dstr->str + start + len, + dstr->length - start - len); + dstr->length -= len; + dstr->str[dstr->length] = '\0'; +} diff --git a/libft/src/dstr/ft_dstrgrow.c b/libft/src/dstr/ft_dstrgrow.c new file mode 100644 index 0000000..40cad86 --- /dev/null +++ b/libft/src/dstr/ft_dstrgrow.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:17:09 by charles #+# #+# */ +/* Updated: 2020/04/04 19:56:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +#define FT_DSTR_GROWTH_FACTOR 1.5 + +/* +** \brief Grow the capacity of a dynamic string +** \param dstr Dynamic string to grow +** \param at_least Minimum capacity - 1 required +** \return Passed dynamic string or NULL on error +*/ + +t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least) +{ + size_t new_capacity; + char *new_str; + + if (at_least < dstr->capacity - 1) + return (dstr); + new_capacity = dstr->capacity <= 1 ? 2 : dstr->capacity; + while (at_least > new_capacity - 1) + new_capacity *= FT_DSTR_GROWTH_FACTOR; + if ((new_str = (char*)malloc(sizeof(char) * new_capacity)) == NULL) + return (NULL); + ft_memcpy(new_str, dstr->str, dstr->capacity); + dstr->capacity = new_capacity; + free(dstr->str); + dstr->str = new_str; + return (dstr); +} diff --git a/libft/src/dstr/ft_dstrinsert.c b/libft/src/dstr/ft_dstrinsert.c new file mode 100644 index 0000000..931f5d8 --- /dev/null +++ b/libft/src/dstr/ft_dstrinsert.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:00:44 by charles #+# #+# */ +/* Updated: 2020/04/04 21:21:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Insert string in dynamic string +** \param dstr Dynamic string where the string will be inserted +** \param inserted Static string to insert +** \param i Index where it should be inserted +** \return Passed dynamic string or NULL on error +*/ + +t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i) +{ + size_t inserted_len; + + if (i > dstr->length) + return (NULL); + inserted_len = ft_strlen(inserted); + if (ft_dstrgrow(dstr, dstr->capacity + inserted_len) == NULL) + return (NULL); + ft_memmove(dstr->str + i + inserted_len, + dstr->str + i, + dstr->length - i + 1); + ft_memcpy(dstr->str + i, inserted, inserted_len); + dstr->length += inserted_len; + return (dstr); +} diff --git a/libft/src/dstr/ft_dstrnew.c b/libft/src/dstr/ft_dstrnew.c new file mode 100644 index 0000000..fe9ce46 --- /dev/null +++ b/libft/src/dstr/ft_dstrnew.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:54:52 by charles #+# #+# */ +/* Updated: 2020/06/09 17:35:14 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Create a new dynamic string +** \param from Static string to create the dynamic one from +** (will be duplicated) +** \return Created dynamic string or NULL on malloc error +*/ + +t_ftdstr *ft_dstrnew(char *from) +{ + char *clone; + t_ftdstr *ret; + + if ((clone = ft_strdup(from)) == NULL) + return (NULL); + if ((ret = ft_dstrwrap(clone)) == NULL) + free(clone); + return (ret); +} diff --git a/libft/src/dstr/ft_dstrsubstitute.c b/libft/src/dstr/ft_dstrsubstitute.c new file mode 100644 index 0000000..84adc29 --- /dev/null +++ b/libft/src/dstr/ft_dstrsubstitute.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrsubstitute.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 00:22:55 by charles #+# #+# */ +/* Updated: 2020/04/05 00:38:40 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Substitute part of a dynamic string for an other string +** \param dstr Dynamic string to substitute in +** \param replacement Replacement text +** \param start Substitution start index +** \param len Substitution length +*/ + +t_ftdstr *ft_dstrsubstitute( + t_ftdstr *dstr, + char *replacement, + size_t start, + size_t len +) +{ + ft_dstrerase(dstr, start, len); + return (ft_dstrinsert(dstr, replacement, start)); +} diff --git a/libft/src/dstr/ft_dstrunwrap.c b/libft/src/dstr/ft_dstrunwrap.c new file mode 100644 index 0000000..5b29b5b --- /dev/null +++ b/libft/src/dstr/ft_dstrunwrap.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrunwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:59:35 by charles #+# #+# */ +/* Updated: 2020/04/04 19:58:41 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Destroy dynamic string but keep the underlying static string +** \param dstr Dynamic string to unwrap +** \return Underlying string of the dynamic one +*/ + +char *ft_dstrunwrap(t_ftdstr *dstr) +{ + char *tmp; + + tmp = dstr->str; + free(dstr); + return (tmp); +} diff --git a/libft/src/dstr/ft_dstrwrap.c b/libft/src/dstr/ft_dstrwrap.c new file mode 100644 index 0000000..0acf684 --- /dev/null +++ b/libft/src/dstr/ft_dstrwrap.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 17:32:30 by charles #+# #+# */ +/* Updated: 2020/06/09 17:33:57 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +t_ftdstr *ft_dstrwrap(char *str) +{ + t_ftdstr *dstr; + + if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL) + return (NULL); + dstr->str = str; + dstr->length = ft_strlen(str); + dstr->capacity = dstr->length + 1; + return (dstr); +} diff --git a/libft/src/ht/ft_htdelone.c b/libft/src/ht/ft_htdelone.c new file mode 100644 index 0000000..bc2e047 --- /dev/null +++ b/libft/src/ht/ft_htdelone.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ +/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +/* +** \brief Delete one hash table entry +** \param key Key of entry to delete +** \param del Function to destroy the entry value +** \note Do nothing if their is to entry which correspond to key +*/ + +void ft_htdelone(t_ftht *ht, char *key, void (*del)(void*)) +{ + ft_inter_htdel_first_order_setup(del); + ft_lstremove_if(ht->buckets + ft_hthash(ht, key), + ft_inter_htkey_cmp, key, + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); +} diff --git a/libft/src/ht/ft_htdestroy.c b/libft/src/ht/ft_htdestroy.c new file mode 100644 index 0000000..c43754e --- /dev/null +++ b/libft/src/ht/ft_htdestroy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ +/* Updated: 2020/04/03 07:01:30 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +/* +** \brief Destroy an hash table. +** \param del Function to delete each entry +** \warning The del function HAS to free the key +*/ + +void ft_htdestroy(t_ftht *ht, t_ftdel_func del) +{ + if (ht == NULL) + return ; + ft_inter_htdel_first_order_setup(del); + while (ht->size-- > 0) + ft_lstdestroy(ht->buckets + ht->size, + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); + free(ht->buckets); + free(ht); +} diff --git a/libft/src/ht/ft_htentry_new.c b/libft/src/ht/ft_htentry_new.c new file mode 100644 index 0000000..12a1159 --- /dev/null +++ b/libft/src/ht/ft_htentry_new.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htentry_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:45:36 by cacharle #+# #+# */ +/* Updated: 2020/02/17 04:09:50 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +/* +** \brief Create a new hash table key/value pair. +** \param key Hash entry string key (always duplicated) +** \return Content or NULL if an allocation failed. +*/ + +t_ftht_entry *ft_htentry_new(char *key, void *value) +{ + t_ftht_entry *content; + + if (key == NULL) + return (NULL); + if ((content = (t_ftht_entry*)malloc(sizeof(t_ftht_entry))) == NULL) + return (NULL); + if ((content->key = ft_strdup(key)) == NULL) + { + free(content); + return (NULL); + } + content->value = value; + return (content); +} diff --git a/libft/src/ht/ft_htget.c b/libft/src/ht/ft_htget.c new file mode 100644 index 0000000..75da785 --- /dev/null +++ b/libft/src/ht/ft_htget.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ +/* Updated: 2020/04/03 07:12:58 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +/* +** \brief Retrieve a value with a key +** \param ht Hash table where key is searched +** \param key Searched key +** \return Value void pointer at key or NULL if not found +*/ + +void *ft_htget(t_ftht *ht, char *key) +{ + size_t digest; + t_ftlst *found; + + if (ht == NULL || key == NULL) + return (NULL); + digest = ft_hthash(ht, key); + found = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); + if (found == NULL) + return (NULL); + return (((t_ftht_entry*)found->data)->value); +} diff --git a/libft/src/ht/ft_hthash.c b/libft/src/ht/ft_hthash.c new file mode 100644 index 0000000..200252f --- /dev/null +++ b/libft/src/ht/ft_hthash.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hthash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:56:01 by cacharle #+# #+# */ +/* Updated: 2020/10/11 14:00:00 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +/* +** \brief Hash a string +** \param ht So that the index is in the hash table bound +** \param key String to hash +** \return Hash +*/ + +size_t ft_hthash(t_ftht *ht, char *key) +{ + size_t digest; + + if (*key == '\0') + return (0); + digest = *key++ << 7; + while (*key != '\0') + { + digest = ((1000003 * digest) ^ *key) & (1 << 16); + key++; + } + return (digest % ht->size); +} diff --git a/libft/src/ht/ft_htiter.c b/libft/src/ht/ft_htiter.c new file mode 100644 index 0000000..b854993 --- /dev/null +++ b/libft/src/ht/ft_htiter.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:02:24 by charles #+# #+# */ +/* Updated: 2020/04/01 18:02:32 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +/* +** \brief Iterate over entry of hash table +** \param ht Iterated hash table +** \param f Function applied to each entry +*/ + +void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*)) +{ + size_t i; + + i = 0; + while (i < ht->size) + { + ft_lstiter(ht->buckets[i], (void (*)(void*))f); + i++; + } +} diff --git a/libft/src/ht/ft_htnew.c b/libft/src/ht/ft_htnew.c new file mode 100644 index 0000000..585c211 --- /dev/null +++ b/libft/src/ht/ft_htnew.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +/* +** \brief Create a new hash table. +** \param size Size of the underlying array of linked list (buckets) +** \return Created hash table or NULL is an allocation failed +*/ + +t_ftht *ft_htnew(size_t size) +{ + t_ftht *ht; + + if (size == 0) + return (NULL); + if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) + return (NULL); + ht->buckets = (t_ftlst**)ft_calloc(size, sizeof(t_ftlst*)); + if (ht->buckets == NULL) + { + free(ht); + return (NULL); + } + ht->size = size; + return (ht); +} diff --git a/libft/src/ht/ft_htset.c b/libft/src/ht/ft_htset.c new file mode 100644 index 0000000..b3a44ee --- /dev/null +++ b/libft/src/ht/ft_htset.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ +/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +/* +** \brief Create/Update a entry in hash table. +** \note If `key` already exist in `ht` +** only updates the list node entry. +** Else create a new list node in addition the list entry. +** \param ht Hash table where the entry is modified +** \param key Key of the new entry +** \param value Value of the new entry +** \param del Destroy function in case the entry is modified. +** \return Pointer to the created entry, NULL if an allocation failed. +*/ + +t_ftht_entry *ft_htset( + t_ftht *ht, + char *key, + void *value, + void (*del)(void*) +) +{ + size_t digest; + t_ftht_entry *entry; + t_ftlst *tmp; + + if (ht == NULL || key == NULL) + return (NULL); + digest = ft_hthash(ht, key); + tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); + if (tmp != NULL) + { + if (del != NULL) + del(((t_ftht_entry*)tmp->data)->value); + ((t_ftht_entry*)tmp->data)->value = value; + return ((t_ftht_entry*)tmp->data); + } + if ((entry = ft_htentry_new(key, value)) == NULL) + return (NULL); + if ((tmp = ft_lstnew(entry)) == NULL) + { + free(entry->key); + free(entry); + return (NULL); + } + ft_lstpush_front(ht->buckets + digest, tmp); + return (entry); +} diff --git a/libft/src/ht/ft_htset_safe.c b/libft/src/ht/ft_htset_safe.c new file mode 100644 index 0000000..fd132d2 --- /dev/null +++ b/libft/src/ht/ft_htset_safe.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htset_safe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/09 15:46:10 by charles #+# #+# */ +/* Updated: 2020/09/09 15:46:24 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +t_ftht_entry *ft_htset_safe( + t_ftht *ht, + char *key, + void *value, + void (*del)(void*) +) +{ + if (value == NULL) + return (NULL); + return (ft_htset(ht, key, value, del)); +} diff --git a/libft/src/ht/ft_inter_htdel_first_order.c b/libft/src/ht/ft_inter_htdel_first_order.c new file mode 100644 index 0000000..b6fd770 --- /dev/null +++ b/libft/src/ht/ft_inter_htdel_first_order.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_inter_htdel_first_order.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 06:56:54 by charles #+# #+# */ +/* Updated: 2020/04/03 06:58:36 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +static t_ftdel_func g_htdelone_value_del_func = NULL; + +void ft_inter_htdel_first_order(t_ftht_entry *entry) +{ + if (g_htdelone_value_del_func != NULL) + g_htdelone_value_del_func(entry->value); + free(entry->key); + free(entry); +} + +void ft_inter_htdel_first_order_setup(t_ftdel_func del) +{ + g_htdelone_value_del_func = del; +} + +void ft_inter_htdel_first_order_teardown(void) +{ + g_htdelone_value_del_func = NULL; +} diff --git a/libft/src/ht/ft_inter_htkey_cmp.c b/libft/src/ht/ft_inter_htkey_cmp.c new file mode 100644 index 0000000..e8a0375 --- /dev/null +++ b/libft/src/ht/ft_inter_htkey_cmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_internal_htkey_equal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:20:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +/* +** Hash table internal function to compare key string in linked list. +*/ + +int ft_inter_htkey_cmp(const void *ref_key, const void *content) +{ + if (ref_key == NULL || content == NULL) + return (-1); + return (ft_strcmp((char*)ref_key, ((t_ftht_entry*)content)->key)); +} diff --git a/libft/src/io/ft_getchar.c b/libft/src/io/ft_getchar.c new file mode 100644 index 0000000..9d233c0 --- /dev/null +++ b/libft/src/io/ft_getchar.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/18 10:29:54 by cacharle #+# #+# */ +/* Updated: 2020/02/14 02:24:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char ft_getchar(void) +{ + char c; + + if (read(STDIN_FILENO, &c, 1) < 0) + return (-1); + return (c); +} diff --git a/libft/src/io/ft_getline.c b/libft/src/io/ft_getline.c new file mode 100644 index 0000000..d1f2330 --- /dev/null +++ b/libft/src/io/ft_getline.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ +/* Updated: 2020/06/19 17:46:24 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int st_find_newline(char *str) +{ + int i; + + i = -1; + while (str[++i]) + if (str[i] == '\n') + return (i); + return (-1); +} + +static int st_free_return(char **ptr, char **ptr2, int ret) +{ + if (ptr != NULL) + { + free(*ptr); + *ptr = NULL; + } + if (ptr2 != NULL) + { + free(*ptr2); + *ptr2 = NULL; + } + return (ret); +} + +static int st_read_line(int fd, char **line, char *rest) +{ + int ret; + int split_at; + char *buf; + + if ((buf = malloc(sizeof(char) * (FTGL_BUFFER_SIZE + 1))) == NULL) + return (st_free_return(line, NULL, FTGL_ERROR)); + while ((ret = read(fd, buf, FTGL_BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((split_at = st_find_newline(buf)) != -1) + { + ft_strcpy(rest, buf + split_at + 1); + buf[split_at] = '\0'; + if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) + return (st_free_return(&buf, NULL, FTGL_ERROR)); + return (st_free_return(&buf, NULL, FTGL_OK)); + } + if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) + return (st_free_return(&buf, NULL, FTGL_ERROR)); + } + if (ret == -1) + return (st_free_return(&buf, line, FTGL_ERROR)); + return (st_free_return(&buf, NULL, ret)); +} + +/* +** if has rest: +** if rest has newline: +** push rest until newline in line, shift rest +** return LINE_READ +** else: +** push rest in line +** +** while can read fd in buf +** if buf has newline: +** push buf until newline in line +** push buf after newline in rest +** return LINE_READ +** push buf in line +** +** return FTGL_EOF +*/ + +int ft_getline(int fd, char **line) +{ + int split_at; + static char rest[OPEN_MAX][FTGL_BUFFER_SIZE + 1] = {{0}}; + + if (fd < 0 || fd > OPEN_MAX || line == NULL || FTGL_BUFFER_SIZE <= 0) + return (FTGL_ERROR); + if ((*line = ft_strdup("")) == NULL) + return (FTGL_ERROR); + if (rest[fd][0] == '\0') + return (st_read_line(fd, line, rest[fd])); + if ((split_at = st_find_newline(rest[fd])) != -1) + { + free(*line); + if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + return (FTGL_ERROR); + ft_strncpy(*line, rest[fd], split_at); + (*line)[split_at] = '\0'; + ft_strcpy(rest[fd], rest[fd] + split_at + 1); + return (FTGL_OK); + } + free(*line); + if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + return (FTGL_ERROR); + ft_strcpy(*line, rest[fd]); + rest[fd][0] = '\0'; + return (st_read_line(fd, line, rest[fd])); +} diff --git a/libft/src/io/ft_putchar.c b/libft/src/io/ft_putchar.c new file mode 100644 index 0000000..2838f0a --- /dev/null +++ b/libft/src/io/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:53:31 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:49:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar(char c) +{ + write(STDOUT_FILENO, &c, 1); +} diff --git a/libft/src/io/ft_putchar_fd.c b/libft/src/io/ft_putchar_fd.c new file mode 100644 index 0000000..97d6f7a --- /dev/null +++ b/libft/src/io/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:42:34 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:49:28 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + if (fd < 0 || fd > OPEN_MAX) + return ; + write(fd, &c, 1); +} diff --git a/libft/src/io/ft_putendl.c b/libft/src/io/ft_putendl.c new file mode 100644 index 0000000..880977e --- /dev/null +++ b/libft/src/io/ft_putendl.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:42:54 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:00:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char *s) +{ + ft_putendl_fd(s, STDOUT_FILENO); +} diff --git a/libft/src/io/ft_putendl_fd.c b/libft/src/io/ft_putendl_fd.c new file mode 100644 index 0000000..a8077fc --- /dev/null +++ b/libft/src/io/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:44:06 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:00:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (s == NULL || fd < 0 || fd > OPEN_MAX) + return ; + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/src/io/ft_putnbr.c b/libft/src/io/ft_putnbr.c new file mode 100644 index 0000000..247df40 --- /dev/null +++ b/libft/src/io/ft_putnbr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:52:33 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:59:34 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + ft_putnbr_fd(n, STDOUT_FILENO); +} diff --git a/libft/src/io/ft_putnbr_fd.c b/libft/src/io/ft_putnbr_fd.c new file mode 100644 index 0000000..169d1b5 --- /dev/null +++ b/libft/src/io/ft_putnbr_fd.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:40:35 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:46:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + unsigned int p_n; + + if (fd < 0 || fd > OPEN_MAX) + return ; + p_n = n; + if (n < 0) + { + ft_putchar_fd('-', fd); + p_n = -n; + } + if (p_n > 9) + ft_putnbr_fd(p_n / 10, fd); + ft_putchar_fd(p_n % 10 | 0x30, fd); +} diff --git a/libft/src/io/ft_putstr.c b/libft/src/io/ft_putstr.c new file mode 100644 index 0000000..14b01a3 --- /dev/null +++ b/libft/src/io/ft_putstr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:52:12 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:48:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + ft_putstr_fd((char*)s, STDOUT_FILENO); +} diff --git a/libft/src/io/ft_putstr_fd.c b/libft/src/io/ft_putstr_fd.c new file mode 100644 index 0000000..d0279ab --- /dev/null +++ b/libft/src/io/ft_putstr_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:40:15 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:47:59 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (s == NULL || fd < 0 || fd > OPEN_MAX) + return ; + write(fd, s, ft_strlen(s)); +} diff --git a/libft/src/lst/ft_lstbsearch.c b/libft/src/lst/ft_lstbsearch.c new file mode 100644 index 0000000..0c48eb0 --- /dev/null +++ b/libft/src/lst/ft_lstbsearch.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstbsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:12 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_lst.h" + +static t_ftlst *st_lstmiddle(t_ftlst *lst, t_ftlst *last) +{ + t_ftlst *slow; + t_ftlst *fast; + + if (lst == NULL) + return (NULL); + slow = lst; + fast = lst; + while (fast != last) + { + fast = fast->next; + if (fast == last) + break ; + slow = slow->next; + fast = fast->next; + } + return (slow); +} + +static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last, + t_ftcompar_func cmp, const void *ref) +{ + int res; + t_ftlst *mid; + + if (lst == NULL) + return (NULL); + mid = st_lstmiddle(lst, last); + if (mid == NULL) + return (NULL); + if (mid->next == NULL) + { + if (cmp(ref, mid->data) == 0) + return (mid); + return (NULL); + } + res = cmp(ref, mid->next->data); + if (res < 0) + return (st_lstbsearch_rec(lst, mid, cmp, ref)); + else if (res > 0) + return (st_lstbsearch_rec(mid->next->next, NULL, cmp, ref)); + return (mid->next); +} + +t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftcompar_func cmp, + const void *ref) +{ + return (st_lstbsearch_rec(lst, NULL, cmp, ref)); +} diff --git a/libft/src/lst/ft_lstdelone.c b/libft/src/lst/ft_lstdelone.c new file mode 100644 index 0000000..3dfbbbb --- /dev/null +++ b/libft/src/lst/ft_lstdelone.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:02 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Delete list node +** \param del Delete function for node's data +*/ + +void ft_lstdelone(t_ftlst *lst, void (*del)(void *)) +{ + if (lst == NULL) + return ; + if (del != NULL) + (*del)(lst->data); + free(lst); +} diff --git a/libft/src/lst/ft_lstdestroy.c b/libft/src/lst/ft_lstdestroy.c new file mode 100644 index 0000000..2af6fb7 --- /dev/null +++ b/libft/src/lst/ft_lstdestroy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:39 by cacharle #+# #+# */ +/* Updated: 2020/08/20 14:36:17 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Destroy a list and set his pointer to NULL +** \param del Delete Function for data of each node +** \return returns NULL +*/ + +void *ft_lstdestroy(t_ftlst **lst, void (*del)(void *)) +{ + if (lst == NULL) + return (NULL); + if (*lst == NULL) + return (NULL); + ft_lstdestroy(&((*lst)->next), del); + ft_lstdelone(*lst, del); + *lst = NULL; + return (NULL); +} diff --git a/libft/src/lst/ft_lstiter.c b/libft/src/lst/ft_lstiter.c new file mode 100644 index 0000000..e46b507 --- /dev/null +++ b/libft/src/lst/ft_lstiter.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:22 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:39 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Iterate of list +** \param f Funtion applied to data of each node +*/ + +void ft_lstiter(t_ftlst *lst, void (*f)(void *)) +{ + if (f == NULL) + return ; + while (lst != NULL) + { + (*f)(lst->data); + lst = lst->next; + } +} diff --git a/libft/src/lst/ft_lstlast.c b/libft/src/lst/ft_lstlast.c new file mode 100644 index 0000000..97b853d --- /dev/null +++ b/libft/src/lst/ft_lstlast.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:40 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Last node +** \return List's last node +*/ + +t_ftlst *ft_lstlast(t_ftlst *lst) +{ + if (lst == NULL) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/libft/src/lst/ft_lstlfind.c b/libft/src/lst/ft_lstlfind.c new file mode 100644 index 0000000..fd7e688 --- /dev/null +++ b/libft/src/lst/ft_lstlfind.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlfind.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 18:00:37 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:05 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +t_ftlst *ft_lstlfind(t_ftlst *lst, t_ftcompar_func cmp, const void *ref) +{ + if (lst == NULL) + return (NULL); + if (cmp(ref, lst->data) == 0) + return (lst); + return (ft_lstlfind(lst->next, cmp, ref)); +} diff --git a/libft/src/lst/ft_lstlsearch.c b/libft/src/lst/ft_lstlsearch.c new file mode 100644 index 0000000..11c528c --- /dev/null +++ b/libft/src/lst/ft_lstlsearch.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:18:33 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftcompar_func cmp, const void *ref) +{ + if (lst == NULL) + return (ft_lstnew(ref)); + if (cmp(ref, lst->data) == 0) + return (lst); + if (lst->next == NULL) + { + lst->next = ft_lstnew(ref); + return (lst->next); + } + return (ft_lstlsearch(lst->next, cmp, ref)); +} diff --git a/libft/src/lst/ft_lstmap.c b/libft/src/lst/ft_lstmap.c new file mode 100644 index 0000000..3182bb0 --- /dev/null +++ b/libft/src/lst/ft_lstmap.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:57 by cacharle #+# #+# */ +/* Updated: 2020/02/15 23:11:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Clone a list and map a function to each node data +** \param lst Origin list +** \param f Function applied to each node's data +** \param del Delete function for cleanning up in case of failed allocation +** \return Mapped clone list +*/ + +t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_ftlst *mapped; + t_ftlst *tmp; + + if (lst == NULL || f == NULL) + return (NULL); + mapped = NULL; + while (lst != NULL) + { + if ((tmp = ft_lstnew((*f)(lst->data))) == NULL) + { + ft_lstdestroy(&mapped, del); + return (NULL); + } + ft_lstpush_back(&mapped, tmp); + lst = lst->next; + } + return (mapped); +} + +/* +** Rest in peace, my beautiful recursion. +** +** t_ftlst *tmp; +** +** if (lst == NULL) +** return (NULL); +** if ((tmp = ft_lstnew(lst->data)) == NULL) +** return (NULL); +** tmp->data = (*f)(tmp->data); +** tmp->next = ft_lstmap(lst->next, f); +** return (tmp); +*/ diff --git a/libft/src/lst/ft_lstnew.c b/libft/src/lst/ft_lstnew.c new file mode 100644 index 0000000..1616b71 --- /dev/null +++ b/libft/src/lst/ft_lstnew.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:01:16 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:35 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Create a list node +** \param data Pointer to data of node +*/ + +t_ftlst *ft_lstnew(void const *data) +{ + t_ftlst *elem; + + if ((elem = (t_ftlst*)malloc(sizeof(t_ftlst))) == NULL) + return (NULL); + elem->data = (void*)data; + elem->next = NULL; + return (elem); +} diff --git a/libft/src/lst/ft_lstpop_back.c b/libft/src/lst/ft_lstpop_back.c new file mode 100644 index 0000000..100ffb2 --- /dev/null +++ b/libft/src/lst/ft_lstpop_back.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpop_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/08/20 13:07:51 by charles #+# #+# */ +/* Updated: 2020/08/20 13:10:38 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Delete last node +** \param del Delete function for node data +*/ + +void ft_lstpop_back(t_ftlst **lst, void (*del)(void *)) +{ + if (lst == NULL || *lst == NULL) + return ; + if ((*lst)->next == NULL) + { + ft_lstdelone(*lst, del); + *lst = NULL; + return ; + } + ft_lstpop_back(&(*lst)->next, del); +} diff --git a/libft/src/lst/ft_lstpop_front.c b/libft/src/lst/ft_lstpop_front.c new file mode 100644 index 0000000..a61350a --- /dev/null +++ b/libft/src/lst/ft_lstpop_front.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpop_front_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:29:58 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Delete head node and replace it with next node +** \param del Delete function for node data +*/ + +void ft_lstpop_front(t_ftlst **lst, void (*del)(void *)) +{ + t_ftlst *tmp; + + if (lst == NULL || *lst == NULL) + return ; + tmp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = tmp; +} diff --git a/libft/src/lst/ft_lstpush_back.c b/libft/src/lst/ft_lstpush_back.c new file mode 100644 index 0000000..1dca078 --- /dev/null +++ b/libft/src/lst/ft_lstpush_back.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:03 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Push new node to the list end +** \param new Pushed node +*/ + +void ft_lstpush_back(t_ftlst **alst, t_ftlst *new) +{ + if (alst == NULL) + return ; + if (*alst == NULL) + { + *alst = new; + return ; + } + ft_lstlast(*alst)->next = new; +} diff --git a/libft/src/lst/ft_lstpush_front.c b/libft/src/lst/ft_lstpush_front.c new file mode 100644 index 0000000..85df649 --- /dev/null +++ b/libft/src/lst/ft_lstpush_front.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:25 by cacharle #+# #+# */ +/* Updated: 2019/10/18 12:16:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Push node to list front +** \param new Pushed node +*/ + +void ft_lstpush_front(t_ftlst **alst, t_ftlst *new) +{ + if (alst == NULL || new == NULL) + return ; + new->next = *alst; + *alst = new; +} diff --git a/libft/src/lst/ft_lstpush_front_node.c b/libft/src/lst/ft_lstpush_front_node.c new file mode 100644 index 0000000..f6ff694 --- /dev/null +++ b/libft/src/lst/ft_lstpush_front_node.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_front_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/08/20 14:38:18 by charles #+# #+# */ +/* Updated: 2020/08/20 14:41:14 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +t_ftlst *ft_lstpush_front_node(t_ftlst **lst, void *data) +{ + t_ftlst *pushed; + + if (data == NULL || (pushed = ft_lstnew(data)) == NULL) + return (NULL); + ft_lstpush_front(lst, pushed); + return (*lst); +} diff --git a/libft/src/lst/ft_lstremove_if.c b/libft/src/lst/ft_lstremove_if.c new file mode 100644 index 0000000..4070355 --- /dev/null +++ b/libft/src/lst/ft_lstremove_if.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstremove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:20:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Remove node on some condition +** \param cmp Comparison function, return 0 if equal +** \param ref Reference data passed has the first arg of `cmp` +** \param del Delete function to free removed node data +*/ + +void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp, + const void *ref, t_ftdel_func del) +{ + t_ftlst *saved_next; + + if (lst == NULL || *lst == NULL) + return ; + if (cmp(ref, (*lst)->data) == 0) + { + saved_next = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = saved_next; + ft_lstremove_if(lst, cmp, ref, del); + return ; + } + ft_lstremove_if(&(*lst)->next, cmp, ref, del); +} diff --git a/libft/src/lst/ft_lstreverse.c b/libft/src/lst/ft_lstreverse.c new file mode 100644 index 0000000..7c2778d --- /dev/null +++ b/libft/src/lst/ft_lstreverse.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstreverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 12:49:14 by cacharle #+# #+# */ +/* Updated: 2020/02/15 22:53:23 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Reverse a list +*/ + +void ft_lstreverse(t_ftlst **lst) +{ + *lst = ft_lstreverse_ret(*lst); +} diff --git a/libft/src/lst/ft_lstreverse_ret.c b/libft/src/lst/ft_lstreverse_ret.c new file mode 100644 index 0000000..36c0c5c --- /dev/null +++ b/libft/src/lst/ft_lstreverse_ret.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstreverse_ret.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 12:51:15 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:20:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Reverse list +** \return Pointer to reversed list +*/ + +t_ftlst *ft_lstreverse_ret(t_ftlst *lst) +{ + t_ftlst *tmp; + + if (lst == NULL) + return (NULL); + if (lst->next == NULL) + return (lst); + tmp = ft_lstreverse_ret(lst->next); + lst->next->next = lst; + lst->next = NULL; + return (tmp); +} diff --git a/libft/src/lst/ft_lstsize.c b/libft/src/lst/ft_lstsize.c new file mode 100644 index 0000000..3c6956b --- /dev/null +++ b/libft/src/lst/ft_lstsize.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:04:28 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:44 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief List size +** \return Number of node in list +*/ + +int ft_lstsize(t_ftlst *lst) +{ + int counter; + + counter = 0; + while (lst != NULL) + { + counter++; + lst = lst->next; + } + return (counter); +} diff --git a/libft/src/lst/ft_lstsort.c b/libft/src/lst/ft_lstsort.c new file mode 100644 index 0000000..9945a0f --- /dev/null +++ b/libft/src/lst/ft_lstsort.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:53:55 by cacharle #+# #+# */ +/* Updated: 2020/02/16 02:18:05 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Sort list +** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater +** \note Use merge sort algorithm +*/ + +void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp) +{ + t_ftlst *fast; + t_ftlst *slow; + t_ftlst *middle; + + if (begin_list == NULL || *begin_list == NULL + || (*begin_list)->next == NULL) + return ; + fast = (*begin_list)->next; + slow = *begin_list; + while (fast != NULL) + { + fast = fast->next; + if (fast != NULL) + { + fast = fast->next; + slow = slow->next; + } + } + middle = slow->next; + slow->next = NULL; + ft_lstsort(begin_list, cmp); + ft_lstsort(&middle, cmp); + *begin_list = ft_lstsorted_merge(*begin_list, middle, cmp); +} diff --git a/libft/src/lst/ft_lstsorted_merge.c b/libft/src/lst/ft_lstsorted_merge.c new file mode 100644 index 0000000..995785f --- /dev/null +++ b/libft/src/lst/ft_lstsorted_merge.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsorted_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:58:52 by cacharle #+# #+# */ +/* Updated: 2020/02/16 02:18:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Merge sorted lists, the new list is also sorted +** \param l1 First list +** \param l2 Second list +** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater +** \return Pointer to first node of merged list +*/ + +t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp) +{ + t_ftlst *merged; + + merged = NULL; + if (l1 == NULL) + return (l2); + if (l2 == NULL) + return (l1); + if (cmp(l1->data, l2->data) < 0) + { + merged = l1; + merged->next = ft_lstsorted_merge(l1->next, l2, cmp); + } + else + { + merged = l2; + merged->next = ft_lstsorted_merge(l1, l2->next, cmp); + } + return (merged); +} diff --git a/libft/src/mem/ft_bzero.c b/libft/src/mem/ft_bzero.c new file mode 100644 index 0000000..d179af0 --- /dev/null +++ b/libft/src/mem/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:50:10 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:29:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/src/mem/ft_calloc.c b/libft/src/mem/ft_calloc.c new file mode 100644 index 0000000..24501bf --- /dev/null +++ b/libft/src/mem/ft_calloc.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 12:45:37 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:05:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + void *mem; + + if ((mem = malloc(count * size)) == NULL) + return (NULL); + ft_bzero(mem, count * size); + return (mem); +} diff --git a/libft/src/mem/ft_memalloc.c b/libft/src/mem/ft_memalloc.c new file mode 100644 index 0000000..5aab2ec --- /dev/null +++ b/libft/src/mem/ft_memalloc.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:07:14 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:28:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + return (ft_calloc(size, 1)); +} diff --git a/libft/src/mem/ft_memccpy.c b/libft/src/mem/ft_memccpy.c new file mode 100644 index 0000000..8ce656a --- /dev/null +++ b/libft/src/mem/ft_memccpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:01:53 by cacharle #+# #+# */ +/* Updated: 2020/01/17 10:54:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dest, const void *src, int c, size_t n) +{ + size_t i; + t_ftbyte *cast_dest; + t_ftbyte *cast_src; + + cast_dest = (t_ftbyte*)dest; + cast_src = (t_ftbyte*)src; + i = -1; + while (++i < n) + { + cast_dest[i] = cast_src[i]; + if (cast_dest[i] == (unsigned char)c) + return (cast_dest + i + 1); + } + return (NULL); +} diff --git a/libft/src/mem/ft_memchr.c b/libft/src/mem/ft_memchr.c new file mode 100644 index 0000000..4fd8689 --- /dev/null +++ b/libft/src/mem/ft_memchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ +/* Updated: 2020/02/13 04:28:00 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + t_ftbyte *cast_s; + + cast_s = (t_ftbyte*)s; + i = -1; + while (++i < n) + if (cast_s[i] == (unsigned char)c) + return (cast_s + i); + return (NULL); +} diff --git a/libft/src/mem/ft_memcmp.c b/libft/src/mem/ft_memcmp.c new file mode 100644 index 0000000..233d796 --- /dev/null +++ b/libft/src/mem/ft_memcmp.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:56:44 by cacharle #+# #+# */ +/* Updated: 2020/01/17 10:54:15 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + t_ftbyte *cast_s1; + t_ftbyte *cast_s2; + + cast_s1 = (t_ftbyte*)s1; + cast_s2 = (t_ftbyte*)s2; + if (n == 0) + return (0); + i = -1; + while (++i < n) + if (cast_s1[i] != cast_s2[i]) + return (cast_s1[i] - cast_s2[i]); + return (0); +} diff --git a/libft/src/mem/ft_memcpy.c b/libft/src/mem/ft_memcpy.c new file mode 100644 index 0000000..d0ef008 --- /dev/null +++ b/libft/src/mem/ft_memcpy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:00:07 by cacharle #+# #+# */ +/* Updated: 2020/01/17 10:39:04 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + long int *long_dest; + const long int *long_src; + + if (dest == src) + return (dest); + while (n % 8 > 0) + { + n--; + ((t_ftbyte*)dest)[n] = ((t_ftbyte*)src)[n]; + } + long_dest = dest; + long_src = src; + n /= 8; + while (n-- > 0) + long_dest[n] = long_src[n]; + return (dest); +} diff --git a/libft/src/mem/ft_memdel.c b/libft/src/mem/ft_memdel.c new file mode 100644 index 0000000..2b21f33 --- /dev/null +++ b/libft/src/mem/ft_memdel.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:00:56 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:22:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdel(void **ap) +{ + if (ap == NULL) + return ; + free(*ap); + *ap = NULL; +} diff --git a/libft/src/mem/ft_memmem.c b/libft/src/mem/ft_memmem.c new file mode 100644 index 0000000..fa1446c --- /dev/null +++ b/libft/src/mem/ft_memmem.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/13 01:54:55 by cacharle #+# #+# */ +/* Updated: 2020/02/13 21:04:46 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_mem.h" + +#define BAD_TABLE_SIZE 256 + +static void st_bad_table_init(size_t bad_table[BAD_TABLE_SIZE], + const char *little, size_t little_len) +{ + size_t i; + + i = 0; + while (i < BAD_TABLE_SIZE) + bad_table[i++] = little_len; + i = 0; + while (i < little_len - 1) + { + bad_table[(int)little[i]] = little_len - i - 1; + i++; + } +} + +static int st_memcmp_end(const void *s1, const void *s2, size_t n) +{ + while (n-- > 0) + if (*(t_ftbyte*)(s1 + n) != *(t_ftbyte*)(s2 + n)) + return (*(t_ftbyte*)(s1 + n) - *(t_ftbyte*)(s2 + n)); + return (0); +} + +void *ft_memmem(const void *big, size_t big_len, + const void *little, size_t little_len) +{ + size_t i; + size_t bad_table[BAD_TABLE_SIZE]; + + if (big_len < little_len || little_len == 0 || big_len == 0) + return (NULL); + st_bad_table_init(bad_table, little, little_len); + i = 0; + while (i <= big_len - little_len) + { + if (st_memcmp_end(big + i, little, little_len) == 0) + return ((void*)big + i); + i += bad_table[*(t_ftbyte*)(big + i + little_len - 1)]; + } + return (NULL); +} diff --git a/libft/src/mem/ft_memmove.c b/libft/src/mem/ft_memmove.c new file mode 100644 index 0000000..2f794fd --- /dev/null +++ b/libft/src/mem/ft_memmove.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:03:21 by cacharle #+# #+# */ +/* Updated: 2020/01/17 10:39:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + long int *long_dst; + const long int *long_src; + void *dst_copy; + + if (dst >= src) + return (ft_memcpy(dst, src, len)); + dst_copy = dst; + while (len % 8 > 0) + { + len--; + *(t_ftbyte*)dst++ = *(t_ftbyte*)src++; + } + long_dst = dst; + long_src = src; + len /= 8; + while (len-- > 0) + *long_dst++ = *long_src++; + return (dst_copy); +} diff --git a/libft/src/mem/ft_memset.c b/libft/src/mem/ft_memset.c new file mode 100644 index 0000000..89f53ff --- /dev/null +++ b/libft/src/mem/ft_memset.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:01:23 by cacharle #+# #+# */ +/* Updated: 2020/01/17 10:39:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + long int buf; + long int *long_s; + + c = (unsigned char)c; + while (n % 8 > 0) + *((t_ftbyte*)s + --n) = c; + buf = (long int)c | (long int)c << 8 | (long int)c << 16 + | (long int)c << 24 | (long int)c << 32 | (long int)c << 40 + | (long int)c << 48 | (long int)c << 56; + n /= 8; + long_s = s; + while (n > 0) + long_s[--n] = buf; + return (s); +} diff --git a/libft/src/mem/ft_memset_pattern4.c b/libft/src/mem/ft_memset_pattern4.c new file mode 100644 index 0000000..112ce6d --- /dev/null +++ b/libft/src/mem/ft_memset_pattern4.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset_pattern4.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/13 03:06:41 by cacharle #+# #+# */ +/* Updated: 2020/02/13 19:58:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_mem.h" + +void ft_memset_pattern4(void *b, const void *pattern4, size_t len) +{ + int i; + + i = len / 4; + while (i-- > 0) + ((int*)b)[i] = *(int*)pattern4; + i = len % 4; + len -= len % 4; + while (i-- > 0) + ((t_ftbyte*)b)[len + i] = ((t_ftbyte*)pattern4)[i]; +} diff --git a/libft/src/mem/ft_memswap.c b/libft/src/mem/ft_memswap.c new file mode 100644 index 0000000..8661fda --- /dev/null +++ b/libft/src/mem/ft_memswap.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memswap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 07:56:43 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:55:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memswap(void *a, void *b, size_t size) +{ + t_ftbyte tmp; + t_ftbyte *cast_a; + t_ftbyte *cast_b; + + cast_a = (t_ftbyte*)a; + cast_b = (t_ftbyte*)b; + while (size-- > 0) + { + tmp = cast_a[size]; + cast_a[size] = cast_b[size]; + cast_b[size] = tmp; + } +} diff --git a/libft/src/str/ft_atoi.c b/libft/src/str/ft_atoi.c new file mode 100644 index 0000000..b8f979d --- /dev/null +++ b/libft/src/str/ft_atoi.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ +/* Updated: 2020/04/04 22:34:33 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief Extract first int in a string +** (takes as much digits has possible) +** \param str String to convert +** \return Extracted int +*/ + +int ft_atoi(const char *str) +{ + return ((int)ft_strtol(str, (char**)NULL, 10)); +} diff --git a/libft/src/str/ft_atoi_strict.c b/libft/src/str/ft_atoi_strict.c new file mode 100644 index 0000000..8be4c4b --- /dev/null +++ b/libft/src/str/ft_atoi_strict.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strict_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 10:06:29 by cacharle #+# #+# */ +/* Updated: 2020/02/14 02:46:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi_strict(const char *s) +{ + char *end; + long ret; + + if (*s != '-' && !ft_isdigit(*s)) + { + errno = EINVAL; + return (0); + } + errno = 0; + ret = ft_strtol(s, &end, 10); + if (errno == ERANGE || ret > INT_MAX || ret < INT_MIN) + { + errno = ERANGE; + return (0); + } + if (*end != '\0') + { + errno = EINVAL; + return (0); + } + return (ret); +} diff --git a/libft/src/str/ft_fnmatch.c b/libft/src/str/ft_fnmatch.c new file mode 100644 index 0000000..5fc35d8 --- /dev/null +++ b/libft/src/str/ft_fnmatch.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fnmatch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/02 23:24:16 by charles #+# #+# */ +/* Updated: 2020/04/03 00:28:46 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Search a glob pattern in a string +** \param pattern Glob pattern '*' are interpreted as zero or more character +** \param string String to search in +** \return True if pattern was found, false otherwise +*/ + +bool ft_fnmatch(const char *pattern, const char *string) +{ + if (*pattern == '\0') + return (*string == '\0'); + if (*string == '\0') + return (*pattern == '\0' || (*pattern == '*' && pattern[1] == '\0')); + if (*pattern == '*') + { + if (ft_fnmatch(pattern + 1, string)) + return (true); + return (ft_fnmatch(pattern, string + 1)); + } + if (*pattern != *string) + return (false); + return (ft_fnmatch(pattern + 1, string + 1)); +} diff --git a/libft/src/str/ft_itoa.c b/libft/src/str/ft_itoa.c new file mode 100644 index 0000000..39b6e12 --- /dev/null +++ b/libft/src/str/ft_itoa.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:19:56 by cacharle #+# #+# */ +/* Updated: 2020/02/14 03:39:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_itoa(int n) +{ + char *str; + int len; + unsigned int u_nbr; + + len = n < 0 || n == 0 ? 1 : 0; + u_nbr = n < 0 ? -n : n; + while (u_nbr > 0) + { + u_nbr /= 10; + len++; + } + if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + str[len] = '\0'; + u_nbr = n < 0 ? -n : n; + if (n < 0) + str[0] = '-'; + while (--len >= (n < 0 ? 1 : 0)) + { + str[len] = (u_nbr % 10) | 0x30; + u_nbr /= 10; + } + return (str); +} diff --git a/libft/src/str/ft_itoa_cpy.c b/libft/src/str/ft_itoa_cpy.c new file mode 100644 index 0000000..a66d016 --- /dev/null +++ b/libft/src/str/ft_itoa_cpy.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_cpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:19:56 by cacharle #+# #+# */ +/* Updated: 2020/09/14 15:59:28 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief itoa but cpy number in a buffer instead of allocating memory +** \param dst Buffer where to put the string representation of the number +** \param n Number to convert +** \return Always dst +*/ + +char *ft_itoa_cpy(char *dst, int n) +{ + int len; + unsigned int u_nbr; + + len = n < 0 || n == 0 ? 1 : 0; + u_nbr = n < 0 ? -n : n; + while (u_nbr > 0) + { + u_nbr /= 10; + len++; + } + dst[len] = '\0'; + u_nbr = n < 0 ? -n : n; + if (n < 0) + dst[0] = '-'; + while (--len >= (n < 0 ? 1 : 0)) + { + dst[len] = (u_nbr % 10) | 0x30; + u_nbr /= 10; + } + return (dst); +} diff --git a/libft/src/str/ft_split.c b/libft/src/str/ft_split.c new file mode 100644 index 0000000..b61b09f --- /dev/null +++ b/libft/src/str/ft_split.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ +/* Updated: 2020/06/09 17:14:58 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t count_segment(char const *s, char c) +{ + size_t counter; + int i; + + counter = 0; + i = 0; + while (s[i]) + { + if (s[i] == c) + { + i++; + continue ; + } + counter++; + while (s[i] && s[i] != c) + i++; + } + return (counter); +} + +char **ft_split(char const *s, char c) +{ + char **strs; + size_t tab_counter; + size_t i; + size_t j; + + if (s == NULL) + return (NULL); + tab_counter = count_segment(s, c); + if ((strs = (char**)malloc(sizeof(char*) * (tab_counter + 1))) == NULL) + return (NULL); + tab_counter = 0; + j = -1; + while (s[++j]) + { + if (s[j] == c) + continue ; + i = 0; + while (s[j + i] && s[j + i] != c) + i++; + if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL) + return (ft_split_destroy(strs)); + j += i - 1; + } + strs[tab_counter] = NULL; + return (strs); +} diff --git a/libft/src/str/ft_splitf.c b/libft/src/str/ft_splitf.c new file mode 100644 index 0000000..2a1afab --- /dev/null +++ b/libft/src/str/ft_splitf.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_splitf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 17:45:52 by charles #+# #+# */ +/* Updated: 2020/06/09 17:47:17 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char **ft_splitf(char *s, char delim) +{ + char **ret; + + ret = ft_split(s, delim); + free(s); + return (ret); +} diff --git a/libft/src/str/ft_strcasecmp.c b/libft/src/str/ft_strcasecmp.c new file mode 100644 index 0000000..6dd86eb --- /dev/null +++ b/libft/src/str/ft_strcasecmp.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcasecmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:08:38 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:31:33 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" +#include "libft_def.h" + +int ft_strcasecmp(const char *s1, const char *s2) +{ + while (*s1 && *s2 && ft_tolower(*s1) == ft_tolower(*s2)) + { + s1++; + s2++; + } + return ((t_ftuchar)ft_tolower(*s1) - (t_ftuchar)ft_tolower(*s2)); +} diff --git a/libft/src/str/ft_strcat.c b/libft/src/str/ft_strcat.c new file mode 100644 index 0000000..d5bc7e0 --- /dev/null +++ b/libft/src/str/ft_strcat.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:09:41 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:03:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *dest, const char *src) +{ + ft_memcpy(dest + ft_strlen(dest), src, ft_strlen(src) + 1); + return (dest); +} diff --git a/libft/src/str/ft_strcat3.c b/libft/src/str/ft_strcat3.c new file mode 100644 index 0000000..1f7c5df --- /dev/null +++ b/libft/src/str/ft_strcat3.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 12:53:05 by charles #+# #+# */ +/* Updated: 2020/04/05 12:55:49 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Wrapper around ft_strcat to concatenate 3 strings +** \param dest Destination of the concatenation +** \param src1 First concatenation +** \param src2 Second concatenation +** \return Pointer to destination +*/ + +char *ft_strcat3(char *dest, const char *src1, const char *src2) +{ + return (ft_strcat(ft_strcat(dest, src1), src2)); +} diff --git a/libft/src/str/ft_strchr.c b/libft/src/str/ft_strchr.c new file mode 100644 index 0000000..50bfc0a --- /dev/null +++ b/libft/src/str/ft_strchr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:14:47 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:04:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + return (ft_memchr(s, c, ft_strlen(s) + 1)); +} diff --git a/libft/src/str/ft_strclr.c b/libft/src/str/ft_strclr.c new file mode 100644 index 0000000..7e412fe --- /dev/null +++ b/libft/src/str/ft_strclr.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:15:18 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:11:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + if (s == NULL) + return ; + ft_bzero(s, ft_strlen(s)); +} diff --git a/libft/src/str/ft_strcmp.c b/libft/src/str/ft_strcmp.c new file mode 100644 index 0000000..aced711 --- /dev/null +++ b/libft/src/str/ft_strcmp.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:16:07 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:18:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s2 && *s1 == *s2) + { + s1++; + s2++; + } + return (*s1 - *s2); +} diff --git a/libft/src/str/ft_strcount.c b/libft/src/str/ft_strcount.c new file mode 100644 index 0000000..87e756d --- /dev/null +++ b/libft/src/str/ft_strcount.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcount.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:17:48 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:19:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcount(char *str, char c) +{ + int counter; + + if (c == '\0') + return (1); + counter = 0; + while (*str) + if (*str++ == c) + counter++; + return (counter); +} diff --git a/libft/src/str/ft_strcpy.c b/libft/src/str/ft_strcpy.c new file mode 100644 index 0000000..ee6ff0d --- /dev/null +++ b/libft/src/str/ft_strcpy.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:38:36 by cacharle #+# #+# */ +/* Updated: 2020/01/17 11:36:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dest, const char *src) +{ + return (ft_memcpy(dest, src, ft_strlen(src) + 1)); +} diff --git a/libft/src/str/ft_strcspn.c b/libft/src/str/ft_strcspn.c new file mode 100644 index 0000000..7cc06a5 --- /dev/null +++ b/libft/src/str/ft_strcspn.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcspn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:30:59 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:32:15 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +size_t ft_strcspn(const char *s, const char *charset) +{ + int i; + + i = 0; + while (ft_strchr(charset, s[i]) == NULL) + i++; + return (i); +} diff --git a/libft/src/str/ft_strdel.c b/libft/src/str/ft_strdel.c new file mode 100644 index 0000000..05cf064 --- /dev/null +++ b/libft/src/str/ft_strdel.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:39:14 by cacharle #+# #+# */ +/* Updated: 2019/11/20 01:58:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strdel(char **as) +{ + ft_memdel((void*)as); +} diff --git a/libft/src/str/ft_strdup.c b/libft/src/str/ft_strdup.c new file mode 100644 index 0000000..b248272 --- /dev/null +++ b/libft/src/str/ft_strdup.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:18:07 by cacharle #+# #+# */ +/* Updated: 2020/02/14 03:39:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *clone; + + if ((clone = (char*)malloc(sizeof(char) * (ft_strlen(s) + 1))) == NULL) + return (NULL); + return (ft_strcpy(clone, s)); +} diff --git a/libft/src/str/ft_strequ.c b/libft/src/str/ft_strequ.c new file mode 100644 index 0000000..75ccb81 --- /dev/null +++ b/libft/src/str/ft_strequ.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:18:34 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:00:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + if (s1 == NULL || s2 == NULL) + return (0); + return (ft_strcmp(s1, s2) == 0); +} diff --git a/libft/src/str/ft_striter.c b/libft/src/str/ft_striter.c new file mode 100644 index 0000000..f410d24 --- /dev/null +++ b/libft/src/str/ft_striter.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:38:16 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:01:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + if (s == NULL || f == NULL) + return ; + while (*s) + (*f)(s++); +} diff --git a/libft/src/str/ft_striteri.c b/libft/src/str/ft_striteri.c new file mode 100644 index 0000000..05f15d4 --- /dev/null +++ b/libft/src/str/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:33:09 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:01:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + if (s == NULL || f == NULL) + return ; + i = 0; + while (s[i]) + { + (*f)(i, &s[i]); + i++; + } +} diff --git a/libft/src/str/ft_strjoin.c b/libft/src/str/ft_strjoin.c new file mode 100644 index 0000000..b65eaa2 --- /dev/null +++ b/libft/src/str/ft_strjoin.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:35:26 by cacharle #+# #+# */ +/* Updated: 2020/02/14 03:40:39 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *joined; + + if (s1 == NULL || s2 == NULL) + return (NULL); + if ((joined = (char*)malloc(sizeof(char) + * (ft_strlen(s1) + ft_strlen(s2) + 1))) == NULL) + return (NULL); + return (ft_strcat(ft_strcpy(joined, s1), s2)); +} diff --git a/libft/src/str/ft_strjoin3.c b/libft/src/str/ft_strjoin3.c new file mode 100644 index 0000000..e5e5530 --- /dev/null +++ b/libft/src/str/ft_strjoin3.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:00:49 by charles #+# #+# */ +/* Updated: 2020/04/01 18:01:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief Join 3 strings in a new malloc'd one +** \param s1 String 1 +** \param s2 String 2 +** \param s3 String 3 +** \return The joined string +*/ + +char *ft_strjoin3(char const *s1, char const *s2, char const *s3) +{ + char *joined; + + if (s1 == NULL || s2 == NULL || s3 == NULL) + return (NULL); + if ((joined = (char*)malloc(sizeof(char) + * (ft_strlen(s1) + ft_strlen(s2) + ft_strlen(s3) + 1))) == NULL) + return (NULL); + ft_strcpy(joined, s1); + ft_strcat(joined, s2); + ft_strcat(joined, s3); + return (joined); +} diff --git a/libft/src/str/ft_strjoinf.c b/libft/src/str/ft_strjoinf.c new file mode 100644 index 0000000..7c96e13 --- /dev/null +++ b/libft/src/str/ft_strjoinf.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 03:41:07 by cacharle #+# #+# */ +/* Updated: 2020/10/08 09:40:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char *ft_strjoinf(char const *s1, char const *s2, t_ftstrjoinf_tag tag) +{ + char *joined; + + if (s1 == NULL || s2 == NULL) + return (NULL); + joined = ft_strjoin(s1, s2); + if (tag == FT_STRJOINF_FST) + free((void*)s1); + else if (tag == FT_STRJOINF_SND) + free((void*)s2); + else if (tag == FT_STRJOINF_ALL) + { + free((void*)s1); + free((void*)s2); + } + return (joined); +} + +char *ft_strjoinf_fst(char const *s1, char const *s2) +{ + return (ft_strjoinf(s1, s2, FT_STRJOINF_FST)); +} + +char *ft_strjoinf_snd(char const *s1, char const *s2) +{ + return (ft_strjoinf(s1, s2, FT_STRJOINF_SND)); +} diff --git a/libft/src/str/ft_strlcat.c b/libft/src/str/ft_strlcat.c new file mode 100644 index 0000000..ce7fa0b --- /dev/null +++ b/libft/src/str/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:31:37 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:31:08 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t i; + size_t j; + size_t dst_len; + size_t src_len; + + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + if (size <= dst_len) + return (src_len + size); + i = 0; + j = dst_len; + while (src[i] && j < size - 1) + dst[j++] = src[i++]; + dst[j] = '\0'; + return (dst_len + src_len); +} diff --git a/libft/src/str/ft_strlcpy.c b/libft/src/str/ft_strlcpy.c new file mode 100644 index 0000000..6afb8f5 --- /dev/null +++ b/libft/src/str/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 12:28:47 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:31:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + unsigned int i; + + if (dst == NULL || src == NULL) + return (0); + if (size == 0) + return (ft_strlen(src)); + i = -1; + while (++i < size - 1 && src[i] != '\0') + dst[i] = src[i]; + dst[i] = '\0'; + return (ft_strlen(src)); +} diff --git a/libft/src/str/ft_strlen.c b/libft/src/str/ft_strlen.c new file mode 100644 index 0000000..0d593e1 --- /dev/null +++ b/libft/src/str/ft_strlen.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ +/* Updated: 2020/01/17 11:13:43 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + unsigned long int *ptr; + const char *cpy; + + ptr = (unsigned long int*)s; + while (TRUE) + { + cpy = (const char*)ptr++; + if (cpy[0] == '\0') + return (cpy - s); + if (cpy[1] == '\0') + return (cpy + 1 - s); + if (cpy[2] == '\0') + return (cpy + 2 - s); + if (cpy[3] == '\0') + return (cpy + 3 - s); + if (cpy[4] == '\0') + return (cpy + 4 - s); + if (cpy[5] == '\0') + return (cpy + 5 - s); + if (cpy[6] == '\0') + return (cpy + 6 - s); + if (cpy[7] == '\0') + return (cpy + 7 - s); + } +} diff --git a/libft/src/str/ft_strmap.c b/libft/src/str/ft_strmap.c new file mode 100644 index 0000000..61d16f1 --- /dev/null +++ b/libft/src/str/ft_strmap.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:29:52 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + size_t i; + size_t len; + char *mapped; + + if (s == NULL || f == NULL) + return (NULL); + len = ft_strlen(s); + if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + i = 0; + while (i < len) + { + mapped[i] = (*f)(s[i]); + i++; + } + mapped[i] = '\0'; + return (mapped); +} diff --git a/libft/src/str/ft_strmapi.c b/libft/src/str/ft_strmapi.c new file mode 100644 index 0000000..71d77e4 --- /dev/null +++ b/libft/src/str/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:29:32 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:15 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char *s, char (*f)(unsigned int, char)) +{ + size_t i; + size_t len; + char *mapped; + + if (s == NULL || f == NULL) + return (NULL); + len = ft_strlen(s); + if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + i = 0; + while (i < len) + { + mapped[i] = (*f)((unsigned int)i, s[i]); + i++; + } + mapped[i] = '\0'; + return (mapped); +} diff --git a/libft/src/str/ft_strncasecmp.c b/libft/src/str/ft_strncasecmp.c new file mode 100644 index 0000000..7153237 --- /dev/null +++ b/libft/src/str/ft_strncasecmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncasecmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:18:36 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:31:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_def.h" + +int ft_strncasecmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while (i + 1 < n && s1[i] && ft_tolower(s1[i]) == ft_tolower(s2[i])) + i++; + return ((t_ftuchar)ft_tolower(s1[i]) - (t_ftuchar)ft_tolower(s2[i])); +} diff --git a/libft/src/str/ft_strncat.c b/libft/src/str/ft_strncat.c new file mode 100644 index 0000000..d68db0a --- /dev/null +++ b/libft/src/str/ft_strncat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:28:37 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:33:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *dest, const char *src, size_t n) +{ + size_t i; + size_t j; + + i = ft_strlen(dest); + j = 0; + while (j < n && src[j]) + { + dest[i + j] = src[j]; + j++; + } + dest[i + j] = '\0'; + return (dest); +} diff --git a/libft/src/str/ft_strncmp.c b/libft/src/str/ft_strncmp.c new file mode 100644 index 0000000..d810e8c --- /dev/null +++ b/libft/src/str/ft_strncmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:17:50 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_def.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while (i + 1 < n && s1[i] == s2[i] && s1[i]) + i++; + return ((t_ftuchar)s1[i] - (t_ftuchar)s2[i]); +} diff --git a/libft/src/str/ft_strncpy.c b/libft/src/str/ft_strncpy.c new file mode 100644 index 0000000..07a4927 --- /dev/null +++ b/libft/src/str/ft_strncpy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:26:59 by cacharle #+# #+# */ +/* Updated: 2020/01/17 10:40:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dest, const char *src, size_t n) +{ + size_t len; + + len = ft_strlen(src); + ft_memcpy(dest, src, n < len ? n : len); + if (len < n) + ft_bzero(dest + len, n - len); + return (dest); +} diff --git a/libft/src/str/ft_strndup.c b/libft/src/str/ft_strndup.c new file mode 100644 index 0000000..894ea4e --- /dev/null +++ b/libft/src/str/ft_strndup.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/25 03:28:52 by cacharle #+# #+# */ +/* Updated: 2020/02/14 03:43:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strndup(const char *s1, size_t n) +{ + char *clone; + + if ((clone = (char*)malloc(sizeof(char) * (n + 1))) == NULL) + return (NULL); + clone[n] = '\0'; + return (ft_strncpy(clone, s1, n)); +} diff --git a/libft/src/str/ft_strnequ.c b/libft/src/str/ft_strnequ.c new file mode 100644 index 0000000..e242ee7 --- /dev/null +++ b/libft/src/str/ft_strnequ.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:30:27 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:00:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + if (s1 == NULL || s2 == NULL) + return (0); + return (ft_strncmp(s1, s2, n) == 0); +} diff --git a/libft/src/str/ft_strnew.c b/libft/src/str/ft_strnew.c new file mode 100644 index 0000000..1bca6d5 --- /dev/null +++ b/libft/src/str/ft_strnew.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:17:34 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:16:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + return ((char*)ft_calloc(size + 1, sizeof(char))); +} diff --git a/libft/src/str/ft_strnlen.c b/libft/src/str/ft_strnlen.c new file mode 100644 index 0000000..5e1569c --- /dev/null +++ b/libft/src/str/ft_strnlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 05:21:04 by cacharle #+# #+# */ +/* Updated: 2020/02/10 05:23:23 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** wrong implementation since it scans beyond maxlen +*/ + +size_t ft_strnlen(const char *s, size_t maxlen) +{ + size_t len; + + len = ft_strlen(s); + return (len > maxlen ? maxlen : len); +} diff --git a/libft/src/str/ft_strnstr.c b/libft/src/str/ft_strnstr.c new file mode 100644 index 0000000..4995637 --- /dev/null +++ b/libft/src/str/ft_strnstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:25:13 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:58:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + size_t needle_len; + + needle_len = ft_strlen(needle); + if (needle_len == 0) + return ((char*)haystack); + while (*haystack && len-- >= needle_len) + { + if (ft_strnequ(haystack, needle, needle_len)) + return ((char*)haystack); + haystack++; + } + return (NULL); +} diff --git a/libft/src/str/ft_strpbrk.c b/libft/src/str/ft_strpbrk.c new file mode 100644 index 0000000..753e4d9 --- /dev/null +++ b/libft/src/str/ft_strpbrk.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strpbrk.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:39:29 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:54:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char *ft_strpbrk(const char *s, const char *charset) +{ + if (s == NULL || charset == NULL) + return (NULL); + while (*s && ft_strchr(charset, *s) == NULL) + s++; + if (*s == '\0') + return (NULL); + return ((char*)s); +} diff --git a/libft/src/str/ft_strrchr.c b/libft/src/str/ft_strrchr.c new file mode 100644 index 0000000..4cedb76 --- /dev/null +++ b/libft/src/str/ft_strrchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:26:24 by cacharle #+# #+# */ +/* Updated: 2019/11/21 18:46:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s) + 1; + while (s[--i] != (char)c) + if (i == 0) + return (NULL); + return ((char*)s + i); +} diff --git a/libft/src/str/ft_strsep.c b/libft/src/str/ft_strsep.c new file mode 100644 index 0000000..50197fb --- /dev/null +++ b/libft/src/str/ft_strsep.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsep.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:44:11 by cacharle #+# #+# */ +/* Updated: 2020/04/04 12:48:24 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char *ft_strsep(char **stringp, const char *delim) +{ + char *tmp; + char *origin; + + if (*stringp == NULL) + return (NULL); + origin = *stringp; + tmp = ft_strpbrk(*stringp, delim); + if (tmp == NULL) + { + *stringp = NULL; + return (origin); + } + *tmp = '\0'; + *stringp = tmp + 1; + return (origin); +} diff --git a/libft/src/str/ft_strsjoin.c b/libft/src/str/ft_strsjoin.c new file mode 100644 index 0000000..0923bde --- /dev/null +++ b/libft/src/str/ft_strsjoin.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 14:30:08 by charles #+# #+# */ +/* Updated: 2020/04/04 23:34:30 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Join null-terminated array of strings +** \param strs Array of strings +** \param delim String iterspersed between strings +** \return Joined string or NULL on error +** \note Empty strings are ignored +*/ + +char *ft_strsjoin(char **strs, char *delim) +{ + int i; + size_t join_len; + size_t delim_len; + char *join; + + delim_len = ft_strlen(delim); + join_len = 0; + i = -1; + while (strs[++i] != NULL) + { + join_len += ft_strlen(strs[i]); + if (strs[i + 1] != NULL) + join_len += delim_len; + } + if ((join = (char*)malloc(sizeof(char) * (join_len + 1))) == NULL) + return (NULL); + join[0] = '\0'; + i = -1; + while (strs[++i] != NULL) + { + ft_strcat(join, strs[i]); + if (*strs[i] != '\0' && strs[i + 1] != NULL) + ft_strcat(join, delim); + } + return (join); +} diff --git a/libft/src/str/ft_strsjoinf.c b/libft/src/str/ft_strsjoinf.c new file mode 100644 index 0000000..36a2892 --- /dev/null +++ b/libft/src/str/ft_strsjoinf.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 14:27:33 by charles #+# #+# */ +/* Updated: 2020/04/04 23:24:24 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Join null-terminated array of strings and free the array +** \param strs Array of strings +** \param delim String which will be put between each string +** \return Joined string or NULL on error +*/ + +char *ft_strsjoinf(char **strs, char *delim) +{ + char *ret; + + ret = ft_strsjoin(strs, delim); + ft_split_destroy(strs); + return (ret); +} diff --git a/libft/src/str/ft_strspn.c b/libft/src/str/ft_strspn.c new file mode 100644 index 0000000..81814e5 --- /dev/null +++ b/libft/src/str/ft_strspn.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strspn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:29:13 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:33:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +size_t ft_strspn(const char *s, const char *charset) +{ + int i; + + i = 0; + while (ft_strchr(charset, s[i]) != NULL) + i++; + return (i); +} diff --git a/libft/src/str/ft_strstr.c b/libft/src/str/ft_strstr.c new file mode 100644 index 0000000..4d4d403 --- /dev/null +++ b/libft/src/str/ft_strstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:15:29 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:58:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *haystack, const char *needle) +{ + size_t needle_len; + + needle_len = ft_strlen(needle); + if (needle_len == 0) + return ((char*)haystack); + while (*haystack) + { + if (ft_strnequ(haystack, needle, needle_len)) + return ((char*)haystack); + haystack++; + } + return (NULL); +} diff --git a/libft/src/str/ft_strsub.c b/libft/src/str/ft_strsub.c new file mode 100644 index 0000000..77bffb2 --- /dev/null +++ b/libft/src/str/ft_strsub.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ +/* Updated: 2020/04/05 13:47:55 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Extract a substring from a string +** \param s String to extract from +** \param start Starting index of the substring +** \param len Substring length +** \return The created substring or NULL on error +*/ + +char *ft_strsub(char const *s, size_t start, size_t len) +{ + char *sub; + size_t s_len; + + if (s == NULL) + return (NULL); + s_len = ft_strlen(s); + if (start > s_len) + return (NULL); + if (start + len > s_len) + len = s_len - start; + if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + sub[len] = '\0'; + return (ft_strncpy(sub, s + start, len)); +} diff --git a/libft/src/str/ft_strsubf.c b/libft/src/str/ft_strsubf.c new file mode 100644 index 0000000..dc49ba5 --- /dev/null +++ b/libft/src/str/ft_strsubf.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsubf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 13:48:13 by charles #+# #+# */ +/* Updated: 2020/04/05 13:51:47 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Wrapper around ft_substr which free the original string +** \param s String to extract from (will be free) +** \param start Starting index of the substring +** \param len Substring length +** \return The created substring or NULL on error +*/ + +char *ft_strsubf(char const *s, size_t start, size_t len) +{ + char *ret; + + ret = ft_strsub(s, start, len); + free((void*)s); + return (ret); +} diff --git a/libft/src/str/ft_strtol.c b/libft/src/str/ft_strtol.c new file mode 100644 index 0000000..499e54c --- /dev/null +++ b/libft/src/str/ft_strtol.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */ +/* Updated: 2020/10/11 14:32:46 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +#define STRTOL_STD_BASE "0123456789abcdefghijklmnopqrstuvwxyz" + +static int st_strtol_handle_base(int base, const char **str) +{ + if (base > 36) + return (-1); + if (base != 16 && base != 0) + return (base); + if (base == 16 && **str == '0' && (*str)[1] == 'x') + { + *str += 2; + return (base); + } + if (**str == '0') + { + (*str)++; + if (**str == 'x') + { + (*str)++; + return (16); + } + else + return (8); + } + return (10); +} + +static void st_setup_base(char base_str[37], int base) +{ + ft_strncpy(base_str, STRTOL_STD_BASE, base); + base_str[base] = '\0'; +} + +static long st_return(int err, char **end, long ret) +{ + if (end != NULL) + *end = NULL; + errno = err; + return (ret); +} + +/* +** If there is no digits doesn't put str in end like the original, +** instead it puts the address of the char after spaces and sign. +** Too much lines and annoyance, I can't be bothered. +*/ + +long ft_strtol(const char *str, char **end, int base) +{ + t_ftbool is_negative; + unsigned long long nb; + char base_str[37]; + + while (ft_isspace(*str)) + str++; + is_negative = *str == '-' ? TRUE : FALSE; + if (*str == '-' || *str == '+') + str++; + if ((base = st_strtol_handle_base(base, &str)) == -1) + return (st_return(EINVAL, end, 0)); + st_setup_base(base_str, base); + nb = 0; + while (*str != '\0' && ft_strchr(base_str, *str) != NULL) + { + nb *= base; + if (is_negative ? (nb > -(unsigned long)LONG_MIN) : (nb > LONG_MAX)) + return (st_return(ERANGE, end, is_negative ? LONG_MIN : LONG_MAX)); + nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str; + if (is_negative ? (nb > -(unsigned long)LONG_MIN) : (nb > LONG_MAX)) + return (st_return(ERANGE, end, is_negative ? LONG_MIN : LONG_MAX)); + } + if (end != NULL) + *end = (char*)str; + return (is_negative ? -nb : nb); +} diff --git a/libft/src/str/ft_strtolower.c b/libft/src/str/ft_strtolower.c new file mode 100644 index 0000000..2eb34c2 --- /dev/null +++ b/libft/src/str/ft_strtolower.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:10:01 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:12:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" +#include "libft_ctype.h" + +char *ft_strtolower(char *s) +{ + int i; + + if (s == NULL) + return (NULL); + i = -1; + while (s[i]) + s[i] = ft_tolower(s[i]); + return (s); +} diff --git a/libft/src/str/ft_strtoupper.c b/libft/src/str/ft_strtoupper.c new file mode 100644 index 0000000..4a751d3 --- /dev/null +++ b/libft/src/str/ft_strtoupper.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtoupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:12:04 by cacharle #+# #+# */ +/* Updated: 2020/02/14 02:49:35 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" +#include "libft_ctype.h" + +char *ft_strtoupper(char *s) +{ + int i; + + if (s == NULL) + return (NULL); + i = -1; + while (s[i]) + s[i] = ft_toupper(s[i]); + return (s); +} diff --git a/libft/src/str/ft_strtrim.c b/libft/src/str/ft_strtrim.c new file mode 100644 index 0000000..fa9b192 --- /dev/null +++ b/libft/src/str/ft_strtrim.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:24:16 by cacharle #+# #+# */ +/* Updated: 2020/04/05 13:50:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t start; + size_t len; + + if (s1 == NULL || set == NULL) + return (NULL); + start = 0; + while (s1[start] && ft_strchr(set, s1[start]) != NULL) + start++; + len = ft_strlen(&s1[start]); + if (len != 0) + while (s1[start + len - 1] + && ft_strchr(set, s1[start + len - 1]) != NULL) + len--; + return (ft_strsub(s1, start, len)); +} diff --git a/libft/src/util/ft_split_destroy.c b/libft/src/util/ft_split_destroy.c new file mode 100644 index 0000000..bc3f4e6 --- /dev/null +++ b/libft/src/util/ft_split_destroy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:30:55 by cacharle #+# #+# */ +/* Updated: 2020/02/27 17:52:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_util.h" + +void *ft_split_destroy(char **strs) +{ + int i; + + i = -1; + while (strs[++i] != NULL) + free(strs[i]); + free(strs); + return (NULL); +} diff --git a/libft/src/vec/ft_vecdestroy.c b/libft/src/vec/ft_vecdestroy.c new file mode 100644 index 0000000..0021573 --- /dev/null +++ b/libft/src/vec/ft_vecdestroy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:06:22 by charles #+# #+# */ +/* Updated: 2020/08/19 17:49:30 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Destroy a vector +** \param vec Vector to destroy +** \param del Delete function applied to each element of the vector +** \return NULL +*/ + +void *ft_vecdestroy(t_ftvec *vec, void (*del)(void *elem)) +{ + if (vec == NULL) + return (NULL); + if (del != NULL) + ft_veciter(vec, del); + free(vec->data); + free(vec); + return (NULL); +} diff --git a/libft/src/vec/ft_vecfrom_lst.c b/libft/src/vec/ft_vecfrom_lst.c new file mode 100644 index 0000000..4a16e4c --- /dev/null +++ b/libft/src/vec/ft_vecfrom_lst.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecfrom_lst.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/14 15:59:24 by charles #+# #+# */ +/* Updated: 2020/06/15 09:48:48 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +t_ftvec *ft_vecfrom_lst(t_ftlst *lst) +{ + t_ftvec *vec; + t_ftlst *curr; + + if ((vec = ft_vecnew(ft_lstsize(lst))) == NULL) + return (NULL); + curr = lst; + while (curr != NULL) + { + ft_vecpush(vec, curr->data); + curr = curr->next; + } + return (vec); +} diff --git a/libft/src/vec/ft_vecgrow.c b/libft/src/vec/ft_vecgrow.c new file mode 100644 index 0000000..2213c88 --- /dev/null +++ b/libft/src/vec/ft_vecgrow.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:13:07 by charles #+# #+# */ +/* Updated: 2020/04/02 10:43:01 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Vector Growth factor +*/ + +#define FT_VEC_GROWTH_FACTOR 1.5 + +/* +** \brief Grow the vector capacity by a constant factor +** \param vec Vector to grow +** \return Passed vector of NULL on error +*/ + +t_ftvec *ft_vecgrow(t_ftvec *vec) +{ + size_t new_capacity; + void **new_data; + + if (vec->capacity <= 1) + new_capacity = 2; + else + new_capacity = vec->capacity * FT_VEC_GROWTH_FACTOR; + if ((new_data = (void**)malloc(sizeof(void*) * new_capacity)) == NULL) + return (NULL); + ft_memcpy(new_data, vec->data, vec->size * sizeof(void*)); + free(vec->data); + vec->data = new_data; + vec->capacity = new_capacity; + return (vec); +} diff --git a/libft/src/vec/ft_vecinsert.c b/libft/src/vec/ft_vecinsert.c new file mode 100644 index 0000000..1682daa --- /dev/null +++ b/libft/src/vec/ft_vecinsert.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/02 10:46:59 by charles #+# #+# */ +/* Updated: 2020/04/02 11:04:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Insert element in vector +** \param vec Vector where element is inserted +** \param i Index where element should be inserted, +** bound checking is performed +** \param elem Element to insert +** \return Passed vector or NULL on error +*/ + +t_ftvec *ft_vecinsert(t_ftvec *vec, size_t i, void *elem) +{ + if (i > vec->size) + return (NULL); + if (vec->capacity <= vec->size) + if (ft_vecgrow(vec) == NULL) + return (NULL); + ft_memmove(vec->data + i + 1, vec->data + i, + (vec->size - i) * sizeof(void*)); + vec->data[i] = elem; + vec->size++; + return (vec); +} diff --git a/libft/src/vec/ft_vecinsert_safe.c b/libft/src/vec/ft_vecinsert_safe.c new file mode 100644 index 0000000..24e8cac --- /dev/null +++ b/libft/src/vec/ft_vecinsert_safe.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecinsert_safe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 13:41:03 by charles #+# #+# */ +/* Updated: 2020/06/09 13:41:04 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +t_ftvec *ft_vecinsert_safe(t_ftvec *vec, size_t i, void *elem) +{ + if (elem == NULL) + return (NULL); + return (ft_vecinsert(vec, i, elem)); +} diff --git a/libft/src/vec/ft_veciter.c b/libft/src/vec/ft_veciter.c new file mode 100644 index 0000000..ec4a917 --- /dev/null +++ b/libft/src/vec/ft_veciter.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_veciter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:09:51 by charles #+# #+# */ +/* Updated: 2020/04/01 20:15:13 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Iterate a function over elements of a vector +** \param vec Iterated vector +** \param f Function applied to each elements +*/ + +void ft_veciter(t_ftvec *vec, void (*f)(void *elem)) +{ + size_t i; + + i = 0; + while (i < vec->size) + { + f(vec->data[i]); + i++; + } +} diff --git a/libft/src/vec/ft_veciter_addr.c b/libft/src/vec/ft_veciter_addr.c new file mode 100644 index 0000000..09f6926 --- /dev/null +++ b/libft/src/vec/ft_veciter_addr.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_veciter_addr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 13:52:38 by charles #+# #+# */ +/* Updated: 2020/06/09 13:53:30 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +void ft_veciter_addr(t_ftvec *vec, void (*f)(void **addr)) +{ + size_t i; + + i = 0; + while (i < vec->size) + { + f(vec->data + i); + i++; + } +} diff --git a/libft/src/vec/ft_vecnew.c b/libft/src/vec/ft_vecnew.c new file mode 100644 index 0000000..0def9c9 --- /dev/null +++ b/libft/src/vec/ft_vecnew.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:03:49 by charles #+# #+# */ +/* Updated: 2020/04/04 21:24:36 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Create a new vector +** \param capacity Initial capacity of the underlying array +** Can't be lower than 1 +** \return Created vector or NULL on malloc error +*/ + +t_ftvec *ft_vecnew(size_t capacity) +{ + t_ftvec *vec; + + if ((vec = (t_ftvec*)malloc(sizeof(t_ftvec))) == NULL) + return (NULL); + if (capacity == 0) + capacity = 1; + if ((vec->data = (void**)malloc(sizeof(void*) * capacity)) == NULL) + { + free(vec); + return (NULL); + } + vec->capacity = capacity; + vec->size = 0; + return (vec); +} diff --git a/libft/src/vec/ft_vecpop.c b/libft/src/vec/ft_vecpop.c new file mode 100644 index 0000000..5b77b46 --- /dev/null +++ b/libft/src/vec/ft_vecpop.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:26:27 by charles #+# #+# */ +/* Updated: 2020/04/01 20:26:34 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Pop last element of a vector +** \param vec Vector poped +** \param del Delete function applied to last element +*/ + +void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)) +{ + if (vec->size == 0) + return ; + if (del != NULL) + del(vec->data[vec->size - 1]); + vec->size--; +} diff --git a/libft/src/vec/ft_vecpush.c b/libft/src/vec/ft_vecpush.c new file mode 100644 index 0000000..026ae3d --- /dev/null +++ b/libft/src/vec/ft_vecpush.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:22:20 by charles #+# #+# */ +/* Updated: 2020/04/02 10:51:38 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Push element at the end of vector +** \param vec Vector where element will be pushed +** \param pushed Element to push +** \return Passed vector or NULL if couldnt grow vector +*/ + +t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed) +{ + if (vec->capacity <= vec->size) + if (ft_vecgrow(vec) == NULL) + return (NULL); + vec->data[vec->size] = pushed; + vec->size++; + return (vec); +} diff --git a/libft/src/vec/ft_vecpush_safe.c b/libft/src/vec/ft_vecpush_safe.c new file mode 100644 index 0000000..8e5a8d4 --- /dev/null +++ b/libft/src/vec/ft_vecpush_safe.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush_safe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 13:29:48 by charles #+# #+# */ +/* Updated: 2020/04/04 14:24:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Wrapper around ft_vecpush which reject null element +** \param vec Pushed vector +** \param pushed Element pushed which can't be NULL +** \return NULL if pushed is NULL, whatever ft_vecpush returns otherwise +*/ + +t_ftvec *ft_vecpush_safe(t_ftvec *vec, void *pushed) +{ + if (pushed == NULL) + return (NULL); + return (ft_vecpush(vec, pushed)); +} diff --git a/libft/src/vec/ft_vecremove.c b/libft/src/vec/ft_vecremove.c new file mode 100644 index 0000000..28ea541 --- /dev/null +++ b/libft/src/vec/ft_vecremove.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecremove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 22:45:07 by charles #+# #+# */ +/* Updated: 2020/06/09 13:28:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Remove element from vector +** \param vec Vector to remove from +** \param i Index of the element to remove +** \param del Delete function applied to ith element +*/ + +void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)) +{ + void *tmp; + + if ((tmp = ft_vectake(vec, i)) == NULL) + return ; + if (del != NULL) + del(tmp); +} diff --git a/libft/src/vec/ft_vecreserve.c b/libft/src/vec/ft_vecreserve.c new file mode 100644 index 0000000..105eb16 --- /dev/null +++ b/libft/src/vec/ft_vecreserve.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecreserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/07/15 17:22:08 by charles #+# #+# */ +/* Updated: 2020/08/20 23:21:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Set vector capacity to at least a certain size +** \param vec Reserved vector +** \param capacity Minimum required capacity +** \return The Vector or NULL on error +*/ + +t_ftvec *ft_vecreserve(t_ftvec *vec, size_t capacity) +{ + while (vec->capacity < capacity) + { + if (ft_vecgrow(vec) == NULL) + return (NULL); + } + return (vec); +} diff --git a/libft/src/vec/ft_vecsort.c b/libft/src/vec/ft_vecsort.c new file mode 100644 index 0000000..8aa5c2a --- /dev/null +++ b/libft/src/vec/ft_vecsort.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 15:53:23 by charles #+# #+# */ +/* Updated: 2020/04/04 19:30:31 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Wrapper around ft_qsort +** \param vec Vector to sort +** \param cmp Function to compare each vector element +*/ + +void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp) +{ + ft_qsort(vec->data, vec->size, sizeof(void*), cmp); +} diff --git a/libft/src/vec/ft_vecswallow.c b/libft/src/vec/ft_vecswallow.c new file mode 100644 index 0000000..614022d --- /dev/null +++ b/libft/src/vec/ft_vecswallow.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecswallow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/07/15 17:17:00 by charles #+# #+# */ +/* Updated: 2020/07/15 18:30:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Insert all element of a vector in an other vector +** and free the swallowed vector +** \param vec Vector where the element will be inserted +** \param index Index of the insertion +** \param swallowed Vector to swallow +** \return Destination vector or NULL on error +*/ + +t_ftvec *ft_vecswallow_at(t_ftvec *vec, size_t index, t_ftvec *swallowed) +{ + size_t i; + + if (ft_vecreserve(vec, vec->size + swallowed->size) == NULL) + return (NULL); + i = -1; + while (++i < swallowed->size) + ft_vecinsert(vec, index, swallowed->data[i]); + ft_vecdestroy(swallowed, NULL); + return (vec); +} diff --git a/libft/src/vec/ft_vectake.c b/libft/src/vec/ft_vectake.c new file mode 100644 index 0000000..25a9038 --- /dev/null +++ b/libft/src/vec/ft_vectake.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vectake.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 13:27:19 by charles #+# #+# */ +/* Updated: 2020/06/09 13:29:02 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +void *ft_vectake(t_ftvec *vec, size_t i) +{ + void *taken; + + if (vec->size == 0 || i > vec->size - 1) + return (NULL); + taken = vec->data[i]; + ft_memmove(vec->data + i, vec->data + i + 1, + (vec->size - i - 1) * sizeof(void*)); + vec->size--; + return (taken); +} diff --git a/libft/src/vec/ft_vecunwrap.c b/libft/src/vec/ft_vecunwrap.c new file mode 100644 index 0000000..c1ab476 --- /dev/null +++ b/libft/src/vec/ft_vecunwrap.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecunwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 13:49:50 by charles #+# #+# */ +/* Updated: 2020/06/09 13:50:33 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +void **ft_vecunwrap(t_ftvec *vec) +{ + void **tmp; + + tmp = vec->data; + free(vec); + return (tmp); +} |
