diff --git a/gnl/get_next_line.c b/gnl/get_next_line.c new file mode 100644 index 0000000..4432819 --- /dev/null +++ b/gnl/get_next_line.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 20:20:17 by vvobis #+# #+# */ +/* Updated: 2024/05/27 17:44:59 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +void *buffer_extend(void *ptr_old, size_t size_new, size_t size_old) +{ + void *ptr_new; + void *ptr_back; + void *ptr_old_free; + + ptr_new = g_calloc(size_new + 1, 1); + if (!ptr_new) + return (free(ptr_old), ptr_old = NULL, NULL); + ptr_back = ptr_new; + ptr_old_free = ptr_old; + while (size_old--) + *(char *)ptr_new++ = *(char *)ptr_old++; + return (free(ptr_old_free), ptr_old_free = NULL, ptr_back); +} + +char *handle_no_nl(char **buf, char **left) +{ + if (left) + { + free(*left); + *left = NULL; + } + if (!buf) + return (NULL); + if (g_strlen(*buf)) + return (*buf); + free(*buf); + *buf = NULL; + return (NULL); +} + +char *line_extract(char **buf_joined, char **left, size_t line_len) +{ + char *line; + + line = g_substr(*buf_joined, 0, line_len); + if (!line) + return (free(*buf_joined), *buf_joined = NULL, NULL); + *left = g_substr(*buf_joined, line_len, g_strlen(*buf_joined) - line_len); + free(*buf_joined); + *buf_joined = NULL; + if (!*left) + return (free(line), line = NULL, NULL); + return (line); +} + +char *line_handle(char **buf_fetch) +{ + char *buf_joined; + size_t line_len; + static char *left; + + if (!buf_fetch) + return (free(left), NULL); + buf_joined = g_strjoin(left, *buf_fetch); + free(*buf_fetch); + *buf_fetch = NULL; + free(left); + left = NULL; + if (!buf_joined) + return (NULL); + line_len = find_newline(buf_joined); + if (line_len) + return (line_extract(&buf_joined, &left, line_len)); + return (handle_no_nl(&buf_joined, &left)); +} + +char *get_next_line(int fd) +{ + char *buf; + ssize_t bytes_read; + size_t buf_size_cur; + size_t buf_size_prev; + + if (fd < 0) + return (line_handle(NULL), NULL); + buf = g_calloc(sizeof(*buf), BUFFER_SIZE + 1); + buf_size_prev = 0; + buf_size_cur = BUFFER_SIZE; + while (1) + { + if (!buf) + return (line_handle(NULL)); + bytes_read = read(fd, buf + buf_size_prev, BUFFER_SIZE); + if (bytes_read < 0) + return (free(buf), buf = NULL, line_handle(NULL)); + if (find_newline(buf) || bytes_read == 0) + break ; + buf_size_prev = buf_size_cur; + buf_size_cur += BUFFER_SIZE; + buf = buffer_extend(buf, buf_size_cur, buf_size_prev); + } + return (line_handle(&buf)); +} diff --git a/gnl/get_next_line.h b/gnl/get_next_line.h new file mode 100644 index 0000000..60a48d4 --- /dev/null +++ b/gnl/get_next_line.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/15 14:07:36 by vvobis #+# #+# */ +/* Updated: 2024/05/27 17:43:21 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H + +# define GET_NEXT_LINE_H + +# ifndef BUFFER_SIZE + +# define BUFFER_SIZE 50 + +# endif + +# define MAX_FD 1024 + +# include +# include +# include +# include + +char *get_next_line(int fd); +char *g_strjoin(char const *s1, char const *s2); +size_t g_strlen(char const *str); +void *g_calloc(size_t n, size_t s); +char *g_substr(char const *s, unsigned int start, size_t len); +char *line_handle(char **buf_fetch); +size_t find_newline(char *buf); + +#endif diff --git a/gnl/get_next_line_utils.c b/gnl/get_next_line_utils.c new file mode 100644 index 0000000..3ddd27b --- /dev/null +++ b/gnl/get_next_line_utils.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/15 14:14:13 by vvobis #+# #+# */ +/* Updated: 2024/05/20 11:38:03 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +size_t g_strlen(char const *str) +{ + size_t i; + + if (!str) + return (0); + i = 0; + while (*str) + { + i++; + str++; + } + return (i); +} + +size_t find_newline(char *buf) +{ + size_t i; + + if (!buf) + return (0); + i = 0; + while (*buf) + { + if (*buf == '\n') + return (i + 1); + i++; + buf++; + } + return (0); +} + +char *g_strjoin(char const *s1, char const *s2) +{ + char *tmp; + unsigned int i; + unsigned int j; + + if (!s2) + return (NULL); + if (!s1) + return (g_substr(s2, 0, g_strlen(s2))); + tmp = g_calloc(g_strlen(s1) + g_strlen(s2) + 1, sizeof(*tmp)); + if (!tmp) + return (NULL); + i = 0; + if (s1) + { + while (s1[i]) + { + tmp[i] = s1[i]; + i++; + } + } + j = 0; + if (s2) + while (s2[j]) + tmp[i++] = s2[j++]; + return (tmp); +} + +char *g_substr(char const *s, unsigned int start, size_t len) +{ + char *tmp; + unsigned int i; + + i = 0; + if (!s || start >= g_strlen(s) || len <= 0) + { + tmp = malloc(1); + if (!tmp) + return (NULL); + tmp[i] = 0; + return (tmp); + } + if (len + start > g_strlen(s)) + len = g_strlen(s) - start; + tmp = g_calloc(len + 1, sizeof(*tmp)); + if (!tmp) + return (NULL); + while (i < len && s[i]) + { + tmp[i] = s[i + start]; + i++; + } + return (tmp); +} + +void *g_calloc(size_t n, size_t s) +{ + char *tmp; + unsigned long i; + + i = 0; + if (n == 0) + return (malloc(0)); + if (SIZE_MAX / n < s) + return (NULL); + tmp = malloc(n * s); + if (tmp) + while (i < n * s) + tmp[i++] = 0; + return ((void *)tmp); +} diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..b8a02a5 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,74 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: vvobis +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/09/17 14:23:19 by vvobis #+# #+# # +# Updated: 2024/12/10 21:16:51 by vvobis ### ########.fr # +# # +# **************************************************************************** # + +NAME := libft.a + +CC := cc + +CFLAGS:= -Wall -Wextra -Werror + +SRC := ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isdigit.c \ + ft_isprint.c ft_memcpy.c ft_memmove.c ft_memset.c \ + ft_strlcat.c ft_strlcpy.c ft_strlen.c \ + ft_isascii.c ft_strchr.c ft_strrchr.c ft_strncmp.c \ + ft_toupper.c ft_tolower.c ft_memchr.c ft_strnstr.c \ + ft_atoi.c ft_atod.c ft_memcmp.c ft_calloc.c ft_strdup.c \ + ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c \ + ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c \ + ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \ + ft_isspace.c ft_free.c ft_read.c + +SRCGNL := ../gnl/get_next_line.c ../gnl/get_next_line_utils.c + +SRCPRINT := printf/ft_fprintf.c printf/ft_printf.c printf/ft_putascii.c printf/ft_putptr.c printf/ft_puthex.c printf/ft_putfloat.c + +SRCBON := ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c + +OBJBON := $(SRCBON:%.c=%.o) + +OBJ := $(SRC:%.c=%.o) + +OBJGNL := $(SRCGNL:%.c=%.o) + +OBJPRINT := $(SRCPRINT:%.c=%.o) + +LIBDIR := lib + +LIBS := printf + +all: $(NAME) + +bonus: $(OBJBON) $(OBJ) + ar rcs libft.a $(OBJ) $(OBJBON) + +$(NAME): $(LIBDIR) $(OBJ) $(OBJGNL) $(OBJPRINT) $(LIBS) + ar rcs $(LIBDIR)/$@ $(OBJ) $(OBJGNL) $(OBJPRINT) + +$(LIBDIR): + mkdir -p $@ + +%.o: %.c + $(CC) -c $(CFLAGS) $^ -o $@ + +re: fclean all + +so: + $(CC) -fPIC $(CFLAGS) $(SRC) test.c + gcc -shared -o libft.so $(OBJ) $(OBJBON) + +fclean: clean + rm -f $(NAME) + make fclean -C $(LIBS) + +clean: + rm -f $(OBJ) $(OBJBON) $(OBJGNL) + make clean -C $(LIBS) diff --git a/libft/ft_atod.c b/libft/ft_atod.c new file mode 100644 index 0000000..bfa80b7 --- /dev/null +++ b/libft/ft_atod.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atod.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: victor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/08 22:25:35 by victor #+# #+# */ +/* Updated: 2024/12/05 20:08:42 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +double atod_collect_fraction(char *n, double d) +{ + int period; + int mult; + + mult = 1; + period = 0; + while (*n && !ft_isspace(*n)) + { + if (*n == '.' && period == 0) + { + period = 1; + n++; + continue ; + } + else if (((*n == '.' && period == 1) || (*n != '.' && !ft_isdigit(*n)))) + return (ft_putendl_fd("Invalid double format: Too many periods", \ + 2), exit(1), 0); + if (period) + mult *= 10; + d *= 10; + d += (*n - '0'); + n++; + } + return (d / mult); +} + +double ft_atod(char *n) +{ + double d; + int sign; + + if (!n) + exit(1); + sign = 1; + if (*n == '-') + { + sign = -1; + n++; + } + d = 0; + return (atod_collect_fraction(n, d) * sign); +} diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..5b742df --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bszilas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/10 10:49:04 by vvobis #+# #+# */ +/* Updated: 2024/09/13 22:19:12 by bszilas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int is_space(char const c) +{ + if ((c >= 9 && c <= 13) || c == ' ') + return (1); + return (0); +} + +int ft_atoi(char const *s) +{ + int nb; + char const *tmp; + + nb = 0; + while (is_space(*s)) + s++; + tmp = s; + if (*tmp == '+' || *tmp == '-') + tmp++; + while (*tmp >= '0' && *tmp <= '9') + { + nb *= 10; + nb += (*tmp - '0'); + tmp++; + } + if (*s == '-') + nb = -nb; + return (nb); +} + +long ft_atol(const char *nptr) +{ + int64_t n; + int sign; + + n = 0; + sign = 1; + while (ft_isspace(*nptr)) + nptr++; + if (*nptr == '-') + sign = -1; + if (sign == -1 || *nptr == '+') + nptr++; + while (*nptr >= '0' && *nptr <= '9') + n = n * 10 + (*nptr++ - '0'); + return (n * sign); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..332517f --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 16:04:55 by vvobis #+# #+# */ +/* Updated: 2024/04/09 20:54:53 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + while (n--) + *(char *)s++ = 0; +} diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c new file mode 100644 index 0000000..dcef84c --- /dev/null +++ b/libft/ft_calloc.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 13:44:00 by vvobis #+# #+# */ +/* Updated: 2024/04/10 16:39:56 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t n, size_t s) +{ + char *tmp; + unsigned long i; + + i = 0; + if (n == 0) + return (malloc(0)); + if (SIZE_MAX / n < s) + return (NULL); + tmp = malloc(n * s); + if (tmp) + while (i < n * s) + tmp[i++] = 0; + return ((void *)tmp); +} diff --git a/libft/ft_free.c b/libft/ft_free.c new file mode 100644 index 0000000..5bdc3a4 --- /dev/null +++ b/libft/ft_free.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */ +/* Updated: 2024/09/10 12:17:34 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_free(void *ptr_ptr) +{ + void **ptr; + + ptr = ptr_ptr; + free(*ptr); + *ptr = NULL; +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..2abb4a5 --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 15:59:18 by vvobis #+# #+# */ +/* Updated: 2024/04/02 15:59:23 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + if (ft_isdigit(c) || ft_isalpha(c)) + return (1); + return (0); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..1dfab51 --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 15:59:49 by vvobis #+# #+# */ +/* Updated: 2024/04/02 16:00:04 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (1); + return (0); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..0693407 --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 19:48:35 by vvobis #+# #+# */ +/* Updated: 2024/04/02 19:49:36 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..4dd21ff --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 16:14:56 by vvobis #+# #+# */ +/* Updated: 2024/04/02 19:38:45 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..99da20a --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 16:00:49 by vvobis #+# #+# */ +/* Updated: 2024/04/02 16:00:52 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/libft/ft_isspace.c b/libft/ft_isspace.c new file mode 100644 index 0000000..ee8f522 --- /dev/null +++ b/libft/ft_isspace.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: victor +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/08 18:27:49 by victor #+# #+# */ +/* Updated: 2024/09/08 18:27:55 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int c) +{ + return (c == ' ' || (c > 9 && c < 14)); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..f973b4a --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/05 20:16:48 by vvobis #+# #+# */ +/* Updated: 2024/04/11 14:48:59 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int get_len(int n) +{ + int i; + + if (n < 0) + n = -n; + i = 0; + while (n) + { + i++; + n /= 10; + } + return (i); +} + +static char *is_negative(int n) +{ + int i; + char *num; + + i = get_len(n); + num = malloc(sizeof(*num) * i + 2); + if (!num) + return (NULL); + num[i + 1] = 0; + while (n) + { + num[i--] = -(n % 10) + 48; + n /= 10; + } + num[i] = 0x2d; + return (num); +} + +static char *is_positive(int n) +{ + int i; + char *num; + + i = get_len(n); + num = malloc(sizeof(*num) * i + 1); + if (!num) + return (NULL); + num[i--] = 0; + while (n) + { + num[i--] = (n % 10) + 48; + n /= 10; + } + return (num); +} + +char *ft_itoa(int n) +{ + if (n == 0) + return (ft_strdup("0")); + else if (n < 0) + return (is_negative(n)); + else + return (is_positive(n)); +} diff --git a/libft/ft_lstadd_back_bonus.c b/libft/ft_lstadd_back_bonus.c new file mode 100644 index 0000000..f3d6959 --- /dev/null +++ b/libft/ft_lstadd_back_bonus.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 13:51:32 by vvobis #+# #+# */ +/* Updated: 2024/04/09 18:17:12 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *tmp; + + if (!new) + return ; + if (*lst) + { + tmp = *lst; + while (tmp->next) + tmp = tmp->next; + tmp->next = new; + } + else + *lst = new; +} diff --git a/libft/ft_lstadd_front_bonus.c b/libft/ft_lstadd_front_bonus.c new file mode 100644 index 0000000..25714f7 --- /dev/null +++ b/libft/ft_lstadd_front_bonus.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 12:53:08 by vvobis #+# #+# */ +/* Updated: 2024/04/09 18:17:50 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (!lst || !new) + return ; + if (!*lst) + *lst = new; + else + { + new->next = *lst; + *lst = new; + } +} diff --git a/libft/ft_lstclear_bonus.c b/libft/ft_lstclear_bonus.c new file mode 100644 index 0000000..8f531fb --- /dev/null +++ b/libft/ft_lstclear_bonus.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 15:44:23 by vvobis #+# #+# */ +/* Updated: 2024/04/09 22:35:41 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + if (!*lst || !del || !lst) + return ; + while (*lst) + { + tmp = (*lst)->next; + del((*lst)->content); + free(*lst); + *lst = tmp; + } +} diff --git a/libft/ft_lstdelone_bonus.c b/libft/ft_lstdelone_bonus.c new file mode 100644 index 0000000..c31153b --- /dev/null +++ b/libft/ft_lstdelone_bonus.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 14:24:03 by vvobis #+# #+# */ +/* Updated: 2024/04/09 15:42:04 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (!lst || !del) + return ; + del(lst->content); + free(lst); +} diff --git a/libft/ft_lstiter_bonus.c b/libft/ft_lstiter_bonus.c new file mode 100644 index 0000000..c38b240 --- /dev/null +++ b/libft/ft_lstiter_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 15:57:51 by vvobis #+# #+# */ +/* Updated: 2024/04/09 18:20:05 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (!lst || !f) + return ; + while (lst) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libft/ft_lstlast_bonus.c b/libft/ft_lstlast_bonus.c new file mode 100644 index 0000000..c83f4b0 --- /dev/null +++ b/libft/ft_lstlast_bonus.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 13:47:08 by vvobis #+# #+# */ +/* Updated: 2024/04/09 15:11:51 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (!lst) + return (NULL); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/libft/ft_lstmap_bonus.c b/libft/ft_lstmap_bonus.c new file mode 100644 index 0000000..9cfc0c5 --- /dev/null +++ b/libft/ft_lstmap_bonus.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 16:18:03 by vvobis #+# #+# */ +/* Updated: 2024/04/09 23:40:57 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void *free_list(t_list **head, void (*del)(void *), void *fcontent) +{ + if (fcontent) + del(fcontent); + ft_lstclear(head, del); + return (NULL); +} + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *tmp; + t_list *head; + void *fcontent; + + if (!lst || !f || !del) + return (NULL); + head = NULL; + while (lst) + { + fcontent = f(lst->content); + if (!fcontent) + return (free_list(&head, del, fcontent)); + tmp = ft_lstnew(fcontent); + if (!tmp) + return (free_list(&head, del, fcontent)); + ft_lstadd_back(&head, tmp); + lst = lst->next; + } + return (head); +} diff --git a/libft/ft_lstnew_bonus.c b/libft/ft_lstnew_bonus.c new file mode 100644 index 0000000..08775c5 --- /dev/null +++ b/libft/ft_lstnew_bonus.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 11:10:05 by vvobis #+# #+# */ +/* Updated: 2024/04/09 18:59:48 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *tmp; + + tmp = malloc(sizeof(*tmp)); + if (!tmp) + return (NULL); + tmp->next = NULL; + tmp->content = content; + return (tmp); +} diff --git a/libft/ft_lstsize_bonus.c b/libft/ft_lstsize_bonus.c new file mode 100644 index 0000000..c8def02 --- /dev/null +++ b/libft/ft_lstsize_bonus.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 13:43:08 by vvobis #+# #+# */ +/* Updated: 2024/04/09 18:21:11 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + if (!lst) + return (0); + i = 1; + while (lst->next) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..3944037 --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 15:13:59 by vvobis #+# #+# */ +/* Updated: 2024/04/09 20:55:24 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(void *s, int c, size_t n) +{ + unsigned char *p; + size_t i; + + i = 0; + p = (unsigned char *)s; + while (i < n) + { + if (p[i] == (unsigned char)c) + return ((void *)&p[i]); + i++; + } + if (p[i - 1] == (unsigned char)c) + return ((void *)&p[i]); + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..54180dc --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 15:27:15 by vvobis #+# #+# */ +/* Updated: 2024/04/09 19:21:02 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(void *s1, void const *s2, size_t n) +{ + unsigned char *p1; + unsigned char *p2; + unsigned int i; + + p1 = (unsigned char *)s1; + p2 = (unsigned char *)s2; + i = 0; + while (i < n) + { + if (p1[i] != p2[i]) + return (p1[i] - p2[i]); + i++; + } + return (0); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..e1e524a --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 17:13:18 by vvobis #+# #+# */ +/* Updated: 2024/04/09 19:21:29 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, void const *src, size_t n) +{ + char *d; + char *s; + + if (n == 0 || (dest == NULL && src == NULL)) + return (dest); + if (dest == NULL || src == NULL) + { + *(char *)dest = 1; + *(char *)src = 1; + } + d = (char *) dest; + s = (char *) src; + while (n--) + *d++ = *s++; + return (dest); +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..d5c700f --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 17:42:28 by vvobis #+# #+# */ +/* Updated: 2024/04/09 19:21:54 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, void const *src, size_t n) +{ + unsigned char *d; + unsigned char *s; + + if (!dest && !src) + return (dest); + d = (unsigned char *)dest; + s = (unsigned char *)src; + if (s < d) + while (n--) + d[n] = s[n]; + else + ft_memcpy(dest, (void *)src, n); + return (dest); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..792867a --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 15:57:27 by vvobis #+# #+# */ +/* Updated: 2024/04/03 11:18:10 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + char *str; + + str = (char *)s; + while (n--) + str[n] = c; + return (s); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100644 index 0000000..6b5d51f --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 17:34:30 by vvobis #+# #+# */ +/* Updated: 2024/04/07 17:53:33 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..1c7d3b4 --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 17:57:33 by vvobis #+# #+# */ +/* Updated: 2024/04/07 18:00:49 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *str, int fd) +{ + if (!str) + return ; + ft_putstr_fd(str, fd); + ft_putchar_fd(0x0a, fd); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..5073c24 --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 18:02:14 by vvobis #+# #+# */ +/* Updated: 2024/04/11 11:23:10 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n < 0) + ft_putchar_fd(0x2d, fd); + if (n <= -10) + ft_putnbr_fd(n / -10, fd); + if (n >= 10) + ft_putnbr_fd(n / 10, fd); + if (n >= 0) + ft_putchar_fd(n % 10 + 0x30, fd); + if (n < 0) + ft_putchar_fd(-(n % -10) + 0x30, fd); +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..328f96c --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 17:48:41 by vvobis #+# #+# */ +/* Updated: 2024/04/07 17:52:16 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *str, int fd) +{ + if (!str) + return ; + while (*str) + ft_putchar_fd(*str++, fd); +} diff --git a/libft/ft_read.c b/libft/ft_read.c new file mode 100644 index 0000000..e4dc8ca --- /dev/null +++ b/libft/ft_read.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/18 12:49:30 by vvobis #+# #+# */ +/* Updated: 2024/12/10 21:18:38 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_read(int fd, char *character, unsigned int size_to_read) +{ + int bytes_read; + + bytes_read = read(fd, character, size_to_read); + if (bytes_read == -1) + { + ft_fprintf(STDERR_FILENO, "Read failed\n"); + } + return (bytes_read); +} diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100644 index 0000000..9e34265 --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/05 20:14:20 by vvobis #+# #+# */ +/* Updated: 2024/04/09 23:55:49 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int count(char const *s, char const c) +{ + int n; + + n = 0; + if (*s != c && *s) + n = 1; + while (*s) + { + if (*s == c && *(s + 1) != c && *(s + 1)) + n++; + s++; + } + return (n); +} + +static int count_sub(char const *s, char const c) +{ + int i; + + i = 0; + while (*s != c && *s) + { + i++; + s++; + } + return (i); +} + +static char **free_all(char **back) +{ + char **tmp; + + tmp = back; + while (*back) + { + free(*back); + back++; + } + free(tmp); + return (NULL); +} + +char **ft_split(char const *s, char const c) +{ + char **tmp; + char **back; + + if (!s) + return (NULL); + tmp = (char **)ft_calloc(sizeof(*tmp), count(s, c) + 1); + if (!tmp) + return (NULL); + back = tmp; + while (*s && tmp) + { + while (*s == c && *s) + s++; + if (*s) + { + *tmp = ft_substr(s, 0, count_sub(s, c)); + if (!*tmp) + return (free_all(back)); + } + tmp++; + while (*s != c && *s) + s++; + } + return (back); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..30d0b93 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */ +/* Updated: 2024/04/09 20:54:14 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(char const *s, int c) +{ + int i; + + i = 0; + while (s[i]) + { + if (s[i] == (char)c) + return ((char *)&s[i]); + i++; + } + if ((char)c == 0) + return ((char *)&s[i]); + return (NULL); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100644 index 0000000..941d91b --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 18:44:05 by vvobis #+# #+# */ +/* Updated: 2024/04/12 10:44:23 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(char const *s) +{ + char *tmp; + int i; + + i = 0; + tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp)); + if (!tmp) + return (NULL); + while (s[i]) + { + tmp[i] = s[i]; + i++; + } + tmp[i] = 0; + return (tmp); +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..8aac9ae --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 17:24:08 by vvobis #+# #+# */ +/* Updated: 2024/04/09 19:27:07 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char const *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + if (!s || !f) + return ; + i = 0; + while (s[i]) + { + f(i, (char *)&s[i]); + i++; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..03be413 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 19:08:03 by vvobis #+# #+# */ +/* Updated: 2024/04/12 10:07:26 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *tmp; + unsigned int i; + unsigned int j; + + if (!s1 || !s2) + return (NULL); + tmp = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(*tmp)); + if (!tmp) + return (NULL); + i = 0; + while (s1[i]) + { + tmp[i] = s1[i]; + i++; + } + j = 0; + while (s2[j]) + { + tmp[i] = s2[j]; + i++; + j++; + } + tmp[i] = 0; + return (tmp); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..f89bf09 --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 16:10:59 by vvobis #+# #+# */ +/* Updated: 2024/04/10 16:41:14 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dest, char const *src, size_t size) +{ + size_t dlen; + size_t slen; + + if (size == 0) + return (ft_strlen(src)); + dlen = ft_strlen(dest); + slen = ft_strlen(src); + if (size <= dlen) + return (slen + size); + if (dlen + slen < size) + { + ft_memcpy(&dest[dlen], src, slen); + dest[dlen + slen] = 0; + } + else + { + ft_memcpy(&dest[dlen], src, size - dlen); + dest[size - 1] = 0; + } + return (dlen + slen); +} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c new file mode 100644 index 0000000..b34a653 --- /dev/null +++ b/libft/ft_strlcpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 19:03:18 by vvobis #+# #+# */ +/* Updated: 2024/04/09 19:28:44 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dest, char const *src, size_t size) +{ + size_t i; + + i = 0; + if (size != 0) + { + while (src[i] != '\0' && i < size - 1) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + } + while (src[i] != '\0') + i++; + return (i); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..27a2023 --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */ +/* Updated: 2024/04/12 10:40:59 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(char const *str) +{ + size_t i; + + i = 0; + while (*str++) + { + i++; + if (i == SIZE_MAX) + break ; + } + return (i); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..664792b --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 17:01:35 by vvobis #+# #+# */ +/* Updated: 2024/04/12 10:13:57 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *tmp; + unsigned int i; + + if (!s || !f) + return (NULL); + tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp)); + if (!tmp) + return (NULL); + i = 0; + while (s[i]) + { + tmp[i] = f(i, s[i]); + i++; + } + tmp[i] = 0; + return (tmp); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..f42fd81 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 13:33:22 by vvobis #+# #+# */ +/* Updated: 2024/04/12 10:29:59 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(char const *s1, char const *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while ((s1[i] || s2[i]) && i < n) + { + if (s1[i] < s2[i]) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + else if (s1[i] > s2[i]) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + i++; + } + return (0); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..2733d0e --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 15:36:41 by vvobis #+# #+# */ +/* Updated: 2024/04/10 16:42:02 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(char const *s1, char const *s2, size_t n) +{ + size_t i; + size_t j; + + if (!*s2) + return ((char *)s1); + i = 0; + while (i < n && s1[i]) + { + j = 0; + if (s1[i + j] == s2[j]) + { + while (s1[i + j] == s2[j] && s1[i + j] && i + j < n) + j++; + if (!s2[j]) + return ((char *)&s1[i]); + } + i++; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..e5d7d65 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */ +/* Updated: 2024/04/09 20:54:22 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(char const *s, int c) +{ + char *n; + int i; + + i = 0; + n = NULL; + while (s[i] != 0) + { + if (s[i] == (char)c) + n = (char *)&s[i]; + i++; + } + if ((char)c == 0) + return ((char *)&s[i]); + if (n) + return (n); + return (NULL); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..e02e096 --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 19:30:35 by vvobis #+# #+# */ +/* Updated: 2024/04/15 13:07:55 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int is_set(char c, char const *s) +{ + while (*s) + if (c == *s++) + return (1); + return (0); +} + +static int find_end(char const *s1, char const *set) +{ + int i; + + i = 0; + while (*s1) + s1++; + while (is_set(*--s1, set)) + i++; + return (i); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + if (!s1) + return (NULL); + if (!set) + return ((char *)s1); + while (is_set(*s1, set) && *s1) + s1++; + if ((int)ft_strlen(s1) > find_end(s1, set)) + return (ft_substr(s1, 0, ft_strlen(s1) - find_end(s1, set))); + else + return (ft_calloc(1, 1)); +} diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100644 index 0000000..b9e148f --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 18:58:25 by vvobis #+# #+# */ +/* Updated: 2024/04/09 21:50:44 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *tmp; + unsigned int i; + + i = 0; + if (!s || start >= ft_strlen(s) || len <= 0) + { + tmp = malloc(1); + if (!tmp) + return (NULL); + tmp[i] = 0; + return (tmp); + } + if (len + start > ft_strlen(s)) + len = ft_strlen(s) - start; + tmp = malloc(sizeof(*tmp) * len + 1); + if (!tmp) + return (NULL); + while (i < len && s[i]) + { + tmp[i] = s[i + start]; + i++; + } + tmp[i] = 0; + return (tmp); +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..d7c4540 --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 12:01:54 by vvobis #+# #+# */ +/* Updated: 2024/04/03 12:15:18 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + c += 32; + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..7bcb06a --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 12:18:44 by vvobis #+# #+# */ +/* Updated: 2024/04/03 12:19:05 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + c -= 32; + return (c); +} diff --git a/libft/lib/libft.a b/libft/lib/libft.a new file mode 100644 index 0000000..88589c4 Binary files /dev/null and b/libft/lib/libft.a differ diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..6f019fc --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bszilas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/09 23:43:36 by vvobis #+# #+# */ +/* Updated: 2024/12/10 21:12:01 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H + +# define LIBFT_H + +# include "../gnl/get_next_line.h" +# include "./printf/ft_printf.h" +# include +# include +# include +# include +# include + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isprint(int c); +int ft_isascii(int c); +int ft_isspace(int c); + +/*Memory Management*/ +int ft_memcmp(void *s1, void const *s2, size_t n); +void *ft_memset(void *s, int c, size_t n); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dest, void const *src, size_t n); +void *ft_memmove(void *dest, void const *src, size_t n); +void *ft_memchr(void *s, int c, size_t n); +void *ft_calloc(size_t n, size_t s); + +/*Info Conversion*/ +int ft_atoi(char const *s); +double ft_atod(char *s); +char *ft_itoa(int n); +char **ft_split(char const *s, char c); +void ft_free(void *ptr_ptr); +long ft_atol(const char *nptr); + +/*String Manip*/ +int ft_toupper(int c); +int ft_tolower(int c); +int ft_strncmp(char const *s1, char const *s2, size_t n); +char *ft_strnstr(char const *s1, char const *s2, size_t n); +char *ft_strchr(char const *s, int c); +char *ft_strrchr(char const *s, int c); +char *ft_strdup(char const *s); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char const *s, void (*f)(unsigned int, char *)); +size_t ft_strlcpy(char *dest, char const *src, size_t size); +size_t ft_strlcat(char *dest, char const *src, size_t size); +size_t ft_strlen(char const *str); + +/*List manip*/ +int ft_lstsize(t_list *lst); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +void ft_lstclear(t_list **lst, void (*del)(void*)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +void ft_lstadd_front(t_list **lst, t_list *node_new); +void ft_lstadd_back(t_list **lst, t_list *node_new); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +t_list *ft_lstnew(void *content); +t_list *ft_lstlast(t_list *lst); + +/*Output*/ +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *str, int fd); +void ft_putendl_fd(char *str, int fd); +void ft_putnbr_fd(int n, int fd); + +/* Get Next Line */ +char *get_next_line(int fd); + +#endif diff --git a/libft/printf/Makefile b/libft/printf/Makefile new file mode 100644 index 0000000..0ad8066 --- /dev/null +++ b/libft/printf/Makefile @@ -0,0 +1,50 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: victor +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/09/14 09:21:46 by victor #+# #+# # +# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr # +# # +# **************************************************************************** # + +LIBDIR := lib +NAME := $(LIBDIR)/libftprintf.a +CC := gcc +CFLAGS = -Wall -Werror -Wextra -g3 +SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c +OBJ := $(SRC:.c=.o) + +ifdef RELEASE +CFLAGS -= -g3 +endif + +MKDIR = mkdir +ifeq ($(OS), Windows_NT) +RM = del /Q +else +RM = rm -rf +MKDIR += -p +endif + +all: $(NAME) + +$(NAME): $(OBJ) + ar rsc $@ $(OBJ) + +$(OBJ): $(SRC) $(LIBDIR) + $(CC) $(CFLAGS) -c $(SRC) + +$(LIBDIR): + $(MKDIR) $@ + +clean: + $(RM) $(OBJ) + +fclean: clean + $(RM) $(LIBDIR) + + +re: fclean all diff --git a/libft/printf/ft_fprintf.c b/libft/printf/ft_fprintf.c new file mode 100644 index 0000000..0bfdfd1 --- /dev/null +++ b/libft/printf/ft_fprintf.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */ +/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "./ft_printf.h" + +int ft_fprintf(int fd, const char *format, ...) +{ + va_list args; + int count; + + if (!format || fd < 0) + return (-1); + va_start(args, format); + count = 0; + while (1) + { + while (*format != '%' && *format) + ft_putchar(*format++, &count, fd); + if (!*format) + break ; + else + format++; + if (!*format || !handle_arg(args, *format, &count, fd)) + return (-1); + format++; + } + va_end(args); + return (count); +} diff --git a/libft/printf/ft_printf.c b/libft/printf/ft_printf.c new file mode 100644 index 0000000..8ff9870 --- /dev/null +++ b/libft/printf/ft_printf.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */ +/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int handle_arg(va_list args, char format, int *count, int fd) +{ + if (format == 'd' || format == 'i') + ft_putnbr(va_arg(args, int), count, fd); + else if (format == 'u') + ft_putnbr(va_arg(args, unsigned int), count, fd); + else if (format == 's') + ft_putstr(va_arg(args, char *), count, fd); + else if (format == 'X') + ft_puthex_upper(va_arg(args, unsigned int), count, fd); + else if (format == 'x') + ft_puthex_lower(va_arg(args, unsigned int), count, fd); + else if (format == 'p') + ft_putptr(va_arg(args, void *), count, fd); + else if (format == 'c') + ft_putchar(va_arg(args, int), count, fd); + else if (format == '%') + ft_putchar('%', count, fd); + else if (format == 'f') + ft_putnbrf(va_arg(args, double), count, 5, fd); + else + return (0); + return (1); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int count; + + if (!format) + return (-1); + va_start(args, format); + count = 0; + while (1) + { + while (*format != '%' && *format) + ft_putchar(*format++, &count, STDOUT_FILENO); + if (!*format) + break ; + else + format++; + if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO)) + return (-1); + format++; + } + va_end(args); + return (count); +} diff --git a/libft/printf/ft_printf.h b/libft/printf/ft_printf.h new file mode 100644 index 0000000..e280307 --- /dev/null +++ b/libft/printf/ft_printf.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */ +/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H + +# define FT_PRINTF_H + +# include +# include +# include + +# ifdef _WIN32 +# include +# undef STDIN_FILENO + +# define STDIN_FILENO _fileno(stdin) +# undef STDOUT_FILENO + +# define STDOUT_FILENO _fileno(stdout) + +# define write _write + +# else + +# include + +# endif + +int ft_fprintf(int fd, const char *format, ...); +int ft_printf(const char *format, ...); + +int handle_arg(va_list args, char format, int *count, int fd); + +void ft_puthex_lower(unsigned long nbr, int *count, int fd); +void ft_puthex_upper(unsigned long nbr, int *count, int fd); +void ft_putchar(int c, int *count, int fd); +void ft_putnbr(long n, int *count, int fd); +void ft_putnbrf(double f, int *count, int precision, int fd); +void ft_putstr(const char *str, int *count, int fd); +void ft_putptr(void *ptr, int *count, int fd); +size_t ft_strlen(const char *s); + +#endif diff --git a/libft/printf/ft_putascii.c b/libft/printf/ft_putascii.c new file mode 100644 index 0000000..7cd4e9f --- /dev/null +++ b/libft/printf/ft_putascii.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */ +/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putchar(int c, int *count, int fd) +{ + write(fd, &c, 1); + *count += 1; +} + +void ft_putnbr(long n, int *count, int fd) +{ + if (n < 0) + ft_putchar(0x2d, count, fd); + if (n <= -10) + ft_putnbr(n / -10, count, fd); + if (n >= 10) + ft_putnbr(n / 10, count, fd); + if (n >= 0) + ft_putchar(n % 10 + 0x30, count, fd); + if (n < 0) + ft_putchar(-(n % -10) + 0x30, count, fd); +} + +void ft_putstr(const char *str, int *count, int fd) +{ + if (!str) + { + *count += ft_fprintf(fd, "(null)"); + return ; + } + *count += ft_fprintf(fd, str); +} diff --git a/libft/printf/ft_putfloat.c b/libft/printf/ft_putfloat.c new file mode 100644 index 0000000..85f17de --- /dev/null +++ b/libft/printf/ft_putfloat.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putfloat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */ +/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putnbrf(double f, int *count, int precision, int fd) +{ + int number; + double fraction; + unsigned int mult; + + mult = 1; + while (precision-- > 0) + { + mult *= 10; + } + number = (int)f; + fraction = f - (double)number; + if (fraction < 0) + fraction = -fraction; + ft_putnbr(number, count, fd); + ft_putchar('.', count, fd); + number = fraction * mult; + ft_putnbr(number, count, fd); +} diff --git a/libft/printf/ft_puthex.c b/libft/printf/ft_puthex.c new file mode 100644 index 0000000..fad5f1b --- /dev/null +++ b/libft/printf/ft_puthex.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_puthex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */ +/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_puthex_upper(unsigned long nbr, int *count, int fd) +{ + char *base_str; + + base_str = "0123456789ABCDEF"; + if (nbr >= 16) + ft_puthex_upper(nbr / 16, count, fd); + ft_putchar(base_str[(nbr % 16)], count, fd); +} + +void ft_puthex_lower(unsigned long nbr, int *count, int fd) +{ + char *base_str; + + base_str = "0123456789abcdef"; + if (nbr >= 16) + ft_puthex_lower(nbr / 16, count, fd); + ft_putchar(base_str[(nbr % 16)], count, fd); +} diff --git a/libft/printf/ft_putptr.c b/libft/printf/ft_putptr.c new file mode 100644 index 0000000..94b94e2 --- /dev/null +++ b/libft/printf/ft_putptr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */ +/* Updated: 2024/11/10 20:42:58 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putptr(void *ptr, int *count, int fd) +{ + void **to_print; + + if (!ptr) + { + *count += ft_fprintf(fd, "(nil)"); + return ; + } + to_print = &ptr; + *count += ft_printf("0x"); + ft_puthex_lower((unsigned long long)*to_print, count, fd); +} diff --git a/libft/printf/ft_strlen.c b/libft/printf/ft_strlen.c new file mode 100644 index 0000000..850cb1d --- /dev/null +++ b/libft/printf/ft_strlen.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */ +/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "limits.h" + +size_t ft_strlen(char const *str) +{ + size_t i; + + i = 0; + while (*str++) + { + i++; + if (i == ULONG_MAX) + break ; + } + return (i); +} diff --git a/memory/Makefile b/memory/Makefile new file mode 100644 index 0000000..a8157dc --- /dev/null +++ b/memory/Makefile @@ -0,0 +1,57 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: vvobis +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/11/10 15:02:31 by vvobis #+# #+# # +# Updated: 2024/12/10 21:23:05 by vvobis ### ########.fr # +# # +# **************************************************************************** # + +CC := gcc + +CFLAGS = -Werror -Wextra -Wall + +SRC := memory.c list.c ft_free.c + +OBJDIR := obj +OBJ := $(SRC:%.c=$(OBJDIR)/%.o) + +LIBDIR := lib +TARGET := $(LIBDIR)/memory.a + +ifdef RELEASE +CFLAGS -= -g3 +endif + +MKDIR = mkdir +ifeq ($(OS), Windows_NT) +RM = del /S /Q +else +RM = rm -rf +MKDIR += -p +endif + +all: $(TARGET) + +$(TARGET): $(OBJ) | $(LIBDIR) + ar rcs $@ $(OBJ) + +$(OBJDIR)/%.o: %.c | $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(LIBDIR): $(OBJDIR) + $(MKDIR) $(LIBDIR) + +$(OBJDIR): + $(MKDIR) $(OBJDIR) + +clean: + $(RM) $(OBJDIR) + +fclean: clean + $(RM) $(LIBDIR) + +re: fclean all diff --git a/memory/ft_free.c b/memory/ft_free.c new file mode 100644 index 0000000..427971c --- /dev/null +++ b/memory/ft_free.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */ +/* Updated: 2024/12/10 21:23:56 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "memory.h" + +void ft_free(void *ptr_ptr) +{ + void **ptr; + + ptr = ptr_ptr; + free(*ptr); + *ptr = NULL; +} diff --git a/memory/lib/memory.a b/memory/lib/memory.a new file mode 100644 index 0000000..24cd73b Binary files /dev/null and b/memory/lib/memory.a differ diff --git a/memory/list.c b/memory/list.c new file mode 100644 index 0000000..83a7917 --- /dev/null +++ b/memory/list.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/27 11:44:46 by vvobis #+# #+# */ +/* Updated: 2024/11/10 15:09:54 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "list.h" +#include "memory.h" + +t_clean *lst_node_new(void *content, void (*del)(void *)) +{ + t_clean *new; + + if (!content || !del) + return (NULL); + new = malloc(sizeof(*new)); + if (!new) + return (NULL); + new->content = content; + new->clean = del; + new->next = NULL; + return (new); +} + +void lst_node_del(t_clean **lst) +{ + (*lst)->clean((*lst)->content); + ft_free(&*lst); +} + +void lst_node_del_clean(t_clean **lst, void *mem) +{ + t_clean *tmp; + t_clean *head; + + if (!lst || !*lst) + return ; + head = *lst; + tmp = *lst; + while ((*lst) && (*lst)->next->content != mem) + (*lst) = (*lst)->next; + if (!*lst) + return ; + tmp = *lst; + if ((*lst)->next && (*lst)->next->next) + { + tmp = (*lst)->next->next; + lst_node_del(&(*lst)->next); + (*lst)->next = tmp; + } + else if ((*lst)->next) + lst_node_del(&(*lst)->next); + else + lst_node_del(lst); + *lst = head; +} + +void lst_list_clean(t_clean **head) +{ + t_clean *tmp; + + while (*head) + { + tmp = (*head)->next; + lst_node_del(head); + *head = tmp; + } + free(*head); +} + +int lst_add_back(t_clean **node, t_clean *new) +{ + t_clean *tmp; + + if (!new) + { + perror("malloc"); + return (0); + } + if (*node) + { + tmp = *node; + while (tmp->next) + tmp = tmp->next; + tmp->next = new; + } + else + *node = new; + return (1); +} diff --git a/memory/list.h b/memory/list.h new file mode 100644 index 0000000..6584300 --- /dev/null +++ b/memory/list.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 14:48:59 by vvobis #+# #+# */ +/* Updated: 2024/11/10 15:08:38 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_C +# define LIST_C + +# include +# include + +typedef struct s_clean +{ + void *content; + void (*clean)(void *del); + struct s_clean *next; +} t_clean; + +/* List */ +t_clean *lst_node_new(void *content, void (*del)(void *)); +void lst_node_del(t_clean **lst); +void lst_node_del_clean(t_clean **lst, void *mem); +void lst_list_clean(t_clean **head); +int lst_add_back(t_clean **node, t_clean *node_new); + +#endif diff --git a/memory/memory.c b/memory/memory.c new file mode 100644 index 0000000..7bf9ede --- /dev/null +++ b/memory/memory.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* memory.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 14:47:33 by vvobis #+# #+# */ +/* Updated: 2024/11/11 11:53:06 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +# include "list.h" +# include "memory.h" +# include +# include + +void lst_memory(void *mem, void (*del)(void *c), int mode) +{ + static t_clean *list; + t_clean *new; + + if (mode == FAIL) + return (lst_list_clean(&list), exit(EXIT_FAILURE)); + else if (mode == END) + return (lst_list_clean(&list)); + else if (mode == FREE) + return (lst_node_del_clean(&list, mem)); + else if (!mem && mode != AT_EXIT) + return (lst_list_clean(&list), perror("malloc"), exit(EXIT_FAILURE)); + new = lst_node_new(mem, del); + if (!new) + return (del(mem), lst_list_clean(&list), \ + perror(""), exit(EXIT_FAILURE)); + lst_add_back(&list, new); +} diff --git a/memory/memory.h b/memory/memory.h new file mode 100644 index 0000000..6ec82ac --- /dev/null +++ b/memory/memory.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* memory.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 14:46:26 by vvobis #+# #+# */ +/* Updated: 2024/12/10 21:22:01 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEMORY_H +# define MEMORY_H + +# include +# include "../libft/libft.h" + +enum e_alloc +{ + ADD, + FAIL, + END, + FREE, + AT_EXIT, +}; + +void lst_memory(void *mem, void (*del)(void *c), int mode); + +#endif diff --git a/memory/obj/ft_free.o b/memory/obj/ft_free.o new file mode 100644 index 0000000..9a0a539 Binary files /dev/null and b/memory/obj/ft_free.o differ diff --git a/memory/obj/list.o b/memory/obj/list.o new file mode 100644 index 0000000..9a94575 Binary files /dev/null and b/memory/obj/list.o differ diff --git a/memory/obj/memory.o b/memory/obj/memory.o new file mode 100644 index 0000000..4124a5a Binary files /dev/null and b/memory/obj/memory.o differ diff --git a/printf/Makefile b/printf/Makefile new file mode 100644 index 0000000..0ad8066 --- /dev/null +++ b/printf/Makefile @@ -0,0 +1,50 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: victor +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/09/14 09:21:46 by victor #+# #+# # +# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr # +# # +# **************************************************************************** # + +LIBDIR := lib +NAME := $(LIBDIR)/libftprintf.a +CC := gcc +CFLAGS = -Wall -Werror -Wextra -g3 +SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c +OBJ := $(SRC:.c=.o) + +ifdef RELEASE +CFLAGS -= -g3 +endif + +MKDIR = mkdir +ifeq ($(OS), Windows_NT) +RM = del /Q +else +RM = rm -rf +MKDIR += -p +endif + +all: $(NAME) + +$(NAME): $(OBJ) + ar rsc $@ $(OBJ) + +$(OBJ): $(SRC) $(LIBDIR) + $(CC) $(CFLAGS) -c $(SRC) + +$(LIBDIR): + $(MKDIR) $@ + +clean: + $(RM) $(OBJ) + +fclean: clean + $(RM) $(LIBDIR) + + +re: fclean all diff --git a/printf/ft_fprintf.c b/printf/ft_fprintf.c new file mode 100644 index 0000000..0bfdfd1 --- /dev/null +++ b/printf/ft_fprintf.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */ +/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "./ft_printf.h" + +int ft_fprintf(int fd, const char *format, ...) +{ + va_list args; + int count; + + if (!format || fd < 0) + return (-1); + va_start(args, format); + count = 0; + while (1) + { + while (*format != '%' && *format) + ft_putchar(*format++, &count, fd); + if (!*format) + break ; + else + format++; + if (!*format || !handle_arg(args, *format, &count, fd)) + return (-1); + format++; + } + va_end(args); + return (count); +} diff --git a/printf/ft_printf.c b/printf/ft_printf.c new file mode 100644 index 0000000..8ff9870 --- /dev/null +++ b/printf/ft_printf.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */ +/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int handle_arg(va_list args, char format, int *count, int fd) +{ + if (format == 'd' || format == 'i') + ft_putnbr(va_arg(args, int), count, fd); + else if (format == 'u') + ft_putnbr(va_arg(args, unsigned int), count, fd); + else if (format == 's') + ft_putstr(va_arg(args, char *), count, fd); + else if (format == 'X') + ft_puthex_upper(va_arg(args, unsigned int), count, fd); + else if (format == 'x') + ft_puthex_lower(va_arg(args, unsigned int), count, fd); + else if (format == 'p') + ft_putptr(va_arg(args, void *), count, fd); + else if (format == 'c') + ft_putchar(va_arg(args, int), count, fd); + else if (format == '%') + ft_putchar('%', count, fd); + else if (format == 'f') + ft_putnbrf(va_arg(args, double), count, 5, fd); + else + return (0); + return (1); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int count; + + if (!format) + return (-1); + va_start(args, format); + count = 0; + while (1) + { + while (*format != '%' && *format) + ft_putchar(*format++, &count, STDOUT_FILENO); + if (!*format) + break ; + else + format++; + if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO)) + return (-1); + format++; + } + va_end(args); + return (count); +} diff --git a/printf/ft_printf.h b/printf/ft_printf.h new file mode 100644 index 0000000..e280307 --- /dev/null +++ b/printf/ft_printf.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */ +/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H + +# define FT_PRINTF_H + +# include +# include +# include + +# ifdef _WIN32 +# include +# undef STDIN_FILENO + +# define STDIN_FILENO _fileno(stdin) +# undef STDOUT_FILENO + +# define STDOUT_FILENO _fileno(stdout) + +# define write _write + +# else + +# include + +# endif + +int ft_fprintf(int fd, const char *format, ...); +int ft_printf(const char *format, ...); + +int handle_arg(va_list args, char format, int *count, int fd); + +void ft_puthex_lower(unsigned long nbr, int *count, int fd); +void ft_puthex_upper(unsigned long nbr, int *count, int fd); +void ft_putchar(int c, int *count, int fd); +void ft_putnbr(long n, int *count, int fd); +void ft_putnbrf(double f, int *count, int precision, int fd); +void ft_putstr(const char *str, int *count, int fd); +void ft_putptr(void *ptr, int *count, int fd); +size_t ft_strlen(const char *s); + +#endif diff --git a/printf/ft_putascii.c b/printf/ft_putascii.c new file mode 100644 index 0000000..7cd4e9f --- /dev/null +++ b/printf/ft_putascii.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */ +/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putchar(int c, int *count, int fd) +{ + write(fd, &c, 1); + *count += 1; +} + +void ft_putnbr(long n, int *count, int fd) +{ + if (n < 0) + ft_putchar(0x2d, count, fd); + if (n <= -10) + ft_putnbr(n / -10, count, fd); + if (n >= 10) + ft_putnbr(n / 10, count, fd); + if (n >= 0) + ft_putchar(n % 10 + 0x30, count, fd); + if (n < 0) + ft_putchar(-(n % -10) + 0x30, count, fd); +} + +void ft_putstr(const char *str, int *count, int fd) +{ + if (!str) + { + *count += ft_fprintf(fd, "(null)"); + return ; + } + *count += ft_fprintf(fd, str); +} diff --git a/printf/ft_putfloat.c b/printf/ft_putfloat.c new file mode 100644 index 0000000..85f17de --- /dev/null +++ b/printf/ft_putfloat.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putfloat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */ +/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putnbrf(double f, int *count, int precision, int fd) +{ + int number; + double fraction; + unsigned int mult; + + mult = 1; + while (precision-- > 0) + { + mult *= 10; + } + number = (int)f; + fraction = f - (double)number; + if (fraction < 0) + fraction = -fraction; + ft_putnbr(number, count, fd); + ft_putchar('.', count, fd); + number = fraction * mult; + ft_putnbr(number, count, fd); +} diff --git a/printf/ft_puthex.c b/printf/ft_puthex.c new file mode 100644 index 0000000..fad5f1b --- /dev/null +++ b/printf/ft_puthex.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_puthex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */ +/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_puthex_upper(unsigned long nbr, int *count, int fd) +{ + char *base_str; + + base_str = "0123456789ABCDEF"; + if (nbr >= 16) + ft_puthex_upper(nbr / 16, count, fd); + ft_putchar(base_str[(nbr % 16)], count, fd); +} + +void ft_puthex_lower(unsigned long nbr, int *count, int fd) +{ + char *base_str; + + base_str = "0123456789abcdef"; + if (nbr >= 16) + ft_puthex_lower(nbr / 16, count, fd); + ft_putchar(base_str[(nbr % 16)], count, fd); +} diff --git a/printf/ft_putptr.c b/printf/ft_putptr.c new file mode 100644 index 0000000..94b94e2 --- /dev/null +++ b/printf/ft_putptr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */ +/* Updated: 2024/11/10 20:42:58 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putptr(void *ptr, int *count, int fd) +{ + void **to_print; + + if (!ptr) + { + *count += ft_fprintf(fd, "(nil)"); + return ; + } + to_print = &ptr; + *count += ft_printf("0x"); + ft_puthex_lower((unsigned long long)*to_print, count, fd); +} diff --git a/printf/ft_strlen.c b/printf/ft_strlen.c new file mode 100644 index 0000000..850cb1d --- /dev/null +++ b/printf/ft_strlen.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */ +/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "limits.h" + +size_t ft_strlen(char const *str) +{ + size_t i; + + i = 0; + while (*str++) + { + i++; + if (i == ULONG_MAX) + break ; + } + return (i); +} diff --git a/prompt_lib/Makefile b/prompt_lib/Makefile new file mode 100644 index 0000000..2c41684 --- /dev/null +++ b/prompt_lib/Makefile @@ -0,0 +1,86 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: anarama +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/05 12:24:47 by victor #+# #+# # +# Updated: 2024/12/10 21:20:03 by vvobis ### ########.fr # +# # +# **************************************************************************** # + +# COMPILER AND FLAGS +CC := cc +CFLAGS = -Werror -Wall -Wextra -g3 + +RM = rm + +ifdef release + CFLAGS -= -g3 +endif + + +MKDIR = mkdir + +ifeq ($(OS), Windows_NT) + RM = del /S /Q +else + RM = rm -rf + MKDIR += -p +endif + +# DIRECTORIES + +SRCDIR := src +OBJDIR := obj + +# SOURCES AND OBJECTS +SRC := prompt_input.c prompt_string_management.c \ + prompt_utils.c tab_completion.c \ + escape_sequences.c arrowkeys.c \ + prompt_print.c tab_get_word.c \ + non_blocking_mode.c prompt_handle_chars.c \ + utils.c terminal.c + +OBJ := $(SRC:%.c=$(OBJDIR)/%.o) +SRCS := $(addprefix $(SRCDIR)/, $(SRC)) + +# TARGETS +MEMORY := ../memory/lib/memory.a +LIBFT := ../libft/lib/libft.a + +LIBS := $(MEMORY) $(LIBFT) +LIBDIR := lib +NAME := $(LIBDIR)/prompt.a + +all: $(NAME) + +test: $(NAME) + $(CC) $(CFLAGS) main.c $(NAME) $(LIBS) + +$(NAME): $(OBJ) prompt.h | $(LIBS) + ar rcs $@ $(OBJ) $(LIBS) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR): $(LIBDIR) + $(MKDIR) $@ + +$(LIBDIR): + $(MKDIR) $@ + +$(LIBS): + make -C ../memory + make -C ../libft + +clean: + make fclean -C ../memory + make fclean -C ../libft + $(RM) $(OBJDIR) + +fclean: clean + $(RM) $(LIBDIR) + +re: fclean all diff --git a/prompt_lib/lib/prompt.a b/prompt_lib/lib/prompt.a new file mode 100644 index 0000000..8f5541a Binary files /dev/null and b/prompt_lib/lib/prompt.a differ diff --git a/prompt_lib/obj/arrowkeys.o b/prompt_lib/obj/arrowkeys.o new file mode 100644 index 0000000..1ceae38 Binary files /dev/null and b/prompt_lib/obj/arrowkeys.o differ diff --git a/prompt_lib/obj/escape_sequences.o b/prompt_lib/obj/escape_sequences.o new file mode 100644 index 0000000..ba81b7d Binary files /dev/null and b/prompt_lib/obj/escape_sequences.o differ diff --git a/prompt_lib/obj/ft_putstr_fd.o b/prompt_lib/obj/ft_putstr_fd.o new file mode 100644 index 0000000..9138b01 Binary files /dev/null and b/prompt_lib/obj/ft_putstr_fd.o differ diff --git a/prompt_lib/obj/ft_read.o b/prompt_lib/obj/ft_read.o new file mode 100644 index 0000000..18ff085 Binary files /dev/null and b/prompt_lib/obj/ft_read.o differ diff --git a/prompt_lib/obj/non_blocking_mode.o b/prompt_lib/obj/non_blocking_mode.o new file mode 100644 index 0000000..2ec18c4 Binary files /dev/null and b/prompt_lib/obj/non_blocking_mode.o differ diff --git a/prompt_lib/obj/prompt_handle_chars.o b/prompt_lib/obj/prompt_handle_chars.o new file mode 100644 index 0000000..48b80ad Binary files /dev/null and b/prompt_lib/obj/prompt_handle_chars.o differ diff --git a/prompt_lib/obj/prompt_input.o b/prompt_lib/obj/prompt_input.o new file mode 100644 index 0000000..1d6dee4 Binary files /dev/null and b/prompt_lib/obj/prompt_input.o differ diff --git a/prompt_lib/obj/prompt_print.o b/prompt_lib/obj/prompt_print.o new file mode 100644 index 0000000..dfde948 Binary files /dev/null and b/prompt_lib/obj/prompt_print.o differ diff --git a/prompt_lib/obj/prompt_string_management.o b/prompt_lib/obj/prompt_string_management.o new file mode 100644 index 0000000..9fa31b7 Binary files /dev/null and b/prompt_lib/obj/prompt_string_management.o differ diff --git a/prompt_lib/obj/prompt_utils.o b/prompt_lib/obj/prompt_utils.o new file mode 100644 index 0000000..049277a Binary files /dev/null and b/prompt_lib/obj/prompt_utils.o differ diff --git a/prompt_lib/obj/tab_completion.o b/prompt_lib/obj/tab_completion.o new file mode 100644 index 0000000..9dac373 Binary files /dev/null and b/prompt_lib/obj/tab_completion.o differ diff --git a/prompt_lib/obj/tab_get_word.o b/prompt_lib/obj/tab_get_word.o new file mode 100644 index 0000000..0081bf4 Binary files /dev/null and b/prompt_lib/obj/tab_get_word.o differ diff --git a/prompt_lib/obj/terminal.o b/prompt_lib/obj/terminal.o new file mode 100644 index 0000000..54083a5 Binary files /dev/null and b/prompt_lib/obj/terminal.o differ diff --git a/prompt_lib/obj/utils.o b/prompt_lib/obj/utils.o new file mode 100644 index 0000000..a1e140b Binary files /dev/null and b/prompt_lib/obj/utils.o differ diff --git a/prompt_lib/prompt.h b/prompt_lib/prompt.h new file mode 100644 index 0000000..9f42b6b --- /dev/null +++ b/prompt_lib/prompt.h @@ -0,0 +1,141 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 14:40:41 by vvobis #+# #+# */ +/* Updated: 2024/12/10 21:19:14 by vvobis ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PROMPT_H +# define PROMPT_H + +# include +# include +# include +# include +# include "../memory/memory.h" +# include +# include "../printf/ft_printf.h" +# include "../libft/libft.h" +# include + +# include + + +# ifdef _WIN32 + +# include +# include + +# define NL "\r\n" + +# undef STDOUT_FILENO +# undef STDIN_FILENO + +# define STDIN_FILENO _fileno(stdin) +# define STDOUT_FILENO _fileno(stdout) + +# else + +# include +# include +# define NL "\n" + +# endif + +# define EOT 4 +# define ESC 27 +# define DEL 127 + +# define SCREEN_DISBLE_WRAPPING "\033[?7l" +# define SCREEN_ENABLE_WRAPPING "\033[?7h" +# define SCREEN_CLEAR_TO_EOL "\033[K" +# define SCREEN_CLEAR "\033[2J" +# define SCREEN_CLEAR_TO_EOF "\033[J" + +# define PROMPT_COMMAND_STACK_SIZE 16 +# define PROMPT_INPUT_BUFFER_SIZE 4096 +# define CURSOR_MOVE_HOME "\033[H" +# define GREEN "\033[0;32m" +# define RESET "\033[0m" +# define RED "\033[0;31m" +# define CURSOR_MOVE_UP "\033[A" +# define CURSOR_MOVE_DOWN "\033[B" +# define CURSOR_MOVE_RIGHT "\033[C" +# define CURSOR_MOVE_LEFT "\033[D" +# define CURSOR_POSITION_GET "\033[6n" +# define CURSOR_POSITION_SET "\033[%d;%dH" +# define CURSOR_POSITION_SAVE "\033[s" +# define CURSOR_POSITION_RESTORE "\033[u" + +# define BUFFER_CAPACITY 64 + +typedef struct s_history_buffer +{ + uint32_t write; + uint32_t read; + uint32_t buffer_capacity; + char *buffer[BUFFER_CAPACITY]; +} t_history_buffer; + +typedef struct s_prompt +{ + bool exists; + char *prompt; + t_history_buffer history; + uint32_t cursor_position[2]; + void (*prompt_display_func)(char *); + uint32_t prompt_length; + char *command; +} t_prompt; + +/* The Main function, all other ones shouldnt be used outside this module */ +/* Returns string of input that has to be freed. Internally, this stores a static prompt struct */ +/* that stores the current state and history of the input buffer */ +char *prompt_get(const char *prompt); +void prompt_destroy(void *prompt); + +int64_t ft_read(int fd, char *character, uint32_t size_to_read); + +t_prompt prompt_create_internal(const char *prompt); +char *prompt_get_input_internal(t_prompt *prompt, uint32_t prompt_initial_size, const char *delimiter); + +uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string); +void prompt_print_custom_string_internal(char *string); + +/* Cursor Manipulation */ +void cursor_position_get(uint32_t cursor_position[2]); +void cursor_position_save(void); +void cursor_position_restore(void); +void cursor_position_set(uint32_t row, uint32_t column); + +/* Prompt Buffer Management */ +bool prompt_handle_escape_sequence_internal(t_prompt *prompt, char buffer[], char **input, uint32_t cursor_position[2]); +bool prompt_handle_new_character_to_input_internal(char **input, char character, uint32_t *cursor_position, uint32_t prompt_length); +uint8_t prompt_handle_single_char_input_internal(char **input, char buffer[], uint32_t cursor_position[2], bool *do_refresh); +bool prompt_handle_multiple_character_internal(char **input, char buffer[], uint32_t cursor_position[2], uint32_t prompt_length); +void prompt_handle_rapid_input_internal(char buffer[], uint32_t cursor_position[2], char **input, uint32_t cursor_position_base); +void prompt_handle_backspace_internal( char *input, uint32_t *cursor_position_current, uint32_t input_length_current); +void prompt_refresh_line_internal(char *input, uint32_t cursor_position_base, uint32_t cursor_position[2]); +char *buffer_size_manage(char **input, uint32_t old_size, uint32_t size_to_add, uint32_t scalar); +void prompt_src_string_insert_internal(char *string_to_insert, char **current_input, char *position_to_insert, uint32_t current_word_length); + +/* Non Blocking mode */ +void blocking_mode_toggle(int fd, int flag); + +/* Tab Completion */ +void prompt_handle_tab_internal(char **input, t_prompt *prompt); +void prompt_handle_tab_no_match_internal(const char *input_path, uint32_t cursor_position_current[2], t_prompt *prompt); +void prompt_handle_tab_yes_match_internal(t_prompt *prompt, const char *next_word_match, char **input, uint32_t current_word_length); +char *prompt_determine_word_internal(char *input, char **input_path, uint32_t cursor_position_current); +void prompt_get_next_word_match_internal(char **input, t_prompt *prompt, char *input_path, bool *is_directory); + +/* Termios */ +void terminal_raw_mode_enable_internal(void); +void terminal_raw_mode_disable_internal(void); + +#endif diff --git a/prompt_lib/src/arrowkeys.c b/prompt_lib/src/arrowkeys.c new file mode 100644 index 0000000..1b9f8f8 --- /dev/null +++ b/prompt_lib/src/arrowkeys.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* arrowkeys.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 11:02:39 by victor #+# #+# */ +/* Updated: 2024/11/11 15:25:23 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +static void prompt_handle_arrow_key_up_internal( t_history_buffer *history, + char **input, + uint32_t cursor_position_current[2]) +{ + if (cursor_position_current[0] > 0) + ft_putstr_fd(CURSOR_MOVE_UP, 1); + if (history->read == 0) + history->read = history->buffer_capacity - 1; + else + history->read -= 1; + if (history->buffer[history->read] == 0) + { + history->read = (history->read + 1) % history->buffer_capacity; + return ; + } + if (history->buffer[history->write] == 0) + history->buffer[history->write] = *input; + *input = history->buffer[history->read]; + cursor_position_current[1] = ft_strlen(*input); +} + +static void prompt_handle_arrow_key_down_internal(t_history_buffer *history, uint32_t cursor_position[2], char **input) +{ + if (history->read == history->write) + return ; + history->read = (history->read + 1) % history->buffer_capacity; + *input = history->buffer[history->read]; + cursor_position[1] = ft_strlen(*input); +} + +static void prompt_handle_arrow_key_right_internal(uint32_t *cursor_position_current, uint32_t prompt_length_current, uint32_t prompt_string_length) +{ + if (cursor_position_current[1] < prompt_length_current) + cursor_position_current[1]++; + cursor_position_set(cursor_position_current[0], cursor_position_current[1] + prompt_string_length); +} + +static void prompt_handle_arrow_key_left_internal( uint32_t *cursor_position_current, uint32_t prompt_string_length) +{ + if (cursor_position_current[1] > 0) + cursor_position_current[1]--; + cursor_position_set(cursor_position_current[0], cursor_position_current[1] + prompt_string_length); +} + +bool prompt_handle_escape_sequence_internal(t_prompt *prompt, char buffer[], char **input, uint32_t cursor_position_current[2]) +{ + uint32_t prompt_length_current; + + prompt_length_current = ft_strlen(*input); + if (buffer[0] == 91 && buffer[1] == 65) + return (prompt_handle_arrow_key_up_internal(&prompt->history, \ + input, cursor_position_current), 1); + else if (buffer[0] == 91 && buffer[1] == 66) + return (prompt_handle_arrow_key_down_internal(&prompt->history, \ + cursor_position_current, input), 1); + else if (buffer[0] == 91 && buffer[1] == 67) + prompt_handle_arrow_key_right_internal(cursor_position_current, prompt_length_current, prompt->prompt_length); + else if (buffer[0] == 91 && buffer[1] == 68) + prompt_handle_arrow_key_left_internal(cursor_position_current, prompt->prompt_length); + return (0); +} diff --git a/prompt_lib/src/escape_sequences.c b/prompt_lib/src/escape_sequences.c new file mode 100644 index 0000000..5a11106 --- /dev/null +++ b/prompt_lib/src/escape_sequences.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* escape_sequences.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 19:29:29 by vvobis #+# #+# */ +/* Updated: 2024/11/11 15:17:42 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +#ifdef _WIN32 + +void cursor_position_get(uint32_t cursor_position[2]) +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO info; + + if (!GetConsoleScreenBufferInfo(hConsole, &info)) + { + ft_printf("Failed to get Cursor Positon\n"); + lst_memory(NULL, NULL, FAIL); + } + else + { + cursor_position[0] = info.dwCursorPosition.Y; + cursor_position[1] = info.dwCursorPosition.X; + } +} + +#else + +void cursor_position_get(uint32_t cursor_position[2]) +{ + char cursor_position_str[32]; + char *cursor_position_str_ptr; + char *cursor_position_str_ptr2; + int bytes_read; + + ft_bzero(&cursor_position_str, 32); + write(1, CURSOR_POSITION_GET, ft_strlen(CURSOR_POSITION_GET)); + bytes_read = read(1, cursor_position_str, 32); + if (bytes_read == -1) + return (perror("read")); + cursor_position_str_ptr = cursor_position_str; + cursor_position_str_ptr2 = ft_strchr(cursor_position_str, ';'); + if (!cursor_position_str_ptr2) + return ; + *cursor_position_str_ptr2 = 0; + while (!(*cursor_position_str_ptr >= '0' + && *cursor_position_str_ptr <= '9')) + cursor_position_str_ptr++; + cursor_position[0] = ft_atoi(cursor_position_str_ptr); + cursor_position_str_ptr = cursor_position_str_ptr2 + 1; + cursor_position_str_ptr2 = ft_strchr(cursor_position_str_ptr, 'R'); + *cursor_position_str_ptr2 = 0; + cursor_position[1] = ft_atoi(cursor_position_str_ptr); +} + +#endif diff --git a/prompt_lib/src/ft_putstr_fd.c b/prompt_lib/src/ft_putstr_fd.c new file mode 100644 index 0000000..889f855 --- /dev/null +++ b/prompt_lib/src/ft_putstr_fd.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 16:15:22 by vvobis #+# #+# */ +/* Updated: 2024/11/11 11:11:34 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#ifdef _WIN32 + +# include +# include + +void ft_putstr_fd(char *buf, int fd) +{ + _write(fd, buf, strlen(buf)); + fflush(stdout); +} + +#else + +#include + +void ft_putstr_fd(char *buf, int fd) +{ + write(fd, buf, strlen(buf)); +} + +#endif diff --git a/prompt_lib/src/ft_read.c b/prompt_lib/src/ft_read.c new file mode 100644 index 0000000..5db7eb7 --- /dev/null +++ b/prompt_lib/src/ft_read.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 18:06:24 by vvobis #+# #+# */ +/* Updated: 2024/11/11 12:22:24 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +#ifdef _WIN32 + +# include +# include + +int64_t ft_read(int fd, char *character, uint32_t size_to_read) +{ + int64_t bytes_read; + char *tmp; + + if (fd == 0) + fd = _fileno(stdin); + else if (fd == 1) + fd = _fileno(stdout); + bytes_read = _read(fd, character, size_to_read); + if (bytes_read == -1) + { + if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) + return (0); + perror("read"); + lst_memory(NULL, NULL, FAIL); + } + if ((tmp = ft_strrchr(character, '\r'))) + { + *tmp = '\n'; + } + return (bytes_read); +} + +// int64_t ft_read(int fd, char *character, uint32_t size_to_read) +// { +// int64_t bytes_read; +// char *tmp; +// +// if (fd == 0) +// fd = _fileno(stdin); +// else if (fd == 1) +// fd = _fileno(stdout); +// bytes_read = _read(fd, character, size_to_read); +// if (bytes_read > 0) +// { +// character[bytes_read] = '\0'; +// tmp = ft_strrchr(character, '\r'); +// if (tmp && (tmp - character < bytes_read - 1) && *(tmp + 1) == '\n') +// { +// ft_memmove(tmp, tmp + 1, bytes_read - (tmp - character) - 1); +// bytes_read--; +// } +// } +// if (bytes_read == -1) +// { +// if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) +// return (0); +// perror("read"); +// lst_memory(NULL, NULL, FAIL); +// } +// return (bytes_read); +// } +// + +# else + +# include + +int64_t ft_read(int fd, char *character, uint32_t size_to_read) +{ + int64_t bytes_read; + + bytes_read = read(fd, character, size_to_read); + if (bytes_read == -1) + { + if (errno == EINTR \ + || errno == EAGAIN \ + || errno == EWOULDBLOCK) + return (0); + perror("read"); + lst_memory(NULL, NULL, FAIL); + } + return (bytes_read); +} + +#endif diff --git a/prompt_lib/src/non_blocking_mode.c b/prompt_lib/src/non_blocking_mode.c new file mode 100644 index 0000000..575c780 --- /dev/null +++ b/prompt_lib/src/non_blocking_mode.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* non_blocking_mode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 17:14:46 by vvobis #+# #+# */ +/* Updated: 2024/11/10 21:21:20 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +#ifdef _WIN32 + +#include + +void blocking_mode_toggle(int fd, int enable) +{ + HANDLE hStd; + DWORD mode; + + if (fd == 0) + { + hStd = GetStdHandle(STD_INPUT_HANDLE); + } + else if (fd == 1) + { + hStd = GetStdHandle(STD_OUTPUT_HANDLE); + } + else + { + return ; + } + if (hStd == INVALID_HANDLE_VALUE) + { + fprintf(stderr, "Error getting standard input handle." NL); + exit(1); + } + + if (!GetConsoleMode(hStd, &mode)) + { + fprintf(stderr, "Error getting console mode." NL); + exit(1); + } + if (enable) + { + mode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); + } + else + { + mode |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); + } + + if (!SetConsoleMode(hStd, mode)) + { + fprintf(stderr, "Error setting console mode." NL); + exit(1); + } +} + +#else + +# include + +void blocking_mode_toggle(int fd, int flag) +{ + if (ioctl(fd, FIONBIO, &flag) == -1) + { + perror("ioctl"); + exit(1); + } +} + +#endif diff --git a/prompt_lib/src/prompt_handle_chars.c b/prompt_lib/src/prompt_handle_chars.c new file mode 100644 index 0000000..ab26fb8 --- /dev/null +++ b/prompt_lib/src/prompt_handle_chars.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt_handle_chars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 17:11:28 by vvobis #+# #+# */ +/* Updated: 2024/11/11 13:13:36 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +uint8_t prompt_handle_single_char_input_internal( char **input, char buffer[], uint32_t cursor_position_current[2], bool *do_refresh) +{ + uint32_t input_length_current; + uint32_t new_buffer_length; + + input_length_current = ft_strlen(*input); + new_buffer_length = ft_strlen(buffer); + if (new_buffer_length == 1) + { + if (ft_isprint(buffer[0]) || buffer[0] == '\n') + return (*do_refresh = prompt_handle_new_character_to_input_internal(input, buffer[0], + cursor_position_current, input_length_current), 1); + else if (buffer[0] == EOT \ + && (input_length_current == 0 \ + || (*input)[input_length_current - 1] == '\n')) + return (ft_putstr_fd(NL, STDIN_FILENO), \ + terminal_raw_mode_disable_internal(), \ + 1); + } + else + { + *do_refresh = prompt_handle_multiple_character_internal(input, buffer, \ + cursor_position_current, input_length_current); + return (1); + } + return (0); +} + +bool prompt_handle_new_character_to_input_internal(char **input, char character, uint32_t *cursor_position_current, uint32_t prompt_length_current) +{ + bool do_refresh; + + do_refresh = false; + *input = buffer_size_manage(input, prompt_length_current, prompt_length_current + 1, PROMPT_INPUT_BUFFER_SIZE); + if (cursor_position_current[1] < prompt_length_current) + { + ft_memmove(&(*input)[cursor_position_current[1] + 1], &(*input)[cursor_position_current[1]], \ + prompt_length_current - cursor_position_current[1]); + } + (*input)[cursor_position_current[1]] = character; + cursor_position_current[1]++; + do_refresh = true; + return (do_refresh); +} + +bool prompt_handle_multiple_character_internal(char **input, char buffer[], uint32_t cursor_position_current[2], uint32_t prompt_length_current) +{ + bool do_refresh; + uint32_t buffer_length; + + do_refresh = false; + buffer_length = ft_strlen(buffer); + buffer_size_manage(input, prompt_length_current, prompt_length_current + buffer_length + 1, PROMPT_INPUT_BUFFER_SIZE); + if (cursor_position_current[1] < prompt_length_current - 1) + { + ft_memmove(&(*input)[cursor_position_current[1] + buffer_length], &(*input)[cursor_position_current[1]], \ + prompt_length_current - cursor_position_current[1] + buffer_length); + do_refresh = true; + } + ft_memcpy(&(*input)[cursor_position_current[1]], buffer, buffer_length); + (cursor_position_current[1]) += buffer_length; + return (do_refresh); +} + +void prompt_handle_backspace_internal(char *input, uint32_t *cursor_position_current, uint32_t input_length_current) +{ + if (cursor_position_current[1] <= 0) + return ; + cursor_position_current[1]--; + ft_memmove(&input[cursor_position_current[1]], &input[cursor_position_current[1] + 1], input_length_current - cursor_position_current[1]); + ft_putstr_fd(CURSOR_MOVE_LEFT, STDOUT_FILENO); +} + +void prompt_handle_rapid_input_internal(char buffer[], uint32_t cursor_position[2], char **input, uint32_t cursor_position_base) +{ + int32_t bytes_read; + bool do_refresh; + + blocking_mode_toggle(STDIN_FILENO, 1); + bytes_read = 1; + do_refresh = true; + while (bytes_read > 0) + { + prompt_handle_multiple_character_internal(input, buffer, cursor_position, ft_strlen(*input)); + ft_bzero(buffer, 100); + bytes_read = ft_read(STDIN_FILENO, buffer, 99); + } + blocking_mode_toggle(STDIN_FILENO, 1); + if (do_refresh) + prompt_refresh_line_internal(*input, cursor_position_base, cursor_position); +} diff --git a/prompt_lib/src/prompt_input.c b/prompt_lib/src/prompt_input.c new file mode 100644 index 0000000..e3725ba --- /dev/null +++ b/prompt_lib/src/prompt_input.c @@ -0,0 +1,127 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt_input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 19:40:20 by vvobis #+# #+# */ +/* Updated: 2024/11/11 14:10:50 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +static uint8_t prompt_handle_accepted_input_internal(t_prompt *prompt, uint32_t cursor_position[2], char **input, char buffer[]) +{ + bool do_refresh; + + do_refresh = true; + switch (buffer[0]) + { + case ESC: + do_refresh = prompt_handle_escape_sequence_internal(prompt, &buffer[1], input, cursor_position); + break ; + case '\t': + prompt_handle_tab_internal(input, prompt); + break ; + case DEL: + prompt_handle_backspace_internal(*input, cursor_position, \ + ft_strlen(*input)); + break ; + case 3: + return (1); + break ; + case 4: + return (1); + break ; + case '\n': + prompt_handle_new_character_to_input_internal(input, buffer[0], cursor_position, ft_strlen(*input)); + prompt->prompt_display_func(prompt->prompt); + cursor_position[0]++; + break ; + default: + prompt_handle_single_char_input_internal(input, buffer, cursor_position, &do_refresh); + break ; + } + if (do_refresh == true) + prompt_refresh_line_internal(*input, prompt->prompt_length, cursor_position); + return (0); +} + +static bool prompt_is_delimiter(char *input, const char *delimiter) +{ + char *tmp; + uint32_t delimiter_length; + + if (*delimiter == '\n') + return (true); + delimiter_length = ft_strlen(delimiter); + tmp = ft_strrchr(input - (*input == '\n' && ft_strlen(input) > 0), '\n'); + if (tmp) + if (tmp++ && ((*tmp == *delimiter && delimiter_length == 1) \ + || ft_strncmp(tmp, delimiter, delimiter_length) == 0)) + return (*tmp = 0, true); + if ((*input == *delimiter && delimiter_length == 1) \ + || ft_strncmp(input, delimiter, delimiter_length) == 0) + return (*input = 0, true); + return (false); +} + +static char *prompt_handle_input(t_prompt *prompt, char *input, uint32_t cursor_position[2], const char *delimiter) +{ + char buffer[100]; + int64_t bytes_read; + + bytes_read = sizeof(buffer); + while (1) + { + ft_bzero(buffer, bytes_read); + bytes_read = ft_read(STDIN_FILENO, buffer, 20); + if (bytes_read > 3) + prompt_handle_rapid_input_internal(buffer, cursor_position, &input, prompt->prompt_length); + else if (bytes_read >= 1) + { + if (buffer[bytes_read - (bytes_read > 0)] == '\n') + if (prompt_is_delimiter(input, delimiter)) + break ; + if (prompt_handle_accepted_input_internal(prompt, cursor_position, &input, buffer) == 1) + break ; + } + } + return (input); +} + +static void prompt_handle_history(t_history_buffer *buffer, char *input) +{ + buffer->buffer[buffer->write++] = input; + buffer->write %= buffer->buffer_capacity; + buffer->buffer[buffer->write] = 0; + buffer->read = buffer->write; +} + +char *prompt_get_input_internal( t_prompt *prompt, uint32_t prompt_initial_size, const char *delimiter) +{ + char *input; + + input = ft_calloc(prompt_initial_size, sizeof(*input)); + if (!input) + return (perror("malloc"), NULL); + lst_memory(input, free, ADD); + terminal_raw_mode_enable_internal(); + prompt->prompt_display_func(prompt->prompt); + cursor_position_get(prompt->cursor_position); + prompt->cursor_position[1] = 0; + if (!delimiter) + delimiter = "\n"; + input = prompt_handle_input(prompt, input, prompt->cursor_position, delimiter); + if (!input) + { + ft_printf("prompt_handle_input returned NULL pointer" NL); + return (NULL); + } + prompt_handle_history(&prompt->history, input); + terminal_raw_mode_disable_internal(); + prompt->command = input; + return (input); +} diff --git a/prompt_lib/src/prompt_print.c b/prompt_lib/src/prompt_print.c new file mode 100644 index 0000000..ba31961 --- /dev/null +++ b/prompt_lib/src/prompt_print.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 16:11:25 by vvobis #+# #+# */ +/* Updated: 2024/11/11 11:40:31 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +void prompt_print_custom_string_internal(char *string) +{ + ft_putstr_fd(string, STDOUT_FILENO); +} diff --git a/prompt_lib/src/prompt_string_management.c b/prompt_lib/src/prompt_string_management.c new file mode 100644 index 0000000..16e55e9 --- /dev/null +++ b/prompt_lib/src/prompt_string_management.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt_string_management.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/09 08:16:45 by victor #+# #+# */ +/* Updated: 2024/11/11 15:23:36 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +void cursor_position_save(void) +{ + ft_putstr_fd(CURSOR_POSITION_SAVE, STDOUT_FILENO); +} + +void cursor_position_restore(void) +{ + ft_putstr_fd(CURSOR_POSITION_RESTORE, STDOUT_FILENO); +} + +#ifdef _WIN32 + +void cursor_position_set(uint32_t row, uint32_t column) +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + + SetConsoleCursorPosition(hConsole, (COORD){.Y = row, .X = column}); +} + +#else + +void cursor_position_set(uint32_t row, uint32_t column) +{ + ft_printf(CURSOR_POSITION_SET, row, column); +} + +#endif + +void prompt_refresh_line_internal(char *input, uint32_t cursor_position_base, uint32_t cursor_position_current[2]) +{ + ft_putstr_fd(SCREEN_CLEAR_TO_EOF, STDOUT_FILENO); + cursor_position_set(cursor_position_current[0], cursor_position_base + cursor_position_current[1] - (cursor_position_current[1] > 0)); + ft_putstr_fd(&input[cursor_position_current[1] - (cursor_position_current[1] > 0)], STDOUT_FILENO); + cursor_position_set(cursor_position_current[0], cursor_position_current[1] + cursor_position_base); +} + +char *buffer_size_manage(char **input, uint32_t old_size, uint32_t size_to_add, uint32_t scalar) +{ + char *input_free_ptr; + uint32_t size_multiplier; + uint32_t new_size; + + if (scalar == 0) + return (*input); + if ((old_size + size_to_add) > (scalar * ((old_size / scalar) + 1))) + { + new_size = old_size + size_to_add; + new_size += new_size % scalar; + size_multiplier = (old_size / scalar) + 2; + input_free_ptr = *input; + *input = ft_calloc(1, new_size * size_multiplier + 1); + if (!*input) + return (perror("malloc"), lst_memory(NULL, NULL, FAIL), NULL); + ft_memcpy(*input, input_free_ptr, old_size); + lst_memory(input_free_ptr, NULL, FREE); + lst_memory(*input, free, ADD); + } + return (*input); +} diff --git a/prompt_lib/src/prompt_utils.c b/prompt_lib/src/prompt_utils.c new file mode 100644 index 0000000..7aa1b84 --- /dev/null +++ b/prompt_lib/src/prompt_utils.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 19:23:40 by anarama #+# #+# */ +/* Updated: 2024/11/11 15:20:32 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +void prompt_destroy(void *prompt) +{ + t_prompt *prompt_ptr; + + prompt_ptr = (t_prompt *)prompt; + ft_free(&prompt_ptr); +} + +#ifdef _WIN32 +uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string) +{ + uint32_t prompt_string_length; + + prompt_string_length = ft_strlen(prompt_string); + prompt->prompt = (char *)prompt_string; + return (prompt_string_length); +} + +#else + +uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string) +{ + uint32_t prompt_string_length; + + prompt_string_length = ft_strlen(prompt_string) + 1; + prompt->prompt = (char *)prompt_string; + return (prompt_string_length); +} + +#endif +t_prompt prompt_create_internal(char const *prompt) +{ + t_prompt tmp; + + tmp = (t_prompt){0}; + tmp.history = (t_history_buffer){0}; + tmp.history.buffer_capacity = BUFFER_CAPACITY; + tmp.exists = true; + if (!prompt) + tmp.prompt = "> "; + tmp.prompt_display_func = prompt_print_custom_string_internal; + return (tmp); +} + +char *prompt_get(const char *prompt) +{ + char *input; + static t_prompt _prompt = {0}; + + if (!_prompt.exists) + _prompt = prompt_create_internal(prompt); + _prompt.prompt = NULL; + lst_memory(&_prompt, prompt_destroy, ADD); + _prompt.prompt_length = prompt_display_string_set_internal(&_prompt, prompt); + prompt_get_input_internal(&_prompt, PROMPT_INPUT_BUFFER_SIZE, "\n"); + if (!_prompt.command || !*_prompt.command) + return (NULL); + ft_putstr_fd(SCREEN_CLEAR_TO_EOF, 1); + input = ft_strdup(_prompt.command); + if (!input) + return (perror("malloc"), lst_memory(NULL, NULL, FAIL), NULL); + return (input); +} + diff --git a/prompt_lib/src/tab_completion.c b/prompt_lib/src/tab_completion.c new file mode 100644 index 0000000..b138dae --- /dev/null +++ b/prompt_lib/src/tab_completion.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tab_completion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: anarama +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 14:53:58 by victor #+# #+# */ +/* Updated: 2024/11/10 22:16:03 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +void prompt_handle_tab_no_match_internal(const char *input_path, uint32_t cursor_position_current[2], t_prompt *prompt) +{ + (void)input_path, (void)cursor_position_current, (void)prompt; + cursor_position_save(); + ft_putstr_fd(SCREEN_CLEAR_TO_EOF, 0); + ft_putstr_fd(NL, 1); + cursor_position_restore(); +} + +void prompt_handle_tab_yes_match_internal(t_prompt *prompt, const char *next_word_match, char **input, uint32_t current_word_length) +{ + prompt_handle_multiple_character_internal(input, (char *)(next_word_match + current_word_length), \ + prompt->cursor_position, ft_strlen(*input)); + cursor_position_save(); + ft_putstr_fd(NL, 1); + ft_putstr_fd(SCREEN_CLEAR_TO_EOF, 0); + cursor_position_restore(); +} + +char *determine_word_internal(char *input, char **input_path, uint32_t cursor_position_current) +{ + char *current_word; + char *current_word_path_end; + char *tmp; + uint32_t i; + + i = cursor_position_current -(cursor_position_current > 0); + current_word_path_end = NULL; + while(i > 0 && input[i] != ' ') + i--; + current_word = &input[i +(i > 0)]; + tmp = ft_strchr(current_word, '/'); + while(tmp) + { + current_word_path_end = tmp; + tmp = ft_strchr(tmp + 1, '/'); + } + if(current_word_path_end) + { + *input_path = ft_substr(current_word, 0, \ + current_word_path_end - current_word + 1); + current_word = current_word_path_end + 1; + return(current_word); + } + return(current_word); +} + +uint32_t find_last_matching_char_internal(const char *current_word, const char *next_word_match) +{ + uint32_t i; + char *current_word_without_path; + + if (!next_word_match) + return(0); + current_word_without_path = ft_strrchr(current_word, '/'); + i = 0; + if (current_word_without_path) + while((current_word_without_path)[i] == next_word_match[i]) + i++; + else + while(current_word[i] == next_word_match[i]) + i++; + return(i); +} + +void prompt_handle_tab_internal(char **input, t_prompt *prompt) +{ + char *input_path; + bool is_directory; + + if(!(*input)[(prompt->cursor_position)[1] - ((prompt->cursor_position)[1] > 0)]) + { return(prompt_handle_tab_no_match_internal(".", prompt->cursor_position, prompt)); + } + input_path = NULL; + is_directory = false; + prompt_get_next_word_match_internal(input, prompt, input_path, &is_directory); +} diff --git a/prompt_lib/src/tab_get_word.c b/prompt_lib/src/tab_get_word.c new file mode 100644 index 0000000..3be1d1c --- /dev/null +++ b/prompt_lib/src/tab_get_word.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tab_get_word.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvobis +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 17:00:19 by vvobis #+# #+# */ +/* Updated: 2024/11/10 21:29:23 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +char *find_next_match( char *current_word, \ + uint32_t current_word_length) +{ + (void)current_word_length, (void)current_word; + ft_printf("UNIMPLEMENTED: %s %s" NL, __FILE__, __LINE__); + return (NULL); +} + +uint32_t get_current_word_length(char *word) +{ + char *word_end; + + if (!word) + return (0); + else if (*word == ' ') + return (1); + word_end = ft_strchr(word, ' '); + if (word_end) + return (word_end - word); + return (ft_strlen(word)); +} + +void prompt_get_next_word_match_internal(char **input, t_prompt *prompt, char *input_path, bool *is_directory) +{ + (void)input, (void)prompt,(void)input_path, (void)is_directory; + ft_printf("UNIMPLEMENTED: %s %s" NL, __FILE__, __LINE__); +} diff --git a/prompt_lib/src/terminal.c b/prompt_lib/src/terminal.c new file mode 100644 index 0000000..1fe32be --- /dev/null +++ b/prompt_lib/src/terminal.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* terminal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marvin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/10 21:56:10 by marvin #+# #+# */ +/* Updated: 2024/11/11 14:26:30 by victor ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../prompt.h" + +#ifdef _WIN32 + +void terminal_raw_mode_enable_internal(void) +{ + HANDLE hStd = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + + GetConsoleMode(hStd, &mode); + mode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); + mode |= ENABLE_VIRTUAL_TERMINAL_INPUT; + SetConsoleMode(hStd, mode); + _setmode(_fileno(stdin), _O_RAW); +} + +void terminal_raw_mode_disable_internal(void) +{ + HANDLE hStd = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + + GetConsoleMode(hStd, &mode); + mode |= (ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); + mode &= ~(ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT); + SetConsoleMode(hStd, mode); + _setmode(_fileno(stdin), _O_TEXT); +} + +#else + +void terminal_raw_mode_enable_internal(void) +{ + struct termios raw; + + if (isatty(0) && isatty(1)) + { + tcgetattr(STDIN_FILENO, &raw); + raw.c_lflag &= ~(ECHOCTL | ICANON); + tcsetattr(STDIN_FILENO, TCSANOW, &raw); + } +} + +void terminal_raw_mode_disable_internal(void) +{ + struct termios orig; + + if (isatty(0) && isatty(1)) + { + tcgetattr(STDIN_FILENO, &orig); + orig.c_lflag |= (ECHO | ECHOCTL | ICANON); + tcsetattr(STDIN_FILENO, TCSANOW, &orig); + } +} + +#endif diff --git a/prompt_lib/src/utils.c b/prompt_lib/src/utils.c new file mode 100644 index 0000000..d486592 --- /dev/null +++ b/prompt_lib/src/utils.c @@ -0,0 +1,213 @@ + +#include "../prompt.h" + +int is_space(char const c) +{ + if ((c >= 9 && c <= 13) || c == ' ') + return (1); + return (0); +} + +int ft_atoi(char const *s) +{ + int nb; + char const *tmp; + + nb = 0; + while (is_space(*s)) + s++; + tmp = s; + if (*tmp == '+' || *tmp == '-') + tmp++; + while (*tmp >= '0' && *tmp <= '9') + { + nb *= 10; + nb += (*tmp - '0'); + tmp++; + } + if (*s == '-') + nb = -nb; + return (nb); +} + +size_t ft_strlen(char const *str) +{ + size_t i; + + i = 0; + if (!str) + return (0); + while (*str++) + { + i++; + if (i == SIZE_MAX) + break ; + } + return (i); +} + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} + +void ft_bzero(void *s, size_t n) +{ + while (n--) + *(char *)s++ = 0; +} + +void *ft_memcpy(void *dest, void const *src, size_t n) +{ + char *d; + char *s; + + if (n == 0 || (dest == NULL && src == NULL)) + return (dest); + if (dest == NULL || src == NULL) + { + *(char *)dest = 1; + *(char *)src = 1; + } + d = (char *) dest; + s = (char *) src; + while (n--) + *d++ = *s++; + return (dest); +} + +void *ft_memmove(void *dest, void const *src, size_t n) +{ + unsigned char *d; + unsigned char *s; + + if (!dest && !src) + return (dest); + d = (unsigned char *)dest; + s = (unsigned char *)src; + if (s < d) + while (n--) + d[n] = s[n]; + else + ft_memcpy(dest, (void *)src, n); + return (dest); +} + +char *ft_strchr(char const *s, int c) +{ + int i; + + i = 0; + while (s[i]) + { + if (s[i] == (char)c) + return ((char *)&s[i]); + i++; + } + if ((char)c == 0) + return ((char *)&s[i]); + return (NULL); +} + +char *ft_strrchr(char const *s, int c) +{ + char *n; + int i; + + i = 0; + n = NULL; + while (s[i] != 0) + { + if (s[i] == (char)c) + n = (char *)&s[i]; + i++; + } + if ((char)c == 0) + return ((char *)&s[i]); + if (n) + return (n); + return (NULL); +} + +void *ft_calloc(size_t n, size_t s) +{ + char *tmp; + unsigned long i; + + i = 0; + if (n == 0) + return (malloc(0)); + if (SIZE_MAX / n < s) + return (NULL); + tmp = malloc(n * s); + if (tmp) + while (i < n * s) + tmp[i++] = 0; + return ((void *)tmp); +} + +int ft_strncmp(char const *s1, char const *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while ((s1[i] || s2[i]) && i < n) + { + if (s1[i] < s2[i]) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + else if (s1[i] > s2[i]) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + i++; + } + return (0); +} + +char *ft_strdup(char const *s) +{ + char *tmp; + int i; + + i = 0; + tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp)); + if (!tmp) + return (NULL); + while (s[i]) + { + tmp[i] = s[i]; + i++; + } + tmp[i] = 0; + return (tmp); +} + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *tmp; + unsigned int i; + + i = 0; + if (!s || start >= ft_strlen(s) || len <= 0) + { + tmp = malloc(1); + if (!tmp) + return (NULL); + tmp[i] = 0; + return (tmp); + } + if (len + start > ft_strlen(s)) + len = ft_strlen(s) - start; + tmp = malloc(sizeof(*tmp) * len + 1); + if (!tmp) + return (NULL); + while (i < len && s[i]) + { + tmp[i] = s[i + start]; + i++; + } + tmp[i] = 0; + return (tmp); +}