]> git.dkaiser.de - 42/push_swap.git/commitdiff
Add libft and input handling
authorDominik Kaiser <dkaiser@2-F-4.42heilbronn.de>
Fri, 12 Apr 2024 16:51:50 +0000 (18:51 +0200)
committerDominik Kaiser <dkaiser@2-F-4.42heilbronn.de>
Fri, 12 Apr 2024 16:51:50 +0000 (18:51 +0200)
57 files changed:
Makefile
input_handling.c [new file with mode: 0644]
libft/Makefile [new file with mode: 0644]
libft/ft_atoi.c [new file with mode: 0644]
libft/ft_bzero.c [new file with mode: 0644]
libft/ft_calloc.c [new file with mode: 0644]
libft/ft_isalnum.c [new file with mode: 0644]
libft/ft_isalpha.c [new file with mode: 0644]
libft/ft_isascii.c [new file with mode: 0644]
libft/ft_isdigit.c [new file with mode: 0644]
libft/ft_isprint.c [new file with mode: 0644]
libft/ft_itoa.c [new file with mode: 0644]
libft/ft_lstadd_back_bonus.c [new file with mode: 0644]
libft/ft_lstadd_front_bonus.c [new file with mode: 0644]
libft/ft_lstclear_bonus.c [new file with mode: 0644]
libft/ft_lstdelone_bonus.c [new file with mode: 0644]
libft/ft_lstiter_bonus.c [new file with mode: 0644]
libft/ft_lstlast_bonus.c [new file with mode: 0644]
libft/ft_lstmap_bonus.c [new file with mode: 0644]
libft/ft_lstnew_bonus.c [new file with mode: 0644]
libft/ft_lstsize_bonus.c [new file with mode: 0644]
libft/ft_memchr.c [new file with mode: 0644]
libft/ft_memcmp.c [new file with mode: 0644]
libft/ft_memcpy.c [new file with mode: 0644]
libft/ft_memmove.c [new file with mode: 0644]
libft/ft_memset.c [new file with mode: 0644]
libft/ft_printaddr.c [new file with mode: 0644]
libft/ft_printf.c [new file with mode: 0644]
libft/ft_printf.h [new file with mode: 0644]
libft/ft_printhex.c [new file with mode: 0644]
libft/ft_printnbr.c [new file with mode: 0644]
libft/ft_putchar_fd.c [new file with mode: 0644]
libft/ft_putendl_fd.c [new file with mode: 0644]
libft/ft_putnbr_fd.c [new file with mode: 0644]
libft/ft_putstr_fd.c [new file with mode: 0644]
libft/ft_split.c [new file with mode: 0644]
libft/ft_strchr.c [new file with mode: 0644]
libft/ft_strdup.c [new file with mode: 0644]
libft/ft_striteri.c [new file with mode: 0644]
libft/ft_strjoin.c [new file with mode: 0644]
libft/ft_strlcat.c [new file with mode: 0644]
libft/ft_strlcpy.c [new file with mode: 0644]
libft/ft_strlen.c [new file with mode: 0644]
libft/ft_strmapi.c [new file with mode: 0644]
libft/ft_strncmp.c [new file with mode: 0644]
libft/ft_strnstr.c [new file with mode: 0644]
libft/ft_strrchr.c [new file with mode: 0644]
libft/ft_strtrim.c [new file with mode: 0644]
libft/ft_substr.c [new file with mode: 0644]
libft/ft_tolower.c [new file with mode: 0644]
libft/ft_toupper.c [new file with mode: 0644]
libft/get_next_line.c [new file with mode: 0644]
libft/get_next_line.h [new file with mode: 0644]
libft/get_next_line_utils.c [new file with mode: 0644]
libft/libft.h [new file with mode: 0644]
main.c
push_swap.h

index a3e62829393611f7217078d70eb9b902c49ee7f1..5edb601302d2e7a6930dbb1ee2461501d2fec4a7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,25 +1,35 @@
 NAME = push_swap
 
+LIBFT = libft
+
 CC = cc
 CFLAGS = -Wall -Wextra -Werror
 
-SRC_FILES = main.c
+SRC_FILES = main.c input_handling.c
 OBJ_FILES = $(SRC_FILES:%.c=%.o)
 
+LIB_DIR = $(LIBFT)
+LIB = ft
+
 all: $(NAME)
 
-$(NAME): $(OBJ_FILES)
-       $(CC) -I. $(CFLAGS) $^ -o $@
+$(NAME): $(OBJ_FILES) | libft
+       $(CC) $(CFLAGS) -I. -L$(LIB_DIR) -l$(LIB) $^ -o $@
 
 %.o: %.c
-       $(CC) -I. $(CFLAGS) -c $< -o $@
+       $(CC) $(CFLAGS) -I. -c $< -o $@
 
 clean:
+       make clean -C $(LIBFT)
        rm -f $(OBJ_FILES)
 
 fclean: clean
+       make fclean -C $(LIBFT)
        rm -f $(NAME)
 
+libft:
+       make all bonus -C $(LIBFT)
+
 re: fclean all
 
