diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb78411 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +out +test/ +Push-Swap-Tester/ +checker_linux +checker +.vscode/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ed9649d --- /dev/null +++ b/Makefile @@ -0,0 +1,101 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: sinlee +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/07/16 04:09:49 by codespace #+# #+# # +# Updated: 2023/07/16 08:34:50 by sinlee ### ########.fr # +# # +# **************************************************************************** # + +# Compiler +NAME = push_swap +CC = gcc +RM = rm -f +INCLUDE = -Iincludes -Ilib/ft_printf/includes -Ilib/libft/includes +CFLAGS = -Wall -Wextra -Werror -g -fsanitize=address + +# Source files +ALGO_DIR = algorithm +STACK_DIR = stack +MISC_DIR = misc +BONUS = checker +SRCS_DIR = srcs +MAIN_FILES = main.c $(ALGO_DIR)/push_swap.c +SRCS_FILES = $(ALGO_DIR)/sort_utils.c $(ALGO_DIR)/sort.c $(ALGO_DIR)/calculation.c $(STACK_DIR)/stack_utils.c $(STACK_DIR)/operations.c $(STACK_DIR)/misc_utils.c $(MISC_DIR)/check.c +SRC_1 = $(addprefix $(SRCS_DIR)/,$(MAIN_FILES)) +SRC_2 = $(addprefix $(SRCS_DIR)/,$(SRCS_FILES)) +BONUS_SRC = srcs/checker.c + +# Object files +OBJ_DIR = obj/ +OBJ_1 = ${SRC_1:.c=.o} +OBJ_2 = ${SRC_2:.c=.o} +BONUS_OBJ =${BONUS_SRC:.c=.o} + +# Libraries +LIBFT_DIR = lib/libft +LIBFT = $(LIBFT_DIR)/libft.a + +PRINTF_DIR = lib/ft_printf +PRINTF = $(PRINTF_DIR)/libftprintf.a + +LIBS = -L$(LIBFT_DIR) -L$(PRINTF_DIR) -lft -lftprintf + +# Colors and text formatting +RESET = \033[0m +BOLD = \033[1m +DIM = \033[2m +UNDERLINE = \033[4m +BLINK = \033[5m +INVERT = \033[7m +LIGHT_BLUE = \033[94m +YELLOW = \033[93m + +# Makefile rules +# @${CC} -c $(CFLAGS) $(INCLUDE) $< -o ${<:.c=.o} +.c.o: + @echo "$(BOLD)$(YELLOW)Compiling $<...$(RESET)" + @$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +${NAME}: ${OBJ_1} ${OBJ_2} $(LIBFT) $(PRINTF) + @echo "$(BOLD)$(LIGHT_BLUE)Linking objects...$(RESET)" + @$(CC) $(CFLAGS) $(INCLUDES) ${OBJ_1} ${OBJ_2} $(LIBS) -o $(NAME) + @echo "$(BOLD)$(LIGHT_BLUE)$(NAME) created successfully!$(RESET)" + @echo "$(BOLD)Copyright Reserved. Lee Sin Liang." + +${BONUS}: ${OBJ_2} ${BONUS_OBJ} $(LIBFT) $(PRINTF) + @echo "$(BOLD)$(LIGHT_BLUE)Linking objects...$(RESET)" + @${CC} ${CFLAGS} ${BONUS_OBJ} ${OBJ_2} $(LIBS) -o ${BONUS} ${INCLUDE} + @echo "$(BOLD)$(LIGHT_BLUE)$(NAME) created successfully!$(RESET)" + @echo "$(BOLD)Copyright Reserved. Lee Sin Liang." + +$(LIBFT): + @echo "$(BOLD)$(LIGHT_BLUE)Building libft...$(RESET)" + @make -C $(LIBFT_DIR) -s + +$(PRINTF): + @echo "$(BOLD)$(LIGHT_BLUE)Building ft_printf...$(RESET)" + @make -C $(PRINTF_DIR) -s + +all: ${NAME} + +bonus: ${BONUS} + +clean: + @echo "$(BOLD)$(LIGHT_BLUE)Cleaning objects...$(RESET)" + @${RM} ${OBJ_1} ${OBJ_2} ${BONUS_OBJ} ${NAME} ${BONUS} + @make -C $(LIBFT_DIR) clean -s + @make -C $(PRINTF_DIR) clean -s + +fclean: clean + @echo "$(BOLD)$(LIGHT_BLUE)Cleaning $(NAME)...$(RESET)" + @${RM} ${OBJ_1} ${OBJ_2} ${BONUS_OBJ} ${NAME} ${BONUS} + @make -C $(LIBFT_DIR) fclean -s + @make -C $(PRINTF_DIR) fclean -s + +re: clean all + +.PHONY: all bonus clean fclean re bonus diff --git a/README.md b/README.md new file mode 100644 index 0000000..d058436 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +``` +push_swap/ +├── includes/ +│ ├── push_swap.h # Header file for push_swap +├── srcs/ +│ ├── main.c # Main entry point of the program +│ ├── push_swap.c # Implementation of push_swap algorithm +│ ├── operations.c # Implementation of stack operations +│ ├── utils.c # Utility functions +├── lib/ +│ ├── ft_printf/ +│ │ ├── includes/ +│ │ │ ├── ft_printf.h # Header file for ft_printf +│ │ ├── srcs/ +│ │ │ ├── ft_printf.c # Implementation of ft_printf function +│ │ │ ├── ... # Other source code files for ft_printf +│ ├── libft/ +│ │ ├── includes/ +│ │ │ ├── libft.h # Header file for libft +│ │ ├── srcs/ +│ │ │ ├── ft_*.c # Implementation files for libft functions +│ │ │ ├── ... # Other source code files for libft +├── Makefile # Makefile for compiling the project +├── README.md # Project documentation and instructions +``` \ No newline at end of file diff --git a/includes/checker.h b/includes/checker.h new file mode 100644 index 0000000..f676e7d --- /dev/null +++ b/includes/checker.h @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* checker.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 11:31:24 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:28 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CHECKER_H +# define CHECKER_H + +# include "ft_printf.h" +# include "libft.h" +# include + +typedef struct s_node +{ + int content; + struct s_node *prev; + struct s_node *next; +} t_node; + +bool is_sorted(t_node *stack, bool reverse); +char *get_next_line(int fd); +bool execute(t_node **stack_a, t_node **stack_b, char *line, + bool s_print); +void print_stack(t_node *stack, char *str, bool advanced, + bool to_first); +t_node *add_node(t_node *node, int content); +void clear_lst_node(t_node *node); +void ft_error(t_node *stack_a); +int ft_atoi_ps(char *str, t_node *stack_a); +int ft_strcmp(const char *s1, const char *s2); +bool is_valid(t_node *stack_a, char *str); + +#endif \ No newline at end of file diff --git a/includes/push_swap.h b/includes/push_swap.h new file mode 100644 index 0000000..3fc99df --- /dev/null +++ b/includes/push_swap.h @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_swap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/04 02:39:31 by sinlee #+# #+# */ +/* Updated: 2023/07/16 04:11:27 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUSH_SWAP_H +# define PUSH_SWAP_H + +# include "ft_printf.h" +# include "libft.h" +# include + +typedef struct s_node +{ + int content; + struct s_node *prev; + struct s_node *next; +} t_node; + +// Stack Utilties +t_node *last_first_node(t_node *node, bool is_last); +t_node *add_node(t_node *node, int content); +t_node *create_node(int content); +void clear_lst_node(t_node *node); +void print_stack(t_node *stack, char *str, bool advanced, + bool to_first); +int stack_len(t_node *stack); +int min(int a, int b); +int max(int a, int b); + +// push swap operations +bool swap(t_node **stack); +bool push(t_node **stack_from, t_node **stack_to); +bool rotate(t_node **stack, bool reverse); +bool execute(t_node **stack_a, t_node **stack_b, char *line, + bool s_print); +int execute_calc(t_node *stack_a, t_node *stack_b, int len, + bool return_pos_b); +int reverse_calc(t_node *stack_a, t_node *stack_b, int len, + bool return_pos_b); +bool multi_execute(t_node **stack_a, t_node **stack_b, + char *line, int n); +void check_exit_else_exec(t_node **stack_a, t_node **stack_b, + char *line); +void exec_smt(t_node **stack_a, t_node **stack_b, int pos[2], + int mode); +void execute_ps(t_node **stack_a, t_node **stack_b, int pos[2], + int mode); +void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]); + +// sorting Utilities +void push_swap(t_node *stack_a, t_node *stack_b, int len); +bool is_sorted(t_node *stack, bool reverse); +int min_max_pos(t_node *stack, bool max, bool pos); +int find_target(t_node *stack_from, t_node *stack_to); +void target_push(t_node *stack, int pos); +void min_max_push(t_node *stack, bool max); +int calc(t_node *stack_a, t_node *stack_b, int len, + bool return_pos_b); +int lcm(int pos[2], int len_a, int len_b, bool return_move); +int node_index(t_node *stack, int target); +int find_min_index(t_node *stack_a, t_node *stack_b, int len); +void sort_three(t_node *stack_a); +void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]); + +// generic Utilities +int ft_strcmp(const char *s1, const char *s2); +void ft_error(t_node *stack_a); +int ft_atoi_ps(char *str, t_node *stack_a); +bool is_valid(t_node *stack_a, char *str); + +#endif \ No newline at end of file diff --git a/lib/ft_printf/.github/workflows/c-cpp.yml b/lib/ft_printf/.github/workflows/c-cpp.yml new file mode 100644 index 0000000..86724c8 --- /dev/null +++ b/lib/ft_printf/.github/workflows/c-cpp.yml @@ -0,0 +1,17 @@ +name: C/C++ CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: make all + run: make all diff --git a/lib/ft_printf/.gitignore b/lib/ft_printf/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/lib/ft_printf/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/lib/ft_printf/LICENSE b/lib/ft_printf/LICENSE new file mode 100644 index 0000000..794a7e9 --- /dev/null +++ b/lib/ft_printf/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Architect + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/ft_printf/Makefile b/lib/ft_printf/Makefile new file mode 100644 index 0000000..9e447eb --- /dev/null +++ b/lib/ft_printf/Makefile @@ -0,0 +1,39 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: sinlee +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/05/19 10:16:35 by sinlee #+# #+# # +# Updated: 2023/07/04 02:43:31 by sinlee ### ########.fr # +# # +# **************************************************************************** # + +NAME = libftprintf.a +CC = gcc +CFLAGS = -Wall -Wextra -Werror +CPPFLAGS = -I includes -I libft/includes +SOURCES = ft_printf.c ft_string.c ft_nbr.c +SRC = $(addprefix src/, $(SOURCES)) +OBJS = ${SRC:.c=.o} +RM = rm -rf + +${NAME} : ${OBJS} + cd libft && $(MAKE) + cp libft/libft.a $(NAME) + ar -rcs $(NAME) $(OBJS) + +all: $(NAME) + +clean: + cd libft && $(MAKE) clean + $(RM) $(OBJS) + +fclean: clean + cd libft && $(MAKE) fclean + $(RM) $(NAME) + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/lib/ft_printf/README.md b/lib/ft_printf/README.md new file mode 100644 index 0000000..c483b0e --- /dev/null +++ b/lib/ft_printf/README.md @@ -0,0 +1,71 @@ +# ft_printf + +This project is a custom implementation of the `printf` function in the C programming language. + +## Introduction + +The `printf` function is a widely used function in the C programming language that allows formatted output to the standard output stream. It provides a convenient way to display various types of data, such as strings, numbers, and other variables, in a specified format. + +In this project, I implemented my own version of the `printf` function called `ft_printf`. It replicates the functionality of the standard `printf` function while maintaining a customizable and extensible codebase. + +## Features + +The `ft_printf` function in this project supports the following features: + +- Printing formatted output to the standard output stream. +- Handling different conversion specifiers, such as `%d`, `%s`, `%c`, `%u`, `%p`, and more. + +## Getting Started + +To compile and use the `ft_printf` function, follow these steps: + +1. Clone the repository to your local machine using the following command: + + ```shell + git clone https://github.com/LeeSinLiang/ft_printf.git + ``` + +2. Navigate to the project directory: + + ```shell + cd ft_printf + ``` + +3. Compile the project using Makefile: + + ```shell + make + ``` + +4. Start using the `ft_printf` function in your code as you would with the standard `printf` function. For example: + + ```c + int ft_printf(const char *str, ...); + + int main() { + ft_printf("Hello, %s!\n", "world"); + return 0; + } + ``` + + Compile your code with the `libftprintf.a` library: + + ```shell + gcc -Wall -Wextra -Werror -I includes main.c -L. -lftprintf -o main + ``` + + Run the executable: + + ```shell + ./main + ``` + + Output: + + ``` + Hello, world! + ``` + + **Note: libft is included to utilize several functions such as `ft_itoa`, `ft_putchar_fd`, `ft_putstr_fd` and more.** + + **More details of libft: https://github.com/LeeSinLiang/libft** diff --git a/lib/ft_printf/includes/ft_printf.h b/lib/ft_printf/includes/ft_printf.h new file mode 100644 index 0000000..999181f --- /dev/null +++ b/lib/ft_printf/includes/ft_printf.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: codespace +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/19 10:08:49 by sinlee #+# #+# */ +/* Updated: 2023/07/16 03:56:49 by codespace ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include "libft.h" +# include +# include +# include + +void ft_putchar_len(char c, int *len); +void ft_putstr_len(char *str, int *len); +void ft_putnbr_len(int n, int *len); +void ft_putnbr_u_len(unsigned int n, int *len); +void ft_putnbr_hexa_len(unsigned int nbr, int *len, bool up); +void ft_putpointer_len(size_t pointer, int *len); +int ft_printf(const char *str, ...); + +#endif \ No newline at end of file diff --git a/lib/ft_printf/libft/.github/workflows/c-cpp.yml b/lib/ft_printf/libft/.github/workflows/c-cpp.yml new file mode 100644 index 0000000..86724c8 --- /dev/null +++ b/lib/ft_printf/libft/.github/workflows/c-cpp.yml @@ -0,0 +1,17 @@ +name: C/C++ CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: make all + run: make all diff --git a/lib/ft_printf/libft/.gitignore b/lib/ft_printf/libft/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/lib/ft_printf/libft/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/lib/ft_printf/libft/LICENSE b/lib/ft_printf/libft/LICENSE new file mode 100644 index 0000000..794a7e9 --- /dev/null +++ b/lib/ft_printf/libft/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Architect + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/ft_printf/libft/Makefile b/lib/ft_printf/libft/Makefile new file mode 100644 index 0000000..9988bfe --- /dev/null +++ b/lib/ft_printf/libft/Makefile @@ -0,0 +1,81 @@ +SRCS_DIR = src/ +SRCS = ft_atoi.c \ + ft_bzero.c \ + ft_calloc.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_itoa.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_putchar_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_putstr_fd.c \ + ft_split.c \ + ft_strchr.c \ + ft_strdup.c \ + ft_strjoin.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strmapi.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strtrim.c \ + ft_substr.c \ + ft_striteri.c \ + ft_tolower.c \ + ft_toupper.c \ + get_next_line.c \ + get_next_line_utils.c + +SRCS_B = ft_lstadd_back.c \ + ft_lstadd_front.c \ + ft_lstclear.c \ + ft_lstdelone.c \ + ft_lstiter.c \ + ft_lstlast.c \ + ft_lstmap.c \ + ft_lstnew.c \ + ft_lstsize.c + + +CFILES = $(addprefix $(SRCS_DIR), $(SRCS)) +CFILES_B = $(addprefix $(SRCS_DIR), $(SRCS_B)) + +OBJS = ${CFILES:.c=.o} +OBJS_B = ${CFILES_B:.c=.o} +INCS = ./includes +NAME = libft.a +LIBC = ar rcs +LIBR = ranlib +CC = gcc +RM = rm -f +CFLAGS = -std=c11 -Wall -Wextra -Werror -I$(INCS) + + +all: ${NAME} + +$(NAME): ${OBJS} ${OBJS_B} + ${LIBC} $(NAME) ${OBJS} ${OBJS_B} + +#for 42 bonus +bonus: ${OBJS} ${OBJS_B} + ${LIBC} $(NAME) ${OBJS_B} + +clean: + ${RM} ${OBJS} ${OBJS_B} + +fclean: clean + ${RM} $(NAME) $(bonus) + +re: fclean all + +.PHONY: all bonus clean fclean re \ No newline at end of file diff --git a/lib/ft_printf/libft/README.md b/lib/ft_printf/libft/README.md new file mode 100644 index 0000000..487ce10 --- /dev/null +++ b/lib/ft_printf/libft/README.md @@ -0,0 +1,56 @@ +# Libft + +Libft is a custom C library that contains implementations of various standard library functions in C. These functions are commonly used in C programming and provide essential functionality for tasks such as string manipulation, memory allocation, and list manipulation. By using Libft, you can have access to these functions without relying on the standard library. + +## Table of Contents + +- [Installation](#installation) +- [Usage](#usage) +- [License](#license) + +## Installation + +To use Libft in your C projects, follow these steps: + +1. Clone the Libft repository to your local machine: + ```bash + https://github.com/LeeSinLiang/Libft + ``` + +2. Compile the library by running the Makefile: + ```bash + cd Libft + make + ``` + +3. Once the compilation is complete, you will have a `libft.a` file that contains the compiled library. You can link this library with your C programs to use the implemented functions. + +4. Finally, compile your C program with the Libft library: + ```bash + gcc my_program.c -L. -lft + ``` + +## Usage + +Libft provides a wide range of functions categorized into different sections, including string manipulation, memory allocation, linked lists, and more. + +To use a specific function from Libft, declare the corresponding prototype function file in your C file and call the function by its name. For example, to use the `ft_atoi` function: + +```c +#include + +int ft_atoi(const char *str); + +int main() +{ + char *str = "123456789"; + printf("%d", ft_atoi(str)); + return (0); +} +``` + +For more details on the available functions and their usage, refer to the individual c files and their corresponding comments. + +## License + +MIT License diff --git a/lib/ft_printf/libft/includes/get_next_line.h b/lib/ft_printf/libft/includes/get_next_line.h new file mode 100644 index 0000000..ea88021 --- /dev/null +++ b/lib/ft_printf/libft/includes/get_next_line.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/25 15:53:31 by sinlee #+# #+# */ +/* Updated: 2023/07/16 04:11:32 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include +# include +# include +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 5 +# endif + +size_t ft_strlen(const char *str); +char *ft_substr(char *s, unsigned int start, size_t len); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +int gnl_ft_strchr(char *s, char c); +int ft_find_newline_pos(char *str); +char *get_next_line(int fd); +void print_debug(char *str); +char *ft_strjoin(char *s1, char *s2); + +#endif \ No newline at end of file diff --git a/lib/ft_printf/libft/includes/libft.h b/lib/ft_printf/libft/includes/libft.h new file mode 100644 index 0000000..023fa7e --- /dev/null +++ b/lib/ft_printf/libft/includes/libft.h @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 09:52:13 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:08:47 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include +# include +# include + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +size_t ft_strlen(const char *str); +void *ft_memset(void *ptr, int value, size_t num); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlcat(char *dest, const char *src, size_t size); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +int ft_atoi(const char *str); +void *ft_calloc(size_t count, size_t size); +char *ft_strdup(const char *s1); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +#endif diff --git a/lib/ft_printf/libft/src/ft_atoi.c b/lib/ft_printf/libft/src/ft_atoi.c new file mode 100644 index 0000000..2216ee9 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_atoi.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/05 10:33:07 by sinlee #+# #+# */ +/* Updated: 2023/05/05 09:38:36 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static bool is_in_string(char c, char *str) +{ + while (*str) + { + if (*str == c) + return (true); + str++; + } + return (false); +} + +static bool is_space(char c) +{ + return (is_in_string(c, "\t\n\v\f\r ")); +} + +int ft_atoi(const char *str) +{ + int ans; + int result; + + ans = 0; + result = 1; + while (is_space(*str)) + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + result *= -1; + str++; + } + while (*str >= '0' && *str <= '9') + { + ans *= 10; + ans += *str - 48; + str++; + } + return (result * ans); +} diff --git a/lib/ft_printf/libft/src/ft_bzero.c b/lib/ft_printf/libft/src/ft_bzero.c new file mode 100644 index 0000000..49e81f3 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_bzero.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:56:50 by sinlee #+# #+# */ +/* Updated: 2023/04/28 12:02:54 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of bzero function + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} + +// test ft_bzero function +// #include + +// int main() +// { +// char str[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore bzero(): %s\n", str); +// ft_bzero(str + 13, 8*sizeof(char)); +// printf("After bzero(): %s|", str); +// char str1[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore bzero(): %s\n", str1); +// bzero(str1 + 13, 8*sizeof(char)); +// printf("After bzero(): %s|", str1); +// return 0; +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_calloc.c b/lib/ft_printf/libft/src/ft_calloc.c new file mode 100644 index 0000000..21227c0 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_calloc.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 09:54:31 by sinlee #+# #+# */ +/* Updated: 2023/05/05 13:57:44 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of calloc function + +#include "libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + void *ptr; + + if (count == SIZE_MAX || size == SIZE_MAX) + return (NULL); + ptr = malloc(count * size); + if (ptr == NULL) + return (NULL); + ft_bzero(ptr, count * size); + return ((void *)ptr); +} + +// test ft_calloc function + +// #include + +// int main() +// { +// char *str; +// int i; + +// printf("ft_calloc: %p \n", ft_calloc(SIZE_MAX, SIZE_MAX)); +// return (0); +// } diff --git a/lib/ft_printf/libft/src/ft_isalnum.c b/lib/ft_printf/libft/src/ft_isalnum.c new file mode 100644 index 0000000..4917204 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_isalnum.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:56:49 by sinlee #+# #+# */ +/* Updated: 2023/04/28 11:41:46 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// implementation of isalnum function + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (1); + return (0); +} + +// test ft_isalnum function +// #include +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_isalnum((int)*(argv[1]))); +// printf("%d\n", isalnum((int)*(argv[1]))); +// } diff --git a/lib/ft_printf/libft/src/ft_isalpha.c b/lib/ft_printf/libft/src/ft_isalpha.c new file mode 100644 index 0000000..befeb3b --- /dev/null +++ b/lib/ft_printf/libft/src/ft_isalpha.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:45:29 by sinlee #+# #+# */ +/* Updated: 2023/04/28 10:51:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return (1); + return (0); +} + +// test ft_isalpha function +// #include +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_isalpha((int)*(argv[1]))); +// printf("%d\n", isalpha((int)*(argv[1]))); +// } diff --git a/lib/ft_printf/libft/src/ft_isascii.c b/lib/ft_printf/libft/src/ft_isascii.c new file mode 100644 index 0000000..7255280 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_isascii.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:41:52 by sinlee #+# #+# */ +/* Updated: 2023/04/28 11:47:03 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// implementation of isascii function + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} + +// test ft_isascii function +// #include +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_isascii((int)*(argv[1]))); +// printf("%d\n", isascii((int)*(argv[1]))); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_isdigit.c b/lib/ft_printf/libft/src/ft_isdigit.c new file mode 100644 index 0000000..d2b0483 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_isdigit.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:54:37 by sinlee #+# #+# */ +/* Updated: 2023/04/28 10:56:24 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of isdigit function + +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/lib/ft_printf/libft/src/ft_isprint.c b/lib/ft_printf/libft/src/ft_isprint.c new file mode 100644 index 0000000..d797dae --- /dev/null +++ b/lib/ft_printf/libft/src/ft_isprint.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:44:54 by sinlee #+# #+# */ +/* Updated: 2023/04/28 11:47:36 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of isprint function + +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/lib/ft_printf/libft/src/ft_itoa.c b/lib/ft_printf/libft/src/ft_itoa.c new file mode 100644 index 0000000..5d07eb0 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_itoa.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 14:04:49 by sinlee #+# #+# */ +/* Updated: 2023/05/03 14:14:49 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement ft_itoa function: convert int to string + +#include "libft.h" + +static int ft_len(int n) +{ + int len; + + len = 0; + if (n <= 0) + len++; + while (n != 0) + { + n /= 10; + len++; + } + return (len); +} + +static int ft_abs(int n) +{ + if (n < 0) + return (-n); + return (n); +} + +char *ft_itoa(int n) +{ + int len; + char *str; + + len = ft_len(n); + str = (char *)malloc((len + 1) * 1); + if (!str) + return (0); + str[len] = '\0'; + len--; + if (n == 0) + str[0] = '0'; + if (n < 0) + str[0] = '-'; + while (n != 0) + { + str[len] = ft_abs(n % 10) + '0'; + n /= 10; + len--; + } + return (str); +} + +// #include +// int main(int argc, char **argv) +// { +// if (argc == 2) +// { +// printf("%s\n", ft_itoa(atoi(argv[1]))); +// } +// return (0); +// } diff --git a/lib/ft_printf/libft/src/ft_lstadd_back.c b/lib/ft_printf/libft/src/ft_lstadd_back.c new file mode 100644 index 0000000..61d1f79 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstadd_back.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:14:44 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:11:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstadd_back function: +// add the node 'new' at the end of the linked list + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *current; + + current = *lst; + if (lst) + { + if (current) + { + current = ft_lstlast(current); + current->next = new; + } + else + *lst = new; + } +} diff --git a/lib/ft_printf/libft/src/ft_lstadd_front.c b/lib/ft_printf/libft/src/ft_lstadd_front.c new file mode 100644 index 0000000..0ad27ab --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstadd_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 15:35:43 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:35:52 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (lst == NULL || new == NULL) + return ; + new->next = *lst; + *lst = new; +} diff --git a/lib/ft_printf/libft/src/ft_lstclear.c b/lib/ft_printf/libft/src/ft_lstclear.c new file mode 100644 index 0000000..908190b --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstclear.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:18:59 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:26:03 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstclear function: +// delete all nodes of list with the function 'del' + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *temp; + + if (!del || !lst || !*lst) + return ; + while (lst && *lst) + { + temp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = temp; + } +} diff --git a/lib/ft_printf/libft/src/ft_lstdelone.c b/lib/ft_printf/libft/src/ft_lstdelone.c new file mode 100644 index 0000000..76c80b1 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstdelone.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:17:03 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:21:56 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstdelone function: +// delete the node 'lst' with the function 'del' + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (!del) + return ; + if (lst) + { + del(lst->content); + free(lst); + } +} diff --git a/lib/ft_printf/libft/src/ft_lstiter.c b/lib/ft_printf/libft/src/ft_lstiter.c new file mode 100644 index 0000000..120e4b1 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstiter.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:25:01 by sinlee #+# #+# */ +/* Updated: 2023/05/05 16:30:09 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstiter function: +// iterates the list 'lst' +// and applies the function 'f' to the content of each node + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *current; + + current = lst; + while (current != NULL) + { + f(current->content); + current = current->next; + } +} diff --git a/lib/ft_printf/libft/src/ft_lstlast.c b/lib/ft_printf/libft/src/ft_lstlast.c new file mode 100644 index 0000000..8338f97 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstlast.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:13:02 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:09:24 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements lstlast function: return last node of list + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + t_list *current; + + current = lst; + while (current != NULL && current->next != NULL) + current = current->next; + return (current); +} +// #include +// int main() +// { +// t_list * l = NULL; +// ft_lstadd_back(&l, ft_lstnew((void*)1)); +// printf("%d", *(int *)(ft_lstlast(l)->content)); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_lstmap.c b/lib/ft_printf/libft/src/ft_lstmap.c new file mode 100644 index 0000000..361a61c --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstmap.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:27:55 by sinlee #+# #+# */ +/* Updated: 2023/05/05 16:31:32 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstmap function: +// iterates the list 'lst' and applies the function 'f' to the +// content of each node + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *current; + t_list *new; + t_list *temp; + + current = lst; + new = NULL; + while (current != NULL) + { + temp = ft_lstnew(f(current->content)); + if (temp == NULL) + { + ft_lstclear(&new, del); + return (NULL); + } + ft_lstadd_back(&new, temp); + current = current->next; + } + return (new); +} diff --git a/lib/ft_printf/libft/src/ft_lstnew.c b/lib/ft_printf/libft/src/ft_lstnew.c new file mode 100644 index 0000000..6cca7cd --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstnew.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 15:21:59 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:34:40 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of lstnew function: add new node to linked list + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *node; + + node = malloc(sizeof(t_list)); + if (!node) + return (NULL); + node->content = content; + node->next = NULL; + return (node); +} + +// test ft_lstnew function + +// #include +// int main() +// { +// t_list *node; + +// node = ft_lstnew("hello"); +// printf("%s", node->content); +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_lstsize.c b/lib/ft_printf/libft/src/ft_lstsize.c new file mode 100644 index 0000000..27d866b --- /dev/null +++ b/lib/ft_printf/libft/src/ft_lstsize.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:09:33 by sinlee #+# #+# */ +/* Updated: 2023/05/05 16:12:41 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement ft_lstsize function: count number of nodes in a linked list + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + t_list *current; + + count = 0; + current = lst; + while (current != NULL) + { + count++; + current = current->next; + } + return (count); +} diff --git a/lib/ft_printf/libft/src/ft_memchr.c b/lib/ft_printf/libft/src/ft_memchr.c new file mode 100644 index 0000000..0367d64 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_memchr.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:32:44 by sinlee #+# #+# */ +/* Updated: 2023/05/03 18:34:32 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t index; + + index = 0; + while (index < n) + { + if (((unsigned char *)s)[index] == (unsigned char)c) + return (((unsigned char *)s) + index); + index++; + } + return (NULL); +} + +// #include + +// int main() +// { +// char s[] = {0, 1, 2, 3, 4, 5}; + +// printf("%s\n", ft_memchr(s, 0, 0)); +// printf("%s\n", memchr(s, 0, 0)); +// printf("---------------------\n"); +// printf("%s\n", ft_memchr(s, 2, 3)); +// printf("%s\n", memchr(s, 2, 3)); +// printf("---------------------\n"); +// printf("%s\n", ft_memchr(s, 2+256, 3)); +// printf("%s\n", memchr(s, 2+256, 3)); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_memcmp.c b/lib/ft_printf/libft/src/ft_memcmp.c new file mode 100644 index 0000000..961bb8c --- /dev/null +++ b/lib/ft_printf/libft/src/ft_memcmp.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:36:51 by sinlee #+# #+# */ +/* Updated: 2023/05/05 12:57:41 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memcmp function + +#include "libft.h" +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + while (n--) + { + if (*(unsigned char *)s1 != *(unsigned char *)s2) + return (*(unsigned char *)s1 - *(unsigned char *)s2); + s1++; + s2++; + } + return (0); +} + +// test ft_memcmp function by comparing with memcmp function + +// #include +// #include + +// int main(int argc, char **argv) +// { +// char s2[] = {0, 0, 127, 0}; +// char s3[] = {0, 0, 42, 0}; +// printf("ft_memcmp: %d\n", ft_memcmp(s2, s3, 4)); +// printf("memcmp: %d\n", memcmp(s2, s3, 4)); +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_memcpy.c b/lib/ft_printf/libft/src/ft_memcpy.c new file mode 100644 index 0000000..f076d45 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_memcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 12:04:51 by sinlee #+# #+# */ +/* Updated: 2023/05/05 12:54:27 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memcpy function + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + + i = 0; + if (dst == NULL && src == NULL) + return (NULL); + while (i < n) + { + ((char *)dst)[i] = ((char *)src)[i]; + i++; + } + return ((void *)dst); +} diff --git a/lib/ft_printf/libft/src/ft_memmove.c b/lib/ft_printf/libft/src/ft_memmove.c new file mode 100644 index 0000000..2bce591 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_memmove.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 12:27:24 by sinlee #+# #+# */ +/* Updated: 2023/04/28 12:54:04 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memmove function + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned char *ptr_dest; + unsigned char *ptr_src; + size_t i; + + ptr_dest = (unsigned char *)dest; + ptr_src = (unsigned char *)src; + i = 0; + if (ptr_dest > ptr_src) + { + while (n--) + ptr_dest[n] = ptr_src[n]; + } + else + { + while (i < n) + { + ptr_dest[i] = ptr_src[i]; + i++; + } + } + return (dest); +} + +// test ft_memmove function + +// #include + +// int main(void) +// { +// char str1[50] = "Hello"; +// char str2[50] = "WorldHi"; +// ft_memmove(str1, str2, 3 * sizeof(char)); +// printf("After memmove(): %s|\n", str1); +// char str3[50] = "Hello"; +// char str4[50] = "WorldHi"; +// memmove(str3, str4, 3 * sizeof(char)); +// printf("After memmove(): %s|", str3); +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_memset.c b/lib/ft_printf/libft/src/ft_memset.c new file mode 100644 index 0000000..446eaad --- /dev/null +++ b/lib/ft_printf/libft/src/ft_memset.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:48:04 by sinlee #+# #+# */ +/* Updated: 2023/04/28 12:01:46 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memset function + +#include "libft.h" + +void *ft_memset(void *ptr, int value, size_t num) +{ + unsigned char *temp; + + temp = (unsigned char *)ptr; + while (num--) + { + *temp = (unsigned char)value; + temp++; + } + return (ptr); +} + +// test ft_memset function +// #include + +// int main() +// { +// char str[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore memset(): %s\n", str); +// ft_memset(str + 13, '.', 8*sizeof(char)); +// printf("After memset(): %s", str); +// char str1[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore memset(): %s\n", str1); +// memset(str1 + 13, '.', 8*sizeof(char)); +// printf("After memset(): %s", str1); +// return 0; +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_putchar_fd.c b/lib/ft_printf/libft/src/ft_putchar_fd.c new file mode 100644 index 0000000..21a74c0 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:29:39 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:30:17 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement putchar_fd function: output character to file descriptor + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/lib/ft_printf/libft/src/ft_putendl_fd.c b/lib/ft_printf/libft/src/ft_putendl_fd.c new file mode 100644 index 0000000..39c5398 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_putendl_fd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:31:51 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:32:21 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_putendl_fd function: +// output string to file descriptor with newline + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (!s) + return ; + ft_putstr_fd(s, fd); + ft_putchar_fd(10, fd); +} diff --git a/lib/ft_printf/libft/src/ft_putnbr_fd.c b/lib/ft_printf/libft/src/ft_putnbr_fd.c new file mode 100644 index 0000000..410271e --- /dev/null +++ b/lib/ft_printf/libft/src/ft_putnbr_fd.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:33:21 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:29:22 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_putnbr_fd function: output integer to file descriptor + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + char *str; + + str = ft_itoa(n); + write(fd, str, ft_strlen(str)); + free(str); +} +// void ft_putnbr_fd(int n, int fd) +// { +// write(fd, ft_itoa(n), ft_strlen(ft_itoa(n))); +// } diff --git a/lib/ft_printf/libft/src/ft_putstr_fd.c b/lib/ft_printf/libft/src/ft_putstr_fd.c new file mode 100644 index 0000000..49d9256 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_putstr_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:31:00 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:31:29 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements putstr_fd function: output string to file descriptor + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (!s) + return ; + write(fd, s, ft_strlen(s)); +} diff --git a/lib/ft_printf/libft/src/ft_split.c b/lib/ft_printf/libft/src/ft_split.c new file mode 100644 index 0000000..f75976b --- /dev/null +++ b/lib/ft_printf/libft/src/ft_split.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/13 14:51:39 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:15:30 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static bool check_seperator(char c_str, char c) +{ + if (c_str == c) + return (true); + return (false); +} + +static int count_strings(char *str, char c) +{ + int count; + + count = 0; + while (*str) + { + while (*str && check_seperator(*str, c)) + str++; + if (*str) + count++; + while (*str && !check_seperator(*str, c)) + str++; + } + return (count); +} + +// count string before character c +static int ft_strclen(char *str, char c) +{ + int i; + + i = 0; + while (str[i] && !check_seperator(str[i], c)) + i++; + return (i); +} + +static char *word(char *str, char c) +{ + int i; + int len; + char *word; + + i = 0; + len = ft_strclen(str, c); + word = (char *)malloc((len + 1) * 1); + while (i < len) + { + word[i] = str[i]; + i++; + } + word[i] = '\0'; + return (word); +} + +char **ft_split(char const *s, char c) +{ + char **arr; + char *str; + int i; + + i = 0; + if (!s) + return (0); + str = (char *)s; + arr = (char **)malloc((count_strings(str, c) + 1) * 8); + while (*str) + { + while (*str && check_seperator(*str, c)) + str++; + if (*str) + { + arr[i] = word(str, c); + i++; + } + while (*str && !check_seperator(*str, c)) + str++; + } + arr[i] = 0; + return (arr); +} + +// #include +// int main(int argc, char **argv) +// { +// int index; +// char **split; + +// split = ft_split(argv[1], argv[2][0]); +// index = 0; +// printf("tab start\n"); +// while (split[index]) +// { +// printf("tab[%d]: $%s$\n", index, split[index]); +// fflush(stdout); +// index++; +// } +// printf("tab end\n"); +// } diff --git a/lib/ft_printf/libft/src/ft_strchr.c b/lib/ft_printf/libft/src/ft_strchr.c new file mode 100644 index 0000000..f515bc8 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strchr.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:02:37 by sinlee #+# #+# */ +/* Updated: 2023/04/28 15:29:27 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement strchr function + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != (char)c) + { + if (!*s) + return (0); + s++; + } + return ((char *)s); +} + +// test ft_strchr function + +// #include + +// int main(void) +// { +// char *str; +// char *ptr; + +// str = "Hello World"; +// ptr = ft_strchr(str, 0); +// printf("%s\n", ptr); +// str = "Hello World"; +// ptr = strchr(str, 0); +// printf("%s\n", ptr); +// return (0); +// } diff --git a/lib/ft_printf/libft/src/ft_strdup.c b/lib/ft_printf/libft/src/ft_strdup.c new file mode 100644 index 0000000..55ba7ae --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strdup.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:06:17 by sinlee #+# #+# */ +/* Updated: 2023/04/28 13:07:54 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of ft_strdup function + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *ptr; + size_t len; + + len = ft_strlen(s1); + ptr = (char *)malloc((len + 1) * sizeof(char)); + if (ptr == NULL) + return (NULL); + ft_strlcpy(ptr, s1, len + 1); + return (ptr); +} + +// test ft_strdup function +// #include +// int main() +// { +// char *str; +// char *str2; + +// str = strdup("Hello World!|"); +// str2 = ft_strdup("Hello World!|"); +// printf("%s\n", str); +// printf("%s\n", str2); +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_striteri.c b/lib/ft_printf/libft/src/ft_striteri.c new file mode 100644 index 0000000..359f69b --- /dev/null +++ b/lib/ft_printf/libft/src/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:25:36 by sinlee #+# #+# */ +/* Updated: 2023/05/03 16:30:18 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement striteri function: apply function to each character of string + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + if (!s || !f) + return ; + i = -1; + while (s[++i]) + f(i, &s[i]); +} diff --git a/lib/ft_printf/libft/src/ft_strjoin.c b/lib/ft_printf/libft/src/ft_strjoin.c new file mode 100644 index 0000000..9eea352 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strjoin.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 11:08:15 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:16:15 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Function name: ft_strjoin +// Prototype: char: *ft_strjoin(char const *s1, char const *s2); +// Parameters: +// s1: The prefix string. s2: The suffix string. +// Return value: The new string. NULL if the allocation fails. +// External functs: malloc +// Description: +// Allocates (with malloc(3)) and returns a new string, +// which is the result of the concatenation of ’s1’ and ’s2’. + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + int i; + + i = -1; + str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!str) + return (0); + while (s1[++i]) + str[i] = s1[i]; + i = -1; + while (s2[++i]) + str[ft_strlen(s1) + i] = s2[i]; + str[ft_strlen(s1) + i] = '\0'; + return (str); +} + +// test ft_strjoin function. check strlcat const +// #include + +// int main() +// { +// char *s1 = "tripouille"; +// char *s2 = "42"; +// char *s3 = ft_strjoin("", "42"); +// printf("%s|\n", s3); +// printf("%d", strcmp(s3, "42")); +// // printf("%s|\n", strjoin("", "42")); +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_strlcat.c b/lib/ft_printf/libft/src/ft_strlcat.c new file mode 100644 index 0000000..f31927c --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strlcat.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/04 16:08:28 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:40:11 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t count(const char *dest) +{ + size_t len; + + len = 0; + while (dest[len] != '\0') + len++; + return (len); +} + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + char *dst; + const char *src_i; + size_t dest_length; + size_t remaining; + + dst = dest; + src_i = src; + remaining = size; + while (remaining-- != 0 && *dst != '\0') + dst++; + dest_length = dst - dest; + remaining = size - dest_length; + if (remaining == 0) + return (dest_length + count((src))); + while (*src != '\0') + { + if (remaining > 1) + { + *dst++ = *src; + remaining--; + } + src++; + } + *dst = '\0'; + return (dest_length + (src - src_i)); +} diff --git a/lib/ft_printf/libft/src/ft_strlcpy.c b/lib/ft_printf/libft/src/ft_strlcpy.c new file mode 100644 index 0000000..7e1d0a9 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strlcpy.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/29 17:39:35 by sinlee #+# #+# */ +/* Updated: 2023/04/28 13:15:17 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of strlcpy function + +#include "libft.h" + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t src_len; + size_t i; + + src_len = ft_strlen(src); + if (size == 0) + return (src_len); + i = 0; + while (i < size - 1 && src[i]) + { + dest[i] = src[i]; + ++i; + } + dest[i] = '\0'; + return (src_len); +} + +// test ft_strlcpy function + +// #include +// int main(void) +// { +// char *string1; +// char string2[10]; + +// string1 = "Hello"; +// printf("base : %s\n", string1); +// strlcpy(string2, string1, 5); +// printf("cpy c : %s\n", string2); +// ft_strlcpy(string2, string1, 5); +// printf("cpy ft : %s\n", string2); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_strlen.c b/lib/ft_printf/libft/src/ft_strlen.c new file mode 100644 index 0000000..ed95a2d --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 09:58:28 by sinlee #+# #+# */ +/* Updated: 2023/04/28 10:00:09 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of ft_strlen function + +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + size_t len; + + len = 0; + while (*str++) + len++; + return (len); +} diff --git a/lib/ft_printf/libft/src/ft_strmapi.c b/lib/ft_printf/libft/src/ft_strmapi.c new file mode 100644 index 0000000..273bbc3 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strmapi.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 14:17:04 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:27:34 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement strmapi function: apply function to each character of string + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *str; + int i; + + if (!s || !f) + return (NULL); + str = (char *)malloc((ft_strlen(s) + 1) * 1); + if (!str) + return (NULL); + i = 0; + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = '\0'; + return (str); +} + +// test ft_strmapi function +// #include + +// char ft_test(unsigned int i, char c) +// { +// i = 0; +// return (c - 32); +// } + +// int main() +// { +// char *str = "Hello World!"; +// char *new_str = ft_strmapi(str, ft_test); +// printf("%s\n", new_str); + // prints only Hello due to the null terminator (space - 32) +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_strncmp.c b/lib/ft_printf/libft/src/ft_strncmp.c new file mode 100644 index 0000000..6409538 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strncmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:25:52 by sinlee #+# #+# */ +/* Updated: 2023/04/28 15:32:15 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement of strncmp function + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + while (n-- && (*s1 || *s2)) + { + if (*s1++ != *s2++) + return (*(unsigned char *)--s1 - *(unsigned char *)--s2); + } + return (0); +} + +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_strncmp(argv[1], argv[2], 5)); +// printf("%d\n", strncmp(argv[1], argv[2], 5)); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_strnstr.c b/lib/ft_printf/libft/src/ft_strnstr.c new file mode 100644 index 0000000..2b8ad99 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strnstr.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 09:32:57 by sinlee #+# #+# */ +/* Updated: 2023/05/05 13:45:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement strnstr function + +#include "libft.h" +#include + +static bool check_occur(char *str, char *to_find, size_t len) +{ + size_t i; + + i = 0; + while (to_find[i] != '\0' && len--) + { + if (str[i] != to_find[i]) + return (false); + i++; + } + if (to_find[i] != '\0') + return (false); + return (true); +} + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + char *haystack_cpy; + + haystack_cpy = (char *)haystack; + if (!needle[0]) + return (haystack_cpy); + while (*haystack_cpy && len > 0) + { + if (!check_occur(haystack_cpy, (char *)needle, len)) + { + haystack_cpy++; + len--; + } + else + return (haystack_cpy); + } + return (0); +} + +// test ft_strnstr by comparing with strnstr function + +// #include +// #include + +// int main(int argc, char **argv) +// { +// char *str1; +// char *str2; +// int n; + +// str1 = argv[1]; +// str2 = argv[2]; +// n = atoi(argv[3]); +// printf("%s\n", ft_strnstr(str1, str2, n)); +// printf("%s\n", strnstr(str1, str2, n)); +// // char haystack[30] = "aaabcabcd"; +// // printf("%s\n", ft_strnstr(haystack, "abcd", 9)); +// // printf("%s\n", strnstr(haystack, "abcd", 9)); +// return (0); +// } diff --git a/lib/ft_printf/libft/src/ft_strrchr.c b/lib/ft_printf/libft/src/ft_strrchr.c new file mode 100644 index 0000000..2ba9224 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strrchr.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:15:38 by sinlee #+# #+# */ +/* Updated: 2023/05/03 18:26:59 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of strrchr function + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + char *ptr; + + ptr = 0; + if (c == 0) + return ((char *)s + ft_strlen(s)); + while (*s) + { + if (*s == (char)c) + ptr = (char *)s; + s++; + } + return (ptr); +} + +// test ft_strrchr function + +// #include + +// int main(void) +// { +// char *str; +// char *ptr; +// char * empty = (char*)calloc(1, 1); + +// str = "tripouille"; +// ptr = ft_strrchr(empty, 'V'); +// printf("%s\n", ptr); +// str = "tripouille"; +// ptr = strrchr(empty, 'V'); +// printf("%s\n", ptr); +// return (0); +// } diff --git a/lib/ft_printf/libft/src/ft_strtrim.c b/lib/ft_printf/libft/src/ft_strtrim.c new file mode 100644 index 0000000..1dbcea5 --- /dev/null +++ b/lib/ft_printf/libft/src/ft_strtrim.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 11:27:47 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:11:55 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Function name: ft_strtrim +// Prototype: char *ft_strtrim(char const *s1, char const *set); +// Parameters: +// s1: The string to be trimmed. +// set: The reference set of characters to trim. +// Return value: The trimmed string. NULL if the allocation fails. +// External functs: malloc +// Description: +// Allocates (with malloc(3)) and returns a copy of ’s1’ +// with the characters specified in ’set’ removed +// from the beginning and the end of the string. + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t i; + size_t j; + char *str; + + str = 0; + if (set == 0) + return (ft_strdup((char *)s1)); + if (s1 != 0) + { + i = 0; + j = ft_strlen(s1); + while (s1[i] && ft_strchr(set, s1[i])) + i++; + while (s1[j - 1] && ft_strchr(set, s1[j - 1]) && j > i) + j--; + str = (char *)malloc(sizeof(char) * (j - i + 1)); + if (str) + ft_strlcpy(str, &s1[i], j - i + 1); + } + return (str); +} + +// test ft_strtrim function + +// #include + +// int main(int argc, char **argv) +// { +// // printf("%s|\n", ft_strtrim(argv[1], argv[2])); +// printf("%s|", ft_strtrim("basic test", " ")); +// return (0); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_substr.c b/lib/ft_printf/libft/src/ft_substr.c new file mode 100644 index 0000000..64eff1f --- /dev/null +++ b/lib/ft_printf/libft/src/ft_substr.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 10:20:56 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:54:36 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Function name: ft_substr +// Prototype: +// char *ft_substr(char const *s, unsigned int start, +// size_t len); +// Parameters: +// s: The string from which to create the substring. +// start: The start index of the substring in the string ’s’. +// len: The maximum length of the substring. +// Return value: +// The substring. +// NULL if the allocation fails. +// External functs: +// malloc +// Description: +// Allocates (with malloc(3)) and returns a substring from the string ’s’. +// The substring begins at index ’start’ and is of maximum size ’len’. + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *substr; + + if (start > ft_strlen(s)) + { + substr = malloc(sizeof(char) * 1); + substr[0] = '\0'; + } + else + { + if (len > ft_strlen(s + start)) + len = ft_strlen(s + start); + substr = malloc(sizeof(char) * (len + 1)); + if (substr == 0) + return (NULL); + else + ft_strlcpy(substr, s + start, len + 1); + } + return (substr); +} + +// #include +// int main() +// { +// printf("%s\n", ft_substr("123456789", 2, 5)); +// } \ No newline at end of file diff --git a/lib/ft_printf/libft/src/ft_tolower.c b/lib/ft_printf/libft/src/ft_tolower.c new file mode 100644 index 0000000..e82458d --- /dev/null +++ b/lib/ft_printf/libft/src/ft_tolower.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 14:59:20 by sinlee #+# #+# */ +/* Updated: 2023/04/28 15:00:49 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement tolower function + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + { + c -= ('A' - 'a'); + } + return (c); +} diff --git a/lib/ft_printf/libft/src/ft_toupper.c b/lib/ft_printf/libft/src/ft_toupper.c new file mode 100644 index 0000000..57a2dcc --- /dev/null +++ b/lib/ft_printf/libft/src/ft_toupper.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 14:57:52 by sinlee #+# #+# */ +/* Updated: 2023/04/28 14:59:02 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement toupper function + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c + ('A' - 'a')); + return (c); +} diff --git a/lib/ft_printf/libft/src/get_next_line.c b/lib/ft_printf/libft/src/get_next_line.c new file mode 100644 index 0000000..cc326c8 --- /dev/null +++ b/lib/ft_printf/libft/src/get_next_line.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 11:43:18 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include + +char *extract_and_ret(char *str, int mode, char *remain_str) +{ + int index; + char *extract; + + index = gnl_ft_strchr(str, '\n'); + if (index != -1 && index != BUFFER_SIZE - 1) + { + if (mode == 1 || mode == 2) + { + extract = ft_substr(str, index + 1, ft_strlen(str) + 1); + if (remain_str != NULL && ft_strlen(remain_str) > 0 && mode == 1) + extract = ft_strjoin(remain_str, extract); + free(remain_str); + return (extract); + } + else + return (ft_substr(str, 0, index + 1)); + } + else if (ft_strlen(str) > 0 && mode == 0) + return (ft_substr(str, 0, ft_strlen(str) + 1)); + else + { + if (mode == 2) + free(str); + return (NULL); + } +} + +char *get_next_line(int fd) +{ + int read_size; + char *buffer; + static char *remain[1024]; + char *buff_all; + + if (fd < 0 || BUFFER_SIZE <= 0) + return (false); + read_size = 1; + buffer = malloc((BUFFER_SIZE + 1) * sizeof(char)); + buff_all = extract_and_ret(remain[fd], 0, remain[fd]); + remain[fd] = extract_and_ret(remain[fd], 2, remain[fd]); + while (gnl_ft_strchr(buff_all, '\n') == -1 && read_size != 0) + { + read_size = read(fd, buffer, BUFFER_SIZE); + if (read_size <= 0) + break ; + buffer[read_size] = '\0'; + buff_all = ft_strjoin(buff_all, extract_and_ret(buffer, 0, remain[fd])); + remain[fd] = extract_and_ret(buffer, 1, remain[fd]); + } + free(buffer); + return (buff_all); +} diff --git a/lib/ft_printf/libft/src/get_next_line_utils.c b/lib/ft_printf/libft/src/get_next_line_utils.c new file mode 100644 index 0000000..fff3a8c --- /dev/null +++ b/lib/ft_printf/libft/src/get_next_line_utils.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 11:43:22 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:42 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include + +size_t ft_strlen(const char *str) +{ + size_t len; + + len = 0; + if (str == 0) + return (0); + while (*str++) + len++; + return (len); +} + +int gnl_ft_strchr(char *s, char c) +{ + int i; + + i = 0; + if (!s) + return (-1); + while (s[i]) + { + if (s[i] == c) + return (i); + i++; + } + return (-1); +} + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t src_len; + size_t i; + + src_len = ft_strlen(src); + if (size == 0) + return (src_len); + i = 0; + while (i < size - 1 && src[i]) + { + dest[i] = src[i]; + ++i; + } + dest[i] = '\0'; + return (src_len); +} + +char *ft_strjoin(char *s1, char *s2) +{ + char *str; + int i; + + i = -1; + str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!str) + return (0); + if (s1) + { + while (s1[++i]) + str[i] = s1[i]; + i = -1; + } + if (s2) + { + while (s2[++i]) + str[ft_strlen(s1) + i] = s2[i]; + } + free(s2); + str[ft_strlen(s1) + i] = '\0'; + free(s1); + return (str); +} + +char *ft_substr(char *s, unsigned int start, size_t len) +{ + char *substr; + + if (start > ft_strlen(s)) + { + substr = malloc(sizeof(char) * 1); + substr[0] = '\0'; + } + else + { + if (len > ft_strlen(s + start)) + len = ft_strlen(s + start); + if (len == 0) + return (NULL); + substr = malloc(sizeof(char) * (len + 1)); + if (substr == 0) + return (NULL); + else + ft_strlcpy(substr, s + start, len + 1); + } + return (substr); +} diff --git a/lib/ft_printf/src/ft_nbr.c b/lib/ft_printf/src/ft_nbr.c new file mode 100644 index 0000000..cdb8cb0 --- /dev/null +++ b/lib/ft_printf/src/ft_nbr.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/19 11:16:41 by sinlee #+# #+# */ +/* Updated: 2023/05/19 14:57:06 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putnbr_len(int n, int *len) +{ + char *nbr; + + nbr = ft_itoa(n); + ft_putstr_len(nbr, len); + free(nbr); +} + +void ft_putnbr_u_len(unsigned int n, int *len) +{ + if (n >= 10) + ft_putnbr_u_len(n / 10, len); + ft_putchar_len((n % 10) + '0', len); +} + +static char *ft_nbr_base_len(unsigned int nbr, char *base) +{ + unsigned int length; + char *ans; + int attr[2]; + char display[11]; + + attr[0] = 0; + attr[1] = 0; + ans = malloc(11); + length = (unsigned int)ft_strlen(base); + while (nbr >= length) + { + display[attr[0]++] = base[(nbr % length)]; + nbr /= length; + } + display[attr[0]] = base[nbr]; + while (attr[0] >= 0) + ans[attr[1]++] = display[attr[0]--]; + ans[attr[1]] = '\0'; + return (ans); +} + +void ft_putnbr_hexa_len(unsigned int nbr, int *len, bool up) +{ + char *ans; + + if (up == true) + ans = ft_nbr_base_len(nbr, "0123456789ABCDEF"); + else + ans = ft_nbr_base_len(nbr, "0123456789abcdef"); + ft_putstr_len(ans, len); + free(ans); +} + +void ft_putpointer_len(uintptr_t pointer, int *len) +{ + char ans[25]; + int i; + char *base; + + base = "0123456789abcdef"; + i = 0; + ft_putstr_len("0x", len); + if (pointer == 0) + { + ft_putchar_len('0', len); + return ; + } + while (pointer != 0) + { + ans[i++] = base[pointer % 16]; + pointer /= 16; + } + while (i--) + ft_putchar_len(ans[i], len); +} diff --git a/lib/ft_printf/src/ft_printf.c b/lib/ft_printf/src/ft_printf.c new file mode 100644 index 0000000..626c101 --- /dev/null +++ b/lib/ft_printf/src/ft_printf.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/19 10:03:13 by sinlee #+# #+# */ +/* Updated: 2023/05/19 14:34:20 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement printf function + +#include "ft_printf.h" + +static void ft_printf_check(char c, va_list *args, int *i, int *len) +{ + if (c == 'c') + ft_putchar_len(va_arg(*args, int), len); + else if (c == 's') + ft_putstr_len(va_arg(*args, char *), len); + else if (c == 'p') + ft_putpointer_len(va_arg(*args, uintptr_t), len); + else if (c == 'd' || c == 'i') + ft_putnbr_len(va_arg(*args, int), len); + else if (c == 'u') + ft_putnbr_u_len(va_arg(*args, unsigned int), len); + else if (c == 'x') + ft_putnbr_hexa_len(va_arg(*args, unsigned int), len, false); + else if (c == 'X') + ft_putnbr_hexa_len(va_arg(*args, unsigned int), len, true); + else if (c == '%') + ft_putchar_len('%', len); + else + (*i)--; +} + +int ft_printf(const char *str, ...) +{ + va_list args; + int i; + int len; + + va_start(args, str); + len = 0; + i = 0; + while (str[i] != '\0') + { + if (str[i] == '%') + { + i++; + ft_printf_check(str[i], &args, &i, &len); + } + else + ft_putchar_len(str[i], &len); + i++; + } + va_end(args); + return (len); +} diff --git a/lib/ft_printf/src/ft_string.c b/lib/ft_printf/src/ft_string.c new file mode 100644 index 0000000..c90c2a7 --- /dev/null +++ b/lib/ft_printf/src/ft_string.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/19 10:44:19 by sinlee #+# #+# */ +/* Updated: 2023/05/19 13:49:14 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_len(char c, int *len) +{ + ft_putchar_fd(c, 1); + (*len)++; +} + +void ft_putstr_len(char *str, int *len) +{ + if (!str) + { + ft_putstr_fd("(null)", 1); + *len += 6; + return ; + } + while (*str) + { + ft_putchar_len(*str, len); + str++; + } +} diff --git a/lib/libft/.github/workflows/c-cpp.yml b/lib/libft/.github/workflows/c-cpp.yml new file mode 100644 index 0000000..86724c8 --- /dev/null +++ b/lib/libft/.github/workflows/c-cpp.yml @@ -0,0 +1,17 @@ +name: C/C++ CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: make all + run: make all diff --git a/lib/libft/.gitignore b/lib/libft/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/lib/libft/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/lib/libft/LICENSE b/lib/libft/LICENSE new file mode 100644 index 0000000..794a7e9 --- /dev/null +++ b/lib/libft/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Architect + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/libft/Makefile b/lib/libft/Makefile new file mode 100644 index 0000000..aa1a9a0 --- /dev/null +++ b/lib/libft/Makefile @@ -0,0 +1,93 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: sinlee +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/07/16 04:12:09 by sinlee #+# #+# # +# Updated: 2023/07/16 04:12:10 by sinlee ### ########.fr # +# # +# **************************************************************************** # + +SRCS_DIR = src/ +SRCS = ft_atoi.c \ + ft_bzero.c \ + ft_calloc.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_itoa.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_putchar_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_putstr_fd.c \ + ft_split.c \ + ft_strchr.c \ + ft_strdup.c \ + ft_strjoin.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strmapi.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strtrim.c \ + ft_substr.c \ + ft_striteri.c \ + ft_tolower.c \ + ft_toupper.c \ + get_next_line.c \ + get_next_line_utils.c + +SRCS_B = ft_lstadd_back.c \ + ft_lstadd_front.c \ + ft_lstclear.c \ + ft_lstdelone.c \ + ft_lstiter.c \ + ft_lstlast.c \ + ft_lstmap.c \ + ft_lstnew.c \ + ft_lstsize.c + + +CFILES = $(addprefix $(SRCS_DIR), $(SRCS)) +CFILES_B = $(addprefix $(SRCS_DIR), $(SRCS_B)) + +OBJS = ${CFILES:.c=.o} +OBJS_B = ${CFILES_B:.c=.o} +INCS = ./includes +NAME = libft.a +LIBC = ar rcs +LIBR = ranlib +CC = gcc +RM = rm -f +CFLAGS = -std=c11 -Wall -Wextra -Werror -I$(INCS) + + +all: ${NAME} + +$(NAME): ${OBJS} ${OBJS_B} + ${LIBC} $(NAME) ${OBJS} ${OBJS_B} + +#for 42 bonus +bonus: ${OBJS} ${OBJS_B} + ${LIBC} $(NAME) ${OBJS_B} + +clean: + ${RM} ${OBJS} ${OBJS_B} + +fclean: clean + ${RM} $(NAME) $(bonus) + +re: fclean all + +.PHONY: all bonus clean fclean re \ No newline at end of file diff --git a/lib/libft/README.md b/lib/libft/README.md new file mode 100644 index 0000000..487ce10 --- /dev/null +++ b/lib/libft/README.md @@ -0,0 +1,56 @@ +# Libft + +Libft is a custom C library that contains implementations of various standard library functions in C. These functions are commonly used in C programming and provide essential functionality for tasks such as string manipulation, memory allocation, and list manipulation. By using Libft, you can have access to these functions without relying on the standard library. + +## Table of Contents + +- [Installation](#installation) +- [Usage](#usage) +- [License](#license) + +## Installation + +To use Libft in your C projects, follow these steps: + +1. Clone the Libft repository to your local machine: + ```bash + https://github.com/LeeSinLiang/Libft + ``` + +2. Compile the library by running the Makefile: + ```bash + cd Libft + make + ``` + +3. Once the compilation is complete, you will have a `libft.a` file that contains the compiled library. You can link this library with your C programs to use the implemented functions. + +4. Finally, compile your C program with the Libft library: + ```bash + gcc my_program.c -L. -lft + ``` + +## Usage + +Libft provides a wide range of functions categorized into different sections, including string manipulation, memory allocation, linked lists, and more. + +To use a specific function from Libft, declare the corresponding prototype function file in your C file and call the function by its name. For example, to use the `ft_atoi` function: + +```c +#include + +int ft_atoi(const char *str); + +int main() +{ + char *str = "123456789"; + printf("%d", ft_atoi(str)); + return (0); +} +``` + +For more details on the available functions and their usage, refer to the individual c files and their corresponding comments. + +## License + +MIT License diff --git a/lib/libft/includes/get_next_line.h b/lib/libft/includes/get_next_line.h new file mode 100644 index 0000000..b8add16 --- /dev/null +++ b/lib/libft/includes/get_next_line.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/25 15:53:31 by sinlee #+# #+# */ +/* Updated: 2023/07/16 04:11:51 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include +# include +# include +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 5 +# endif + +size_t ft_strlen(const char *str); +char *ft_substr(char *s, unsigned int start, size_t len); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +int gnl_ft_strchr(char *s, char c); +int ft_find_newline_pos(char *str); +char *get_next_line(int fd); +void print_debug(char *str); +char *ft_strjoin(char *s1, char *s2); + +#endif \ No newline at end of file diff --git a/lib/libft/includes/libft.h b/lib/libft/includes/libft.h new file mode 100644 index 0000000..023fa7e --- /dev/null +++ b/lib/libft/includes/libft.h @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 09:52:13 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:08:47 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include +# include +# include + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +size_t ft_strlen(const char *str); +void *ft_memset(void *ptr, int value, size_t num); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlcat(char *dest, const char *src, size_t size); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +int ft_atoi(const char *str); +void *ft_calloc(size_t count, size_t size); +char *ft_strdup(const char *s1); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +#endif diff --git a/lib/libft/src/ft_atoi.c b/lib/libft/src/ft_atoi.c new file mode 100644 index 0000000..2216ee9 --- /dev/null +++ b/lib/libft/src/ft_atoi.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/05 10:33:07 by sinlee #+# #+# */ +/* Updated: 2023/05/05 09:38:36 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static bool is_in_string(char c, char *str) +{ + while (*str) + { + if (*str == c) + return (true); + str++; + } + return (false); +} + +static bool is_space(char c) +{ + return (is_in_string(c, "\t\n\v\f\r ")); +} + +int ft_atoi(const char *str) +{ + int ans; + int result; + + ans = 0; + result = 1; + while (is_space(*str)) + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + result *= -1; + str++; + } + while (*str >= '0' && *str <= '9') + { + ans *= 10; + ans += *str - 48; + str++; + } + return (result * ans); +} diff --git a/lib/libft/src/ft_bzero.c b/lib/libft/src/ft_bzero.c new file mode 100644 index 0000000..49e81f3 --- /dev/null +++ b/lib/libft/src/ft_bzero.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:56:50 by sinlee #+# #+# */ +/* Updated: 2023/04/28 12:02:54 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of bzero function + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} + +// test ft_bzero function +// #include + +// int main() +// { +// char str[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore bzero(): %s\n", str); +// ft_bzero(str + 13, 8*sizeof(char)); +// printf("After bzero(): %s|", str); +// char str1[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore bzero(): %s\n", str1); +// bzero(str1 + 13, 8*sizeof(char)); +// printf("After bzero(): %s|", str1); +// return 0; +// } \ No newline at end of file diff --git a/lib/libft/src/ft_calloc.c b/lib/libft/src/ft_calloc.c new file mode 100644 index 0000000..21227c0 --- /dev/null +++ b/lib/libft/src/ft_calloc.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 09:54:31 by sinlee #+# #+# */ +/* Updated: 2023/05/05 13:57:44 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of calloc function + +#include "libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + void *ptr; + + if (count == SIZE_MAX || size == SIZE_MAX) + return (NULL); + ptr = malloc(count * size); + if (ptr == NULL) + return (NULL); + ft_bzero(ptr, count * size); + return ((void *)ptr); +} + +// test ft_calloc function + +// #include + +// int main() +// { +// char *str; +// int i; + +// printf("ft_calloc: %p \n", ft_calloc(SIZE_MAX, SIZE_MAX)); +// return (0); +// } diff --git a/lib/libft/src/ft_isalnum.c b/lib/libft/src/ft_isalnum.c new file mode 100644 index 0000000..4917204 --- /dev/null +++ b/lib/libft/src/ft_isalnum.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:56:49 by sinlee #+# #+# */ +/* Updated: 2023/04/28 11:41:46 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// implementation of isalnum function + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (1); + return (0); +} + +// test ft_isalnum function +// #include +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_isalnum((int)*(argv[1]))); +// printf("%d\n", isalnum((int)*(argv[1]))); +// } diff --git a/lib/libft/src/ft_isalpha.c b/lib/libft/src/ft_isalpha.c new file mode 100644 index 0000000..befeb3b --- /dev/null +++ b/lib/libft/src/ft_isalpha.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:45:29 by sinlee #+# #+# */ +/* Updated: 2023/04/28 10:51:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return (1); + return (0); +} + +// test ft_isalpha function +// #include +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_isalpha((int)*(argv[1]))); +// printf("%d\n", isalpha((int)*(argv[1]))); +// } diff --git a/lib/libft/src/ft_isascii.c b/lib/libft/src/ft_isascii.c new file mode 100644 index 0000000..7255280 --- /dev/null +++ b/lib/libft/src/ft_isascii.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:41:52 by sinlee #+# #+# */ +/* Updated: 2023/04/28 11:47:03 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// implementation of isascii function + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} + +// test ft_isascii function +// #include +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_isascii((int)*(argv[1]))); +// printf("%d\n", isascii((int)*(argv[1]))); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_isdigit.c b/lib/libft/src/ft_isdigit.c new file mode 100644 index 0000000..d2b0483 --- /dev/null +++ b/lib/libft/src/ft_isdigit.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:54:37 by sinlee #+# #+# */ +/* Updated: 2023/04/28 10:56:24 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of isdigit function + +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/lib/libft/src/ft_isprint.c b/lib/libft/src/ft_isprint.c new file mode 100644 index 0000000..d797dae --- /dev/null +++ b/lib/libft/src/ft_isprint.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:44:54 by sinlee #+# #+# */ +/* Updated: 2023/04/28 11:47:36 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of isprint function + +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/lib/libft/src/ft_itoa.c b/lib/libft/src/ft_itoa.c new file mode 100644 index 0000000..5d07eb0 --- /dev/null +++ b/lib/libft/src/ft_itoa.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 14:04:49 by sinlee #+# #+# */ +/* Updated: 2023/05/03 14:14:49 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement ft_itoa function: convert int to string + +#include "libft.h" + +static int ft_len(int n) +{ + int len; + + len = 0; + if (n <= 0) + len++; + while (n != 0) + { + n /= 10; + len++; + } + return (len); +} + +static int ft_abs(int n) +{ + if (n < 0) + return (-n); + return (n); +} + +char *ft_itoa(int n) +{ + int len; + char *str; + + len = ft_len(n); + str = (char *)malloc((len + 1) * 1); + if (!str) + return (0); + str[len] = '\0'; + len--; + if (n == 0) + str[0] = '0'; + if (n < 0) + str[0] = '-'; + while (n != 0) + { + str[len] = ft_abs(n % 10) + '0'; + n /= 10; + len--; + } + return (str); +} + +// #include +// int main(int argc, char **argv) +// { +// if (argc == 2) +// { +// printf("%s\n", ft_itoa(atoi(argv[1]))); +// } +// return (0); +// } diff --git a/lib/libft/src/ft_lstadd_back.c b/lib/libft/src/ft_lstadd_back.c new file mode 100644 index 0000000..61d1f79 --- /dev/null +++ b/lib/libft/src/ft_lstadd_back.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:14:44 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:11:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstadd_back function: +// add the node 'new' at the end of the linked list + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *current; + + current = *lst; + if (lst) + { + if (current) + { + current = ft_lstlast(current); + current->next = new; + } + else + *lst = new; + } +} diff --git a/lib/libft/src/ft_lstadd_front.c b/lib/libft/src/ft_lstadd_front.c new file mode 100644 index 0000000..0ad27ab --- /dev/null +++ b/lib/libft/src/ft_lstadd_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 15:35:43 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:35:52 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (lst == NULL || new == NULL) + return ; + new->next = *lst; + *lst = new; +} diff --git a/lib/libft/src/ft_lstclear.c b/lib/libft/src/ft_lstclear.c new file mode 100644 index 0000000..908190b --- /dev/null +++ b/lib/libft/src/ft_lstclear.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:18:59 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:26:03 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstclear function: +// delete all nodes of list with the function 'del' + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *temp; + + if (!del || !lst || !*lst) + return ; + while (lst && *lst) + { + temp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = temp; + } +} diff --git a/lib/libft/src/ft_lstdelone.c b/lib/libft/src/ft_lstdelone.c new file mode 100644 index 0000000..76c80b1 --- /dev/null +++ b/lib/libft/src/ft_lstdelone.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:17:03 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:21:56 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstdelone function: +// delete the node 'lst' with the function 'del' + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (!del) + return ; + if (lst) + { + del(lst->content); + free(lst); + } +} diff --git a/lib/libft/src/ft_lstiter.c b/lib/libft/src/ft_lstiter.c new file mode 100644 index 0000000..120e4b1 --- /dev/null +++ b/lib/libft/src/ft_lstiter.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:25:01 by sinlee #+# #+# */ +/* Updated: 2023/05/05 16:30:09 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstiter function: +// iterates the list 'lst' +// and applies the function 'f' to the content of each node + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *current; + + current = lst; + while (current != NULL) + { + f(current->content); + current = current->next; + } +} diff --git a/lib/libft/src/ft_lstlast.c b/lib/libft/src/ft_lstlast.c new file mode 100644 index 0000000..8338f97 --- /dev/null +++ b/lib/libft/src/ft_lstlast.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:13:02 by sinlee #+# #+# */ +/* Updated: 2023/05/05 17:09:24 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements lstlast function: return last node of list + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + t_list *current; + + current = lst; + while (current != NULL && current->next != NULL) + current = current->next; + return (current); +} +// #include +// int main() +// { +// t_list * l = NULL; +// ft_lstadd_back(&l, ft_lstnew((void*)1)); +// printf("%d", *(int *)(ft_lstlast(l)->content)); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_lstmap.c b/lib/libft/src/ft_lstmap.c new file mode 100644 index 0000000..361a61c --- /dev/null +++ b/lib/libft/src/ft_lstmap.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:27:55 by sinlee #+# #+# */ +/* Updated: 2023/05/05 16:31:32 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_lstmap function: +// iterates the list 'lst' and applies the function 'f' to the +// content of each node + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *current; + t_list *new; + t_list *temp; + + current = lst; + new = NULL; + while (current != NULL) + { + temp = ft_lstnew(f(current->content)); + if (temp == NULL) + { + ft_lstclear(&new, del); + return (NULL); + } + ft_lstadd_back(&new, temp); + current = current->next; + } + return (new); +} diff --git a/lib/libft/src/ft_lstnew.c b/lib/libft/src/ft_lstnew.c new file mode 100644 index 0000000..6cca7cd --- /dev/null +++ b/lib/libft/src/ft_lstnew.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 15:21:59 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:34:40 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of lstnew function: add new node to linked list + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *node; + + node = malloc(sizeof(t_list)); + if (!node) + return (NULL); + node->content = content; + node->next = NULL; + return (node); +} + +// test ft_lstnew function + +// #include +// int main() +// { +// t_list *node; + +// node = ft_lstnew("hello"); +// printf("%s", node->content); +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_lstsize.c b/lib/libft/src/ft_lstsize.c new file mode 100644 index 0000000..27d866b --- /dev/null +++ b/lib/libft/src/ft_lstsize.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/05 16:09:33 by sinlee #+# #+# */ +/* Updated: 2023/05/05 16:12:41 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement ft_lstsize function: count number of nodes in a linked list + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + t_list *current; + + count = 0; + current = lst; + while (current != NULL) + { + count++; + current = current->next; + } + return (count); +} diff --git a/lib/libft/src/ft_memchr.c b/lib/libft/src/ft_memchr.c new file mode 100644 index 0000000..0367d64 --- /dev/null +++ b/lib/libft/src/ft_memchr.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:32:44 by sinlee #+# #+# */ +/* Updated: 2023/05/03 18:34:32 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t index; + + index = 0; + while (index < n) + { + if (((unsigned char *)s)[index] == (unsigned char)c) + return (((unsigned char *)s) + index); + index++; + } + return (NULL); +} + +// #include + +// int main() +// { +// char s[] = {0, 1, 2, 3, 4, 5}; + +// printf("%s\n", ft_memchr(s, 0, 0)); +// printf("%s\n", memchr(s, 0, 0)); +// printf("---------------------\n"); +// printf("%s\n", ft_memchr(s, 2, 3)); +// printf("%s\n", memchr(s, 2, 3)); +// printf("---------------------\n"); +// printf("%s\n", ft_memchr(s, 2+256, 3)); +// printf("%s\n", memchr(s, 2+256, 3)); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_memcmp.c b/lib/libft/src/ft_memcmp.c new file mode 100644 index 0000000..961bb8c --- /dev/null +++ b/lib/libft/src/ft_memcmp.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:36:51 by sinlee #+# #+# */ +/* Updated: 2023/05/05 12:57:41 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memcmp function + +#include "libft.h" +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + while (n--) + { + if (*(unsigned char *)s1 != *(unsigned char *)s2) + return (*(unsigned char *)s1 - *(unsigned char *)s2); + s1++; + s2++; + } + return (0); +} + +// test ft_memcmp function by comparing with memcmp function + +// #include +// #include + +// int main(int argc, char **argv) +// { +// char s2[] = {0, 0, 127, 0}; +// char s3[] = {0, 0, 42, 0}; +// printf("ft_memcmp: %d\n", ft_memcmp(s2, s3, 4)); +// printf("memcmp: %d\n", memcmp(s2, s3, 4)); +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_memcpy.c b/lib/libft/src/ft_memcpy.c new file mode 100644 index 0000000..f076d45 --- /dev/null +++ b/lib/libft/src/ft_memcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 12:04:51 by sinlee #+# #+# */ +/* Updated: 2023/05/05 12:54:27 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memcpy function + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + + i = 0; + if (dst == NULL && src == NULL) + return (NULL); + while (i < n) + { + ((char *)dst)[i] = ((char *)src)[i]; + i++; + } + return ((void *)dst); +} diff --git a/lib/libft/src/ft_memmove.c b/lib/libft/src/ft_memmove.c new file mode 100644 index 0000000..2bce591 --- /dev/null +++ b/lib/libft/src/ft_memmove.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 12:27:24 by sinlee #+# #+# */ +/* Updated: 2023/04/28 12:54:04 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memmove function + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned char *ptr_dest; + unsigned char *ptr_src; + size_t i; + + ptr_dest = (unsigned char *)dest; + ptr_src = (unsigned char *)src; + i = 0; + if (ptr_dest > ptr_src) + { + while (n--) + ptr_dest[n] = ptr_src[n]; + } + else + { + while (i < n) + { + ptr_dest[i] = ptr_src[i]; + i++; + } + } + return (dest); +} + +// test ft_memmove function + +// #include + +// int main(void) +// { +// char str1[50] = "Hello"; +// char str2[50] = "WorldHi"; +// ft_memmove(str1, str2, 3 * sizeof(char)); +// printf("After memmove(): %s|\n", str1); +// char str3[50] = "Hello"; +// char str4[50] = "WorldHi"; +// memmove(str3, str4, 3 * sizeof(char)); +// printf("After memmove(): %s|", str3); +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_memset.c b/lib/libft/src/ft_memset.c new file mode 100644 index 0000000..446eaad --- /dev/null +++ b/lib/libft/src/ft_memset.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 11:48:04 by sinlee #+# #+# */ +/* Updated: 2023/04/28 12:01:46 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of memset function + +#include "libft.h" + +void *ft_memset(void *ptr, int value, size_t num) +{ + unsigned char *temp; + + temp = (unsigned char *)ptr; + while (num--) + { + *temp = (unsigned char)value; + temp++; + } + return (ptr); +} + +// test ft_memset function +// #include + +// int main() +// { +// char str[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore memset(): %s\n", str); +// ft_memset(str + 13, '.', 8*sizeof(char)); +// printf("After memset(): %s", str); +// char str1[50] = "GeeksForGeeks is for programming geeks."; +// printf("\nBefore memset(): %s\n", str1); +// memset(str1 + 13, '.', 8*sizeof(char)); +// printf("After memset(): %s", str1); +// return 0; +// } \ No newline at end of file diff --git a/lib/libft/src/ft_putchar_fd.c b/lib/libft/src/ft_putchar_fd.c new file mode 100644 index 0000000..21a74c0 --- /dev/null +++ b/lib/libft/src/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:29:39 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:30:17 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement putchar_fd function: output character to file descriptor + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/lib/libft/src/ft_putendl_fd.c b/lib/libft/src/ft_putendl_fd.c new file mode 100644 index 0000000..39c5398 --- /dev/null +++ b/lib/libft/src/ft_putendl_fd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:31:51 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:32:21 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_putendl_fd function: +// output string to file descriptor with newline + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (!s) + return ; + ft_putstr_fd(s, fd); + ft_putchar_fd(10, fd); +} diff --git a/lib/libft/src/ft_putnbr_fd.c b/lib/libft/src/ft_putnbr_fd.c new file mode 100644 index 0000000..410271e --- /dev/null +++ b/lib/libft/src/ft_putnbr_fd.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:33:21 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:29:22 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements ft_putnbr_fd function: output integer to file descriptor + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + char *str; + + str = ft_itoa(n); + write(fd, str, ft_strlen(str)); + free(str); +} +// void ft_putnbr_fd(int n, int fd) +// { +// write(fd, ft_itoa(n), ft_strlen(ft_itoa(n))); +// } diff --git a/lib/libft/src/ft_putstr_fd.c b/lib/libft/src/ft_putstr_fd.c new file mode 100644 index 0000000..49d9256 --- /dev/null +++ b/lib/libft/src/ft_putstr_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:31:00 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:31:29 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implements putstr_fd function: output string to file descriptor + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (!s) + return ; + write(fd, s, ft_strlen(s)); +} diff --git a/lib/libft/src/ft_split.c b/lib/libft/src/ft_split.c new file mode 100644 index 0000000..f75976b --- /dev/null +++ b/lib/libft/src/ft_split.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/13 14:51:39 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:15:30 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static bool check_seperator(char c_str, char c) +{ + if (c_str == c) + return (true); + return (false); +} + +static int count_strings(char *str, char c) +{ + int count; + + count = 0; + while (*str) + { + while (*str && check_seperator(*str, c)) + str++; + if (*str) + count++; + while (*str && !check_seperator(*str, c)) + str++; + } + return (count); +} + +// count string before character c +static int ft_strclen(char *str, char c) +{ + int i; + + i = 0; + while (str[i] && !check_seperator(str[i], c)) + i++; + return (i); +} + +static char *word(char *str, char c) +{ + int i; + int len; + char *word; + + i = 0; + len = ft_strclen(str, c); + word = (char *)malloc((len + 1) * 1); + while (i < len) + { + word[i] = str[i]; + i++; + } + word[i] = '\0'; + return (word); +} + +char **ft_split(char const *s, char c) +{ + char **arr; + char *str; + int i; + + i = 0; + if (!s) + return (0); + str = (char *)s; + arr = (char **)malloc((count_strings(str, c) + 1) * 8); + while (*str) + { + while (*str && check_seperator(*str, c)) + str++; + if (*str) + { + arr[i] = word(str, c); + i++; + } + while (*str && !check_seperator(*str, c)) + str++; + } + arr[i] = 0; + return (arr); +} + +// #include +// int main(int argc, char **argv) +// { +// int index; +// char **split; + +// split = ft_split(argv[1], argv[2][0]); +// index = 0; +// printf("tab start\n"); +// while (split[index]) +// { +// printf("tab[%d]: $%s$\n", index, split[index]); +// fflush(stdout); +// index++; +// } +// printf("tab end\n"); +// } diff --git a/lib/libft/src/ft_strchr.c b/lib/libft/src/ft_strchr.c new file mode 100644 index 0000000..f515bc8 --- /dev/null +++ b/lib/libft/src/ft_strchr.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:02:37 by sinlee #+# #+# */ +/* Updated: 2023/04/28 15:29:27 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement strchr function + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != (char)c) + { + if (!*s) + return (0); + s++; + } + return ((char *)s); +} + +// test ft_strchr function + +// #include + +// int main(void) +// { +// char *str; +// char *ptr; + +// str = "Hello World"; +// ptr = ft_strchr(str, 0); +// printf("%s\n", ptr); +// str = "Hello World"; +// ptr = strchr(str, 0); +// printf("%s\n", ptr); +// return (0); +// } diff --git a/lib/libft/src/ft_strdup.c b/lib/libft/src/ft_strdup.c new file mode 100644 index 0000000..55ba7ae --- /dev/null +++ b/lib/libft/src/ft_strdup.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 10:06:17 by sinlee #+# #+# */ +/* Updated: 2023/04/28 13:07:54 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of ft_strdup function + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *ptr; + size_t len; + + len = ft_strlen(s1); + ptr = (char *)malloc((len + 1) * sizeof(char)); + if (ptr == NULL) + return (NULL); + ft_strlcpy(ptr, s1, len + 1); + return (ptr); +} + +// test ft_strdup function +// #include +// int main() +// { +// char *str; +// char *str2; + +// str = strdup("Hello World!|"); +// str2 = ft_strdup("Hello World!|"); +// printf("%s\n", str); +// printf("%s\n", str2); +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_striteri.c b/lib/libft/src/ft_striteri.c new file mode 100644 index 0000000..359f69b --- /dev/null +++ b/lib/libft/src/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 15:25:36 by sinlee #+# #+# */ +/* Updated: 2023/05/03 16:30:18 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement striteri function: apply function to each character of string + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + if (!s || !f) + return ; + i = -1; + while (s[++i]) + f(i, &s[i]); +} diff --git a/lib/libft/src/ft_strjoin.c b/lib/libft/src/ft_strjoin.c new file mode 100644 index 0000000..9eea352 --- /dev/null +++ b/lib/libft/src/ft_strjoin.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 11:08:15 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:16:15 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Function name: ft_strjoin +// Prototype: char: *ft_strjoin(char const *s1, char const *s2); +// Parameters: +// s1: The prefix string. s2: The suffix string. +// Return value: The new string. NULL if the allocation fails. +// External functs: malloc +// Description: +// Allocates (with malloc(3)) and returns a new string, +// which is the result of the concatenation of ’s1’ and ’s2’. + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + int i; + + i = -1; + str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!str) + return (0); + while (s1[++i]) + str[i] = s1[i]; + i = -1; + while (s2[++i]) + str[ft_strlen(s1) + i] = s2[i]; + str[ft_strlen(s1) + i] = '\0'; + return (str); +} + +// test ft_strjoin function. check strlcat const +// #include + +// int main() +// { +// char *s1 = "tripouille"; +// char *s2 = "42"; +// char *s3 = ft_strjoin("", "42"); +// printf("%s|\n", s3); +// printf("%d", strcmp(s3, "42")); +// // printf("%s|\n", strjoin("", "42")); +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_strlcat.c b/lib/libft/src/ft_strlcat.c new file mode 100644 index 0000000..f31927c --- /dev/null +++ b/lib/libft/src/ft_strlcat.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/04 16:08:28 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:40:11 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t count(const char *dest) +{ + size_t len; + + len = 0; + while (dest[len] != '\0') + len++; + return (len); +} + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + char *dst; + const char *src_i; + size_t dest_length; + size_t remaining; + + dst = dest; + src_i = src; + remaining = size; + while (remaining-- != 0 && *dst != '\0') + dst++; + dest_length = dst - dest; + remaining = size - dest_length; + if (remaining == 0) + return (dest_length + count((src))); + while (*src != '\0') + { + if (remaining > 1) + { + *dst++ = *src; + remaining--; + } + src++; + } + *dst = '\0'; + return (dest_length + (src - src_i)); +} diff --git a/lib/libft/src/ft_strlcpy.c b/lib/libft/src/ft_strlcpy.c new file mode 100644 index 0000000..7e1d0a9 --- /dev/null +++ b/lib/libft/src/ft_strlcpy.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/29 17:39:35 by sinlee #+# #+# */ +/* Updated: 2023/04/28 13:15:17 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of strlcpy function + +#include "libft.h" + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t src_len; + size_t i; + + src_len = ft_strlen(src); + if (size == 0) + return (src_len); + i = 0; + while (i < size - 1 && src[i]) + { + dest[i] = src[i]; + ++i; + } + dest[i] = '\0'; + return (src_len); +} + +// test ft_strlcpy function + +// #include +// int main(void) +// { +// char *string1; +// char string2[10]; + +// string1 = "Hello"; +// printf("base : %s\n", string1); +// strlcpy(string2, string1, 5); +// printf("cpy c : %s\n", string2); +// ft_strlcpy(string2, string1, 5); +// printf("cpy ft : %s\n", string2); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_strlen.c b/lib/libft/src/ft_strlen.c new file mode 100644 index 0000000..ed95a2d --- /dev/null +++ b/lib/libft/src/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 09:58:28 by sinlee #+# #+# */ +/* Updated: 2023/04/28 10:00:09 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of ft_strlen function + +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + size_t len; + + len = 0; + while (*str++) + len++; + return (len); +} diff --git a/lib/libft/src/ft_strmapi.c b/lib/libft/src/ft_strmapi.c new file mode 100644 index 0000000..273bbc3 --- /dev/null +++ b/lib/libft/src/ft_strmapi.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 14:17:04 by sinlee #+# #+# */ +/* Updated: 2023/05/03 15:27:34 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement strmapi function: apply function to each character of string + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *str; + int i; + + if (!s || !f) + return (NULL); + str = (char *)malloc((ft_strlen(s) + 1) * 1); + if (!str) + return (NULL); + i = 0; + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = '\0'; + return (str); +} + +// test ft_strmapi function +// #include + +// char ft_test(unsigned int i, char c) +// { +// i = 0; +// return (c - 32); +// } + +// int main() +// { +// char *str = "Hello World!"; +// char *new_str = ft_strmapi(str, ft_test); +// printf("%s\n", new_str); + // prints only Hello due to the null terminator (space - 32) +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_strncmp.c b/lib/libft/src/ft_strncmp.c new file mode 100644 index 0000000..6409538 --- /dev/null +++ b/lib/libft/src/ft_strncmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:25:52 by sinlee #+# #+# */ +/* Updated: 2023/04/28 15:32:15 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement of strncmp function + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + while (n-- && (*s1 || *s2)) + { + if (*s1++ != *s2++) + return (*(unsigned char *)--s1 - *(unsigned char *)--s2); + } + return (0); +} + +// #include + +// int main(int argc, char **argv) +// { +// printf("%d\n", ft_strncmp(argv[1], argv[2], 5)); +// printf("%d\n", strncmp(argv[1], argv[2], 5)); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_strnstr.c b/lib/libft/src/ft_strnstr.c new file mode 100644 index 0000000..2b8ad99 --- /dev/null +++ b/lib/libft/src/ft_strnstr.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 09:32:57 by sinlee #+# #+# */ +/* Updated: 2023/05/05 13:45:45 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement strnstr function + +#include "libft.h" +#include + +static bool check_occur(char *str, char *to_find, size_t len) +{ + size_t i; + + i = 0; + while (to_find[i] != '\0' && len--) + { + if (str[i] != to_find[i]) + return (false); + i++; + } + if (to_find[i] != '\0') + return (false); + return (true); +} + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + char *haystack_cpy; + + haystack_cpy = (char *)haystack; + if (!needle[0]) + return (haystack_cpy); + while (*haystack_cpy && len > 0) + { + if (!check_occur(haystack_cpy, (char *)needle, len)) + { + haystack_cpy++; + len--; + } + else + return (haystack_cpy); + } + return (0); +} + +// test ft_strnstr by comparing with strnstr function + +// #include +// #include + +// int main(int argc, char **argv) +// { +// char *str1; +// char *str2; +// int n; + +// str1 = argv[1]; +// str2 = argv[2]; +// n = atoi(argv[3]); +// printf("%s\n", ft_strnstr(str1, str2, n)); +// printf("%s\n", strnstr(str1, str2, n)); +// // char haystack[30] = "aaabcabcd"; +// // printf("%s\n", ft_strnstr(haystack, "abcd", 9)); +// // printf("%s\n", strnstr(haystack, "abcd", 9)); +// return (0); +// } diff --git a/lib/libft/src/ft_strrchr.c b/lib/libft/src/ft_strrchr.c new file mode 100644 index 0000000..2ba9224 --- /dev/null +++ b/lib/libft/src/ft_strrchr.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 15:15:38 by sinlee #+# #+# */ +/* Updated: 2023/05/03 18:26:59 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implementation of strrchr function + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + char *ptr; + + ptr = 0; + if (c == 0) + return ((char *)s + ft_strlen(s)); + while (*s) + { + if (*s == (char)c) + ptr = (char *)s; + s++; + } + return (ptr); +} + +// test ft_strrchr function + +// #include + +// int main(void) +// { +// char *str; +// char *ptr; +// char * empty = (char*)calloc(1, 1); + +// str = "tripouille"; +// ptr = ft_strrchr(empty, 'V'); +// printf("%s\n", ptr); +// str = "tripouille"; +// ptr = strrchr(empty, 'V'); +// printf("%s\n", ptr); +// return (0); +// } diff --git a/lib/libft/src/ft_strtrim.c b/lib/libft/src/ft_strtrim.c new file mode 100644 index 0000000..1dbcea5 --- /dev/null +++ b/lib/libft/src/ft_strtrim.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 11:27:47 by sinlee #+# #+# */ +/* Updated: 2023/05/05 15:11:55 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Function name: ft_strtrim +// Prototype: char *ft_strtrim(char const *s1, char const *set); +// Parameters: +// s1: The string to be trimmed. +// set: The reference set of characters to trim. +// Return value: The trimmed string. NULL if the allocation fails. +// External functs: malloc +// Description: +// Allocates (with malloc(3)) and returns a copy of ’s1’ +// with the characters specified in ’set’ removed +// from the beginning and the end of the string. + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t i; + size_t j; + char *str; + + str = 0; + if (set == 0) + return (ft_strdup((char *)s1)); + if (s1 != 0) + { + i = 0; + j = ft_strlen(s1); + while (s1[i] && ft_strchr(set, s1[i])) + i++; + while (s1[j - 1] && ft_strchr(set, s1[j - 1]) && j > i) + j--; + str = (char *)malloc(sizeof(char) * (j - i + 1)); + if (str) + ft_strlcpy(str, &s1[i], j - i + 1); + } + return (str); +} + +// test ft_strtrim function + +// #include + +// int main(int argc, char **argv) +// { +// // printf("%s|\n", ft_strtrim(argv[1], argv[2])); +// printf("%s|", ft_strtrim("basic test", " ")); +// return (0); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_substr.c b/lib/libft/src/ft_substr.c new file mode 100644 index 0000000..64eff1f --- /dev/null +++ b/lib/libft/src/ft_substr.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/03 10:20:56 by sinlee #+# #+# */ +/* Updated: 2023/05/05 14:54:36 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Function name: ft_substr +// Prototype: +// char *ft_substr(char const *s, unsigned int start, +// size_t len); +// Parameters: +// s: The string from which to create the substring. +// start: The start index of the substring in the string ’s’. +// len: The maximum length of the substring. +// Return value: +// The substring. +// NULL if the allocation fails. +// External functs: +// malloc +// Description: +// Allocates (with malloc(3)) and returns a substring from the string ’s’. +// The substring begins at index ’start’ and is of maximum size ’len’. + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *substr; + + if (start > ft_strlen(s)) + { + substr = malloc(sizeof(char) * 1); + substr[0] = '\0'; + } + else + { + if (len > ft_strlen(s + start)) + len = ft_strlen(s + start); + substr = malloc(sizeof(char) * (len + 1)); + if (substr == 0) + return (NULL); + else + ft_strlcpy(substr, s + start, len + 1); + } + return (substr); +} + +// #include +// int main() +// { +// printf("%s\n", ft_substr("123456789", 2, 5)); +// } \ No newline at end of file diff --git a/lib/libft/src/ft_tolower.c b/lib/libft/src/ft_tolower.c new file mode 100644 index 0000000..e82458d --- /dev/null +++ b/lib/libft/src/ft_tolower.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 14:59:20 by sinlee #+# #+# */ +/* Updated: 2023/04/28 15:00:49 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement tolower function + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + { + c -= ('A' - 'a'); + } + return (c); +} diff --git a/lib/libft/src/ft_toupper.c b/lib/libft/src/ft_toupper.c new file mode 100644 index 0000000..57a2dcc --- /dev/null +++ b/lib/libft/src/ft_toupper.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/28 14:57:52 by sinlee #+# #+# */ +/* Updated: 2023/04/28 14:59:02 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// implement toupper function + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c + ('A' - 'a')); + return (c); +} diff --git a/lib/libft/src/get_next_line.c b/lib/libft/src/get_next_line.c new file mode 100644 index 0000000..653887e --- /dev/null +++ b/lib/libft/src/get_next_line.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 11:43:18 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:59 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include + +char *extract_and_ret(char *str, int mode, char *remain_str) +{ + int index; + char *extract; + + index = gnl_ft_strchr(str, '\n'); + if (index != -1 && index != BUFFER_SIZE - 1) + { + if (mode == 1 || mode == 2) + { + extract = ft_substr(str, index + 1, ft_strlen(str) + 1); + if (remain_str != NULL && ft_strlen(remain_str) > 0 && mode == 1) + extract = ft_strjoin(remain_str, extract); + free(remain_str); + return (extract); + } + else + return (ft_substr(str, 0, index + 1)); + } + else if (ft_strlen(str) > 0 && mode == 0) + return (ft_substr(str, 0, ft_strlen(str) + 1)); + else + { + if (mode == 2) + free(str); + return (NULL); + } +} + +char *get_next_line(int fd) +{ + int read_size; + char *buffer; + static char *remain[1024]; + char *buff_all; + + if (fd < 0 || BUFFER_SIZE <= 0) + return (false); + read_size = 1; + buffer = malloc((BUFFER_SIZE + 1) * sizeof(char)); + buff_all = extract_and_ret(remain[fd], 0, remain[fd]); + remain[fd] = extract_and_ret(remain[fd], 2, remain[fd]); + while (gnl_ft_strchr(buff_all, '\n') == -1 && read_size != 0) + { + read_size = read(fd, buffer, BUFFER_SIZE); + if (read_size <= 0) + break ; + buffer[read_size] = '\0'; + buff_all = ft_strjoin(buff_all, extract_and_ret(buffer, 0, remain[fd])); + remain[fd] = extract_and_ret(buffer, 1, remain[fd]); + } + free(buffer); + return (buff_all); +} diff --git a/lib/libft/src/get_next_line_utils.c b/lib/libft/src/get_next_line_utils.c new file mode 100644 index 0000000..23fdefd --- /dev/null +++ b/lib/libft/src/get_next_line_utils.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 11:43:22 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:12:02 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include + +size_t ft_strlen(const char *str) +{ + size_t len; + + len = 0; + if (str == 0) + return (0); + while (*str++) + len++; + return (len); +} + +int gnl_ft_strchr(char *s, char c) +{ + int i; + + i = 0; + if (!s) + return (-1); + while (s[i]) + { + if (s[i] == c) + return (i); + i++; + } + return (-1); +} + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t src_len; + size_t i; + + src_len = ft_strlen(src); + if (size == 0) + return (src_len); + i = 0; + while (i < size - 1 && src[i]) + { + dest[i] = src[i]; + ++i; + } + dest[i] = '\0'; + return (src_len); +} + +char *ft_strjoin(char *s1, char *s2) +{ + char *str; + int i; + + i = -1; + str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!str) + return (0); + if (s1) + { + while (s1[++i]) + str[i] = s1[i]; + i = -1; + } + if (s2) + { + while (s2[++i]) + str[ft_strlen(s1) + i] = s2[i]; + } + free(s2); + str[ft_strlen(s1) + i] = '\0'; + free(s1); + return (str); +} + +char *ft_substr(char *s, unsigned int start, size_t len) +{ + char *substr; + + if (start > ft_strlen(s)) + { + substr = malloc(sizeof(char) * 1); + substr[0] = '\0'; + } + else + { + if (len > ft_strlen(s + start)) + len = ft_strlen(s + start); + if (len == 0) + return (NULL); + substr = malloc(sizeof(char) * (len + 1)); + if (substr == 0) + return (NULL); + else + ft_strlcpy(substr, s + start, len + 1); + } + return (substr); +} diff --git a/srcs/algorithm/calculation.c b/srcs/algorithm/calculation.c new file mode 100644 index 0000000..166a1c7 --- /dev/null +++ b/srcs/algorithm/calculation.c @@ -0,0 +1,127 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* calculation.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/06 02:07:07 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:14 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// calculate the number of moves needed to push to stack b + +#include "push_swap.h" + +// least common move +// 'rr' (0) or 'rrr' (1) +// nlcm[2]: nlcm[0] = rr, nlcm[1] = rrr + +static int min_lcm(int *arr, int len, bool return_index) +{ + int min; + int i; + int target_index; + + i = 0; + target_index = 0; + min = arr[0]; + while (++i < len) + { + if (arr[i] < min && arr[i] >= 0) + { + min = arr[i]; + target_index = i; + } + } + if (return_index == true) + return (target_index); + else + return (min); +} + +// calculate minimum moves only, does not reflect actual how to +// pos[2], pos[0] = pos_a, pos[1] = pos_b +int lcm(int pos[2], int len_a, int len_b, bool return_move) +{ + int nlcm[4]; + int temp_pos_b; + + temp_pos_b = -1; + if (pos[1] == 0) + { + temp_pos_b = pos[1]; + pos[1] = pos[0]; + } + if (pos[0] < pos[1]) + nlcm[0] = pos[1]; + else + nlcm[0] = pos[0]; + if (len_a - pos[0] < len_b - pos[1]) + nlcm[1] = (len_b - pos[1]); + else + nlcm[1] = (len_a - pos[0]); + nlcm[2] = pos[0] + (len_b - pos[1]); + nlcm[3] = (len_a - pos[0]) + pos[1]; + if (temp_pos_b != -1) + pos[1] = temp_pos_b; + if (return_move == true) + return (min_lcm(nlcm, 4, true)); + else + return (min_lcm(nlcm, 4, false)); +} + +// stack_a is the current pointer towards the certain element in linked_list. +// not the first! +// target_attr[2]: target_attr[0] = target, target_attr[1] = is_init +// nmoves_to_top[2]: nmoves_to_top[0] = a, nmoves_to_top[1] = b +// + 1 for push to stack +int calc(t_node *stack_a, t_node *stack_b, int len, bool return_pos_b) +{ + t_node *tmp; + int nmoves_to_top[2]; + int target; + + nmoves_to_top[0] = node_index(stack_a, stack_a->content); + tmp = last_first_node(stack_b, false); + if (stack_b) + { + target = tmp->content; + while (tmp) + { + if ((tmp->content > target && tmp->content < stack_a->content) + || (tmp->content < stack_a->content + && target > stack_a->content)) + target = tmp->content; + tmp = tmp->next; + } + nmoves_to_top[1] = node_index(stack_b, target); + if (return_pos_b == true) + return (nmoves_to_top[1]); + else + return (lcm(nmoves_to_top, len, stack_len(stack_b), false) + 1); + } + return (nmoves_to_top[0] + 1); +} + +int execute_calc(t_node *stack_a, t_node *stack_b, int len, bool return_pos_b) +{ + int nmoves; + int pos[2]; + + if ((stack_a->content < min_max_pos(stack_b, false, false)) + || (stack_a->content > min_max_pos(stack_b, true, false))) + { + pos[0] = node_index(stack_a, stack_a->content); + pos[1] = min_max_pos(stack_b, true, true); + if (return_pos_b == true) + nmoves = pos[1]; + else + nmoves = (lcm(pos, stack_len(stack_a), stack_len(stack_b), false) + + 1); + } + else + nmoves = calc(stack_a, stack_b, len, return_pos_b); + return (nmoves); +} diff --git a/srcs/algorithm/push_swap.c b/srcs/algorithm/push_swap.c new file mode 100644 index 0000000..cca0c38 --- /dev/null +++ b/srcs/algorithm/push_swap.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/05 04:44:43 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:12 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// push swap +// approach no. 1: sort via permutations and divide and conquer +// approach no. 2 (tba): search for the best optimal step to sort before +// actual sorting + +#include "push_swap.h" + +void execute_ps(t_node **stack_a, t_node **stack_b, int pos[2], int mode) +{ + if (mode == 0 || mode == 1) + exec_smt(stack_a, stack_b, pos, mode); + else if (mode == 2) + { + multi_execute(stack_a, stack_b, "ra", pos[0]); + multi_execute(stack_a, stack_b, "rrb", stack_len(*stack_b) - pos[1]); + } + else if (mode == 3) + { + multi_execute(stack_a, stack_b, "rra", stack_len(*stack_a) - pos[0]); + multi_execute(stack_a, stack_b, "rb", pos[1]); + } +} + +void sort_three(t_node *stack_a) +{ + if (stack_a->content > stack_a->next->content) + { + if (stack_a->content < stack_a->next->next->content) + execute(&stack_a, NULL, "sa", false); + else if (stack_a->next->content > stack_a->next->next->content) + { + execute(&stack_a, NULL, "sa", false); + execute(&stack_a, NULL, "rra", false); + } + else + execute(&stack_a, NULL, "ra", false); + } + else if (stack_a->content < stack_a->next->content) + { + if (stack_a->content > stack_a->next->next->content) + execute(&stack_a, NULL, "rra", false); + else if (stack_a->next->content > stack_a->next->next->content) + { + execute(&stack_a, NULL, "sa", false); + execute(&stack_a, NULL, "ra", false); + } + } +} + +// push 2 smallest number to stack b, sort 3 stack a, push back to stack a +void sort_tri_adv(t_node *stack_a, t_node *stack_b, int len) +{ + int tmp; + + tmp = len; + while (tmp-- > 3) + { + min_max_push(stack_a, false); + execute(&stack_a, &stack_b, "pb", false); + } + sort_three(stack_a); + multi_execute(&stack_a, &stack_b, "pa", len - 3); +} + +// attr[3]: attr[0] = min, attr[1] = index, attr[2] = len +void sort(t_node *stack_a, t_node *stack_b) +{ + int i; + int pos[2]; + int mode; + + multi_execute(&stack_a, &stack_b, "pb", 2); + while (stack_len(stack_a) > 3) + { + pos[0] = find_min_index(stack_a, stack_b, stack_len(stack_a)); + i = -1; + while (++i < pos[0]) + stack_a = stack_a->next; + pos[1] = execute_calc(stack_a, stack_b, stack_len(stack_a), true); + mode = lcm(pos, stack_len(stack_a), stack_len(stack_b), true); + if (mode == 1) + reverse_pos(&stack_a, &stack_b, pos); + execute_ps(&stack_a, &stack_b, pos, mode); + execute(&stack_a, &stack_b, "pb", false); + } + sort_three(stack_a); + while (stack_len(stack_b) > 0) + { + target_push(stack_a, find_target(stack_b, stack_a)); + execute(&stack_a, &stack_b, "pa", false); + } + min_max_push(stack_a, false); +} + +void push_swap(t_node *stack_a, t_node *stack_b, int len) +{ + stack_a = last_first_node(stack_a, false); + if (is_sorted(stack_a, false)) + return ; + if (len == 2) + execute(&stack_a, &stack_b, "sa", false); + else if (len == 3) + sort_three(stack_a); + else if (len > 3 && len < 6) + sort_tri_adv(stack_a, stack_b, len); + else + sort(stack_a, stack_b); +} diff --git a/srcs/algorithm/sort.c b/srcs/algorithm/sort.c new file mode 100644 index 0000000..4075d2f --- /dev/null +++ b/srcs/algorithm/sort.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/13 11:59:58 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:09 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +// identify the position of the target in stack_a +int find_target(t_node *stack_from, t_node *stack_to) +{ + t_node *tmp; + int target; + + tmp = last_first_node(stack_to, false); + target = tmp->content; + if (stack_from->content > min_max_pos(stack_to, true, false)) + return (min_max_pos(stack_to, false, true)); + while (tmp) + { + if ((tmp->content < target && tmp->content > stack_from->content) + || (tmp->content > stack_from->content + && target < stack_from->content)) + target = tmp->content; + tmp = tmp->next; + } + return (node_index(stack_to, target)); +} + +void target_push(t_node *stack, int pos) +{ + int len; + + stack = last_first_node(stack, false); + len = stack_len(stack); + if (pos <= len / 2) + { + while (pos--) + execute(&stack, NULL, "ra", false); + } + else + { + pos = len - pos; + while (pos--) + execute(&stack, NULL, "rra", false); + } +} + +void exec_smt(t_node **stack_a, t_node **stack_b, int pos[2], int mode) +{ + if (mode == 0) + { + multi_execute(stack_a, stack_b, "rr", min(pos[0], pos[1])); + if (max(pos[0], pos[1]) == pos[0] && pos[0] != pos[1]) + multi_execute(stack_a, stack_a, "ra", pos[0] - pos[1]); + else if (max(pos[0], pos[1]) == pos[1] && pos[0] != pos[1]) + multi_execute(stack_a, stack_b, "rb", pos[1] - pos[0]); + } + else if (mode == 1) + { + multi_execute(stack_a, stack_b, "rrr", min(pos[0], pos[1])); + if (max(pos[0], pos[1]) == pos[0] && pos[0] != pos[1]) + multi_execute(stack_a, stack_a, "rra", pos[0] - pos[1]); + else if (max(pos[0], pos[1]) == pos[1] && pos[0] != pos[1] + && pos[1] != 0) + multi_execute(stack_a, stack_b, "rrb", pos[1] - pos[0]); + } +} diff --git a/srcs/algorithm/sort_utils.c b/srcs/algorithm/sort_utils.c new file mode 100644 index 0000000..d4f2c4b --- /dev/null +++ b/srcs/algorithm/sort_utils.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/05 08:37:28 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:10 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +bool is_sorted(t_node *stack, bool reverse) +{ + t_node *tmp; + + tmp = last_first_node(stack, false); + if (stack_len(stack) == 0 || !stack) + return (false); + while (tmp->next) + { + if (tmp->content > tmp->next->content && reverse == false) + return (false); + if (tmp->content < tmp->next->content && reverse == true) + return (false); + tmp = tmp->next; + } + return (true); +} + +// retrieve the first index found that matches the target +int node_index(t_node *stack, int target) +{ + t_node *tmp; + int index; + + index = 0; + tmp = last_first_node(stack, false); + if (stack_len(stack) == 0 || !stack) + return (-1); + while (tmp) + { + if (tmp->content == target) + return (index); + tmp = tmp->next; + index++; + } + return (-1); +} + +// attr[0] = traverse_index, attr[1] = target_index +int min_max_pos(t_node *stack, bool max, bool pos) +{ + int target; + int attr[2]; + t_node *tmp; + + tmp = last_first_node(stack, false); + attr[0] = -1; + attr[1] = 0; + target = tmp->content; + while (++attr[0] < stack_len(stack)) + { + if ((tmp->content > target && max == true) || (tmp->content < target + && max == false)) + { + target = tmp->content; + attr[1] = attr[0]; + } + tmp = tmp->next; + } + if (pos == true) + return (attr[1]); + else + return (target); +} + +void min_max_push(t_node *stack, bool max) +{ + int pos; + + pos = min_max_pos(stack, max, true); + target_push(stack, pos); +} + +int find_min_index(t_node *stack_a, t_node *stack_b, int len) +{ + int min; + int index[2]; + t_node *tmp; + + tmp = last_first_node(stack_a, false); + min = execute_calc(stack_a, stack_b, len, false); + index[0] = 0; + index[1] = 0; + while (tmp) + { + if (execute_calc(tmp, stack_b, len, false) < min) + { + min = execute_calc(tmp, stack_b, len, false); + index[1] = index[0]; + } + tmp = tmp->next; + index[0]++; + } + return (index[1]); +} diff --git a/srcs/checker.c b/srcs/checker.c new file mode 100644 index 0000000..9e64ebd --- /dev/null +++ b/srcs/checker.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* checker.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 11:28:28 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:18 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// checker for push_swap program + +#include "checker.h" + +void checker(t_node **stack_a, t_node **stack_b) +{ + char *line; + char **split; + + line = get_next_line(0); + while (line) + { + split = ft_split(line, '\n'); + if (ft_strchr(line, '\n') == 0 || split[0] == 0) + ft_error(*stack_a); + free(line); + if (execute(stack_a, stack_b, split[0], true) == false) + ft_error(*stack_a); + free(split[0]); + free(split); + line = get_next_line(0); + } + if (is_sorted(*stack_a, false) && *stack_b == NULL) + ft_printf("OK\n"); + else + ft_printf("KO\n"); +} + +int main(int argc, char **argv) +{ + int i; + t_node *stack_a; + t_node *stack_b; + + i = 0; + stack_a = NULL; + stack_b = NULL; + if (argc > 1) + { + while (++i < argc) + { + if (is_valid(stack_a, argv[i]) == false) + { + ft_printf("Triggered\n"); + ft_error(stack_a); + } + stack_a = add_node(stack_a, ft_atoi(argv[i])); + } + checker(&stack_a, &stack_b); + clear_lst_node(stack_a); + clear_lst_node(stack_b); + } +} diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..2aa8e15 --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/04 03:07:36 by sinlee #+# #+# */ +/* Updated: 2023/07/16 04:11:19 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +// push_swap program +// pos[0] = pos_a, pos[1] = pos_b +int main(int argc, char **argv) +{ + int i; + t_node *stack_a; + t_node *stack_b; + + i = 0; + stack_a = NULL; + stack_b = NULL; + if (argc > 1) + { + while (++i < argc) + { + if (is_valid(stack_a, argv[i]) == false) + ft_error(stack_a); + stack_a = add_node(stack_a, ft_atoi(argv[i])); + } + push_swap(stack_a, stack_b, argc - 1); + clear_lst_node(stack_a); + clear_lst_node(stack_b); + } + return (0); +} diff --git a/srcs/misc/check.c b/srcs/misc/check.c new file mode 100644 index 0000000..624a6dd --- /dev/null +++ b/srcs/misc/check.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: codespace +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 14:33:27 by codespace #+# #+# */ +/* Updated: 2023/07/16 03:46:26 by codespace ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_error(t_node *stack_a) +{ + clear_lst_node(stack_a); + ft_printf("Error\n"); + exit(EXIT_FAILURE); +} + +int ft_atoi_ps(char *str, t_node *stack_a) +{ + long long int ans; + int result; + + ans = 0; + result = 1; + while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\f' + || *str == '\v' || *str == '\r') + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + result *= -1; + str++; + } + while (*str) + { + if (!ft_isdigit(*str)) + ft_error(stack_a); + ans *= 10; + ans += *str - 48; + str++; + } + if ((result * ans) > 2147483647 || (result * ans) < -2147483648) + ft_error(stack_a); + return (result * ans); +} + +bool is_valid(t_node *stack_a, char *str) +{ + t_node *tmp; + int nbr; + + tmp = last_first_node(stack_a, false); + nbr = ft_atoi_ps(str, stack_a); + while (tmp) + { + if (tmp->content == nbr) + return (false); + tmp = tmp->next; + } + return (true); +} + +void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]) +{ + pos[0] = stack_len(*stack_a) - pos[0]; + if (pos[1] != 0) + pos[1] = stack_len(*stack_b) - pos[1]; +} diff --git a/srcs/stack/misc_utils.c b/srcs/stack/misc_utils.c new file mode 100644 index 0000000..555a45f --- /dev/null +++ b/srcs/stack/misc_utils.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* misc_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/05 08:52:19 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:08 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int max(int a, int b) +{ + if (a > b) + return (a); + return (b); +} + +int min(int a, int b) +{ + if (a < b) + return (a); + return (b); +} + +int stack_len(t_node *stack) +{ + int len; + t_node *tmp; + + len = 0; + tmp = last_first_node(stack, false); + while (tmp) + { + len++; + tmp = tmp->next; + } + return (len); +} + +int ft_strcmp(const char *s1, const char *s2) +{ + while ((*s1 || *s2)) + { + if (*s1++ != *s2++) + return (*(unsigned char *)--s1 - *(unsigned char *)--s2); + } + return (0); +} + +void check_exit_else_exec(t_node **stack_a, t_node **stack_b, char *line) +{ + if (stack_a == NULL || stack_b == NULL) + ft_printf("%s\n", line); + else if (is_sorted(*stack_a, false) && stack_len(*stack_b) == 0) + { + clear_lst_node(*stack_a); + clear_lst_node(*stack_b); + exit(0); + } + else if (line != NULL) + ft_printf("%s\n", line); +} diff --git a/srcs/stack/operations.c b/srcs/stack/operations.c new file mode 100644 index 0000000..6287ab7 --- /dev/null +++ b/srcs/stack/operations.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* operations.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/04 03:27:49 by sinlee #+# #+# */ +/* Updated: 2023/07/16 04:11:04 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +// double pointer array to get mem address of node +bool swap(t_node **stack) +{ + t_node *tmp; + + *stack = last_first_node(*stack, false); + if (stack == NULL || *stack == NULL || (*stack)->next == NULL) + return (false); + tmp = (*stack)->next; + (*stack)->next = tmp->next; + if (tmp->next != NULL) + tmp->next->prev = *stack; + tmp->next = *stack; + tmp->prev = NULL; + (*stack)->prev = tmp; + *stack = tmp; + return (true); +} + +bool push(t_node **stack_from, t_node **stack_to) +{ + t_node *tmp; + + if (stack_from == NULL || *stack_from == NULL) + return (false); + *stack_from = last_first_node(*stack_from, false); + *stack_to = last_first_node(*stack_to, false); + tmp = (*stack_from)->next; + if (tmp != NULL) + tmp->prev = NULL; + (*stack_from)->next = *stack_to; + if (*stack_to != NULL) + (*stack_to)->prev = *stack_from; + *stack_to = *stack_from; + *stack_from = tmp; + return (true); +} + +bool rotate(t_node **stack, bool reverse) +{ + t_node *tmp; + + *stack = last_first_node(*stack, false); + if (stack == NULL || *stack == NULL || (*stack)->next == NULL) + return (false); + tmp = *stack; + if (!reverse) + { + *stack = (*stack)->next; + (*stack)->prev = NULL; + tmp->next = NULL; + tmp->prev = last_first_node(*stack, true); + last_first_node(*stack, true)->next = tmp; + } + else + { + *stack = last_first_node(*stack, true); + (*stack)->prev->next = NULL; + (*stack)->prev = NULL; + (*stack)->next = tmp; + tmp->prev = *stack; + } + return (true); +} + +bool execute(t_node **stack_a, t_node **stack_b, char *line, bool s_print) +{ + if (!s_print) + ft_printf("%s\n", line); + if (ft_strcmp(line, "sa") == 0) + return (swap(stack_a)); + else if (ft_strcmp(line, "sb") == 0) + return (swap(stack_b)); + else if (ft_strcmp(line, "ss") == 0) + return (swap(stack_a) && swap(stack_b)); + else if (ft_strcmp(line, "pa") == 0) + return (push(stack_b, stack_a)); + else if (ft_strcmp(line, "pb") == 0) + return (push(stack_a, stack_b)); + else if (ft_strcmp(line, "ra") == 0) + return (rotate(stack_a, false)); + else if (ft_strcmp(line, "rb") == 0) + return (rotate(stack_b, false)); + else if (ft_strcmp(line, "rr") == 0) + return (rotate(stack_a, false) && rotate(stack_b, false)); + else if (ft_strcmp(line, "rra") == 0) + return (rotate(stack_a, true)); + else if (ft_strcmp(line, "rrb") == 0) + return (rotate(stack_b, true)); + else if (ft_strcmp(line, "rrr") == 0) + return (rotate(stack_a, true) && rotate(stack_b, true)); + return (false); +} + +bool multi_execute(t_node **stack_a, t_node **stack_b, char *line, int n) +{ + while (n--) + { + if (!execute(stack_a, stack_b, line, false)) + return (false); + } + return (true); +} diff --git a/srcs/stack/stack_utils.c b/srcs/stack/stack_utils.c new file mode 100644 index 0000000..1ffc7de --- /dev/null +++ b/srcs/stack/stack_utils.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stack_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sinlee +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/04 05:29:26 by codespace #+# #+# */ +/* Updated: 2023/07/16 04:11:06 by sinlee ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void print_stack(t_node *stack, char *str, bool advanced, bool to_first) +{ + t_node *tmp; + + if (to_first == true) + tmp = last_first_node(stack, false); + else + tmp = stack; + ft_printf("----Printing Stack %s (Top to Down)-------\n", str); + while (tmp) + { + ft_printf("%d ", tmp->content); + if (advanced) + { + if (tmp->prev) + ft_printf("Previous: %d ", tmp->prev->content); + else + ft_printf("Previous: NULL "); + if (tmp->next) + ft_printf("Next: %d\n", tmp->next->content); + else + ft_printf("Next: NULL\n"); + } + tmp = tmp->next; + } + ft_printf("\n"); +} + +t_node *create_node(int content) +{ + t_node *node; + + node = (t_node *)malloc(sizeof(t_node)); + if (node == NULL) + return (NULL); + node->content = content; + node->prev = NULL; + node->next = NULL; + return (node); +} + +void clear_lst_node(t_node *node) +{ + t_node *temp; + + if (!node) + return ; + node = last_first_node(node, false); + while (node) + { + temp = node->next; + free(node); + node = temp; + } +} + +t_node *add_node(t_node *node, int content) +{ + t_node *new; + + new = create_node(content); + if (!new) + return (NULL); + if (!node) + return (new); + node->next = new; + new->prev = node; + return (new); +} + +// function that returns either last or first node, +// depending on the 'is_last' variable. if is_last == true, is last node +t_node *last_first_node(t_node *node, bool is_last) +{ + if (!node) + return (NULL); + if (!is_last) + { + while (node->prev) + node = node->prev; + return (node); + } + else + { + while (node->next) + node = node->next; + return (node); + } +}