intra to gitea
This commit is contained in:
commit
6cea9e2446
|
|
@ -0,0 +1,96 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# #
|
||||
# Updated: 2024/11/07 07:46:53 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = push_swap
|
||||
PRINTF = libftprintf.a
|
||||
LIBFT = libft.a
|
||||
SRC_FILES = main.c op.c sort_tiny.c stack.c utils.c sort_utils.c sort.c \
|
||||
calc_cheap.c calc_cheap_utils.c sort_2.c pars.c
|
||||
INCLUDES_FILES = include/pushswap.h
|
||||
SRC_DIR = srcs/
|
||||
SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
|
||||
OBJ_DIR = obj/
|
||||
OBJ = $(addprefix $(OBJ_DIR), $(SRC_FILES:.c=.o))
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Werror -Wextra -g3
|
||||
INCLUDE = -I include
|
||||
RM = rm -rf
|
||||
MKDIR = mkdir -p
|
||||
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[0;33m
|
||||
CYAN = \033[0;36m
|
||||
RESET = \033[0m
|
||||
BOLD = \033[1m
|
||||
WHITE = \033[0;97m
|
||||
|
||||
LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏
|
||||
|
||||
define PUSHSWAP_ART
|
||||
________ ___ ___ ________ ___ ___ ________ ___ __ ________ ________
|
||||
|\ __ \|\ \|\ \|\ ____\|\ \|\ \|\ ____\|\ \ |\ \|\ __ \|\ __ \\
|
||||
\ \ \|\ \ \ \\\ \ \ \___|\ \ \\\ \ \ \___|\ \ \ \ \ \ \ \|\ \ \ \|\ \\
|
||||
\ \ ____\ \ \\\ \ \_____ \ \ __ \ \_____ \ \ \ __\ \ \ \ __ \ \ ____\\
|
||||
\ \ \___|\ \ \\\ \|____|\ \ \ \ \ \|____|\ \ \ \|\__\_\ \ \ \ \ \ \ \___||
|
||||
\ \__\ \ \_______\____\_\ \ \__\ \__\____\_\ \ \____________\ \__\ \__\ \__\\
|
||||
\|__| \|_______|\_________\|__|\|__|\_________\|____________|\|__|\|__|\|__|
|
||||
\|_________| \|_________|
|
||||
|
||||
endef
|
||||
export PUSHSWAP_ART
|
||||
|
||||
all: print_art $(NAME)
|
||||
|
||||
print_art:
|
||||
@printf "$(CYAN)%s$(RESET)\n" "$$PUSHSWAP_ART"
|
||||
|
||||
$(NAME): $(OBJ) $(INCLUDES_FILES)
|
||||
@printf "$(BOLD)$(WHITE)Hello Sir,\nWelcome to the $(GREEN)PUSH_SWAP$(WHITE) compilation process.\nPlease hold on as we prepare your program.\n\n$(RESET)"
|
||||
@make -s -C printf_fd
|
||||
@make -s -C libft
|
||||
@printf "$(YELLOW)Compiling Push_swap, Please wait...$(RESET)"
|
||||
@for char in $(LOADING_CHARS); do \
|
||||
printf "\r$(YELLOW)Compiling Push_swap, Please wait... $$char$(RESET)"; \
|
||||
sleep 0.1; \
|
||||
done
|
||||
@$(CC) $(CFLAGS) $(OBJ) $(INCLUDE) printf_fd/$(PRINTF) libft/$(LIBFT) -o $(NAME)
|
||||
@printf "\r$(GREEN)Great news ! $(WHITE)Push_swap compiled successfully ! $(RESET)\n\n"
|
||||
@printf "$(BOLD)$(WHITE)Compilation complete !\n$(GREEN)PUSH_SWAP$(WHITE) is ready to use !\n"
|
||||
|
||||
$(OBJ_DIR)%.o: $(SRC_DIR)%.c
|
||||
@$(MKDIR) $(OBJ_DIR)
|
||||
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
|
||||
|
||||
clean:
|
||||
@make clean -s -C printf_fd
|
||||
@make clean -s -C libft
|
||||
@$(RM) $(OBJ_DIR)
|
||||
@printf "$(WHITE)Clean process completed for $(GREEN)Push_swap.$(RESET)\n"
|
||||
|
||||
clean1:
|
||||
@$(RM) $(OBJ_DIR)
|
||||
@printf "$(WHITE)Clean process completed for $(GREEN)Push_swap.$(RESET)\n"
|
||||
|
||||
fclean: clean1
|
||||
@make fclean -s -C printf_fd
|
||||
@make fclean -s -C libft
|
||||
@$(RM) $(NAME)
|
||||
@$(RM) $(PRINTF)
|
||||
@$(RM) $(LIBFT)
|
||||
@printf "$(WHITE)Full clean process completed for $(GREEN)Push_swap.$(RESET)\n"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re print_art
|
||||
|
||||
norm:
|
||||
norminette $(SRC_DIR) include/* printf_fd/* libft/*
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pushswap.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 18:06:41 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/06 01:29:32 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PUSHSWAP_H
|
||||
# define PUSHSWAP_H
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* INCLUDES */
|
||||
/* ************************************************************************** */
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <limits.h>
|
||||
# include "../printf_fd/ftprintf.h"
|
||||
# include "../libft/libft.h"
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* STRUCTURES */
|
||||
/* ************************************************************************** */
|
||||
|
||||
typedef struct s_node
|
||||
{
|
||||
int value;
|
||||
struct s_node *next;
|
||||
} t_node;
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
t_node *top;
|
||||
int size;
|
||||
} t_stack;
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* STACK OPERATIONS */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/* op.c */
|
||||
void add_stack(t_stack *stack, int value);
|
||||
int swap(t_stack *stack);
|
||||
int push(t_stack *stack_1, t_stack *stack_2);
|
||||
int rotate(t_stack *stack);
|
||||
int rrotate(t_stack *stack);
|
||||
|
||||
/* stack.c */
|
||||
void free_stack(t_stack *stack);
|
||||
t_stack *init_stack(void);
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* SORTING FUNCTIONS */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/* sort_tiny.c */
|
||||
void sort_2(t_stack *stack_a);
|
||||
void sort_3(t_stack *stack_a);
|
||||
void sort_3_bis(t_node *v1, t_node *v2, t_node *v3, t_stack *stack_a);
|
||||
|
||||
/* sort.c */
|
||||
void sort(t_stack *stack_a, t_stack *stack_b);
|
||||
void final_sort(t_stack *stack_a);
|
||||
void opti_2(int *move);
|
||||
void opti(int *move);
|
||||
|
||||
/* sort_2.c */
|
||||
void make_move(t_stack *stack_a, t_stack *stack_b, int cheap_ind);
|
||||
int calc_move_stack_2(t_stack *stack, int index);
|
||||
void make_move_a(t_stack *stack_a, int move);
|
||||
void make_move_b(t_stack *stack_b, int move);
|
||||
void make_move_r(t_stack *stack_b, t_stack *stack_a, int move);
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* UTILITY FUNCTIONS */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/* utils.c */
|
||||
t_node *go_to(t_node *top, int pos);
|
||||
int valid_input(const char *str);
|
||||
int in_stack(t_stack *stack, int n);
|
||||
int is_sorted(t_stack *stack);
|
||||
|
||||
/* sort_utils.c */
|
||||
int get_median(t_stack *stack, int size);
|
||||
void sort_int_table(int *tab, int size);
|
||||
int median_calc(int *tab, int size);
|
||||
|
||||
/* calc_cheaper_utils.c */
|
||||
int calc_index_min(t_stack *stack_a);
|
||||
int calc_index_max(t_stack *stack_a);
|
||||
int return_index(t_stack *stack, int value);
|
||||
int return_value(t_stack *stack, int index);
|
||||
int calc_index_a(t_stack *stack_a, int value);
|
||||
|
||||
/* calc_cheaper.c */
|
||||
int calc_cheaper(t_stack *stack_a, t_stack *stack_b);
|
||||
int calc_move(t_stack *stack_a, t_stack *stack_b, int index_b);
|
||||
int calc_move_stack(t_stack *stack, int index);
|
||||
|
||||
/* pars.c */
|
||||
char **pars(char **av);
|
||||
void free_array(char **array);
|
||||
char *init_empty_str(void);
|
||||
/* main.c */
|
||||
int main(int ac, char **av);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/05/19 12:59:31 by lfirmin #+# #+# #
|
||||
# Updated: 2024/05/22 15:18:38 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = libft.a
|
||||
HEADER = ./
|
||||
SRC = ft_isalnum.c ft_isprint.c ft_memcmp.c ft_putchar_fd.c ft_split.c \
|
||||
ft_strlcat.c ft_strncmp.c ft_substr.c ft_atoi.c ft_isalpha.c \
|
||||
ft_itoa.c ft_memcpy.c ft_putendl_fd.c ft_strchr.c ft_strlcpy.c \
|
||||
ft_strnstr.c ft_tolower.c ft_bzero.c ft_isascii.c ft_strtrim.c \
|
||||
ft_memmove.c ft_putnbr_fd.c ft_strdup.c ft_strlen.c ft_strrchr.c \
|
||||
ft_toupper.c ft_calloc.c ft_isdigit.c ft_memchr.c ft_memset.c \
|
||||
ft_putstr_fd.c ft_strjoin.c ft_strmapi.c ft_striteri.c \
|
||||
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 ft_strcmp.c \
|
||||
ft_atoll.c ft_print_array.c ft_strcat.c
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror -g3
|
||||
INCLUDE = -I $(HEADER)
|
||||
OBJ_DIR = obj/
|
||||
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
|
||||
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[0;33m
|
||||
RESET = \033[0m
|
||||
WHITE = \033[0;97m
|
||||
|
||||
LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@printf "$(YELLOW)Compiling libft, Please wait...$(RESET)"
|
||||
@for char in $(LOADING_CHARS); do \
|
||||
printf "\r$(YELLOW)Compiling libft, Please wait... $$char$(RESET)"; \
|
||||
sleep 0.1; \
|
||||
done
|
||||
@ar rc $(NAME) $(OBJ)
|
||||
@ranlib $(NAME)
|
||||
@printf "\r$(GREEN)Great news ! $(WHITE)Libft compiled successfully ! $(RESET)\n"
|
||||
|
||||
$(OBJ_DIR)%.o: %.c
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@printf "$(WHITE)Clean process completed for $(GREEN)Libft.$(RESET)\n"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@printf "$(WHITE)Full clean process completed for $(GREEN)Libft.$(RESET)\n"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 10:09:46 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/03 21:02:02 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i;
|
||||
int s;
|
||||
int m;
|
||||
|
||||
i = 0;
|
||||
s = 0;
|
||||
m = 0;
|
||||
while ((str[i] <= 13 && str[i] >= 9) || str[i] == 32)
|
||||
i++;
|
||||
while (str[i] == '-' || str[i] == '+')
|
||||
{
|
||||
s++;
|
||||
if (s >= 2)
|
||||
return (0);
|
||||
if (str[i] == '-')
|
||||
m++;
|
||||
i++;
|
||||
}
|
||||
s = 0;
|
||||
while (str[i] >= 48 && str[i] <= 57)
|
||||
s = s * 10 + (str[i++] - 48);
|
||||
if (m % 2 == 1)
|
||||
s = s * -1;
|
||||
return (s);
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoll.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/03 17:25:06 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:25:33 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
long long ft_atoll(const char *str)
|
||||
{
|
||||
long long result;
|
||||
int sign;
|
||||
|
||||
result = 0;
|
||||
sign = 1;
|
||||
while (*str == ' ' || (*str >= 9 && *str <= 13))
|
||||
str++;
|
||||
if (*str == '-' || *str == '+')
|
||||
{
|
||||
if (*str == '-')
|
||||
sign = -1;
|
||||
str++;
|
||||
}
|
||||
while (*str >= '0' && *str <= '9')
|
||||
{
|
||||
if (result > INT_MAX || result < INT_MIN)
|
||||
return (2147483648);
|
||||
result = result * 10 + (*str - '0');
|
||||
str++;
|
||||
}
|
||||
return (result * sign);
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_bzero.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 00:20:00 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/30 19:49:23 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_bzero(void *b, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
unsigned char *r;
|
||||
|
||||
r = (unsigned char *)b;
|
||||
i = 0;
|
||||
while (i < len)
|
||||
r[i++] = '\0';
|
||||
return (b);
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_calloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 11:14:02 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/03 18:15:05 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_calloc(size_t c, size_t s)
|
||||
{
|
||||
void *ptr;
|
||||
long long int tmp1;
|
||||
long long int tmp2;
|
||||
|
||||
tmp1 = (long long int)c;
|
||||
tmp2 = (long long int)s;
|
||||
if ((c > 4294967295 || s > 4294967295) && (tmp1 < 0 && tmp2 < 0))
|
||||
return (NULL);
|
||||
if (tmp1 * tmp2 < 0)
|
||||
return (NULL);
|
||||
ptr = malloc(c * s);
|
||||
if (!ptr)
|
||||
return (NULL);
|
||||
ft_bzero(ptr, c * s);
|
||||
return (ptr);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isalnum.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 02:29:49 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isalnum(int c)
|
||||
{
|
||||
if ((c >= 97 && c <= 122) || (c >= 48 && c <= 57) || (c >= 65 && c <= 90))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isalpha.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 01:55:52 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isalpha(int c)
|
||||
{
|
||||
if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isascii.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 02:36:17 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isascii(int c)
|
||||
{
|
||||
if (c >= 0 && c <= 127)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isdigit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 02:31:51 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isdigit(int c)
|
||||
{
|
||||
if (c >= 48 && c <= 57)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isprint.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 02:38:24 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/23 06:21:54 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isprint(int c)
|
||||
{
|
||||
if (((c >= 0 && c <= 31) || c >= 127 || c == EOF))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/24 12:59:00 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/29 10:31:36 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
static size_t ft_count(long int n)
|
||||
{
|
||||
size_t c;
|
||||
|
||||
c = 0;
|
||||
if (n <= 0)
|
||||
c = 1;
|
||||
while (n != 0)
|
||||
{
|
||||
n = n / 10;
|
||||
c++;
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *str;
|
||||
size_t size;
|
||||
long int num;
|
||||
|
||||
num = n;
|
||||
size = ft_count(num);
|
||||
str = (char *)malloc(size + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[size] = '\0';
|
||||
if (num < 0)
|
||||
num = -num;
|
||||
while (size--)
|
||||
{
|
||||
str[size] = num % 10 + '0';
|
||||
num = num / 10;
|
||||
if (n < 0 && size == 0)
|
||||
str[size] = '-';
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_back.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 02:01:18 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/03 15:59:33 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstadd_back(t_list **lst, t_list *new)
|
||||
{
|
||||
t_list *last;
|
||||
|
||||
if (!new)
|
||||
return ;
|
||||
if (!lst || !*lst)
|
||||
{
|
||||
*lst = new;
|
||||
return ;
|
||||
}
|
||||
last = ft_lstlast(*lst);
|
||||
last->next = new;
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_front.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/31 20:34:57 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/31 20:41:34 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstadd_front(t_list **lst, t_list *new)
|
||||
{
|
||||
if (lst && new)
|
||||
{
|
||||
new->next = *lst;
|
||||
*lst = new;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstclear.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 20:46:07 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/01 20:51:07 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstclear(t_list **lst, void (*del)(void*))
|
||||
{
|
||||
t_list *t;
|
||||
|
||||
if (lst)
|
||||
{
|
||||
while (*lst)
|
||||
{
|
||||
t = (*lst)->next;
|
||||
ft_lstdelone(*lst, del);
|
||||
(*lst) = t;
|
||||
}
|
||||
(*lst) = NULL;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstdelone.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 20:32:56 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/01 20:32:56 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void*))
|
||||
{
|
||||
if (!lst || !del)
|
||||
return ;
|
||||
del(lst->content);
|
||||
free(lst);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstiter.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 21:05:12 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/01 21:05:12 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstiter(t_list *lst, void (*f)(void *))
|
||||
{
|
||||
if (!lst || !f)
|
||||
return ;
|
||||
while (lst)
|
||||
{
|
||||
f(lst->content);
|
||||
lst = lst->next;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstlast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 01:52:09 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/01 01:57:22 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstlast(t_list *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
if (!lst->next)
|
||||
return (lst);
|
||||
lst = lst->next;
|
||||
}
|
||||
return (lst);
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstmap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/02 18:21:09 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/03 15:53:13 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||
{
|
||||
t_list *new;
|
||||
t_list *save;
|
||||
|
||||
if (!lst || !del || !f)
|
||||
return (NULL);
|
||||
new = ft_lstnew(lst->content);
|
||||
if (!new)
|
||||
return (NULL);
|
||||
new->content = f(new->content);
|
||||
save = new;
|
||||
lst = lst->next;
|
||||
while (lst)
|
||||
{
|
||||
new->next = ft_lstnew(lst->content);
|
||||
if (!new->next)
|
||||
{
|
||||
ft_lstclear(&save, del);
|
||||
return (NULL);
|
||||
}
|
||||
new->next->content = f(new->next->content);
|
||||
new = new->next;
|
||||
lst = lst->next;
|
||||
}
|
||||
new->next = NULL;
|
||||
return (save);
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstnew.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/31 18:41:23 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/31 20:33:26 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstnew(void *content)
|
||||
{
|
||||
t_list *new;
|
||||
|
||||
new = (t_list *)malloc(sizeof(*new));
|
||||
if (!new)
|
||||
return (NULL);
|
||||
new->content = content;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstsize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/31 22:00:49 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/31 22:12:17 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_lstsize(t_list *lst)
|
||||
{
|
||||
int i;
|
||||
t_list *count;
|
||||
|
||||
count = lst;
|
||||
i = 0;
|
||||
while (count)
|
||||
{
|
||||
count = count->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 07:40:39 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memchr(const void *s, int c, size_t n)
|
||||
{
|
||||
unsigned char *str;
|
||||
|
||||
str = (unsigned char *)s;
|
||||
while (n--)
|
||||
{
|
||||
if (*str == (unsigned char)c)
|
||||
return (str);
|
||||
str++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 07:57:58 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 11:34:35 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_memcmp(const void *ptr1, const void *ptr2, size_t num)
|
||||
{
|
||||
unsigned char *pt1;
|
||||
unsigned char *pt2;
|
||||
size_t i;
|
||||
|
||||
pt1 = (unsigned char *)ptr1;
|
||||
pt2 = (unsigned char *)ptr2;
|
||||
i = 0;
|
||||
while (i < num)
|
||||
{
|
||||
if (pt1[i] != pt2[i])
|
||||
return (pt1[i] - pt2[i]);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 01:08:31 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/30 18:46:00 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memcpy(void *dest, const void *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
unsigned char *r;
|
||||
unsigned char *s;
|
||||
|
||||
i = 0;
|
||||
r = (unsigned char *)dest;
|
||||
s = (unsigned char *)src;
|
||||
if (dest == (void *)0 && src == (void *)0)
|
||||
return (dest);
|
||||
while (i < len)
|
||||
{
|
||||
r[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memmov.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 01:26:13 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memmove(void *s1, const void *s2, size_t len)
|
||||
{
|
||||
unsigned char *dest;
|
||||
unsigned char *src;
|
||||
unsigned int i;
|
||||
|
||||
dest = (unsigned char *)s1;
|
||||
src = (unsigned char *)s2;
|
||||
i = 0;
|
||||
if (dest == NULL && src == NULL)
|
||||
return (NULL);
|
||||
if (dest < src)
|
||||
{
|
||||
while (i < len)
|
||||
{
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (0 < len--)
|
||||
dest[len] = src[len];
|
||||
}
|
||||
return (dest);
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 02:42:40 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memset(void *b, int c, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
unsigned char *r;
|
||||
|
||||
i = 0;
|
||||
r = (unsigned char *)b;
|
||||
while (i < len)
|
||||
r[i++] = (unsigned char)c;
|
||||
return (b);
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_array.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/05 02:29:54 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/05 02:29:54 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void put_str(char *str)
|
||||
{
|
||||
if (!str)
|
||||
return ;
|
||||
while (*str)
|
||||
write(1, str++, 1);
|
||||
}
|
||||
|
||||
void ft_print_str_array(char **array)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (array == NULL)
|
||||
{
|
||||
put_str("Tableau invalide\n");
|
||||
return ;
|
||||
}
|
||||
i = 0;
|
||||
put_str("Contenu du tableau:\n");
|
||||
while (array[i])
|
||||
{
|
||||
put_str(array[i]);
|
||||
write(1, "\n", 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/29 11:17:45 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/29 11:20:42 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putchar_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/29 11:35:44 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/29 11:38:36 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putendl_fd(char *s, int fd)
|
||||
{
|
||||
ft_putstr_fd(s, fd);
|
||||
ft_putchar_fd('\n', fd);
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/29 11:42:38 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/29 11:56:04 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putnbr_fd(int n, int fd)
|
||||
{
|
||||
long int nb;
|
||||
|
||||
nb = n;
|
||||
if (nb < 0)
|
||||
{
|
||||
nb = -nb;
|
||||
ft_putchar_fd('-', fd);
|
||||
}
|
||||
if (nb >= 10)
|
||||
{
|
||||
ft_putnbr_fd(nb / 10, fd);
|
||||
ft_putnbr_fd(nb % 10, fd);
|
||||
}
|
||||
else
|
||||
ft_putchar_fd(nb + '0', fd);
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/29 11:27:05 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/29 11:33:18 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putstr_fd(char *s, int fd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
ft_putchar_fd(s[i], fd);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/22 05:56:37 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/04 12:08:51 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
static size_t ft_countword(char const *s, char c)
|
||||
{
|
||||
size_t count;
|
||||
|
||||
if (!*s)
|
||||
return (0);
|
||||
count = 0;
|
||||
while (*s)
|
||||
{
|
||||
while (*s == c)
|
||||
s++;
|
||||
if (*s)
|
||||
count++;
|
||||
while (*s != c && *s)
|
||||
s++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
char **lst;
|
||||
size_t word_len;
|
||||
int i;
|
||||
|
||||
lst = (char **)malloc((ft_countword(s, c) + 1) * sizeof(char *));
|
||||
if (!s || !lst)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (*s)
|
||||
{
|
||||
while (*s == c && *s)
|
||||
s++;
|
||||
if (*s)
|
||||
{
|
||||
if (!ft_strchr(s, c))
|
||||
word_len = ft_strlen(s);
|
||||
else
|
||||
word_len = ft_strchr(s, c) - s;
|
||||
lst[i++] = ft_substr(s, 0, word_len);
|
||||
s += word_len;
|
||||
}
|
||||
}
|
||||
lst[i] = NULL;
|
||||
return (lst);
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strcat.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/05 02:21:14 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/05 08:19:47 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int should_add_space(char *dest, char *src)
|
||||
{
|
||||
size_t dest_len;
|
||||
size_t src_len;
|
||||
|
||||
dest_len = ft_strlen(dest);
|
||||
src_len = ft_strlen(src);
|
||||
if (dest_len > 0 && src_len > 0)
|
||||
{
|
||||
if (dest[dest_len - 1] != ' ' && src[0] != ' ')
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *create_new_str(char *dest, char *src, int add_space)
|
||||
{
|
||||
size_t dest_len;
|
||||
size_t src_len;
|
||||
char *new_str;
|
||||
|
||||
dest_len = ft_strlen(dest);
|
||||
src_len = ft_strlen(src);
|
||||
if (add_space == 1)
|
||||
new_str = malloc(sizeof(char) * (dest_len + src_len + 2));
|
||||
else
|
||||
new_str = malloc(sizeof(char) * (dest_len + src_len + 1));
|
||||
if (!new_str)
|
||||
return (NULL);
|
||||
return (new_str);
|
||||
}
|
||||
|
||||
void fill_new_str(char *new_str, char *dest, char *src, int add_space)
|
||||
{
|
||||
size_t dest_len;
|
||||
size_t src_len;
|
||||
|
||||
dest_len = ft_strlen(dest);
|
||||
src_len = ft_strlen(src);
|
||||
if (add_space == 1)
|
||||
{
|
||||
ft_memset(new_str, 0, dest_len + src_len + 2);
|
||||
ft_strlcpy(new_str, dest, dest_len + 1);
|
||||
new_str[dest_len] = ' ';
|
||||
ft_strlcpy(new_str + dest_len + 1, src, src_len + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_memset(new_str, 0, dest_len + src_len + 1);
|
||||
ft_strlcpy(new_str, dest, dest_len + 1);
|
||||
ft_strlcpy(new_str + dest_len, src, src_len + 1);
|
||||
}
|
||||
}
|
||||
|
||||
char *ft_strcat(char *dest, char *src)
|
||||
{
|
||||
char *new_str;
|
||||
int add_space;
|
||||
|
||||
if (!dest || !src)
|
||||
return (NULL);
|
||||
add_space = should_add_space(dest, src);
|
||||
new_str = create_new_str(dest, src, add_space);
|
||||
if (!new_str)
|
||||
return (NULL);
|
||||
fill_new_str(new_str, dest, src, add_space);
|
||||
free(dest);
|
||||
return (new_str);
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 06:46:40 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/24 08:51:25 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strchr(char const *str, int c)
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
if (*str == (char)c)
|
||||
return ((char *)str);
|
||||
str++;
|
||||
}
|
||||
if (*str == (char)c)
|
||||
return ((char *)str);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strcmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/19 21:01:26 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/10/19 21:01:26 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_strcmp(char *s1, char *s2)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
|
||||
i++;
|
||||
return (s1[i] - s2[i]);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 05:28:24 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strdup(const char *src)
|
||||
{
|
||||
char *dest;
|
||||
int i;
|
||||
int size;
|
||||
|
||||
size = 0;
|
||||
while (src[size] != '\0')
|
||||
size++;
|
||||
dest = malloc(sizeof(char) * (size + 1));
|
||||
if (dest == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i != size)
|
||||
{
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_striteri.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/29 10:54:40 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/29 11:12:05 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char*))
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (s)
|
||||
{
|
||||
while (s[i])
|
||||
{
|
||||
f(i, &s[i]);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strjoin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/22 01:00:14 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/24 08:49:06 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strjoin(const char *s1, const char *s2)
|
||||
{
|
||||
char *res;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (s1 == NULL && s2 == NULL)
|
||||
return (NULL);
|
||||
res = (char *) malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
res[j++] = s1[i++];
|
||||
i = 0;
|
||||
while (s2[i])
|
||||
res[j++] = s2[i++];
|
||||
res[j] = 0;
|
||||
return (res);
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcat.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 03:55:25 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlcat(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
size_t r;
|
||||
size_t s;
|
||||
|
||||
i = 0;
|
||||
r = ft_strlen(dst);
|
||||
s = ft_strlen(src);
|
||||
if (size <= r)
|
||||
return (s + size);
|
||||
while (r + i < size - 1 && src[i] != '\0')
|
||||
{
|
||||
dst[r + i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dst[r + i] = '\0';
|
||||
return (r + s);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 02:18:17 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/30 18:36:18 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlcpy(char *dst, const char *src, size_t dsts)
|
||||
{
|
||||
size_t srcs;
|
||||
size_t i;
|
||||
|
||||
ft_strlen(src);
|
||||
if (!src || !dst)
|
||||
return (0);
|
||||
srcs = ft_strlen(src);
|
||||
i = 0;
|
||||
if (dsts != 0)
|
||||
{
|
||||
while (src[i] != '\0' && i < (dsts - 1))
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dst[i] = '\0';
|
||||
}
|
||||
return (srcs);
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/19 23:16:45 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/23 07:36:59 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
i++;
|
||||
return ((size_t)i);
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strmapi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/28 15:10:42 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/28 17:00:27 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
|
||||
{
|
||||
char *str;
|
||||
int i;
|
||||
|
||||
if (!s)
|
||||
return (NULL);
|
||||
str = (char *)malloc(ft_strlen(s) + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
str[i] = f(i, s[i]);
|
||||
++i;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strncmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 06:55:09 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/24 08:47:59 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_strncmp(const char *s1, const char *s2, unsigned int n)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned char c1;
|
||||
unsigned char c2;
|
||||
|
||||
i = 0;
|
||||
if (n == 0)
|
||||
return (0);
|
||||
while (i < n)
|
||||
{
|
||||
c1 = (unsigned char)s1[i];
|
||||
c2 = (unsigned char)s2[i];
|
||||
if (c1 != c2 || c1 == '\0' || c2 == '\0')
|
||||
return (c1 - c2);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strnstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 08:22:59 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strnstr(const char *hay, const char *need, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
size_t n;
|
||||
|
||||
i = 0;
|
||||
if (need[0] == '\0')
|
||||
return ((char *)hay);
|
||||
while (hay[i] != '\0')
|
||||
{
|
||||
n = 0;
|
||||
while (hay[i + n] == need[n] && (i + n) < len)
|
||||
{
|
||||
if (hay[i + n] == '\0' && need[n] == '\0')
|
||||
return ((char *)&hay[i]);
|
||||
n++;
|
||||
}
|
||||
if (need[n] == '\0')
|
||||
return ((char *)hay + i);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strrchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 06:42:38 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/30 18:30:00 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strrchr(const char *str, int c)
|
||||
{
|
||||
int i;
|
||||
int last_occurrence;
|
||||
|
||||
ft_strlen(str);
|
||||
i = 0;
|
||||
last_occurrence = -1;
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == (char)c)
|
||||
last_occurrence = i;
|
||||
i++;
|
||||
}
|
||||
if ((char)c == '\0')
|
||||
return ((char *)&str[i]);
|
||||
if (last_occurrence != -1)
|
||||
return ((char *)&str[last_occurrence]);
|
||||
return (NULL);
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strtrim.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/23 21:32:01 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/31 02:04:11 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int is_in_set(char c, const char *set)
|
||||
{
|
||||
while (*set)
|
||||
{
|
||||
if (c == *set)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
set++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static char *ft_strncpy(char *dest, const char *src, unsigned int n)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
i = 0;
|
||||
while (src[i] != '\0' && i < n)
|
||||
{
|
||||
dest[i] = src[i];
|
||||
++i;
|
||||
}
|
||||
while (i < n)
|
||||
{
|
||||
dest[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
||||
|
||||
char *ft_strtrim(const char *s1, const char *set)
|
||||
{
|
||||
size_t s;
|
||||
size_t e;
|
||||
size_t len;
|
||||
char *str;
|
||||
|
||||
if (!s1 || !set)
|
||||
return (NULL);
|
||||
s = 0;
|
||||
while (s1[s] && is_in_set(s1[s], set))
|
||||
s++;
|
||||
e = ft_strlen(s1);
|
||||
while (e > s && is_in_set(s1[e - 1], set))
|
||||
e--;
|
||||
len = e - s;
|
||||
str = (char *)malloc(sizeof(char) * (len + 1));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
ft_strncpy(str, s1 + s, len);
|
||||
str[len] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_substr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/22 00:15:54 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/24 08:28:01 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
char *sub;
|
||||
size_t i;
|
||||
|
||||
if ((ft_strlen(s) - start) < len)
|
||||
len = ft_strlen(s) - start;
|
||||
if (ft_strlen(s) < start)
|
||||
return (ft_strdup(""));
|
||||
i = 0;
|
||||
sub = (char *) malloc(len + 1);
|
||||
if (!sub)
|
||||
return (NULL);
|
||||
while (len > 0)
|
||||
{
|
||||
sub[i++] = s[start++];
|
||||
len--;
|
||||
}
|
||||
sub[i] = '\0';
|
||||
return (sub);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_tolower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 04:50:40 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 11:09:36 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_tolower(int c)
|
||||
{
|
||||
if (c >= 65 && c <= 90)
|
||||
return (c + 32);
|
||||
return (c);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_toupper.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 04:50:40 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/05/22 09:59:44 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "libft.h"
|
||||
|
||||
int ft_toupper(int c)
|
||||
{
|
||||
if (c >= 97 && c <= 122)
|
||||
return (c - 32);
|
||||
return (c);
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libft.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/21 11:19:08 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/02 21:12:47 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#ifndef LIBFT_H
|
||||
# define LIBFT_H
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <stddef.h>
|
||||
# include <stdio.h>
|
||||
# include <unistd.h>
|
||||
# include <limits.h>
|
||||
|
||||
typedef struct s_list {
|
||||
void *content;
|
||||
struct s_list *next;
|
||||
} t_list;
|
||||
|
||||
int ft_isalnum(int c);
|
||||
int ft_isalpha(int c);
|
||||
int ft_isascii(int c);
|
||||
int ft_isdigit(int c);
|
||||
int ft_isprint(int c);
|
||||
int ft_atoi(const char *str);
|
||||
int ft_memcmp(const void *ptr1, const void *ptr2, size_t num);
|
||||
int ft_strncmp(const char *s1, const char *s2, unsigned int n);
|
||||
int ft_toupper(int c);
|
||||
int ft_tolower(int c);
|
||||
int ft_lstsize(t_list *lst);
|
||||
int ft_strcmp(char *s1, char *s2);
|
||||
long long ft_atoll(const char *str);
|
||||
|
||||
size_t ft_strlen(const char *s);
|
||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||
size_t ft_strlcpy(char *dst, const char *src, size_t dsts);
|
||||
|
||||
void *ft_bzero(void *s, size_t n);
|
||||
void *ft_calloc(size_t count, size_t n);
|
||||
void *ft_memchr(const void *s, int c, size_t n);
|
||||
void *ft_memcpy(void *dest, const void *src, size_t len);
|
||||
void *ft_memset(void *b, int c, size_t len);
|
||||
void *ft_memmove(void *s1, const void *s2, size_t len);
|
||||
void *ft_memset(void *str, int c, size_t n);
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||
void ft_lstadd_front(t_list **lst, t_list *new);
|
||||
void ft_lstadd_back(t_list **lst, t_list *new);
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void*));
|
||||
void ft_lstclear(t_list **lst, void (*del)(void*));
|
||||
void ft_print_str_array(char **array);
|
||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), \
|
||||
void (*del)(void *));
|
||||
|
||||
char *ft_strchr(char const *str, int c);
|
||||
char *ft_strdup(const char *src);
|
||||
char *ft_strjoin(const char *s1, const char *s2);
|
||||
char *ft_strnstr(const char *hay, const char *need, size_t len);
|
||||
char *ft_strrchr(const char *str, int c);
|
||||
char *ft_strtrim(char const *s1, char const *set);
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||
char **ft_split(char const *s, char c);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
char *ft_itoa(int n);
|
||||
char *ft_strcat(char *dest, char *src);
|
||||
|
||||
t_list *ft_lstnew(void *content);
|
||||
t_list *ft_lstlast(t_list *lst);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# #
|
||||
# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = libftprintf.a
|
||||
HEADER = ./
|
||||
SRC = ft_putchar.c ft_put_ptr.c ft_conv_ith.c ft_hex_len.c \
|
||||
ft_sorting.c ft_printf.c ft_print_ith.c ft_printchar.c \
|
||||
ft_printstr.c ft_printpercent.c ft_print_unsigned.c \
|
||||
ft_print_nbr.c ft_print_ptr.c ft_adrr_len.c ft_print_null.c \
|
||||
ft_itoa.c ft_unsigned_itoa.c ft_count.c
|
||||
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
INCLUDE = -I $(HEADER)
|
||||
OBJ_DIR = obj/
|
||||
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
|
||||
|
||||
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[0;33m
|
||||
RESET = \033[0m
|
||||
WHITE = \033[0;97m
|
||||
|
||||
LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@printf "$(YELLOW)Compiling ft_printf, Please wait...$(RESET)"
|
||||
@for char in $(LOADING_CHARS); do \
|
||||
printf "\r$(YELLOW)Compiling ft_print, Please wait... $$char$(RESET)"; \
|
||||
sleep 0.1; \
|
||||
done
|
||||
@ar rc $(NAME) $(OBJ)``
|
||||
@ranlib $(NAME)
|
||||
@printf "\r$(GREEN)Great news ! $(WHITE)ft_printf compiled successfully ! $(RESET)\n"
|
||||
|
||||
$(OBJ_DIR)%.o: %.c
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@printf "$(WHITE)Clean process completed for $(GREEN)Ft_printf.$(RESET)\n"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@printf "$(WHITE)Full clean process completed for $(GREEN)Ft_printf.$(RESET)\n"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_adrr_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 05:16:57 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:50:34 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_adrr_len(unsigned long long nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (nbr == 0)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (nbr != 0)
|
||||
{
|
||||
i++;
|
||||
nbr = nbr / 16;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_conv_ith.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:46:47 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:23 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
void ft_conv_ith(int fd, unsigned int nbr, const char str)
|
||||
{
|
||||
if (nbr >= 16)
|
||||
{
|
||||
ft_conv_ith(fd, (nbr / 16), str);
|
||||
nbr = nbr % 16;
|
||||
}
|
||||
if (nbr <= 9)
|
||||
ft_putchar(fd, nbr + '0');
|
||||
else
|
||||
{
|
||||
if (str == 'x')
|
||||
ft_putchar(fd, nbr - 10 + 'a');
|
||||
else if (str == 'X')
|
||||
ft_putchar(fd, nbr - 10 + 'A');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_count.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:52:36 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:47:07 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
size_t ft_count(long long int n)
|
||||
{
|
||||
size_t c;
|
||||
|
||||
c = 0;
|
||||
if (n <= 0)
|
||||
c = 1;
|
||||
while (n != 0)
|
||||
{
|
||||
n = n / 10;
|
||||
c++;
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_hex_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:45:50 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:49:59 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_hex_len(unsigned int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (nbr == 0)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (nbr != 0)
|
||||
{
|
||||
i++;
|
||||
nbr = nbr / 16;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:51:58 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:53:33 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *str;
|
||||
size_t size;
|
||||
long int num;
|
||||
|
||||
num = n;
|
||||
size = ft_count(num);
|
||||
str = (char *)malloc(size + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[size] = '\0';
|
||||
if (num < 0)
|
||||
num = -num;
|
||||
while (size--)
|
||||
{
|
||||
str[size] = num % 10 + '0';
|
||||
num = num / 10;
|
||||
if (n < 0 && size == 0)
|
||||
str[size] = '-';
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_ith.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:49:51 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_ith(int fd, unsigned int nbr, const char str)
|
||||
{
|
||||
if (nbr == 0)
|
||||
return (write(fd, "0", 1));
|
||||
ft_conv_ith(fd, nbr, str);
|
||||
return (ft_hex_len(nbr));
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_nbr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:52:47 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_nbr(int fd, int nb)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
str = ft_itoa(nb);
|
||||
len = ft_printstr(fd, str);
|
||||
free (str);
|
||||
return (len);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_null.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 07:01:50 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:39 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_null(int fd)
|
||||
{
|
||||
write(fd, "(null)", 6);
|
||||
return (6);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_ptr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:51:10 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_ptr(int fd, unsigned long long ptr)
|
||||
{
|
||||
int print_length;
|
||||
|
||||
if (ptr == 0)
|
||||
{
|
||||
write(fd, "(nil)", 5);
|
||||
return (5);
|
||||
}
|
||||
print_length = 2;
|
||||
write(fd, "0x", 2);
|
||||
{
|
||||
ft_put_ptr(fd, ptr);
|
||||
print_length = print_length + ft_adrr_len(ptr);
|
||||
}
|
||||
return (print_length);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_unsigned.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:50:53 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:51:31 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_unsigned(int fd, unsigned int nb)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
str = ft_unsigned_itoa(nb);
|
||||
len = ft_printstr(fd, str);
|
||||
free (str);
|
||||
return (len);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printchar.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:56:02 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:56:25 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printchar(int fd, int c)
|
||||
{
|
||||
write (fd, &c, 1);
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:55:41 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printf_fd(int fd, const char *str, ...)
|
||||
{
|
||||
int i;
|
||||
va_list list;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
va_start(list, str);
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '%')
|
||||
{
|
||||
count += ft_sorting(list, str[i + 1], fd);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
write (fd, &str[i], 1);
|
||||
count++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
va_end(list);
|
||||
return (count);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printpercent.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:47:38 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:50:08 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printpercent(int fd)
|
||||
{
|
||||
ft_putchar(fd, '%');
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:55:08 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:05 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printstr(int fd, char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!s)
|
||||
return (ft_print_null(fd));
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
write (fd, &s[i], 1);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put_ptr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:54:06 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:52 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
void ft_put_ptr(int fd, unsigned long long num)
|
||||
{
|
||||
if (num >= 16)
|
||||
{
|
||||
ft_put_ptr(fd, num / 16);
|
||||
ft_put_ptr(fd, num % 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (num <= 9)
|
||||
ft_putchar(fd, num + '0');
|
||||
else
|
||||
ft_putchar(fd, num - 10 + 'a');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:45:04 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:49:11 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
void ft_putchar(int fd, char c)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sorting.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:56:45 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:51:17 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_sorting(va_list list, const char str, int fd)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (str == 'c')
|
||||
count += ft_printchar(fd, va_arg(list, int));
|
||||
else if (str == 's')
|
||||
count += ft_printstr(fd, va_arg(list, char *));
|
||||
else if (str == 'p')
|
||||
count += ft_print_ptr(fd, va_arg(list, unsigned long long));
|
||||
else if (str == 'd' || str == 'i')
|
||||
count += ft_print_nbr(fd, va_arg(list, int));
|
||||
else if (str == 'u')
|
||||
count += ft_print_unsigned(fd, va_arg(list, unsigned int));
|
||||
else if (str == 'x' || str == 'X')
|
||||
count += ft_print_ith(fd, va_arg(list, unsigned int), str);
|
||||
else if (str == '%')
|
||||
count += ft_printpercent(fd);
|
||||
return (count);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_unsigned_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:51:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:45:46 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
char *ft_unsigned_itoa(unsigned int n)
|
||||
{
|
||||
char *str;
|
||||
size_t size;
|
||||
long int num;
|
||||
|
||||
num = n;
|
||||
size = ft_count(num);
|
||||
str = (char *)malloc(size + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[size] = '\0';
|
||||
if (num < 0)
|
||||
num = -num;
|
||||
while (size--)
|
||||
{
|
||||
str[size] = num % 10 + '0';
|
||||
num = num / 10;
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ftprintf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:49:08 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FTPRINTF_H
|
||||
# define FTPRINTF_H
|
||||
|
||||
# include <stdarg.h>
|
||||
# include <unistd.h>
|
||||
# include <stdio.h>
|
||||
# include "stdlib.h"
|
||||
|
||||
void ft_putchar(int fd, char c);
|
||||
void ft_put_ptr(int fd, unsigned long long num);
|
||||
void ft_conv_ith(int fd, unsigned int nbr, const char str);
|
||||
|
||||
int ft_hex_len(unsigned int nbr);
|
||||
int ft_sorting(va_list list, const char str, int fd);
|
||||
int ft_printf_fd(int fd, const char *str, ...);
|
||||
int ft_print_ith(int fd, unsigned int nbr, const char str);
|
||||
int ft_printchar(int fd, int c);
|
||||
int ft_printstr(int fd, char *s);
|
||||
int ft_printpercent(int fd);
|
||||
int ft_print_unsigned(int fd, unsigned int nb);
|
||||
int ft_print_nbr(int fd, int nb);
|
||||
int ft_print_ptr(int fd, unsigned long long ptr);
|
||||
int ft_adrr_len(unsigned long long nbr);
|
||||
int ft_print_null(int fd);
|
||||
|
||||
char *ft_itoa(int n);
|
||||
char *ft_unsigned_itoa(unsigned int n);
|
||||
|
||||
size_t ft_count(long long int n);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* calc_cheap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/03 11:32:29 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:05:31 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int calc_move(t_stack *stack_a, t_stack *stack_b, int index_b)
|
||||
{
|
||||
int index_a;
|
||||
int value_b;
|
||||
|
||||
value_b = return_value(stack_b, index_b);
|
||||
index_a = calc_index_a(stack_a, value_b);
|
||||
return (calc_move_stack(stack_b, index_b) \
|
||||
+ calc_move_stack(stack_a, index_a));
|
||||
}
|
||||
|
||||
int calc_cheaper(t_stack *stack_a, t_stack *stack_b)
|
||||
{
|
||||
int index;
|
||||
int cheaper;
|
||||
int cheaper_index;
|
||||
t_node *curr;
|
||||
|
||||
curr = stack_b->top;
|
||||
index = 1;
|
||||
cheaper_index = 1;
|
||||
cheaper = calc_move(stack_a, stack_b, index);
|
||||
index++;
|
||||
while (curr->next)
|
||||
{
|
||||
if (cheaper > calc_move(stack_a, stack_b, index))
|
||||
{
|
||||
cheaper = calc_move(stack_a, stack_b, index);
|
||||
cheaper_index = index;
|
||||
}
|
||||
index++;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (cheaper_index);
|
||||
}
|
||||
|
||||
int calc_move_stack(t_stack *stack, int index)
|
||||
{
|
||||
if (index <= stack->size / 2)
|
||||
return (index - 1);
|
||||
return (stack->size - index + 1);
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* calc_cheap_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/02 01:37:25 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:07:37 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int calc_index_min(t_stack *stack_a)
|
||||
{
|
||||
t_node *curr;
|
||||
int min;
|
||||
|
||||
curr = stack_a->top;
|
||||
min = curr->value;
|
||||
curr = curr->next;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->value < min)
|
||||
min = curr->value;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (min);
|
||||
}
|
||||
|
||||
int calc_index_max(t_stack *stack_a)
|
||||
{
|
||||
t_node *curr;
|
||||
int max;
|
||||
|
||||
curr = stack_a->top;
|
||||
max = curr->value;
|
||||
curr = curr->next;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->value > max)
|
||||
max = curr->value;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (max);
|
||||
}
|
||||
|
||||
int return_index(t_stack *stack, int value)
|
||||
{
|
||||
t_node *curr;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
curr = stack->top;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->value == value)
|
||||
return (i);
|
||||
i++;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int calc_index_a(t_stack *stack_a, int value)
|
||||
{
|
||||
t_node *curr;
|
||||
int i;
|
||||
|
||||
i = 2;
|
||||
curr = stack_a->top;
|
||||
if (value > calc_index_max(stack_a) || value < calc_index_min(stack_a))
|
||||
return (return_index(stack_a, calc_index_min(stack_a)));
|
||||
while (curr && curr->next)
|
||||
{
|
||||
if (curr->value < value && curr->next->value > value)
|
||||
return (i);
|
||||
i++;
|
||||
curr = curr->next;
|
||||
}
|
||||
if (curr->value < value && stack_a->top->value > value)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int return_value(t_stack *stack, int index)
|
||||
{
|
||||
t_node *curr;
|
||||
|
||||
if (!stack || !stack->top || index <= 0)
|
||||
return (0);
|
||||
if (index > stack->size)
|
||||
return (0);
|
||||
curr = stack->top;
|
||||
while (index > 1 && curr)
|
||||
{
|
||||
if (!curr->next)
|
||||
return (0);
|
||||
curr = curr->next;
|
||||
index--;
|
||||
}
|
||||
return (curr->value);
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 19:13:52 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 19:40:43 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int fill_stack(t_stack *stack_a, char **av)
|
||||
{
|
||||
int i;
|
||||
int num;
|
||||
|
||||
i = 0;
|
||||
while (av[i])
|
||||
{
|
||||
if (ft_atoll(av[i]) > 2147483647)
|
||||
return (ft_printf_fd(2, "Error\n"), 0);
|
||||
num = ft_atoll(av[i]);
|
||||
if (valid_input(av[i]) == 0 && num <= INT_MAX && num >= INT_MIN \
|
||||
&& in_stack(stack_a, (int)num) == 0)
|
||||
{
|
||||
add_stack(stack_a, (int)num);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *stack_b;
|
||||
char **av_pars;
|
||||
|
||||
if (ac < 2)
|
||||
return (0);
|
||||
stack_a = init_stack();
|
||||
stack_b = init_stack();
|
||||
if (!stack_a || !stack_b)
|
||||
return (1);
|
||||
av_pars = pars(av);
|
||||
if (!av_pars)
|
||||
return (ft_printf_fd(1, "error\n"), 1);
|
||||
if (!fill_stack(stack_a, av_pars))
|
||||
return (free_stack(stack_a), free_stack(stack_b), \
|
||||
free_array(av_pars), 1);
|
||||
if (is_sorted(stack_a) == 1)
|
||||
return (free_stack(stack_a), free_stack(stack_b), \
|
||||
free_array(av_pars), 1);
|
||||
sort(stack_a, stack_b);
|
||||
return (free_stack(stack_a), free_stack(stack_b), free_array(av_pars), 0);
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* op.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 19:20:06 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:48:32 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void add_stack(t_stack *stack, int value)
|
||||
{
|
||||
t_node *new_node;
|
||||
t_node *current;
|
||||
|
||||
new_node = malloc(sizeof(t_node));
|
||||
if (!new_node)
|
||||
return ;
|
||||
new_node->value = value;
|
||||
new_node->next = NULL;
|
||||
if (stack->top == NULL)
|
||||
{
|
||||
stack->top = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
current = stack->top;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
current->next = new_node;
|
||||
}
|
||||
stack->size++;
|
||||
}
|
||||
|
||||
int swap(t_stack *stack)
|
||||
{
|
||||
t_node *first;
|
||||
t_node *second;
|
||||
|
||||
if (stack->top == NULL || stack->top->next == NULL)
|
||||
return (0);
|
||||
first = stack->top;
|
||||
second = first->next;
|
||||
first->next = second->next;
|
||||
second->next = first;
|
||||
stack->top = second;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int push(t_stack *stack_1, t_stack *stack_2)
|
||||
{
|
||||
t_node *first_1;
|
||||
t_node *first_2;
|
||||
t_node *tmp;
|
||||
|
||||
if (stack_1->top == NULL)
|
||||
return (0);
|
||||
first_1 = stack_1->top;
|
||||
first_2 = stack_2->top;
|
||||
tmp = first_1->next;
|
||||
first_1->next = first_2;
|
||||
stack_1->top = tmp;
|
||||
stack_2->top = first_1;
|
||||
stack_2->size++;
|
||||
stack_1->size--;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int rrotate(t_stack *stack)
|
||||
{
|
||||
t_node *first;
|
||||
t_node *last;
|
||||
t_node *before_last;
|
||||
|
||||
if (stack->size < 2)
|
||||
return (0);
|
||||
first = stack->top;
|
||||
before_last = stack->top;
|
||||
last = go_to(stack->top, stack->size);
|
||||
before_last = go_to(stack->top, (stack->size - 1));
|
||||
if (!last || !before_last)
|
||||
return (-1);
|
||||
before_last->next = NULL;
|
||||
last->next = first;
|
||||
stack->top = last;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int rotate(t_stack *stack)
|
||||
{
|
||||
t_node *first;
|
||||
t_node *last;
|
||||
t_node *after_first;
|
||||
|
||||
if (stack->size < 2)
|
||||
return (0);
|
||||
first = stack->top;
|
||||
last = stack->top;
|
||||
last = go_to(stack->top, stack->size);
|
||||
if (!last)
|
||||
return (-1);
|
||||
after_first = first->next;
|
||||
last->next = first;
|
||||
first->next = NULL;
|
||||
stack->top = after_first;
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pars.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/05 08:26:16 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/05 08:26:16 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
char *init_empty_str(void)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = malloc(sizeof(char));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[0] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
void free_array(char **array)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!array)
|
||||
return ;
|
||||
i = 0;
|
||||
while (array[i])
|
||||
{
|
||||
if (array[i])
|
||||
free(array[i]);
|
||||
i++;
|
||||
}
|
||||
free(array);
|
||||
}
|
||||
|
||||
char **pars(char **av)
|
||||
{
|
||||
char *str;
|
||||
char **av_pars;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
str = init_empty_str();
|
||||
if (!str)
|
||||
return (printf("error\n"), NULL);
|
||||
while (av[i])
|
||||
{
|
||||
str = ft_strcat(str, av[i]);
|
||||
if (!str)
|
||||
return (printf("error\n"), NULL);
|
||||
i++;
|
||||
}
|
||||
av_pars = ft_split(str, ' ');
|
||||
free(str);
|
||||
if (!av_pars)
|
||||
{
|
||||
printf("error\n");
|
||||
return (NULL);
|
||||
}
|
||||
return (av_pars);
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/16 10:00:34 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:13:21 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void sort(t_stack *stack_a, t_stack *stack_b)
|
||||
{
|
||||
int median;
|
||||
t_node *current;
|
||||
|
||||
current = stack_a->top;
|
||||
median = get_median(stack_a, stack_a->size);
|
||||
if (stack_a->size == 2)
|
||||
sort_2(stack_a);
|
||||
else
|
||||
{
|
||||
while (current->next->next->next)
|
||||
{
|
||||
push(stack_a, stack_b);
|
||||
ft_printf_fd(1, "pb\n");
|
||||
if (current->value > median)
|
||||
{
|
||||
rotate(stack_b);
|
||||
ft_printf_fd(1, "rb\n");
|
||||
}
|
||||
current = stack_a->top;
|
||||
}
|
||||
sort_3(stack_a);
|
||||
while (stack_b->size != 0)
|
||||
make_move(stack_a, stack_b, calc_cheaper(stack_a, stack_b));
|
||||
final_sort(stack_a);
|
||||
}
|
||||
}
|
||||
|
||||
void final_sort(t_stack *stack_a)
|
||||
{
|
||||
int index_min;
|
||||
|
||||
index_min = return_index(stack_a, calc_index_min(stack_a));
|
||||
make_move_a(stack_a, calc_move_stack_2(stack_a, index_min));
|
||||
}
|
||||
|
||||
void opti(int *move)
|
||||
{
|
||||
if (move[0] > 0 && move[1] > 0)
|
||||
{
|
||||
if (move[0] > move[1])
|
||||
{
|
||||
move[2] = move[1];
|
||||
move[0] = move[0] - move[1];
|
||||
move[1] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
move[2] = move[0];
|
||||
move[1] = move[1] - move[0];
|
||||
move[0] = 0;
|
||||
}
|
||||
}
|
||||
else if (move[0] < 0 && move[1] < 0)
|
||||
opti_2(move);
|
||||
}
|
||||
|
||||
void opti_2(int *move)
|
||||
{
|
||||
if (move[0] < move[1])
|
||||
{
|
||||
move[2] = move[0];
|
||||
move[1] = move[1] - move[0];
|
||||
move[0] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
move[2] = move[1];
|
||||
move[0] = move[0] - move[1];
|
||||
move[1] = 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/03 11:40:53 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:10:08 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void make_move(t_stack *stack_a, t_stack *stack_b, int cheap_ind)
|
||||
{
|
||||
int move[3];
|
||||
int value_b;
|
||||
int index_a;
|
||||
|
||||
move[1] = calc_move_stack_2(stack_b, cheap_ind);
|
||||
value_b = return_value(stack_b, cheap_ind);
|
||||
index_a = calc_index_a(stack_a, value_b);
|
||||
move[0] = calc_move_stack_2(stack_a, index_a);
|
||||
move[2] = 0;
|
||||
opti(move);
|
||||
make_move_b(stack_b, move[1]);
|
||||
make_move_a(stack_a, move[0]);
|
||||
make_move_r(stack_b, stack_a, move[2]);
|
||||
push(stack_b, stack_a);
|
||||
ft_printf_fd(1, "pa\n");
|
||||
}
|
||||
|
||||
void make_move_a(t_stack *stack_a, int move)
|
||||
{
|
||||
if (move > 0)
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "ra\n");
|
||||
move--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "rra\n");
|
||||
move++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void make_move_b(t_stack *stack_b, int move)
|
||||
{
|
||||
if (move > 0)
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rotate(stack_b);
|
||||
ft_printf_fd(1, "rb\n");
|
||||
move--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rrotate(stack_b);
|
||||
ft_printf_fd(1, "rrb\n");
|
||||
move++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void make_move_r(t_stack *stack_b, t_stack *stack_a, int move)
|
||||
{
|
||||
if (move > 0)
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rotate(stack_b);
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "rr\n");
|
||||
move--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rrotate(stack_b);
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "rrr\n");
|
||||
move++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int calc_move_stack_2(t_stack *stack, int index)
|
||||
{
|
||||
t_node *curr;
|
||||
int i;
|
||||
int ind;
|
||||
|
||||
ind = index;
|
||||
curr = stack->top;
|
||||
while (ind > 1)
|
||||
{
|
||||
curr = curr->next;
|
||||
ind--;
|
||||
}
|
||||
if (index - 1 < stack->size - index + 1)
|
||||
i = index - 1;
|
||||
else
|
||||
i = (stack->size - index + 1) * -1;
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_tiny.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/21 10:30:01 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:58:41 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void sort_2(t_stack *stack_a)
|
||||
{
|
||||
t_node *value1;
|
||||
t_node *value2;
|
||||
|
||||
value1 = stack_a->top;
|
||||
value2 = value1->next;
|
||||
if (value1->value >= value2->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "ra\n");
|
||||
}
|
||||
}
|
||||
|
||||
void sort_3(t_stack *stack_a)
|
||||
{
|
||||
t_node *v1;
|
||||
t_node *v2;
|
||||
t_node *v3;
|
||||
|
||||
v1 = stack_a->top;
|
||||
v2 = v1->next;
|
||||
v3 = v2->next;
|
||||
if (v2->value < v1->value && v2->value > v3->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
swap(stack_a);
|
||||
ft_printf_fd(1, "ra\nsa\n");
|
||||
}
|
||||
if (v2->value < v1->value && v2->value < v3->value && v1->value < v3->value)
|
||||
{
|
||||
ft_printf_fd(1, "sa\n");
|
||||
swap(stack_a);
|
||||
}
|
||||
if (v2->value < v1->value && v2->value < v3->value && v1->value > v3->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "ra\n");
|
||||
}
|
||||
sort_3_bis(v1, v2, v3, stack_a);
|
||||
}
|
||||
|
||||
void sort_3_bis(t_node *v1, t_node *v2, t_node *v3, t_stack *stack_a)
|
||||
{
|
||||
if (v2->value > v1->value && v2->value > v3->value && v1->value < v3->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
swap(stack_a);
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "ra\nsa\nrra\n");
|
||||
}
|
||||
if (v2->value > v1->value && v2->value > v3->value && v1->value > v3->value)
|
||||
{
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "rra\n");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/16 08:33:48 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:57:30 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int get_median(t_stack *stack, int size)
|
||||
{
|
||||
int *table;
|
||||
int i;
|
||||
t_node *current;
|
||||
int median;
|
||||
|
||||
if (!size || !stack || !stack->top)
|
||||
return (ft_printf_fd(2, "Error\n"), -1);
|
||||
table = malloc(size * sizeof(int));
|
||||
if (!table)
|
||||
return (ft_printf_fd(2, "Error\n"), -1);
|
||||
i = 0;
|
||||
current = stack->top;
|
||||
while (current && i < size)
|
||||
{
|
||||
table[i++] = current->value;
|
||||
current = current->next;
|
||||
}
|
||||
sort_int_table(table, size);
|
||||
median = median_calc(table, size);
|
||||
return (free(table), median);
|
||||
}
|
||||
|
||||
void sort_int_table(int *tab, int size)
|
||||
{
|
||||
int a;
|
||||
int tmp;
|
||||
|
||||
a = 1;
|
||||
if (size > 1)
|
||||
{
|
||||
while (a < size)
|
||||
{
|
||||
if (tab[a] < tab[a - 1])
|
||||
{
|
||||
tmp = tab[a];
|
||||
tab[a] = tab[a - 1];
|
||||
tab[a - 1] = tmp;
|
||||
a = 0;
|
||||
}
|
||||
++a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int median_calc(int *tab, int size)
|
||||
{
|
||||
if (size % 2 == 0)
|
||||
return (tab[size / 2 -1]);
|
||||
else
|
||||
return (tab[(size - 1) / 2 + 1]);
|
||||
}
|
||||
|
||||
int find_index_between(t_stack *stack_a, int value)
|
||||
{
|
||||
t_node *a_cur;
|
||||
int index;
|
||||
int found;
|
||||
|
||||
a_cur = stack_a->top;
|
||||
index = 1;
|
||||
found = 0;
|
||||
while (a_cur && a_cur->next && !found)
|
||||
{
|
||||
if (a_cur->value < value && a_cur->next->value > value)
|
||||
found = 1;
|
||||
else
|
||||
{
|
||||
index++;
|
||||
a_cur = a_cur->next;
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
return (index);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 19:00:13 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:08:42 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
t_stack *init_stack(void)
|
||||
{
|
||||
t_stack *stack;
|
||||
|
||||
stack = malloc(sizeof(t_stack));
|
||||
if (!stack)
|
||||
{
|
||||
return (NULL);
|
||||
ft_printf_fd(2, "Error");
|
||||
}
|
||||
stack->top = NULL;
|
||||
stack->size = 0;
|
||||
return (stack);
|
||||
}
|
||||
|
||||
void free_stack(t_stack *stack)
|
||||
{
|
||||
t_node *current;
|
||||
t_node *next;
|
||||
|
||||
current = stack->top;
|
||||
while (current)
|
||||
{
|
||||
next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(stack);
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 00:09:54 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:51:14 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
t_node *go_to(t_node *top, int pos)
|
||||
{
|
||||
t_node *node;
|
||||
int i;
|
||||
|
||||
if (pos < 1)
|
||||
return (top);
|
||||
node = top;
|
||||
i = 1;
|
||||
while (i < pos)
|
||||
{
|
||||
node = node->next;
|
||||
i++;
|
||||
}
|
||||
return (node);
|
||||
}
|
||||
|
||||
int valid_input(const char *str)
|
||||
{
|
||||
int i;
|
||||
int sign_count;
|
||||
long long num;
|
||||
|
||||
i = 0;
|
||||
sign_count = 0;
|
||||
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
while (str[i] == '+' || str[i] == '-')
|
||||
{
|
||||
sign_count++;
|
||||
i++;
|
||||
}
|
||||
if (sign_count > 1 || str[i] == '\0')
|
||||
return (ft_printf_fd(2, "Error\n"), 1);
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] < '0' || str[i] > '9')
|
||||
return (ft_printf_fd(2, "Error\n"), 1);
|
||||
i++;
|
||||
}
|
||||
num = ft_atoll(str);
|
||||
if (num > INT_MAX || num < INT_MIN)
|
||||
return (ft_printf_fd(2, "Error\n"), 1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int in_stack(t_stack *stack, int n)
|
||||
{
|
||||
t_node *current;
|
||||
|
||||
current = stack->top;
|
||||
while (current)
|
||||
{
|
||||
if (current->value == n)
|
||||
{
|
||||
ft_printf_fd(2, "Error\n");
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_sorted(t_stack *stack)
|
||||
{
|
||||
t_node *current;
|
||||
|
||||
if (!stack || !stack->top)
|
||||
return (1);
|
||||
current = stack->top;
|
||||
while (current->next)
|
||||
{
|
||||
if (current->value > current->next->value)
|
||||
return (0);
|
||||
current = current->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
Loading…
Reference in New Issue