-.PHONY: all clean fclean re
+.PHONY: all clean fclean libft re
diff --git a/input_handling.c b/input_handling.c
new file mode 100644 (file)
index 0000000..93e37f6
--- /dev/null
@@ -0,0 +1,96 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   input_handling.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/12 17:31:49 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/12 18:49:38 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "push_swap.h"
+
+static int     is_nbr(char *str)
+{
+       if (*str == '-')
+               str++;
+       while (*str)
+       {
+               if (ft_isdigit(*str))
+                       str++;
+               else
+                       return (0);
+       }
+       return (1);
+}
+
+static int     is_input_only_nbrs(int argc, char *argv[])
+{
+       while (argc-- > 1)
+       {
+               if (!is_nbr(argv[argc]))
+                       return (0);
+       }
+       return (1);
+}
+
+static int     are_numbers_unique(t_list *stack)
+{
+       t_list  *cmp_elem;
+
+       while (stack->next)
+       {
+               cmp_elem = stack->next;
+               while (cmp_elem->next)
+               {
+                       if (*(int *)stack->content == *(int *)cmp_elem->content)
+                               return (0);
+                       cmp_elem = cmp_elem->next;
+               }
+               stack = stack->next;
+       }
+       return (1);
+}
+
+static t_list  *get_stack_from_input(int argc, char *argv[])
+{
+       t_list  *result;
+       t_list  *cur;
+       int             *content;
+
+       result = NULL;
+       while (argc-- > 1)
+       {
+               content = malloc(sizeof(int));
+               if (content)
+               {
+                       *content = ft_atoi(argv[argc]);
+                       cur = ft_lstnew(content);
+                       if (cur)
+                       {
+                               ft_lstadd_front(&result, cur);
+                               continue ;
+                       }
+                       free(content);
+               }
+               ft_lstclear(&result, free);
+               return (NULL);
+       }
+       return (result);
+}
+
+t_list *create_stack(int argc, char *argv[])
+{
+       t_list  *result;
+
+       if (!is_input_only_nbrs(argc, argv))
+               return (NULL);
+       result = get_stack_from_input(argc, argv);
+       if (!result)
+               return (NULL);
+       if (!are_numbers_unique(result))
+               ft_lstclear(&result, free);
+       return (result);
+}
diff --git a/libft/Makefile b/libft/Makefile
new file mode 100644 (file)
index 0000000..7bab267
--- /dev/null
@@ -0,0 +1,75 @@
+NAME=libft.a
+CC=cc
+CFLAGS=-Wall -Wextra -Werror
+
+SRC_FILES = 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 \
+                       ft_printf.c \
+                       ft_printnbr.c \
+                       ft_printhex.c \
+                       ft_printaddr.c \
+                       get_next_line.c \
+                       get_next_line_utils.c
+
+
+OBJ_FILES = $(SRC_FILES:.c=.o)
+
+BONUS_SRC = 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
+
+BONUS_OBJ = $(BONUS_SRC:.c=.o)
+
+all: $(NAME)
+
+$(NAME): $(OBJ_FILES)
+       ar rcs $(NAME) $(OBJ_FILES)
+
+clean:
+       rm -f $(OBJ_FILES) $(BONUS_OBJ)
+
+fclean: clean
+       rm -f $(NAME)
+
+re: fclean all
+
+bonus: $(BONUS_OBJ)
+       ar rcs $(NAME) $(BONUS_OBJ)
diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c
new file mode 100644 (file)
index 0000000..90200fb
--- /dev/null
@@ -0,0 +1,49 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_atoi.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 16:25:27 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:17:19 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_atoi(const char *str)
+{
+       int     result;
+       int     i;
+       int     posneg;
+
+       posneg = 1;
+       result = 0;
+       i = 0;
+       while ((str[i] >= '\t' && str[i] <= '\r') || str[i] == ' ')
+       {
+               i++;
+       }
+       if (str[i] == '-')
+       {
+               posneg = -1;
+               i++;
+       }
+       else if (str[i] == '+')
+               i++;
+       while (str[i] >= '0' && str[i] <= '9')
+       {
+               result = 10 * result + str[i] - '0';
+               i++;
+       }
+       return (result * posneg);
+}
+
+/* #include <stdio.h> */
+/* #include <stdlib.h> */
+/* int main() { */
+/*     char str[] = "         -42eaeouai"; */
+/*     printf("atoi: %d\n", atoi(str)); */
+/*     printf("ft_atoi: %d\n", ft_atoi(str)); */
+/* } */
diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c
new file mode 100644 (file)
index 0000000..78af51a
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_bzero.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/05 10:57:36 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:14:10 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_bzero(void *s, size_t n)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < n)
+       {
+               ((char *)s)[i] = 0;
+               i++;
+       }
+}
diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c
new file mode 100644 (file)
index 0000000..0dcc434
--- /dev/null
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_calloc.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 19:45:56 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 15:34:06 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   *ft_calloc(size_t count, size_t size)
+{
+       void    *result;
+
+       result = malloc(count * size);
+       if (result)
+               ft_bzero(result, count * size);
+       return (result);
+}
diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c
new file mode 100644 (file)
index 0000000..d705500
--- /dev/null
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_isalnum.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:14:49 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:10:50 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_isalnum(int c)
+{
+       if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0'
+                       && c <= '9'))
+               return (1);
+       return (0);
+}
diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c
new file mode 100644 (file)
index 0000000..054d96c
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_isalpha.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 19:59:23 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:10:10 by dkaiser          ###   ########.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 (file)
index 0000000..6570d44
--- /dev/null
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_isascii.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:23:25 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/04 21:24:46 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+int    ft_isascii(int c)
+{
+       if (c >= 0 && c < 128)
+               return (1);
+       return (0);
+}
diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c
new file mode 100644 (file)
index 0000000..2601fee
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_isdigit.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:07:03 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:10:23 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+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 (file)
index 0000000..c04cf4f
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_isprint.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:25:31 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:11:14 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_isprint(int c)
+{
+       if (c >= ' ' && c <= '~')
+               return (1);
+       return (0);
+}
diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c
new file mode 100644 (file)
index 0000000..401ba6a
--- /dev/null
@@ -0,0 +1,68 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_itoa.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 10:42:08 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:18:58 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int     get_last_digit(int n)
+{
+       int     last_digit;
+
+       last_digit = n % 10;
+       if (last_digit < 0)
+               last_digit *= -1;
+       return (last_digit);
+}
+
+static int     get_len(int n)
+{
+       int     len;
+
+       len = 0;
+       if (n == 0)
+               return (1);
+       if (n < 0)
+               len++;
+       while (n)
+       {
+               len++;
+               n /= 10;
+       }
+       return (len);
+}
+
+char   *ft_itoa(int n)
+{
+       char    *result;
+       int             len;
+
+       len = get_len(n);
+       result = malloc(sizeof(char) * (len + 1));
+       if (!result)
+               return (0);
+       result[len] = '\0';
+       if (n == 0)
+               result[0] = '0';
+       if (n < 0)
+               result[0] = '-';
+       while (n)
+       {
+               result[--len] = '0' + get_last_digit(n);
+               n /= 10;
+       }
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() { */
+/*     char* itoa = ft_itoa(0); */
+/*     printf("%s\n", itoa); */
+/* } */
diff --git a/libft/ft_lstadd_back_bonus.c b/libft/ft_lstadd_back_bonus.c
new file mode 100644 (file)
index 0000000..b2913af
--- /dev/null
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstadd_back_bonus.c                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 16:03:26 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/12 17:01:15 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstadd_back(t_list **lst, t_list *new_elem)
+{
+       t_list  *current;
+
+       if (!*lst)
+       {
+               *lst = new_elem;
+               return ;
+       }
+       current = *lst;
+       while (current->next)
+       {
+               current = current->next;
+       }
+       current->next = new_elem;
+}
diff --git a/libft/ft_lstadd_front_bonus.c b/libft/ft_lstadd_front_bonus.c
new file mode 100644 (file)
index 0000000..f15d88f
--- /dev/null
@@ -0,0 +1,19 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstadd_front_bonus.c                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 15:14:01 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/12 17:01:37 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstadd_front(t_list **lst, t_list *new_elem)
+{
+       new_elem->next = *lst;
+       *lst = new_elem;
+}
diff --git a/libft/ft_lstclear_bonus.c b/libft/ft_lstclear_bonus.c
new file mode 100644 (file)
index 0000000..f661fc9
--- /dev/null
@@ -0,0 +1,32 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstclear_bonus.c                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 16:26:23 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/11 12:48:24 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstclear(t_list **lst, void (*del)(void *))
+{
+       t_list  *current;
+       t_list  *next;
+
+       if (*lst)
+       {
+               current = *lst;
+               while (current->next)
+               {
+                       next = current->next;
+                       ft_lstdelone(current, del);
+                       current = next;
+               }
+               ft_lstdelone(current, del);
+       }
+       *lst = NULL;
+}
diff --git a/libft/ft_lstdelone_bonus.c b/libft/ft_lstdelone_bonus.c
new file mode 100644 (file)
index 0000000..9dd78c8
--- /dev/null
@@ -0,0 +1,19 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstdelone_bonus.c                               :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 16:20:31 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 16:36:18 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstdelone(t_list *lst, void (*del)(void *))
+{
+       del(lst->content);
+       free(lst);
+}
diff --git a/libft/ft_lstiter_bonus.c b/libft/ft_lstiter_bonus.c
new file mode 100644 (file)
index 0000000..73efbe9
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstiter_bonus.c                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 16:39:47 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 16:41:07 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_lstiter(t_list *lst, void (*f)(void *))
+{
+       if (!lst)
+               return ;
+       while (lst->next)
+       {
+               f(lst->content);
+               lst = lst->next;
+       }
+       f(lst->content);
+}
diff --git a/libft/ft_lstlast_bonus.c b/libft/ft_lstlast_bonus.c
new file mode 100644 (file)
index 0000000..e0b4d6d
--- /dev/null
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstlast_bonus.c                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 15:54:05 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 16:36:04 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+t_list *ft_lstlast(t_list *lst)
+{
+       if (!lst)
+               return (0);
+       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 (file)
index 0000000..43120cd
--- /dev/null
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstmap_bonus.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 16:45:21 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/11 13:36:23 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
+{
+       t_list  *result;
+       t_list  *cur_new;
+       void    *content;
+
+       if (!lst || !f || !del)
+               return (NULL);
+       result = NULL;
+       while (lst)
+       {
+               content = f(lst->content);
+               cur_new = ft_lstnew(content);
+               if (!cur_new)
+               {
+                       del(content);
+                       ft_lstclear(&result, del);
+                       return (NULL);
+               }
+               lst = lst->next;
+               ft_lstadd_back(&result, cur_new);
+       }
+       return (result);
+}
diff --git a/libft/ft_lstnew_bonus.c b/libft/ft_lstnew_bonus.c
new file mode 100644 (file)
index 0000000..eeb6030
--- /dev/null
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstnew_bonus.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 14:57:39 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/11 12:52:00 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+t_list *ft_lstnew(void *content)
+{
+       t_list  *result;
+
+       result = malloc(sizeof(t_list));
+       if (!result)
+               return (NULL);
+       result->content = content;
+       result->next = NULL;
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() { */
+/*     t_list l; */
+/*     char s[] = "Hello"; */
+
+/*     l = *ft_lstnew(&s); */
+/*     printf("%s\n", l.content); */
+/* } */
diff --git a/libft/ft_lstsize_bonus.c b/libft/ft_lstsize_bonus.c
new file mode 100644 (file)
index 0000000..c91b0c9
--- /dev/null
@@ -0,0 +1,28 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_lstsize_bonus.c                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 15:35:38 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 16:36:09 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_lstsize(t_list *lst)
+{
+       int     len;
+
+       if (!lst)
+               return (0);
+       len = 1;
+       while (lst->next)
+       {
+               lst = lst->next;
+               len++;
+       }
+       return (len);
+}
diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c
new file mode 100644 (file)
index 0000000..568faa6
--- /dev/null
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_memchr.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 15:23:01 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:16:25 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   *ft_memchr(const void *s, int c, size_t n)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < n)
+       {
+               if (*((unsigned char *)s + i) == (unsigned char)c)
+                       return ((void *)s + i);
+               i++;
+       }
+       return (0);
+}
+
+/* #include <stdio.h> */
+/* int main () { */
+/*     char str[] = "Hello world"; */
+/*     printf("memchr: %s\n", (char *) memchr(str, 'o', 11)); */
+/*     printf("ft_memchr: %s\n", (char *) ft_memchr(str, 'o', 11)); */
+/* } */
diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c
new file mode 100644 (file)
index 0000000..0bd52df
--- /dev/null
@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_memcmp.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 15:54:13 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:13:54 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_memcmp(const void *s1, const void *s2, size_t n)
+{
+       size_t  i;
+       int             result;
+
+       i = 0;
+       result = 0;
+       while (!result && i < n)
+       {
+               result = *((unsigned char *)s1 + i) - *((unsigned char *)s2 + i);
+               i++;
+       }
+       return (result);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    str1[] = "Hello"; */
+/*     char    str2[] = "Hellu"; */
+
+/*     printf("memcmp: %d\n", memcmp(str1, str2, 5)); */
+/*     printf("ft_memcmp: %d\n", ft_memcmp(str1, str2, 5)); */
+/* } */
diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c
new file mode 100644 (file)
index 0000000..768d500
--- /dev/null
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_memcpy.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/05 11:15:12 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:14:24 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   *ft_memcpy(void *dst, const void *src, size_t n)
+{
+       char    *dst_ptr;
+       char    *src_ptr;
+
+       dst_ptr = (char *)dst;
+       src_ptr = (char *)src;
+       if (!dst && !src)
+               return (dst);
+       while (n--)
+               *dst_ptr++ = *src_ptr++;
+       return (dst);
+}
+
+/* #include <stdio.h> */
+/* int main(void) */
+/* { */
+/*     char src[] = "a"; */
+/*     char src2[] = "a"; */
+/*     char dst[] = ""; */
+/*     char dst2[] = ""; */
+/*     printf("ft_memcpy: %s\n", (char *) ft_memcpy(dst, src, 20)); */
+/*     printf("memcpy: %s\n", (char *) memcpy(dst2, src2, 20)); */
+/* } */
diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c
new file mode 100644 (file)
index 0000000..5d12f20
--- /dev/null
@@ -0,0 +1,50 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_memmove.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/05 11:41:44 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:14:38 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   *ft_memmove(void *dst, const void *src, size_t len)
+{
+       size_t  i;
+
+       if (!dst && !src)
+               return (dst);
+       if (dst > src)
+       {
+               i = len;
+               while (i-- > 0)
+               {
+                       *((char *)dst + i) = *((char *)src + i);
+               }
+       }
+       else
+       {
+               i = 0;
+               while (i < len)
+               {
+                       *((char *)dst + i) = *((char *)src + i);
+                       i++;
+               }
+       }
+       return (dst);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    text[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; */
+/*     char    text2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; */
+
+/*     printf("memmove: %s\n", memmove(text + 5, text, 5)); */
+/*     printf("ft_memmove: %s\n", ft_memmove(text2 + 5, text2, 5)); */
+/* } */
diff --git a/libft/ft_memset.c b/libft/ft_memset.c
new file mode 100644 (file)
index 0000000..58c5e1e
--- /dev/null
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_memset.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/05 09:58:19 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:13:09 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   *ft_memset(void *b, int c, size_t len)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < len)
+       {
+               ((char *)b)[i] = (unsigned char)c;
+               i++;
+       }
+       return (b);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    str[] = "Hello world"; */
+
+/*     printf("STR: %s\n", str); */
+/*     printf("ft_memset: %s\n", ft_memset((char *)str, 'A' + 255, 5)); */
+/*     printf("memset: %s\n", memset((char *)str, 'A' + 255, 5)); */
+/* } */
diff --git a/libft/ft_printaddr.c b/libft/ft_printaddr.c
new file mode 100644 (file)
index 0000000..f60ce0d
--- /dev/null
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printaddr.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 10:33:53 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/18 11:42:58 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_printf.h"
+
+static void    printaddr_rec(unsigned long addr, int *len)
+{
+       char    c;
+       int             success;
+
+       if (*len < 0)
+               return ;
+       if (addr % 16 < 10)
+               c = '0' + (addr % 16);
+       else
+               c = ('a' - 10) + (addr % 16);
+       if (addr > 15)
+               printaddr_rec(addr / 16, len);
+       success = write(1, &c, 1);
+       if (success < 0 || *len < 2)
+               *len = -1;
+       else
+               (*len)++;
+}
+
+int    ft_printaddr(void *addr)
+{
+       int     len;
+
+       len = write(1, "0x", 2);
+       printaddr_rec((unsigned long)addr, &len);
+       return (len);
+}
diff --git a/libft/ft_printf.c b/libft/ft_printf.c
new file mode 100644 (file)
index 0000000..fc77f81
--- /dev/null
@@ -0,0 +1,90 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printf.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/12 18:18:59 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/18 11:42:34 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_printf.h"
+
+static int     print_char(char c)
+{
+       return (write(1, &c, 1));
+}
+
+static int     print_str(char *str)
+{
+       int     success;
+       int     len;
+
+       if (str)
+       {
+               len = 0;
+               while (str[len])
+                       len++;
+               success = write(1, str, len);
+               if (success < 0)
+                       return (-1);
+               return (len);
+       }
+       else
+       {
+               success = write(1, "(null)", 6);
+               if (success == 6)
+                       return (6);
+               return (-1);
+       }
+}
+
+static int     print_conv(va_list args, char c)
+{
+       if (c == 'c')
+               return (print_char(va_arg(args, int)));
+       if (c == 's')
+               return (print_str(va_arg(args, char *)));
+       if (c == 'p')
+               return (ft_printaddr(va_arg(args, void *)));
+       if (c == 'd' || c == 'i')
+               return (ft_printnbr(va_arg(args, int)));
+       if (c == 'u')
+               return (ft_printunbr(va_arg(args, unsigned int)));
+       if (c == 'x' || c == 'X')
+               return (ft_printhex(va_arg(args, unsigned int), c));
+       if (c == '%')
+               return (print_char('%'));
+       return (-1);
+}
+
+int    ft_printf(const char *fmt, ...)
+{
+       int             result;
+       int             last_result;
+       int             success;
+       va_list args;
+
+       result = 0;
+       va_start(args, fmt);
+       while (*fmt)
+       {
+               last_result = result;
+               if (*fmt == '%')
+                       result += print_conv(args, *(++fmt));
+               else
+               {
+                       success = write(1, fmt, 1);
+                       if (success <= 0)
+                               return (-1);
+                       result++;
+               }
+               fmt++;
+               if (result < last_result)
+                       return (-1);
+       }
+       va_end(args);
+       return (result);
+}
diff --git a/libft/ft_printf.h b/libft/ft_printf.h
new file mode 100644 (file)
index 0000000..22f6a2a
--- /dev/null
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printf.h                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/12 18:19:47 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/15 13:33:09 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef FT_PRINTF_H
+# define FT_PRINTF_H
+
+# include <stdarg.h>
+# include <unistd.h>
+
+int    ft_printf(const char *fmt, ...);
+int    ft_printnbr(int nbr);
+int    ft_printunbr(unsigned int nbr);
+int    ft_printhex(unsigned int nbr, char fmt);
+int    ft_printaddr(void *addr);
+#endif // FT_PRINTF_H
diff --git a/libft/ft_printhex.c b/libft/ft_printhex.c
new file mode 100644 (file)
index 0000000..4605eac
--- /dev/null
@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printhex.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/13 15:50:35 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/18 11:42:46 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_printf.h"
+
+static void    printhex_rec(unsigned int nbr, char fmt, int *len)
+{
+       char    c;
+       int             success;
+
+       if (*len < 0)
+               return ;
+       if (nbr % 16 < 10)
+               c = '0' + (nbr % 16);
+       else
+               c = (fmt - 33) + (nbr % 16);
+       if (nbr > 15)
+               printhex_rec(nbr / 16, fmt, len);
+       if (*len < 0)
+               return ;
+       success = write(1, &c, 1);
+       if (success < 0)
+               *len = -1;
+       else
+               (*len)++;
+}
+
+int    ft_printhex(unsigned int nbr, char fmt)
+{
+       int     len;
+
+       len = 0;
+       printhex_rec(nbr, fmt, &len);
+       return (len);
+}
diff --git a/libft/ft_printnbr.c b/libft/ft_printnbr.c
new file mode 100644 (file)
index 0000000..2a42ed8
--- /dev/null
@@ -0,0 +1,80 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printnbr.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/13 15:18:40 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/18 11:41:27 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_printf.h"
+
+static void    printnbr_rec(int n, int *len)
+{
+       char    c;
+       int             success;
+
+       if (*len < 0)
+               return ;
+       c = '0' + n % 10;
+       if (n > 9)
+               printnbr_rec(n / 10, len);
+       if (*len < 0)
+               return ;
+       success = write(1, &c, 1);
+       if (success < 0)
+               *len = -1;
+       else
+               (*len)++;
+}
+
+int    ft_printnbr(int nbr)
+{
+       int     len;
+
+       len = 0;
+       if (nbr == -2147483648)
+               return (write(1, "-2147483648", 11));
+       if (nbr < 0)
+       {
+               len = write(1, "-", 1);
+               nbr *= -1;
+       }
+       printnbr_rec(nbr, &len);
+       if (len < 0)
+               return (-1);
+       return (len);
+}
+
+static void    printunbr_rec(unsigned int n, int *len)
+{
+       char    c;
+       int             success;
+
+       if (*len < 0)
+               return ;
+       c = '0' + n % 10;
+       if (n > 9)
+               printunbr_rec(n / 10, len);
+       if (*len < 0)
+               return ;
+       success = write(1, &c, 1);
+       if (success < 0)
+               *len = -1;
+       else
+               (*len)++;
+}
+
+int    ft_printunbr(unsigned int nbr)
+{
+       int     len;
+
+       len = 0;
+       printunbr_rec(nbr, &len);
+       if (len < 0)
+               return (-1);
+       return (len);
+}
diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c
new file mode 100644 (file)
index 0000000..5334e0e
--- /dev/null
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_putchar_fd.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 20:36:14 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:19:59 by dkaiser          ###   ########.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 (file)
index 0000000..f8de15e
--- /dev/null
@@ -0,0 +1,19 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_putendl_fd.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 20:47:17 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 14:07:58 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_putendl_fd(char *s, int fd)
+{
+       write(fd, s, ft_strlen(s));
+       write(fd, "\n", 1);
+}
diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c
new file mode 100644 (file)
index 0000000..96f5f4b
--- /dev/null
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_putnbr_fd.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 10:43:09 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:20:59 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static void    putnbr_fd_rec(int n, int fd)
+{
+       char    c;
+
+       c = '0' + n % 10;
+       if (n > 9)
+               putnbr_fd_rec(n / 10, fd);
+       write(fd, &c, 1);
+}
+
+void   ft_putnbr_fd(int n, int fd)
+{
+       if (n == -2147483648)
+       {
+               write(fd, "-2147483648", 11);
+               return ;
+       }
+       if (n < 0)
+       {
+               write(fd, "-", 1);
+               n *= -1;
+       }
+       putnbr_fd_rec(n, fd);
+}
diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c
new file mode 100644 (file)
index 0000000..cb78491
--- /dev/null
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_putstr_fd.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 20:43:05 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 14:07:37 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_putstr_fd(char *s, int fd)
+{
+       write(fd, s, ft_strlen(s));
+}
diff --git a/libft/ft_split.c b/libft/ft_split.c
new file mode 100644 (file)
index 0000000..e9ba9f9
--- /dev/null
@@ -0,0 +1,104 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_split.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/08 15:36:44 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:09:06 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int     get_word_count(char const *s, char c)
+{
+       int     word_count;
+       int     cur_len;
+
+       word_count = 0;
+       cur_len = 0;
+       while (*s)
+       {
+               if (!(*s) || *s == c)
+               {
+                       cur_len = 0;
+               }
+               else
+               {
+                       if (!cur_len)
+                               word_count++;
+                       cur_len++;
+               }
+               s++;
+       }
+       return (word_count);
+}
+
+static char    *get_next_token(char const **ptr_s, char c)
+{
+       int                     i;
+       int                     len;
+       char const      *s;
+       char            *result;
+
+       s = *ptr_s;
+       while (*s && *s == c)
+               s++;
+       if (!*s)
+               return (0);
+       len = 0;
+       while (s[len] && s[len] != c)
+               len++;
+       result = malloc(sizeof(char) * (len + 1));
+       if (!result)
+               return (0);
+       i = 0;
+       while (i < len)
+               result[i++] = *(s++);
+       result[i] = '\0';
+       *ptr_s = s;
+       return (result);
+}
+
+char   **ft_split(char const *s, char c)
+{
+       char    **result;
+       int             word_count;
+       int             w;
+
+       word_count = get_word_count((char *)s, c);
+       result = malloc(sizeof(char *) * (word_count + 1));
+       if (!result)
+               return (0);
+       w = 0;
+       while (w < word_count)
+       {
+               result[w] = get_next_token(&s, c);
+               if (!result[w])
+               {
+                       while (w--)
+                               free(result[w]);
+                       free(result);
+                       return (0);
+               }
+               w++;
+       }
+       result[w] = 0;
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() */
+/* { */
+/*     char s[] = "      split       this for   me  !"; */
+/*     char **split = ft_split(s, ' '); */
+
+/*     if (split) { */
+/*         while(*split) { */
+/*             printf("%s\n", *split); */
+/*             split++; */
+/*         } */
+/*     } */
+/* } */
diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c
new file mode 100644 (file)
index 0000000..ecf522e
--- /dev/null
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strchr.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 14:12:39 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:15:56 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char   *ft_strchr(const char *s, int c)
+{
+       int     i;
+
+       i = 0;
+       while (s[i] != '\0')
+       {
+               if (s[i] == (char)c)
+                       return ((char *)&s[i]);
+               i++;
+       }
+       if (!(char)c)
+               return ((char *)&s[i]);
+       return (0);
+}
+
+/* #include <stdio.h> */
+/* #include <string.h> */
+
+/* int main(void) */
+/* { */
+/*     char    str[] = "teste"; */
+
+/*     printf("strchr: %p\n", strchr(str, '\0')); */
+/*     printf("ft_strchr: %p\n", ft_strchr(str, '\0')); */
+/* } */
diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c
new file mode 100644 (file)
index 0000000..c7a6591
--- /dev/null
@@ -0,0 +1,36 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strdup.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 19:54:16 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 15:28:58 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char   *ft_strdup(const char *s1)
+{
+       char    *result;
+       int             len;
+
+       len = ft_strlen(s1);
+       result = malloc(len + 1);
+       if (!result)
+               return (0);
+       result[len] = '\0';
+       while (len--)
+               result[len] = s1[len];
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() { */
+/*     char *output; */
+/*     char input[] = "test"; */
+/*     output = ft_strdup(input); */
+/*     printf("%s\n", output); */
+/* } */
diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c
new file mode 100644 (file)
index 0000000..e7c8b89
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_striteri.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 11:24:32 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:19:39 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_striteri(char *s, void (*f)(unsigned int, char *))
+{
+       unsigned int    i;
+
+       i = 0;
+       while (s[i])
+       {
+               f(i, &s[i]);
+               i++;
+       }
+}
diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c
new file mode 100644 (file)
index 0000000..526592b
--- /dev/null
@@ -0,0 +1,54 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strjoin.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/07 10:15:33 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 14:02:07 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int     copy_str(char *dst, const char *src)
+{
+       int     i;
+
+       i = 0;
+       while (src[i])
+       {
+               dst[i] = src[i];
+               i++;
+       }
+       return (i);
+}
+
+char   *ft_strjoin(char const *s1, char const *s2)
+{
+       int             len;
+       char    *result;
+
+       len = ft_strlen(s1) + ft_strlen(s2);
+       result = malloc(len + 1);
+       if (result)
+       {
+               result[len] = '\0';
+               len = copy_str(result, s1);
+               len = copy_str(result + len, s2);
+               return (result);
+       }
+       else
+               return (0);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    s1[] = "Hello "; */
+/*     char    s2[] = "World"; */
+
+/*     printf("%s\n", ft_strjoin(s1, s2)); */
+/* } */
diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c
new file mode 100644 (file)
index 0000000..d49dfad
--- /dev/null
@@ -0,0 +1,72 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strlcat.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 13:36:59 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:15:10 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+/* size_t      ft_strlcat(char *dst, const char *src, size_t dstsize) */
+/* { */
+/*     size_t  len; */
+/*     size_t  i; */
+
+/*     if (dstsize == 0) */
+/*             return (0); */
+/*     len = 0; */
+/*     while (len < dstsize - 1 && dst[len] != '\0') */
+/*     { */
+/*             len++; */
+/*     } */
+/*     i = 0; */
+/*     while (len < dstsize - 1 && src[i] != '\0') */
+/*     { */
+/*             dst[len] = src[i]; */
+/*             len++; */
+/*             i++; */
+/*     } */
+/*     dst[len] = '\0'; */
+/*     return (len); */
+/* } */
+
+size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
+{
+       size_t  src_len;
+       size_t  dst_len;
+       size_t  i;
+
+       src_len = 0;
+       while (src[src_len])
+               src_len++;
+       dst_len = 0;
+       while (dst[dst_len])
+               dst_len++;
+       if (dst_len >= dstsize)
+               return (dstsize + src_len);
+       i = 0;
+       while (i < src_len && dst_len + i < dstsize - 1)
+       {
+               dst[dst_len + i] = src[i];
+               i++;
+       }
+       dst[dst_len + i] = '\0';
+       return (dst_len + src_len);
+}
+
+/* #include <stdio.h> */
+/* int main () { */
+/*     char dst[12] = "Hello "; */
+/*     char dst2[12] = "Hello "; */
+/*     char src[] = "World123"; */
+
+/*     size_t len = strlcat(dst, src, 12); */
+/*     size_t len2 = ft_strlcat(dst2, src, 12); */
+/*     printf("strlcat (%zu): %s\n", len, dst); */
+/*     printf("ft_strlcat (%zu): %s\n", len2, dst2); */
+/* } */
diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c
new file mode 100644 (file)
index 0000000..1cc62e5
--- /dev/null
@@ -0,0 +1,53 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strlcpy.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 12:45:25 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:14:56 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
+{
+       size_t  len;
+
+       len = 0;
+       if (dstsize > 0)
+       {
+               while (len < dstsize - 1 && src[len] != '\0')
+               {
+                       dst[len] = src[len];
+                       len++;
+               }
+               dst[len] = '\0';
+               while (src[len] != '\0')
+                       len++;
+       }
+       else
+       {
+               while (src[len] != '\0')
+                       len++;
+       }
+       return (len);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    src[] = "Hello"; */
+/*     char    dst[] = "01234567890123456789"; */
+/*     char    dst2[] = "01234567890123456789"; */
+/*     size_t  len; */
+/*     size_t  len2; */
+
+/*     len = strlcpy(dst, src, 0); */
+/*     len2 = ft_strlcpy(dst2, src, 0); */
+/*     printf("strlcpy (%zu): %s\n", len, dst); */
+/*     printf("ft_strlcpy (%zu): %s\n", len2, dst2); */
+/* } */
diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c
new file mode 100644 (file)
index 0000000..867c5eb
--- /dev/null
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strlen.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:29:35 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:11:48 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_strlen(const char *str)
+{
+       int     len;
+
+       len = 0;
+       while (str[len])
+               len++;
+       return (len);
+}
diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c
new file mode 100644 (file)
index 0000000..a500621
--- /dev/null
@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strmapi.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 11:11:30 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 14:06:41 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char   *ft_strmapi(char const *s, char (*f)(unsigned int, char))
+{
+       char                    *result;
+       unsigned int    i;
+
+       i = ft_strlen(s);
+       result = malloc(sizeof(char) * (i + 1));
+       if (!result)
+               return (0);
+       result[i] = '\0';
+       i = 0;
+       while (s[i])
+       {
+               result[i] = f(i, s[i]);
+               i++;
+       }
+       return (result);
+}
+
+/* char func (unsigned int i, char c) */
+/* { */
+/*     c += i; */
+/*     return (c); */
+/* } */
+
+/* #include <stdio.h> */
+/* int main(){ */
+/*     char str[] = "AAAAAAAA"; */
+/*     printf("%s\n", ft_strmapi(str, func)); */
+/* } */
diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c
new file mode 100644 (file)
index 0000000..9d30ee7
--- /dev/null
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strncmp.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 14:45:10 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:16:10 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_strncmp(const char *s1, const char *s2, size_t n)
+{
+       int     result;
+
+       result = 0;
+       while (!result && n > 0 && (*s1 || *s2))
+       {
+               result = (unsigned char)*s1 - (unsigned char)*s2;
+               if (*s1)
+                       s1++;
+               if (*s2)
+                       s2++;
+               n--;
+       }
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() { */
+/*     char str1[] = "Hello"; */
+/*     char str2[] = "Hellu"; */
+/*     printf("strncmp: %d\n", strncmp(str1, str2, 5)); */
+/*     printf("ft_strncmp: %d\n", ft_strncmp(str1, str2, 5)); */
+/* } */
diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c
new file mode 100644 (file)
index 0000000..373e90c
--- /dev/null
@@ -0,0 +1,47 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strnstr.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 16:07:54 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/11 15:04:28 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char   *ft_strnstr(const char *haystack, const char *needle, size_t len)
+{
+       size_t  i;
+       size_t  k;
+
+       if (!*haystack && *needle)
+               return (0);
+       i = 0;
+       if (*needle == '\0' || needle == haystack)
+               return ((char *)haystack);
+       while (i < len && haystack[i])
+       {
+               k = 0;
+               while (haystack[i + k] && haystack[i + k] == needle[k] && i + k < len)
+                       k++;
+               if (!needle[k])
+                       return ((char *)haystack + i);
+               i++;
+       }
+       return (0);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    haystack[] = "abc"; */
+/*     char    needle[] = "abcde"; */
+/*     size_t  len = 5; */
+
+/*     printf("strnstr: %s\n", strnstr(haystack, needle, len)); */
+/*     printf("ft_strnstr: %s\n", ft_strnstr(haystack, needle, len)); */
+/* } */
diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c
new file mode 100644 (file)
index 0000000..5d31654
--- /dev/null
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strrchr.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 14:25:30 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 12:51:38 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+char   *ft_strrchr(const char *s, int c)
+{
+       int             i;
+       char    *last_occurrence;
+
+       last_occurrence = 0;
+       i = 0;
+       while (s[i] != '\0')
+       {
+               if (s[i] == (char)c)
+                       last_occurrence = (char *)&s[i];
+               i++;
+       }
+       if (!(char)c)
+               last_occurrence = (char *)&s[i];
+       return (last_occurrence);
+}
+
+/* #include <stdio.h> */
+/* #include <string.h> */
+
+/* int main(void) */
+/* { */
+/*     char    str[] = "Hello world"; */
+
+/*     printf("strrchr: %s\n", strrchr(str, 'o')); */
+/*     printf("ft_strrchr: %s\n", ft_strrchr(str, 'o')); */
+/* } */
diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c
new file mode 100644 (file)
index 0000000..9caf12f
--- /dev/null
@@ -0,0 +1,61 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strtrim.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/07 10:24:17 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 14:03:13 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int     is_in(char c, const char *str)
+{
+       while (*str)
+       {
+               if (c == *str)
+                       return (1);
+               else
+                       str++;
+       }
+       return (0);
+}
+
+char   *ft_strtrim(char const *s1, char const *set)
+{
+       int             i;
+       char    *start;
+       char    *end;
+       char    *result;
+
+       start = (char *)s1;
+       while (is_in(*start, set))
+               start++;
+       i = ft_strlen(s1);
+       end = (char *)s1 + i - 1;
+       while (end > start && is_in(*end, set))
+               end--;
+       i = end - start + 1;
+       result = malloc(i + 1);
+       if (result)
+       {
+               result[i] = '\0';
+               i = 0;
+               while (start <= end)
+                       result[i++] = *(start++);
+               return (result);
+       }
+       return (0);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    s1[] = " \t   \t  \t  "; */
+
+/*     printf("%s\n", ft_strtrim(s1, " \t")); */
+/* } */
diff --git a/libft/ft_substr.c b/libft/ft_substr.c
new file mode 100644 (file)
index 0000000..81ca8c6
--- /dev/null
@@ -0,0 +1,45 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_substr.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/06 21:58:31 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 15:39:38 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char   *ft_substr(char const *s, unsigned int start, size_t len)
+{
+       unsigned int    slen;
+       size_t                  i;
+       char                    *result;
+
+       slen = ft_strlen(s);
+       if (start >= slen)
+               len = 0;
+       if (slen - start < len)
+               len = (slen - start);
+       result = malloc(len + 1);
+       if (!result)
+               return (0);
+       result[len] = '\0';
+       i = 0;
+       while (i < len)
+       {
+               result[i] = s[i + start];
+               i++;
+       }
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main () */
+/* { */
+/*     char s[] = "Hello there"; */
+/*     char *substr = ft_substr(s, 0, 2); */
+/*     printf("%s\n", substr); */
+/* } */
diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c
new file mode 100644 (file)
index 0000000..c81b9ef
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_tolower.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:49:28 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:15:39 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_tolower(int c)
+{
+       if (c >= 'A' && c <= 'Z')
+               return (c + 32);
+       return (c);
+}
diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c
new file mode 100644 (file)
index 0000000..51e4ba6
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_toupper.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/04 21:33:34 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:15:26 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_toupper(int c)
+{
+       if (c >= 'a' && c <= 'z')
+               return (c - 32);
+       return (c);
+}
diff --git a/libft/get_next_line.c b/libft/get_next_line.c
new file mode 100644 (file)
index 0000000..46edb73
--- /dev/null
@@ -0,0 +1,84 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 14:13:51 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/25 15:26:21 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+
+static int     get_next_line_len(char *buf, int pos)
+{
+       int     len;
+
+       len = 0;
+       while (pos + len < BUFFER_SIZE)
+       {
+               if (buf[pos + len] == '\n')
+               {
+                       len++;
+                       break ;
+               }
+               if (!buf[pos + len])
+                       break ;
+               len++;
+       }
+       return (len);
+}
+
+static void    get_next_line_rec(int fd, char *buf, char **result, int pos)
+{
+       int     len;
+       int     i;
+       int     readlen;
+
+       len = get_next_line_len(buf, pos);
+       *result = str_add_buffer(*result, buf + pos, len);
+       if (pos + len == BUFFER_SIZE && buf[BUFFER_SIZE - 1] == '\n')
+               return (clear_buffer(buf, pos));
+       i = 0;
+       while (i < len)
+               buf[pos + i++] = '\0';
+       if (pos + len == BUFFER_SIZE && *result)
+       {
+               readlen = read(fd, buf, BUFFER_SIZE);
+               if (readlen < 0)
+               {
+                       clear_buffer(buf, 0);
+                       free(*result);
+                       *result = NULL;
+               }
+               else if (readlen > 0)
+                       get_next_line_rec(fd, buf, result, 0);
+       }
+}
+
+char   *get_next_line(int fd)
+{
+       static char     buf[BUFFER_SIZE];
+       int                     i;
+       char            *result;
+       int                     readlen;
+
+       i = 0;
+       readlen = 0;
+       result = NULL;
+       while (i < BUFFER_SIZE && !buf[i])
+               i++;
+       if (i == BUFFER_SIZE)
+       {
+               readlen = read(fd, buf, BUFFER_SIZE);
+               if (readlen < 0)
+                       clear_buffer(buf, 0);
+               else if (readlen > 0)
+                       return (get_next_line(fd));
+               return (NULL);
+       }
+       get_next_line_rec(fd, buf, &result, i);
+       return (result);
+}
diff --git a/libft/get_next_line.h b/libft/get_next_line.h
new file mode 100644 (file)
index 0000000..63c477b
--- /dev/null
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line.h                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 14:14:07 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/25 14:19:27 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef GET_NEXT_LINE_H
+# define GET_NEXT_LINE_H
+# include <stdlib.h>
+# include <unistd.h>
+
+# ifndef BUFFER_SIZE
+#  define BUFFER_SIZE 42
+# endif
+
+int            ft_strlen(const char *str);
+char   *get_next_line(int fd);
+void   clear_buffer(char *buf, int start);
+char   *str_add_buffer(char *old_str, char *buf, int buf_len);
+#endif // GET_NEXT_LINE_H
diff --git a/libft/get_next_line_utils.c b/libft/get_next_line_utils.c
new file mode 100644 (file)
index 0000000..61171be
--- /dev/null
@@ -0,0 +1,60 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line_utils.c                              :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 14:14:59 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/26 10:49:08 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+
+int    ft_strlen(const char *str)
+{
+       int     len;
+
+       if (!str)
+               return (0);
+       len = 0;
+       while (str[len])
+               len++;
+       return (len);
+}
+
+void   clear_buffer(char *buf, int start)
+{
+       while (start < BUFFER_SIZE)
+               buf[start++] = '\0';
+}
+
+char   *str_add_buffer(char *old_str, char *buf, int buf_len)
+{
+       char    *result;
+       int             len;
+       int             i;
+
+       if (!old_str)
+               len = buf_len;
+       else
+               len = ft_strlen(old_str) + buf_len;
+       result = malloc(sizeof(char) * (len + 1));
+       if (!result)
+       {
+               free(old_str);
+               return (NULL);
+       }
+       result[len] = '\0';
+       i = 0;
+       while (old_str && old_str[i])
+       {
+               result[i] = old_str[i];
+               i++;
+       }
+       while (i < len)
+               result[i++] = *(buf++);
+       free(old_str);
+       return (result);
+}
diff --git a/libft/libft.h b/libft/libft.h
new file mode 100644 (file)
index 0000000..cc06bda
--- /dev/null
@@ -0,0 +1,73 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   libft.h                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 16:37:54 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/12 17:00:55 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef LIBFT_H
+# define LIBFT_H
+
+# include "ft_printf.h"
+# include "get_next_line.h"
+# include <stdlib.h>
+# include <string.h>
+# include <unistd.h>
+
+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_strlen(const char *str);
+void                           *ft_memset(void *b, int c, size_t len);
+void                           ft_bzero(void *s, size_t n);
+void                           *ft_memcpy(void *dst, const void *src, size_t n);
+void                           *ft_memmove(void *dst, const void *src, size_t len);
+size_t                         ft_strlcpy(char *dst, const char *src, size_t dstsize);
+size_t                         ft_strlcat(char *dst, const char *src, size_t dstsize);
+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 *haystack, const char *needle,
+                                               size_t len);
+int                                    ft_atoi(const char *str);
+void                           *ft_calloc(size_t count, size_t size);
+char                           *ft_strdup(const char *s1);
+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);
+
+typedef struct s_list
+{
+       void                    *content;
+       struct s_list   *next;
+}                                      t_list;
+t_list                         *ft_lstnew(void *content);
+void                           ft_lstadd_front(t_list **lst, t_list *new_elem);
+int                                    ft_lstsize(t_list *lst);
+t_list                         *ft_lstlast(t_list *lst);
+void                           ft_lstadd_back(t_list **lst, t_list *new_elem);
+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
diff --git a/main.c b/main.c
index d3d95a64a99a05c3ae4677fc9c5e7c901b3cbff3..2643ec2714672324f50c5ea453cd59b8464f4775 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,33 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/12 17:03:30 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/12 18:49:56 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft/libft.h"
 #include "push_swap.h"
 
-#include <stdio.h>
+static void    print_content(void *content)
+{
+       ft_printf("%d\n", *(int *)content);
+}
 
-int main(int argc, char *argv[])
+int    main(int argc, char *argv[])
 {
+       t_list  *stack_a;
+
+       stack_a = create_stack(argc, argv);
+       ft_lstiter(stack_a, print_content);
+       if (!stack_a)
+               return 1; // TODO: Print error message
+
+       // TODO: Sort stack
+       // TODO: Optimize commands
+       // TODO: Print commands
 }
index aa1fb3840e0a7377a56325c8c6b381a46e1affb2..0c86ee766152b5b74348e0c70d81b3b0dccaf0e0 100644 (file)
@@ -1,6 +1,35 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   push_swap.h                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/12 16:59:09 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/12 18:15:21 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
 #ifndef PUSH_SWAP_H
-#define PUSH_SWAP_H
+# define PUSH_SWAP_H
+# include "libft/libft.h"
+# include <stdlib.h>
 
+enum   e_pscmd
+{
+       SA,
+       SB,
+       SS,
+       PA,
+       PB,
+       RA,
+       RB,
+       RR,
+       RRA,
+       RRB,
+       RRR
+};
 
+t_list *create_stack(int argc, char *argv[]);
 
 #endif // PUSH_SWAP_H