diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7157b26 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +# **************************************************************************** # +# # +# ::: o_ :::::: ::: # +# Makefile :+: / :+::+: :+: # +# +:+ > +:++:+ +:+ # +# By: whaffman +#+ +:+ +#++#++:++#++ # +# +#+ +#+#+ +#++#+ +#+ \o/ # +# Created: 2024/10/15 11:48:46 by whaffman #+#+# #+#+# #+# #+# | # +# Updated: 2024/10/15 12:40:48 by whaffman ### ### ### ### / \ # +# # +# **************************************************************************** # + +NAME = libftprintf.a + +SOURCES = ft_printf.c + +OBJECTS = $(SOURCES:.c=.o) + +CC = gcc + +CFLAGS = -Wall -Wextra -Werror + +AR = AR + +all: $(NAME) libft.a + +libft.a: + $(MAKE) -C libft + $(MAKE) -C libft clean + mkdir lib + mkdir inc + cp libft/libft.h inc/libft.h + mv libft/libft.a lib/libft.a + +$(NAME): $(OBJECTS) + $(AR) -r $@ $? + +%.o: %.c + $(CC) -I./inc -L./lib -lft -c $(CFLAGS) $? + +clean: + rm -f $(OBJECTS) + $(MAKE) -C libft clean + +fclean: clean + rm -f $(NAME) + rm -rf lib + $(MAKE) -C libft fclean + +re: fclean all + +.PHONY: all bonus clean fclean re diff --git a/README.md b/README.md index ea98149..d245707 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Using the libtool command is forbidden. • Your libftprintf.a has to be created at the root of your repository. 5 +```%[$][flags][width][.precision][length modifier]conversion``` + You have to implement the following conversions: • %c Prints a single character. • %s Prints a string (as defined by the common C convention). @@ -44,7 +46,22 @@ You have to implement the following conversions: ## Bonus • Manage any combination of the following flags: ’-0.’ and the field minimum width under all conversions. + - '-' The converted value is to be left adjusted on the field + boundary. (The default is right justification.) The + converted value is padded on the right with blanks + - '0' The value should be zero padded. For d, i, o, u, x, X, a, + A, e, E, f, F, g, and G conversions, the converted value + is padded on the left with zeros rather than blanks. + + - '.' precision + • Manage all the following flags: ’# +’ (Yes, one of them is a space) + - '#' For x and X conversions, a nonzero result has the string + "0x" (or "0X" for X conversions) prepended to it. + - ' ' (a space) A blank should be left before a positive number + (or empty string) produced by a signed conversion. + - '+' A sign (+ or -) should always be placed before a number + produced by a signed conversion. If you plan to complete the bonus part, think about the implementation of your extra features from the start. This way, diff --git a/ft_printf.c b/ft_printf.c new file mode 100644 index 0000000..72878c0 --- /dev/null +++ b/ft_printf.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_printf.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/11 14:49:07 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/15 12:43:16 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +int ft_printf(const char *str, ...) +{ + return (0); +} diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..d100be4 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,54 @@ +# **************************************************************************** # +# # +# ::: o_ :::::: ::: # +# Makefile :+: / :+::+: :+: # +# +:+ > +:++:+ +:+ # +# By: whaffman +#+ +:+ +#++#++:++#++ # +# +#+ +#+#+ +#++#+ +#+ \o/ # +# Created: 2024/10/10 16:44:36 by whaffman #+#+# #+#+# #+# #+# | # +# Updated: 2024/10/10 18:00:05 by whaffman ### ### ### ### / \ # +# # +# **************************************************************************** # + +NAME = libft.a + +SOURCES = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c \ + ft_isascii.c ft_isdigit.c ft_isprint.c ft_itoa.c ft_memchr.c \ + ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_putchar_fd.c \ + ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c ft_split.c ft_strchr.c \ + ft_strdup.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlcpy.c \ + ft_strlen.c ft_strmapi.c ft_strncmp.c ft_strnstr.c ft_strrchr.c \ + ft_strtrim.c ft_substr.c ft_tolower.c ft_toupper.c + +BONUS_SOURCES = ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c \ + ft_lstdelone.c ft_lstiter.c ft_lstlast.c ft_lstmap.c \ + ft_lstnew.c ft_lstsize.c + +OBJECTS = $(SOURCES:.c=.o) + +BONUS_OBJECTS = $(BONUS_SOURCES:.c=.o) + +CC = gcc +CFLAGS = -Wall -Wextra -Werror + +all: $(NAME) + +$(NAME): $(OBJECTS) + $(AR) -r $@ $? + +bonus: $(OBJECTS) $(BONUS_OBJECTS) + $(AR) -r $(NAME) $? + +%.o: %.c + $(CC) -c $(CFLAGS) $? + +clean: + rm -f $(OBJECTS) $(BONUS_OBJECTS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all bonus clean fclean re + diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..14d9ecc --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_atoi.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:13 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:13 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isspace(int c) +{ + return (' ' == c || '\f' == c || \ + '\n' == c || '\r' == c || \ + '\t' == c || '\v' == c); +} + +int ft_atoi(const char *nptr) +{ + int sign; + int res; + + while (ft_isspace(*nptr)) + nptr++; + res = 0; + sign = 1; + if (*nptr == '-' || *nptr == '+') + { + if (*nptr == '-') + sign *= -1; + nptr++; + } + while (ft_isdigit(*nptr)) + res = 10 * res + sign * (*nptr++ - '0'); + return (res); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..d65233d --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_bzero.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:14 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:14 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c new file mode 100644 index 0000000..dc8cd85 --- /dev/null +++ b/libft/ft_calloc.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_calloc.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:14 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:14 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + if (nmemb && nmemb > (size_t) -1 / nmemb) + { + return (0); + } + ptr = malloc(nmemb * size); + if (ptr) + ft_bzero(ptr, nmemb * size); + return (ptr); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..7439e0e --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_isalnum.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:15 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:15 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isdigit(c) || ft_isalpha(c)); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..1d35b48 --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_isalpha.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:15 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:15 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return (('A' <= c && 'Z' >= c) || ('a' <= c && 'z' >= c)); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..25cc440 --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_isascii.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:16 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:16 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (0 <= c && 127 >= c); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..f1d4fb8 --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_isdigit.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:16 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:16 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return ('0' <= c && '9' >= c); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..b7cc59f --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_isprint.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:17 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:17 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (32 <= c && 126 >= c); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..71d9d07 --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_itoa.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:17 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:17 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static char *itoa_stralloc(int n) +{ + int i; + + i = 0; + if (n <= 0) + i++; + while (n) + { + i++; + n /= 10; + } + return (ft_calloc((i + 1), sizeof(char))); +} + +char *ft_itoa(int n) +{ + int i; + long ln; + long divider; + char *result; + + result = itoa_stralloc(n); + if (!result) + return (NULL); + ln = n; + i = 0; + divider = 1; + if (ln < 0) + { + ln *= -1; + result[i++] = '-'; + } + while (divider * 10 <= ln) + divider *= 10; + while (divider) + { + result[i++] = ln / divider + '0'; + ln %= divider; + divider /= 10; + } + return (result); +} diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c new file mode 100644 index 0000000..d082af3 --- /dev/null +++ b/libft/ft_lstadd_back.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstadd_back.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:18 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:18 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *last; + + if (!new) + return ; + if (!*lst) + { + *lst = new; + return ; + } + last = ft_lstlast(*lst); + last->next = new; +} diff --git a/libft/ft_lstadd_front.c b/libft/ft_lstadd_front.c new file mode 100644 index 0000000..c6b3b8a --- /dev/null +++ b/libft/ft_lstadd_front.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstadd_front.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:18 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:18 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (new) + { + new->next = *lst; + *lst = new; + } +} diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c new file mode 100644 index 0000000..703f25f --- /dev/null +++ b/libft/ft_lstclear.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstclear.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:19 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:19 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *next; + + if (!del || !lst) + return ; + while (*lst) + { + if ((*lst)->next) + next = (*lst)->next; + else + next = NULL; + del((*lst)->content); + free(*lst); + *lst = next; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100644 index 0000000..4a43b09 --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstdelone.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:19 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:19 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#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.c b/libft/ft_lstiter.c new file mode 100644 index 0000000..c13c11a --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstiter.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:19 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:19 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#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.c b/libft/ft_lstlast.c new file mode 100644 index 0000000..b63451f --- /dev/null +++ b/libft/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstlast.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:20 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:20 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#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.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..1d5ecf5 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstmap.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:20 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:20 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_list; + t_list *new_elem; + + if (!lst || !f) + return (NULL); + new_list = NULL; + while (lst) + { + new_elem = ft_lstnew(f(lst->content)); + if (!new_elem) + { + ft_lstclear(&new_list, del); + return (NULL); + } + ft_lstadd_back(&new_list, new_elem); + lst = lst->next; + } + return (new_list); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100644 index 0000000..586f273 --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstnew.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:21 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:21 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *node; + + node = malloc(sizeof(t_list)); + if (node) + { + node->content = content; + node->next = NULL; + } + return (node); +} diff --git a/libft/ft_lstsize.c b/libft/ft_lstsize.c new file mode 100644 index 0000000..0d19b96 --- /dev/null +++ b/libft/ft_lstsize.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_lstsize.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:21 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:21 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + + count = 1; + if (!lst) + return (0); + while (lst->next) + { + lst = lst->next; + count++; + } + return (count); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..b9d8b3c --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_memchr.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:22 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:22 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + while (0 < n--) + { + if (*(unsigned char *)s == (unsigned char) c) + return ((void *) s); + s++; + } + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..0c138ee --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_memcmp.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:22 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:22 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *str1; + unsigned char *str2; + size_t i; + + str1 = (unsigned char *)s1; + str2 = (unsigned char *)s2; + i = 0; + while (i < n) + { + if (str1[i] != str2[i]) + return (str1[i] - str2[i]); + i++; + } + return (0); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..63742b0 --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_memcpy.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:23 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:23 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + if (dest == NULL && src == NULL) + return (NULL); + while (0 < n--) + ((unsigned char *)dest)[n] = ((unsigned char *)src)[n]; + return (dest); +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..86a2395 --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_memmove.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:23 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:23 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned char *dest_ptr; + unsigned char *src_ptr; + + dest_ptr = (unsigned char *)dest; + src_ptr = (unsigned char *)src; + if (dest_ptr == src_ptr) + return (dest); + else if (dest_ptr < src_ptr) + { + while (n-- > 0) + *dest_ptr++ = *src_ptr++; + } + else + { + while (n-- > 0) + dest_ptr[n] = src_ptr[n]; + } + return (dest); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..9cd0b10 --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_memset.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:24 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:24 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + while (0 < n--) + ((unsigned char *)s)[n] = (unsigned char) c; + return (s); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100644 index 0000000..3c46a60 --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_putchar_fd.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:24 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:24 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + if (fd < 0) + return ; + write(fd, &c, 1); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..1eb1d8f --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_putendl_fd.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:25 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:25 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (fd < 0 || s == NULL) + return ; + ft_putstr_fd(s, fd); + write(fd, "\n", 1); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..7069b7d --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_putnbr_fd.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:25 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:25 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + long ln; + long divider; + char c; + + if (fd < 0) + return ; + ln = n; + divider = 1; + if (n < 0) + { + if (!write(fd, "-", 1)) + return ; + ln *= -1; + } + while (divider * 10 <= ln) + divider *= 10; + while (divider) + { + c = ln / divider + '0'; + if (!write(fd, &c, 1)) + return ; + ln %= divider; + divider /= 10; + } +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..eda4cf8 --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_putstr_fd.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:26 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:26 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + size_t length; + + if (s == NULL || fd < 0) + return ; + length = ft_strlen(s); + write(fd, s, length); +} diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100644 index 0000000..288fba7 --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_split.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:26 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:26 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static int count_words(char const *s, char c) +{ + int n; + int i; + + n = 0; + i = 0; + while (s[i]) + { + if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) + n++; + i++; + } + return (n); +} + +char **ft_split(char const *s, char c) +{ + char **result; + int n; + int i; + int j; + + if (!s) + return (NULL); + result = malloc((count_words(s, c) + 1) * sizeof(char *)); + if (!result) + return (NULL); + i = 0; + n = 0; + while (s[i]) + { + j = 0; + while (s[i] == c) + i++; + while (s[i + j] && s[i + j] != c) + j++; + if (j) + result[n++] = ft_substr(s, i, j); + i += j; + } + result[n] = NULL; + return (result); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..b57b931 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strchr.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:27 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:27 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s) + { + if (*(unsigned char *) s == (unsigned char) c) + return ((char *) s); + s++; + } + if (*(unsigned char *) s == (unsigned char) c) + return ((char *) s); + return (NULL); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100644 index 0000000..d589159 --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strdup.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:27 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:27 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strdup(const char *s) +{ + size_t size; + char *d; + + size = ft_strlen(s) + 1; + d = (char *) malloc(size * sizeof(char)); + if (d) + ft_strlcpy(d, s, size); + return (d); +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..c335a44 --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_striteri.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:28 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:28 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + i = 0; + if (!s) + return ; + while (s[i]) + { + f(i, &s[i]); + i++; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..a2f1ec3 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strjoin.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:28 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:28 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + size_t len_s1; + size_t len_s2; + + if (!s1 || !s2) + return (NULL); + len_s1 = ft_strlen(s1); + len_s2 = ft_strlen(s2); + str = (char *)malloc(len_s1 + len_s2 + 1); + if (!str) + return (NULL); + ft_memcpy(str, s1, len_s1); + ft_memcpy(str + len_s1, s2, len_s2); + str[len_s1 + len_s2] = '\0'; + return (str); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..04cce3a --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strlcat.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:29 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:29 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + unsigned int src_len; + unsigned int dst_len; + unsigned int i; + + src_len = ft_strlen(src); + if (size == 0) + return (src_len); + dst_len = ft_strlen(dst); + if (dst_len >= (long int) size) + return (size + src_len); + i = 0; + while (src[i] && i < size - dst_len - 1) + { + dst[dst_len + i] = src[i]; + i++; + } + dst[dst_len + i] = '\0'; + return (src_len + dst_len); +} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c new file mode 100644 index 0000000..76b0730 --- /dev/null +++ b/libft/ft_strlcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strlcpy.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:29 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:29 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t i; + size_t result; + + i = 0; + if (size == 0) + return (ft_strlen(src)); + while (src[i] && i < size - 1) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + result = ft_strlen(src); + return (result); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..01cfa5f --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strlen.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:30 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:30 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t length; + + length = 0; + while (*s++) + length++; + return (length); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..0917410 --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strmapi.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:30 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:30 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + int i; + char *result; + + if (!s) + return (NULL); + result = malloc((ft_strlen(s) + 1) * sizeof(char)); + if (!result) + return (NULL); + i = 0; + while (s[i]) + { + result[i] = f(i, s[i]); + i++; + } + result[i] = '\0'; + return (result); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..874432e --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strncmp.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:31 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:31 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + if (n == 0) + return (0); + while (*s1 && *s1 == *s2 && i < n - 1) + { + s1++; + s2++; + i++; + } + return ((unsigned char) *s1 - (unsigned char) *s2); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..e92eaef --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strnstr.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:31 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:31 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + + i = 0; + if (*little == '\0') + return ((char *) big); + while (big[i] && i < len) + { + j = 0; + while (little[j] && big[i + j] == little [j] && i + j < len) + j++; + if (little[j] == '\0') + return ((char *) &big[i]); + i++; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..9902210 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strrchr.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:32 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:32 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + char *result; + + result = NULL; + while (*s) + { + if (*(unsigned char *) s == (unsigned char) c) + result = (char *) s; + s++; + } + if ('\0' == c) + result = (char *) s; + return (result); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..a8145d2 --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_strtrim.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:32 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:32 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + char *front; + char *back; + + if (!s1) + return (NULL); + if (!ft_strlen(set)) + return (ft_strdup(s1)); + front = (char *) s1; + back = (char *) s1; + while (*back) + back++; + back--; + while (ft_strchr(set, *front)) + front++; + while (ft_strchr(set, *back)) + back--; + return (ft_substr(s1, front - s1, back - front + 1)); +} diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100644 index 0000000..601f2f8 --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_substr.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:33 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:33 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *substr; + size_t len_s; + + if (!s) + return (NULL); + len_s = ft_strlen(s); + if (start >= len_s) + return (ft_strdup("")); + if (len_s - start < len) + len = len_s - start; + substr = malloc(len * sizeof(char) + 1); + if (!substr) + return (NULL); + ft_memcpy(substr, &s[start], len); + substr[len] = '\0'; + return (substr); +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..c22cbd7 --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_tolower.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:33 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:33 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if ('A' <= c && 'Z' >= c) + return (c - 'A' + 'a'); + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..63ee2fa --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_toupper.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/10/10 17:00:33 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/10/10 17:00:33 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if ('a' <= c && 'z' >= c) + return (c - 'a' + 'A'); + return (c); +} diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..72ad276 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: whaffman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 11:15:10 by whaffman #+# #+# */ +/* Updated: 2024/07/10 16:27:58 by whaffman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# 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_isascii(int c); +int ft_isprint(int c); +size_t ft_strlen(const char *s); +void *ft_memset(void *s, int c, size_t n); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlcat(char *dst, const char *src, size_t size); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); +int ft_atoi(const char *nptr); +void *ft_calloc(size_t nmemb, size_t size); +char *ft_strdup(const char *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_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); + +/* + * BONUS + */ + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +#endif