├── .gitignore ├── lib ├── libft │ ├── .github │ │ └── workflows │ │ │ └── c-cpp.yml │ ├── .gitignore │ ├── src │ │ ├── ft_isdigit.c │ │ ├── ft_isprint.c │ │ ├── ft_putchar_fd.c │ │ ├── ft_lstadd_front.c │ │ ├── ft_toupper.c │ │ ├── ft_tolower.c │ │ ├── ft_putstr_fd.c │ │ ├── ft_strlen.c │ │ ├── ft_putendl_fd.c │ │ ├── ft_lstdelone.c │ │ ├── ft_striteri.c │ │ ├── ft_lstsize.c │ │ ├── ft_memcpy.c │ │ ├── ft_putnbr_fd.c │ │ ├── ft_lstiter.c │ │ ├── ft_lstclear.c │ │ ├── ft_lstadd_back.c │ │ ├── ft_isalpha.c │ │ ├── ft_isascii.c │ │ ├── ft_isalnum.c │ │ ├── ft_lstlast.c │ │ ├── ft_strncmp.c │ │ ├── ft_lstnew.c │ │ ├── ft_strchr.c │ │ ├── ft_calloc.c │ │ ├── ft_lstmap.c │ │ ├── ft_strdup.c │ │ ├── ft_bzero.c │ │ ├── ft_atoi.c │ │ ├── ft_memcmp.c │ │ ├── ft_strrchr.c │ │ ├── ft_memchr.c │ │ ├── ft_strlcpy.c │ │ ├── ft_strlcat.c │ │ ├── ft_memset.c │ │ ├── ft_strmapi.c │ │ ├── ft_itoa.c │ │ ├── ft_memmove.c │ │ ├── ft_strjoin.c │ │ ├── ft_substr.c │ │ ├── ft_strtrim.c │ │ ├── ft_strnstr.c │ │ ├── get_next_line.c │ │ ├── get_next_line_utils.c │ │ └── ft_split.c │ ├── LICENSE │ ├── includes │ │ ├── get_next_line.h │ │ └── libft.h │ ├── README.md │ └── Makefile └── ft_printf │ ├── .github │ └── workflows │ │ └── c-cpp.yml │ ├── libft │ ├── .github │ │ └── workflows │ │ │ └── c-cpp.yml │ ├── .gitignore │ ├── src │ │ ├── ft_isdigit.c │ │ ├── ft_isprint.c │ │ ├── ft_putchar_fd.c │ │ ├── ft_lstadd_front.c │ │ ├── ft_toupper.c │ │ ├── ft_tolower.c │ │ ├── ft_putstr_fd.c │ │ ├── ft_strlen.c │ │ ├── ft_putendl_fd.c │ │ ├── ft_lstdelone.c │ │ ├── ft_striteri.c │ │ ├── ft_lstsize.c │ │ ├── ft_memcpy.c │ │ ├── ft_putnbr_fd.c │ │ ├── ft_lstiter.c │ │ ├── ft_lstclear.c │ │ ├── ft_lstadd_back.c │ │ ├── ft_isalpha.c │ │ ├── ft_isascii.c │ │ ├── ft_isalnum.c │ │ ├── ft_lstlast.c │ │ ├── ft_strncmp.c │ │ ├── ft_lstnew.c │ │ ├── ft_strchr.c │ │ ├── ft_calloc.c │ │ ├── ft_lstmap.c │ │ ├── ft_strdup.c │ │ ├── ft_bzero.c │ │ ├── ft_atoi.c │ │ ├── ft_memcmp.c │ │ ├── ft_strrchr.c │ │ ├── ft_memchr.c │ │ ├── ft_strlcpy.c │ │ ├── ft_strlcat.c │ │ ├── ft_memset.c │ │ ├── ft_strmapi.c │ │ ├── ft_itoa.c │ │ ├── ft_memmove.c │ │ ├── ft_strjoin.c │ │ ├── ft_substr.c │ │ ├── ft_strtrim.c │ │ ├── ft_strnstr.c │ │ ├── get_next_line.c │ │ ├── get_next_line_utils.c │ │ └── ft_split.c │ ├── LICENSE │ ├── includes │ │ ├── get_next_line.h │ │ └── libft.h │ ├── README.md │ └── Makefile │ ├── .gitignore │ ├── LICENSE │ ├── src │ ├── ft_string.c │ ├── ft_printf.c │ └── ft_nbr.c │ ├── includes │ └── ft_printf.h │ ├── Makefile │ └── README.md ├── .github └── workflows │ └── c-cpp.yml ├── srcs ├── main.c ├── stack │ ├── misc_utils.c │ ├── stack_utils.c │ └── operations.c ├── checker.c ├── misc │ └── check.c └── algorithm │ ├── sort.c │ └── sort_utils.c ├── includes ├── checker.h └── push_swap.h ├── README.md └── Makefile /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | test/ 3 | Push-Swap-Tester/ 4 | checker_linux 5 | checker 6 | .vscode/ -------------------------------------------------------------------------------- /lib/libft/.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: make all 17 | run: make all 18 | -------------------------------------------------------------------------------- /lib/ft_printf/.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: make all 17 | run: make all 18 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: make all 17 | run: make all 18 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: make 17 | run: make 18 | - name: make bonus 19 | run: make bonus 20 | - name: make re 21 | run: make re 22 | -------------------------------------------------------------------------------- /lib/libft/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /lib/ft_printf/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /lib/libft/src/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:54:37 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 10:56:24 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of isdigit function 14 | 15 | #include "libft.h" 16 | 17 | int ft_isdigit(int c) 18 | { 19 | if (c >= '0' && c <= '9') 20 | return (1); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:44:54 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 11:47:36 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of isprint function 14 | 15 | #include "libft.h" 16 | 17 | int ft_isprint(int c) 18 | { 19 | if (c >= 32 && c <= 126) 20 | return (1); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:29:39 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:30:17 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement putchar_fd function: output character to file descriptor 14 | 15 | #include "libft.h" 16 | 17 | void ft_putchar_fd(char c, int fd) 18 | { 19 | write(fd, &c, 1); 20 | } 21 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:54:37 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 10:56:24 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of isdigit function 14 | 15 | #include "libft.h" 16 | 17 | int ft_isdigit(int c) 18 | { 19 | if (c >= '0' && c <= '9') 20 | return (1); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:44:54 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 11:47:36 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of isprint function 14 | 15 | #include "libft.h" 16 | 17 | int ft_isprint(int c) 18 | { 19 | if (c >= 32 && c <= 126) 20 | return (1); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 15:35:43 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:35:52 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | if (lst == NULL || new == NULL) 18 | return ; 19 | new->next = *lst; 20 | *lst = new; 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/src/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 14:57:52 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 14:59:02 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement toupper function 14 | 15 | #include "libft.h" 16 | 17 | int ft_toupper(int c) 18 | { 19 | if (c >= 'a' && c <= 'z') 20 | return (c + ('A' - 'a')); 21 | return (c); 22 | } 23 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:29:39 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:30:17 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement putchar_fd function: output character to file descriptor 14 | 15 | #include "libft.h" 16 | 17 | void ft_putchar_fd(char c, int fd) 18 | { 19 | write(fd, &c, 1); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/src/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 14:59:20 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 15:00:49 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement tolower function 14 | 15 | #include "libft.h" 16 | 17 | int ft_tolower(int c) 18 | { 19 | if (c >= 'A' && c <= 'Z') 20 | { 21 | c -= ('A' - 'a'); 22 | } 23 | return (c); 24 | } 25 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 15:35:43 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:35:52 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | if (lst == NULL || new == NULL) 18 | return ; 19 | new->next = *lst; 20 | *lst = new; 21 | } 22 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 14:57:52 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 14:59:02 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement toupper function 14 | 15 | #include "libft.h" 16 | 17 | int ft_toupper(int c) 18 | { 19 | if (c >= 'a' && c <= 'z') 20 | return (c + ('A' - 'a')); 21 | return (c); 22 | } 23 | -------------------------------------------------------------------------------- /lib/ft_printf/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Architect 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 14:59:20 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 15:00:49 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement tolower function 14 | 15 | #include "libft.h" 16 | 17 | int ft_tolower(int c) 18 | { 19 | if (c >= 'A' && c <= 'Z') 20 | { 21 | c -= ('A' - 'a'); 22 | } 23 | return (c); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Architect 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Architect 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/libft/src/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:31:00 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:31:29 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements putstr_fd function: output string to file descriptor 14 | 15 | #include "libft.h" 16 | 17 | void ft_putstr_fd(char *s, int fd) 18 | { 19 | if (!s) 20 | return ; 21 | write(fd, s, ft_strlen(s)); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 09:58:28 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 10:00:09 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of ft_strlen function 14 | 15 | #include "libft.h" 16 | 17 | size_t ft_strlen(const char *str) 18 | { 19 | size_t len; 20 | 21 | len = 0; 22 | while (*str++) 23 | len++; 24 | return (len); 25 | } 26 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:31:00 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:31:29 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements putstr_fd function: output string to file descriptor 14 | 15 | #include "libft.h" 16 | 17 | void ft_putstr_fd(char *s, int fd) 18 | { 19 | if (!s) 20 | return ; 21 | write(fd, s, ft_strlen(s)); 22 | } 23 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 09:58:28 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 10:00:09 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of ft_strlen function 14 | 15 | #include "libft.h" 16 | 17 | size_t ft_strlen(const char *str) 18 | { 19 | size_t len; 20 | 21 | len = 0; 22 | while (*str++) 23 | len++; 24 | return (len); 25 | } 26 | -------------------------------------------------------------------------------- /lib/libft/src/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:31:51 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:32:21 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_putendl_fd function: 14 | // output string to file descriptor with newline 15 | 16 | #include "libft.h" 17 | 18 | void ft_putendl_fd(char *s, int fd) 19 | { 20 | if (!s) 21 | return ; 22 | ft_putstr_fd(s, fd); 23 | ft_putchar_fd(10, fd); 24 | } 25 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:31:51 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:32:21 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_putendl_fd function: 14 | // output string to file descriptor with newline 15 | 16 | #include "libft.h" 17 | 18 | void ft_putendl_fd(char *s, int fd) 19 | { 20 | if (!s) 21 | return ; 22 | ft_putstr_fd(s, fd); 23 | ft_putchar_fd(10, fd); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:17:03 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:21:56 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstdelone function: 14 | // delete the node 'lst' with the function 'del' 15 | 16 | #include "libft.h" 17 | 18 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 19 | { 20 | if (!del) 21 | return ; 22 | if (lst) 23 | { 24 | del(lst->content); 25 | free(lst); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:25:36 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 16:30:18 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement striteri function: apply function to each character of string 14 | 15 | #include "libft.h" 16 | 17 | void ft_striteri(char *s, void (*f)(unsigned int, char*)) 18 | { 19 | unsigned int i; 20 | 21 | if (!s || !f) 22 | return ; 23 | i = -1; 24 | while (s[++i]) 25 | f(i, &s[i]); 26 | } 27 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:17:03 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:21:56 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstdelone function: 14 | // delete the node 'lst' with the function 'del' 15 | 16 | #include "libft.h" 17 | 18 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 19 | { 20 | if (!del) 21 | return ; 22 | if (lst) 23 | { 24 | del(lst->content); 25 | free(lst); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:25:36 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 16:30:18 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement striteri function: apply function to each character of string 14 | 15 | #include "libft.h" 16 | 17 | void ft_striteri(char *s, void (*f)(unsigned int, char*)) 18 | { 19 | unsigned int i; 20 | 21 | if (!s || !f) 22 | return ; 23 | i = -1; 24 | while (s[++i]) 25 | f(i, &s[i]); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:09:33 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 16:12:41 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement ft_lstsize function: count number of nodes in a linked list 14 | 15 | #include "libft.h" 16 | 17 | int ft_lstsize(t_list *lst) 18 | { 19 | int count; 20 | t_list *current; 21 | 22 | count = 0; 23 | current = lst; 24 | while (current != NULL) 25 | { 26 | count++; 27 | current = current->next; 28 | } 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/src/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 12:04:51 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 12:54:27 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memcpy function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_memcpy(void *dst, const void *src, size_t n) 18 | { 19 | size_t i; 20 | 21 | i = 0; 22 | if (dst == NULL && src == NULL) 23 | return (NULL); 24 | while (i < n) 25 | { 26 | ((char *)dst)[i] = ((char *)src)[i]; 27 | i++; 28 | } 29 | return ((void *)dst); 30 | } 31 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:09:33 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 16:12:41 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement ft_lstsize function: count number of nodes in a linked list 14 | 15 | #include "libft.h" 16 | 17 | int ft_lstsize(t_list *lst) 18 | { 19 | int count; 20 | t_list *current; 21 | 22 | count = 0; 23 | current = lst; 24 | while (current != NULL) 25 | { 26 | count++; 27 | current = current->next; 28 | } 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 12:04:51 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 12:54:27 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memcpy function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_memcpy(void *dst, const void *src, size_t n) 18 | { 19 | size_t i; 20 | 21 | i = 0; 22 | if (dst == NULL && src == NULL) 23 | return (NULL); 24 | while (i < n) 25 | { 26 | ((char *)dst)[i] = ((char *)src)[i]; 27 | i++; 28 | } 29 | return ((void *)dst); 30 | } 31 | -------------------------------------------------------------------------------- /lib/ft_printf/src/ft_string.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_string.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/19 10:44:19 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/19 13:49:14 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_len(char c, int *len) 16 | { 17 | ft_putchar_fd(c, 1); 18 | (*len)++; 19 | } 20 | 21 | void ft_putstr_len(char *str, int *len) 22 | { 23 | if (!str) 24 | { 25 | ft_putstr_fd("(null)", 1); 26 | *len += 6; 27 | return ; 28 | } 29 | while (*str) 30 | { 31 | ft_putchar_len(*str, len); 32 | str++; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /lib/libft/src/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:33:21 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:29:22 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_putnbr_fd function: output integer to file descriptor 14 | 15 | #include "libft.h" 16 | 17 | void ft_putnbr_fd(int n, int fd) 18 | { 19 | char *str; 20 | 21 | str = ft_itoa(n); 22 | write(fd, str, ft_strlen(str)); 23 | free(str); 24 | } 25 | // void ft_putnbr_fd(int n, int fd) 26 | // { 27 | // write(fd, ft_itoa(n), ft_strlen(ft_itoa(n))); 28 | // } 29 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 15:33:21 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:29:22 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_putnbr_fd function: output integer to file descriptor 14 | 15 | #include "libft.h" 16 | 17 | void ft_putnbr_fd(int n, int fd) 18 | { 19 | char *str; 20 | 21 | str = ft_itoa(n); 22 | write(fd, str, ft_strlen(str)); 23 | free(str); 24 | } 25 | // void ft_putnbr_fd(int n, int fd) 26 | // { 27 | // write(fd, ft_itoa(n), ft_strlen(ft_itoa(n))); 28 | // } 29 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:25:01 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 16:30:09 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstiter function: 14 | // iterates the list 'lst' 15 | // and applies the function 'f' to the content of each node 16 | 17 | #include "libft.h" 18 | 19 | void ft_lstiter(t_list *lst, void (*f)(void *)) 20 | { 21 | t_list *current; 22 | 23 | current = lst; 24 | while (current != NULL) 25 | { 26 | f(current->content); 27 | current = current->next; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:18:59 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:26:03 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstclear function: 14 | // delete all nodes of list with the function 'del' 15 | 16 | #include "libft.h" 17 | 18 | void ft_lstclear(t_list **lst, void (*del)(void *)) 19 | { 20 | t_list *temp; 21 | 22 | if (!del || !lst || !*lst) 23 | return ; 24 | while (lst && *lst) 25 | { 26 | temp = (*lst)->next; 27 | ft_lstdelone(*lst, del); 28 | *lst = temp; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:25:01 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 16:30:09 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstiter function: 14 | // iterates the list 'lst' 15 | // and applies the function 'f' to the content of each node 16 | 17 | #include "libft.h" 18 | 19 | void ft_lstiter(t_list *lst, void (*f)(void *)) 20 | { 21 | t_list *current; 22 | 23 | current = lst; 24 | while (current != NULL) 25 | { 26 | f(current->content); 27 | current = current->next; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:18:59 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:26:03 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstclear function: 14 | // delete all nodes of list with the function 'del' 15 | 16 | #include "libft.h" 17 | 18 | void ft_lstclear(t_list **lst, void (*del)(void *)) 19 | { 20 | t_list *temp; 21 | 22 | if (!del || !lst || !*lst) 23 | return ; 24 | while (lst && *lst) 25 | { 26 | temp = (*lst)->next; 27 | ft_lstdelone(*lst, del); 28 | *lst = temp; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:14:44 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:11:45 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstadd_back function: 14 | // add the node 'new' at the end of the linked list 15 | 16 | #include "libft.h" 17 | 18 | void ft_lstadd_back(t_list **lst, t_list *new) 19 | { 20 | t_list *current; 21 | 22 | current = *lst; 23 | if (lst) 24 | { 25 | if (current) 26 | { 27 | current = ft_lstlast(current); 28 | current->next = new; 29 | } 30 | else 31 | *lst = new; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lib/libft/src/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:45:29 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 10:51:45 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 18 | return (1); 19 | return (0); 20 | } 21 | 22 | // test ft_isalpha function 23 | // #include 24 | // #include 25 | 26 | // int main(int argc, char **argv) 27 | // { 28 | // printf("%d\n", ft_isalpha((int)*(argv[1]))); 29 | // printf("%d\n", isalpha((int)*(argv[1]))); 30 | // } 31 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:14:44 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:11:45 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstadd_back function: 14 | // add the node 'new' at the end of the linked list 15 | 16 | #include "libft.h" 17 | 18 | void ft_lstadd_back(t_list **lst, t_list *new) 19 | { 20 | t_list *current; 21 | 22 | current = *lst; 23 | if (lst) 24 | { 25 | if (current) 26 | { 27 | current = ft_lstlast(current); 28 | current->next = new; 29 | } 30 | else 31 | *lst = new; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:45:29 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 10:51:45 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 18 | return (1); 19 | return (0); 20 | } 21 | 22 | // test ft_isalpha function 23 | // #include 24 | // #include 25 | 26 | // int main(int argc, char **argv) 27 | // { 28 | // printf("%d\n", ft_isalpha((int)*(argv[1]))); 29 | // printf("%d\n", isalpha((int)*(argv[1]))); 30 | // } 31 | -------------------------------------------------------------------------------- /lib/libft/src/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:41:52 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 11:47:03 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | // implementation of isascii function 16 | 17 | int ft_isascii(int c) 18 | { 19 | if (c >= 0 && c <= 127) 20 | return (1); 21 | return (0); 22 | } 23 | 24 | // test ft_isascii function 25 | // #include 26 | // #include 27 | 28 | // int main(int argc, char **argv) 29 | // { 30 | // printf("%d\n", ft_isascii((int)*(argv[1]))); 31 | // printf("%d\n", isascii((int)*(argv[1]))); 32 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:41:52 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 11:47:03 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | // implementation of isascii function 16 | 17 | int ft_isascii(int c) 18 | { 19 | if (c >= 0 && c <= 127) 20 | return (1); 21 | return (0); 22 | } 23 | 24 | // test ft_isascii function 25 | // #include 26 | // #include 27 | 28 | // int main(int argc, char **argv) 29 | // { 30 | // printf("%d\n", ft_isascii((int)*(argv[1]))); 31 | // printf("%d\n", isascii((int)*(argv[1]))); 32 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:56:49 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 11:41:46 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | // implementation of isalnum function 16 | 17 | int ft_isalnum(int c) 18 | { 19 | if (ft_isalpha(c) || ft_isdigit(c)) 20 | return (1); 21 | return (0); 22 | } 23 | 24 | // test ft_isalnum function 25 | // #include 26 | // #include 27 | 28 | // int main(int argc, char **argv) 29 | // { 30 | // printf("%d\n", ft_isalnum((int)*(argv[1]))); 31 | // printf("%d\n", isalnum((int)*(argv[1]))); 32 | // } 33 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:56:49 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 11:41:46 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | // implementation of isalnum function 16 | 17 | int ft_isalnum(int c) 18 | { 19 | if (ft_isalpha(c) || ft_isdigit(c)) 20 | return (1); 21 | return (0); 22 | } 23 | 24 | // test ft_isalnum function 25 | // #include 26 | // #include 27 | 28 | // int main(int argc, char **argv) 29 | // { 30 | // printf("%d\n", ft_isalnum((int)*(argv[1]))); 31 | // printf("%d\n", isalnum((int)*(argv[1]))); 32 | // } 33 | -------------------------------------------------------------------------------- /lib/libft/src/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:13:02 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:09:24 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements lstlast function: return last node of list 14 | 15 | #include "libft.h" 16 | 17 | t_list *ft_lstlast(t_list *lst) 18 | { 19 | t_list *current; 20 | 21 | current = lst; 22 | while (current != NULL && current->next != NULL) 23 | current = current->next; 24 | return (current); 25 | } 26 | // #include 27 | // int main() 28 | // { 29 | // t_list * l = NULL; 30 | // ft_lstadd_back(&l, ft_lstnew((void*)1)); 31 | // printf("%d", *(int *)(ft_lstlast(l)->content)); 32 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:13:02 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:09:24 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements lstlast function: return last node of list 14 | 15 | #include "libft.h" 16 | 17 | t_list *ft_lstlast(t_list *lst) 18 | { 19 | t_list *current; 20 | 21 | current = lst; 22 | while (current != NULL && current->next != NULL) 23 | current = current->next; 24 | return (current); 25 | } 26 | // #include 27 | // int main() 28 | // { 29 | // t_list * l = NULL; 30 | // ft_lstadd_back(&l, ft_lstnew((void*)1)); 31 | // printf("%d", *(int *)(ft_lstlast(l)->content)); 32 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:25:52 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 15:32:15 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement of strncmp function 14 | 15 | #include "libft.h" 16 | 17 | int ft_strncmp(const char *s1, const char *s2, size_t n) 18 | { 19 | while (n-- && (*s1 || *s2)) 20 | { 21 | if (*s1++ != *s2++) 22 | return (*(unsigned char *)--s1 - *(unsigned char *)--s2); 23 | } 24 | return (0); 25 | } 26 | 27 | // #include 28 | 29 | // int main(int argc, char **argv) 30 | // { 31 | // printf("%d\n", ft_strncmp(argv[1], argv[2], 5)); 32 | // printf("%d\n", strncmp(argv[1], argv[2], 5)); 33 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:25:52 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 15:32:15 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement of strncmp function 14 | 15 | #include "libft.h" 16 | 17 | int ft_strncmp(const char *s1, const char *s2, size_t n) 18 | { 19 | while (n-- && (*s1 || *s2)) 20 | { 21 | if (*s1++ != *s2++) 22 | return (*(unsigned char *)--s1 - *(unsigned char *)--s2); 23 | } 24 | return (0); 25 | } 26 | 27 | // #include 28 | 29 | // int main(int argc, char **argv) 30 | // { 31 | // printf("%d\n", ft_strncmp(argv[1], argv[2], 5)); 32 | // printf("%d\n", strncmp(argv[1], argv[2], 5)); 33 | // } -------------------------------------------------------------------------------- /lib/ft_printf/includes/ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: codespace +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/19 10:08:49 by sinlee #+# #+# */ 9 | /* Updated: 2023/07/16 03:56:49 by codespace ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include "libft.h" 17 | # include 18 | # include 19 | # include 20 | 21 | void ft_putchar_len(char c, int *len); 22 | void ft_putstr_len(char *str, int *len); 23 | void ft_putnbr_len(int n, int *len); 24 | void ft_putnbr_u_len(unsigned int n, int *len); 25 | void ft_putnbr_hexa_len(unsigned int nbr, int *len, bool up); 26 | void ft_putpointer_len(size_t pointer, int *len); 27 | int ft_printf(const char *str, ...); 28 | 29 | #endif -------------------------------------------------------------------------------- /lib/libft/src/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 15:21:59 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:34:40 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of lstnew function: add new node to linked list 14 | 15 | #include "libft.h" 16 | 17 | t_list *ft_lstnew(void *content) 18 | { 19 | t_list *node; 20 | 21 | node = malloc(sizeof(t_list)); 22 | if (!node) 23 | return (NULL); 24 | node->content = content; 25 | node->next = NULL; 26 | return (node); 27 | } 28 | 29 | // test ft_lstnew function 30 | 31 | // #include 32 | // int main() 33 | // { 34 | // t_list *node; 35 | 36 | // node = ft_lstnew("hello"); 37 | // printf("%s", node->content); 38 | // return (0); 39 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 15:21:59 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:34:40 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of lstnew function: add new node to linked list 14 | 15 | #include "libft.h" 16 | 17 | t_list *ft_lstnew(void *content) 18 | { 19 | t_list *node; 20 | 21 | node = malloc(sizeof(t_list)); 22 | if (!node) 23 | return (NULL); 24 | node->content = content; 25 | node->next = NULL; 26 | return (node); 27 | } 28 | 29 | // test ft_lstnew function 30 | 31 | // #include 32 | // int main() 33 | // { 34 | // t_list *node; 35 | 36 | // node = ft_lstnew("hello"); 37 | // printf("%s", node->content); 38 | // return (0); 39 | // } -------------------------------------------------------------------------------- /lib/ft_printf/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sinlee +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/05/19 10:16:35 by sinlee #+# #+# # 9 | # Updated: 2023/07/04 02:43:31 by sinlee ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libftprintf.a 14 | CC = gcc 15 | CFLAGS = -Wall -Wextra -Werror 16 | CPPFLAGS = -I includes -I libft/includes 17 | SOURCES = ft_printf.c ft_string.c ft_nbr.c 18 | SRC = $(addprefix src/, $(SOURCES)) 19 | OBJS = ${SRC:.c=.o} 20 | RM = rm -rf 21 | 22 | ${NAME} : ${OBJS} 23 | cd libft && $(MAKE) 24 | cp libft/libft.a $(NAME) 25 | ar -rcs $(NAME) $(OBJS) 26 | 27 | all: $(NAME) 28 | 29 | clean: 30 | cd libft && $(MAKE) clean 31 | $(RM) $(OBJS) 32 | 33 | fclean: clean 34 | cd libft && $(MAKE) fclean 35 | $(RM) $(NAME) 36 | 37 | re: fclean all 38 | 39 | .PHONY: all clean fclean re -------------------------------------------------------------------------------- /lib/libft/src/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:02:37 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 15:29:27 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement strchr function 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strchr(const char *s, int c) 18 | { 19 | while (*s != (char)c) 20 | { 21 | if (!*s) 22 | return (0); 23 | s++; 24 | } 25 | return ((char *)s); 26 | } 27 | 28 | // test ft_strchr function 29 | 30 | // #include 31 | 32 | // int main(void) 33 | // { 34 | // char *str; 35 | // char *ptr; 36 | 37 | // str = "Hello World"; 38 | // ptr = ft_strchr(str, 0); 39 | // printf("%s\n", ptr); 40 | // str = "Hello World"; 41 | // ptr = strchr(str, 0); 42 | // printf("%s\n", ptr); 43 | // return (0); 44 | // } 45 | -------------------------------------------------------------------------------- /lib/libft/src/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 09:54:31 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 13:57:44 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of calloc function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_calloc(size_t count, size_t size) 18 | { 19 | void *ptr; 20 | 21 | if (count == SIZE_MAX || size == SIZE_MAX) 22 | return (NULL); 23 | ptr = malloc(count * size); 24 | if (ptr == NULL) 25 | return (NULL); 26 | ft_bzero(ptr, count * size); 27 | return ((void *)ptr); 28 | } 29 | 30 | // test ft_calloc function 31 | 32 | // #include 33 | 34 | // int main() 35 | // { 36 | // char *str; 37 | // int i; 38 | 39 | // printf("ft_calloc: %p \n", ft_calloc(SIZE_MAX, SIZE_MAX)); 40 | // return (0); 41 | // } 42 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:02:37 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 15:29:27 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement strchr function 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strchr(const char *s, int c) 18 | { 19 | while (*s != (char)c) 20 | { 21 | if (!*s) 22 | return (0); 23 | s++; 24 | } 25 | return ((char *)s); 26 | } 27 | 28 | // test ft_strchr function 29 | 30 | // #include 31 | 32 | // int main(void) 33 | // { 34 | // char *str; 35 | // char *ptr; 36 | 37 | // str = "Hello World"; 38 | // ptr = ft_strchr(str, 0); 39 | // printf("%s\n", ptr); 40 | // str = "Hello World"; 41 | // ptr = strchr(str, 0); 42 | // printf("%s\n", ptr); 43 | // return (0); 44 | // } 45 | -------------------------------------------------------------------------------- /lib/libft/includes/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/25 15:53:31 by sinlee #+# #+# */ 9 | /* Updated: 2023/07/17 10:25:19 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | # define GET_NEXT_LINE_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | # ifndef BUFFER_SIZE 24 | # define BUFFER_SIZE 5 25 | # endif 26 | 27 | size_t gnl_strlen(const char *str); 28 | char *gnl_substr(char *s, unsigned int start, size_t len); 29 | size_t gnl_strlcpy(char *dest, const char *src, size_t size); 30 | int gnl_strchr(char *s, char c); 31 | char *get_next_line(int fd); 32 | char *gnl_strjoin(char *s1, char *s2); 33 | 34 | #endif -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 09:54:31 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 13:57:44 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of calloc function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_calloc(size_t count, size_t size) 18 | { 19 | void *ptr; 20 | 21 | if (count == SIZE_MAX || size == SIZE_MAX) 22 | return (NULL); 23 | ptr = malloc(count * size); 24 | if (ptr == NULL) 25 | return (NULL); 26 | ft_bzero(ptr, count * size); 27 | return ((void *)ptr); 28 | } 29 | 30 | // test ft_calloc function 31 | 32 | // #include 33 | 34 | // int main() 35 | // { 36 | // char *str; 37 | // int i; 38 | 39 | // printf("ft_calloc: %p \n", ft_calloc(SIZE_MAX, SIZE_MAX)); 40 | // return (0); 41 | // } 42 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/includes/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/25 15:53:31 by sinlee #+# #+# */ 9 | /* Updated: 2023/07/17 10:25:19 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | # define GET_NEXT_LINE_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | # ifndef BUFFER_SIZE 24 | # define BUFFER_SIZE 5 25 | # endif 26 | 27 | size_t gnl_strlen(const char *str); 28 | char *gnl_substr(char *s, unsigned int start, size_t len); 29 | size_t gnl_strlcpy(char *dest, const char *src, size_t size); 30 | int gnl_strchr(char *s, char c); 31 | char *get_next_line(int fd); 32 | char *gnl_strjoin(char *s1, char *s2); 33 | 34 | #endif -------------------------------------------------------------------------------- /lib/libft/src/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:27:55 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 16:31:32 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstmap function: 14 | // iterates the list 'lst' and applies the function 'f' to the 15 | // content of each node 16 | 17 | #include "libft.h" 18 | 19 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 20 | { 21 | t_list *current; 22 | t_list *new; 23 | t_list *temp; 24 | 25 | current = lst; 26 | new = NULL; 27 | while (current != NULL) 28 | { 29 | temp = ft_lstnew(f(current->content)); 30 | if (temp == NULL) 31 | { 32 | ft_lstclear(&new, del); 33 | return (NULL); 34 | } 35 | ft_lstadd_back(&new, temp); 36 | current = current->next; 37 | } 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/05 16:27:55 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 16:31:32 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implements ft_lstmap function: 14 | // iterates the list 'lst' and applies the function 'f' to the 15 | // content of each node 16 | 17 | #include "libft.h" 18 | 19 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 20 | { 21 | t_list *current; 22 | t_list *new; 23 | t_list *temp; 24 | 25 | current = lst; 26 | new = NULL; 27 | while (current != NULL) 28 | { 29 | temp = ft_lstnew(f(current->content)); 30 | if (temp == NULL) 31 | { 32 | ft_lstclear(&new, del); 33 | return (NULL); 34 | } 35 | ft_lstadd_back(&new, temp); 36 | current = current->next; 37 | } 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /lib/libft/src/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:06:17 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 13:07:54 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of ft_strdup function 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strdup(const char *s1) 18 | { 19 | char *ptr; 20 | size_t len; 21 | 22 | len = ft_strlen(s1); 23 | ptr = (char *)malloc((len + 1) * sizeof(char)); 24 | if (ptr == NULL) 25 | return (NULL); 26 | ft_strlcpy(ptr, s1, len + 1); 27 | return (ptr); 28 | } 29 | 30 | // test ft_strdup function 31 | // #include 32 | // int main() 33 | // { 34 | // char *str; 35 | // char *str2; 36 | 37 | // str = strdup("Hello World!|"); 38 | // str2 = ft_strdup("Hello World!|"); 39 | // printf("%s\n", str); 40 | // printf("%s\n", str2); 41 | // return (0); 42 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 10:06:17 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 13:07:54 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of ft_strdup function 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strdup(const char *s1) 18 | { 19 | char *ptr; 20 | size_t len; 21 | 22 | len = ft_strlen(s1); 23 | ptr = (char *)malloc((len + 1) * sizeof(char)); 24 | if (ptr == NULL) 25 | return (NULL); 26 | ft_strlcpy(ptr, s1, len + 1); 27 | return (ptr); 28 | } 29 | 30 | // test ft_strdup function 31 | // #include 32 | // int main() 33 | // { 34 | // char *str; 35 | // char *str2; 36 | 37 | // str = strdup("Hello World!|"); 38 | // str2 = ft_strdup("Hello World!|"); 39 | // printf("%s\n", str); 40 | // printf("%s\n", str2); 41 | // return (0); 42 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:56:50 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 12:02:54 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of bzero function 14 | 15 | #include "libft.h" 16 | 17 | void ft_bzero(void *s, size_t n) 18 | { 19 | ft_memset(s, 0, n); 20 | } 21 | 22 | // test ft_bzero function 23 | // #include 24 | 25 | // int main() 26 | // { 27 | // char str[50] = "GeeksForGeeks is for programming geeks."; 28 | // printf("\nBefore bzero(): %s\n", str); 29 | // ft_bzero(str + 13, 8*sizeof(char)); 30 | // printf("After bzero(): %s|", str); 31 | // char str1[50] = "GeeksForGeeks is for programming geeks."; 32 | // printf("\nBefore bzero(): %s\n", str1); 33 | // bzero(str1 + 13, 8*sizeof(char)); 34 | // printf("After bzero(): %s|", str1); 35 | // return 0; 36 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:56:50 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 12:02:54 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of bzero function 14 | 15 | #include "libft.h" 16 | 17 | void ft_bzero(void *s, size_t n) 18 | { 19 | ft_memset(s, 0, n); 20 | } 21 | 22 | // test ft_bzero function 23 | // #include 24 | 25 | // int main() 26 | // { 27 | // char str[50] = "GeeksForGeeks is for programming geeks."; 28 | // printf("\nBefore bzero(): %s\n", str); 29 | // ft_bzero(str + 13, 8*sizeof(char)); 30 | // printf("After bzero(): %s|", str); 31 | // char str1[50] = "GeeksForGeeks is for programming geeks."; 32 | // printf("\nBefore bzero(): %s\n", str1); 33 | // bzero(str1 + 13, 8*sizeof(char)); 34 | // printf("After bzero(): %s|", str1); 35 | // return 0; 36 | // } -------------------------------------------------------------------------------- /srcs/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/04 03:07:36 by sinlee #+# #+# */ 9 | /* Updated: 2023/07/17 14:36:58 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | // push_swap program 16 | // pos[0] = pos_a, pos[1] = pos_b 17 | int main(int argc, char **argv) 18 | { 19 | int i; 20 | t_node *stack_a; 21 | t_node *stack_b; 22 | 23 | i = 0; 24 | stack_a = NULL; 25 | stack_b = NULL; 26 | if (argc == 2) 27 | argv = ft_split(argv[1], ' '); 28 | if (argc == 2) 29 | i = -1; 30 | if (argc > 1) 31 | { 32 | while (argv[++i]) 33 | { 34 | if (is_valid(stack_a, argv[i]) == false) 35 | ft_error(stack_a); 36 | stack_a = add_node(stack_a, ft_atoi(argv[i])); 37 | } 38 | push_swap(stack_a, stack_b, stack_len(stack_a)); 39 | clear_lst_node(stack_a); 40 | clear_lst_node(stack_b); 41 | } 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /lib/libft/src/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/05 10:33:07 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 09:38:36 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | static bool is_in_string(char c, char *str) 16 | { 17 | while (*str) 18 | { 19 | if (*str == c) 20 | return (true); 21 | str++; 22 | } 23 | return (false); 24 | } 25 | 26 | static bool is_space(char c) 27 | { 28 | return (is_in_string(c, "\t\n\v\f\r ")); 29 | } 30 | 31 | int ft_atoi(const char *str) 32 | { 33 | int ans; 34 | int result; 35 | 36 | ans = 0; 37 | result = 1; 38 | while (is_space(*str)) 39 | str++; 40 | if (*str == '+' || *str == '-') 41 | { 42 | if (*str == '-') 43 | result *= -1; 44 | str++; 45 | } 46 | while (*str >= '0' && *str <= '9') 47 | { 48 | ans *= 10; 49 | ans += *str - 48; 50 | str++; 51 | } 52 | return (result * ans); 53 | } 54 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/05 10:33:07 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 09:38:36 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | static bool is_in_string(char c, char *str) 16 | { 17 | while (*str) 18 | { 19 | if (*str == c) 20 | return (true); 21 | str++; 22 | } 23 | return (false); 24 | } 25 | 26 | static bool is_space(char c) 27 | { 28 | return (is_in_string(c, "\t\n\v\f\r ")); 29 | } 30 | 31 | int ft_atoi(const char *str) 32 | { 33 | int ans; 34 | int result; 35 | 36 | ans = 0; 37 | result = 1; 38 | while (is_space(*str)) 39 | str++; 40 | if (*str == '+' || *str == '-') 41 | { 42 | if (*str == '-') 43 | result *= -1; 44 | str++; 45 | } 46 | while (*str >= '0' && *str <= '9') 47 | { 48 | ans *= 10; 49 | ans += *str - 48; 50 | str++; 51 | } 52 | return (result * ans); 53 | } 54 | -------------------------------------------------------------------------------- /lib/libft/src/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:36:51 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 12:57:41 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memcmp function 14 | 15 | #include "libft.h" 16 | #include 17 | 18 | int ft_memcmp(const void *s1, const void *s2, size_t n) 19 | { 20 | while (n--) 21 | { 22 | if (*(unsigned char *)s1 != *(unsigned char *)s2) 23 | return (*(unsigned char *)s1 - *(unsigned char *)s2); 24 | s1++; 25 | s2++; 26 | } 27 | return (0); 28 | } 29 | 30 | // test ft_memcmp function by comparing with memcmp function 31 | 32 | // #include 33 | // #include 34 | 35 | // int main(int argc, char **argv) 36 | // { 37 | // char s2[] = {0, 0, 127, 0}; 38 | // char s3[] = {0, 0, 42, 0}; 39 | // printf("ft_memcmp: %d\n", ft_memcmp(s2, s3, 4)); 40 | // printf("memcmp: %d\n", memcmp(s2, s3, 4)); 41 | // return (0); 42 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:15:38 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 18:26:59 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of strrchr function 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strrchr(const char *s, int c) 18 | { 19 | char *ptr; 20 | 21 | ptr = 0; 22 | if (c == 0) 23 | return ((char *)s + ft_strlen(s)); 24 | while (*s) 25 | { 26 | if (*s == (char)c) 27 | ptr = (char *)s; 28 | s++; 29 | } 30 | return (ptr); 31 | } 32 | 33 | // test ft_strrchr function 34 | 35 | // #include 36 | 37 | // int main(void) 38 | // { 39 | // char *str; 40 | // char *ptr; 41 | // char * empty = (char*)calloc(1, 1); 42 | 43 | // str = "tripouille"; 44 | // ptr = ft_strrchr(empty, 'V'); 45 | // printf("%s\n", ptr); 46 | // str = "tripouille"; 47 | // ptr = strrchr(empty, 'V'); 48 | // printf("%s\n", ptr); 49 | // return (0); 50 | // } 51 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:36:51 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 12:57:41 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memcmp function 14 | 15 | #include "libft.h" 16 | #include 17 | 18 | int ft_memcmp(const void *s1, const void *s2, size_t n) 19 | { 20 | while (n--) 21 | { 22 | if (*(unsigned char *)s1 != *(unsigned char *)s2) 23 | return (*(unsigned char *)s1 - *(unsigned char *)s2); 24 | s1++; 25 | s2++; 26 | } 27 | return (0); 28 | } 29 | 30 | // test ft_memcmp function by comparing with memcmp function 31 | 32 | // #include 33 | // #include 34 | 35 | // int main(int argc, char **argv) 36 | // { 37 | // char s2[] = {0, 0, 127, 0}; 38 | // char s3[] = {0, 0, 42, 0}; 39 | // printf("ft_memcmp: %d\n", ft_memcmp(s2, s3, 4)); 40 | // printf("memcmp: %d\n", memcmp(s2, s3, 4)); 41 | // return (0); 42 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:15:38 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 18:26:59 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of strrchr function 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strrchr(const char *s, int c) 18 | { 19 | char *ptr; 20 | 21 | ptr = 0; 22 | if (c == 0) 23 | return ((char *)s + ft_strlen(s)); 24 | while (*s) 25 | { 26 | if (*s == (char)c) 27 | ptr = (char *)s; 28 | s++; 29 | } 30 | return (ptr); 31 | } 32 | 33 | // test ft_strrchr function 34 | 35 | // #include 36 | 37 | // int main(void) 38 | // { 39 | // char *str; 40 | // char *ptr; 41 | // char * empty = (char*)calloc(1, 1); 42 | 43 | // str = "tripouille"; 44 | // ptr = ft_strrchr(empty, 'V'); 45 | // printf("%s\n", ptr); 46 | // str = "tripouille"; 47 | // ptr = strrchr(empty, 'V'); 48 | // printf("%s\n", ptr); 49 | // return (0); 50 | // } 51 | -------------------------------------------------------------------------------- /lib/libft/src/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:32:44 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 18:34:32 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t index; 18 | 19 | index = 0; 20 | while (index < n) 21 | { 22 | if (((unsigned char *)s)[index] == (unsigned char)c) 23 | return (((unsigned char *)s) + index); 24 | index++; 25 | } 26 | return (NULL); 27 | } 28 | 29 | // #include 30 | 31 | // int main() 32 | // { 33 | // char s[] = {0, 1, 2, 3, 4, 5}; 34 | 35 | // printf("%s\n", ft_memchr(s, 0, 0)); 36 | // printf("%s\n", memchr(s, 0, 0)); 37 | // printf("---------------------\n"); 38 | // printf("%s\n", ft_memchr(s, 2, 3)); 39 | // printf("%s\n", memchr(s, 2, 3)); 40 | // printf("---------------------\n"); 41 | // printf("%s\n", ft_memchr(s, 2+256, 3)); 42 | // printf("%s\n", memchr(s, 2+256, 3)); 43 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 15:32:44 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 18:34:32 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t index; 18 | 19 | index = 0; 20 | while (index < n) 21 | { 22 | if (((unsigned char *)s)[index] == (unsigned char)c) 23 | return (((unsigned char *)s) + index); 24 | index++; 25 | } 26 | return (NULL); 27 | } 28 | 29 | // #include 30 | 31 | // int main() 32 | // { 33 | // char s[] = {0, 1, 2, 3, 4, 5}; 34 | 35 | // printf("%s\n", ft_memchr(s, 0, 0)); 36 | // printf("%s\n", memchr(s, 0, 0)); 37 | // printf("---------------------\n"); 38 | // printf("%s\n", ft_memchr(s, 2, 3)); 39 | // printf("%s\n", memchr(s, 2, 3)); 40 | // printf("---------------------\n"); 41 | // printf("%s\n", ft_memchr(s, 2+256, 3)); 42 | // printf("%s\n", memchr(s, 2+256, 3)); 43 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 17:39:35 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 13:15:17 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of strlcpy function 14 | 15 | #include "libft.h" 16 | 17 | size_t ft_strlcpy(char *dest, const char *src, size_t size) 18 | { 19 | size_t src_len; 20 | size_t i; 21 | 22 | src_len = ft_strlen(src); 23 | if (size == 0) 24 | return (src_len); 25 | i = 0; 26 | while (i < size - 1 && src[i]) 27 | { 28 | dest[i] = src[i]; 29 | ++i; 30 | } 31 | dest[i] = '\0'; 32 | return (src_len); 33 | } 34 | 35 | // test ft_strlcpy function 36 | 37 | // #include 38 | // int main(void) 39 | // { 40 | // char *string1; 41 | // char string2[10]; 42 | 43 | // string1 = "Hello"; 44 | // printf("base : %s\n", string1); 45 | // strlcpy(string2, string1, 5); 46 | // printf("cpy c : %s\n", string2); 47 | // ft_strlcpy(string2, string1, 5); 48 | // printf("cpy ft : %s\n", string2); 49 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 17:39:35 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 13:15:17 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of strlcpy function 14 | 15 | #include "libft.h" 16 | 17 | size_t ft_strlcpy(char *dest, const char *src, size_t size) 18 | { 19 | size_t src_len; 20 | size_t i; 21 | 22 | src_len = ft_strlen(src); 23 | if (size == 0) 24 | return (src_len); 25 | i = 0; 26 | while (i < size - 1 && src[i]) 27 | { 28 | dest[i] = src[i]; 29 | ++i; 30 | } 31 | dest[i] = '\0'; 32 | return (src_len); 33 | } 34 | 35 | // test ft_strlcpy function 36 | 37 | // #include 38 | // int main(void) 39 | // { 40 | // char *string1; 41 | // char string2[10]; 42 | 43 | // string1 = "Hello"; 44 | // printf("base : %s\n", string1); 45 | // strlcpy(string2, string1, 5); 46 | // printf("cpy c : %s\n", string2); 47 | // ft_strlcpy(string2, string1, 5); 48 | // printf("cpy ft : %s\n", string2); 49 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/04 16:08:28 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:40:11 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t count(const char *dest) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | while (dest[len] != '\0') 21 | len++; 22 | return (len); 23 | } 24 | 25 | size_t ft_strlcat(char *dest, const char *src, size_t size) 26 | { 27 | char *dst; 28 | const char *src_i; 29 | size_t dest_length; 30 | size_t remaining; 31 | 32 | dst = dest; 33 | src_i = src; 34 | remaining = size; 35 | while (remaining-- != 0 && *dst != '\0') 36 | dst++; 37 | dest_length = dst - dest; 38 | remaining = size - dest_length; 39 | if (remaining == 0) 40 | return (dest_length + count((src))); 41 | while (*src != '\0') 42 | { 43 | if (remaining > 1) 44 | { 45 | *dst++ = *src; 46 | remaining--; 47 | } 48 | src++; 49 | } 50 | *dst = '\0'; 51 | return (dest_length + (src - src_i)); 52 | } 53 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/04 16:08:28 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:40:11 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t count(const char *dest) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | while (dest[len] != '\0') 21 | len++; 22 | return (len); 23 | } 24 | 25 | size_t ft_strlcat(char *dest, const char *src, size_t size) 26 | { 27 | char *dst; 28 | const char *src_i; 29 | size_t dest_length; 30 | size_t remaining; 31 | 32 | dst = dest; 33 | src_i = src; 34 | remaining = size; 35 | while (remaining-- != 0 && *dst != '\0') 36 | dst++; 37 | dest_length = dst - dest; 38 | remaining = size - dest_length; 39 | if (remaining == 0) 40 | return (dest_length + count((src))); 41 | while (*src != '\0') 42 | { 43 | if (remaining > 1) 44 | { 45 | *dst++ = *src; 46 | remaining--; 47 | } 48 | src++; 49 | } 50 | *dst = '\0'; 51 | return (dest_length + (src - src_i)); 52 | } 53 | -------------------------------------------------------------------------------- /lib/libft/src/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:48:04 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 12:01:46 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memset function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_memset(void *ptr, int value, size_t num) 18 | { 19 | unsigned char *temp; 20 | 21 | temp = (unsigned char *)ptr; 22 | while (num--) 23 | { 24 | *temp = (unsigned char)value; 25 | temp++; 26 | } 27 | return (ptr); 28 | } 29 | 30 | // test ft_memset function 31 | // #include 32 | 33 | // int main() 34 | // { 35 | // char str[50] = "GeeksForGeeks is for programming geeks."; 36 | // printf("\nBefore memset(): %s\n", str); 37 | // ft_memset(str + 13, '.', 8*sizeof(char)); 38 | // printf("After memset(): %s", str); 39 | // char str1[50] = "GeeksForGeeks is for programming geeks."; 40 | // printf("\nBefore memset(): %s\n", str1); 41 | // memset(str1 + 13, '.', 8*sizeof(char)); 42 | // printf("After memset(): %s", str1); 43 | // return 0; 44 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 11:48:04 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 12:01:46 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memset function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_memset(void *ptr, int value, size_t num) 18 | { 19 | unsigned char *temp; 20 | 21 | temp = (unsigned char *)ptr; 22 | while (num--) 23 | { 24 | *temp = (unsigned char)value; 25 | temp++; 26 | } 27 | return (ptr); 28 | } 29 | 30 | // test ft_memset function 31 | // #include 32 | 33 | // int main() 34 | // { 35 | // char str[50] = "GeeksForGeeks is for programming geeks."; 36 | // printf("\nBefore memset(): %s\n", str); 37 | // ft_memset(str + 13, '.', 8*sizeof(char)); 38 | // printf("After memset(): %s", str); 39 | // char str1[50] = "GeeksForGeeks is for programming geeks."; 40 | // printf("\nBefore memset(): %s\n", str1); 41 | // memset(str1 + 13, '.', 8*sizeof(char)); 42 | // printf("After memset(): %s", str1); 43 | // return 0; 44 | // } -------------------------------------------------------------------------------- /includes/checker.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 11:31:24 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 11:44:46 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef CHECKER_H 14 | # define CHECKER_H 15 | 16 | # include "ft_printf.h" 17 | # include "libft.h" 18 | # include 19 | 20 | typedef struct s_node 21 | { 22 | int content; 23 | struct s_node *prev; 24 | struct s_node *next; 25 | } t_node; 26 | 27 | bool is_sorted(t_node *stack, bool reverse); 28 | char *get_next_line(int fd); 29 | bool execute(t_node **stack_a, t_node **stack_b, char *line, 30 | bool s_print); 31 | void print_stack(t_node *stack, char *str, bool advanced, 32 | bool to_first); 33 | int stack_len(t_node *stack); 34 | t_node *add_node(t_node *node, int content); 35 | void clear_lst_node(t_node *node); 36 | void ft_error(t_node *stack_a); 37 | int ft_atoi_ps(char *str, t_node *stack_a); 38 | int ft_strcmp(const char *s1, const char *s2); 39 | bool is_valid(t_node *stack_a, char *str); 40 | 41 | #endif -------------------------------------------------------------------------------- /lib/libft/src/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 14:17:04 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:27:34 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement strmapi function: apply function to each character of string 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 18 | { 19 | char *str; 20 | int i; 21 | 22 | if (!s || !f) 23 | return (NULL); 24 | str = (char *)malloc((ft_strlen(s) + 1) * 1); 25 | if (!str) 26 | return (NULL); 27 | i = 0; 28 | while (s[i]) 29 | { 30 | str[i] = f(i, s[i]); 31 | i++; 32 | } 33 | str[i] = '\0'; 34 | return (str); 35 | } 36 | 37 | // test ft_strmapi function 38 | // #include 39 | 40 | // char ft_test(unsigned int i, char c) 41 | // { 42 | // i = 0; 43 | // return (c - 32); 44 | // } 45 | 46 | // int main() 47 | // { 48 | // char *str = "Hello World!"; 49 | // char *new_str = ft_strmapi(str, ft_test); 50 | // printf("%s\n", new_str); 51 | // prints only Hello due to the null terminator (space - 32) 52 | // return (0); 53 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 14:17:04 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 15:27:34 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement strmapi function: apply function to each character of string 14 | 15 | #include "libft.h" 16 | 17 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 18 | { 19 | char *str; 20 | int i; 21 | 22 | if (!s || !f) 23 | return (NULL); 24 | str = (char *)malloc((ft_strlen(s) + 1) * 1); 25 | if (!str) 26 | return (NULL); 27 | i = 0; 28 | while (s[i]) 29 | { 30 | str[i] = f(i, s[i]); 31 | i++; 32 | } 33 | str[i] = '\0'; 34 | return (str); 35 | } 36 | 37 | // test ft_strmapi function 38 | // #include 39 | 40 | // char ft_test(unsigned int i, char c) 41 | // { 42 | // i = 0; 43 | // return (c - 32); 44 | // } 45 | 46 | // int main() 47 | // { 48 | // char *str = "Hello World!"; 49 | // char *new_str = ft_strmapi(str, ft_test); 50 | // printf("%s\n", new_str); 51 | // prints only Hello due to the null terminator (space - 32) 52 | // return (0); 53 | // } -------------------------------------------------------------------------------- /lib/libft/README.md: -------------------------------------------------------------------------------- 1 | # Libft 2 | 3 | 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. 4 | 5 | ## Table of Contents 6 | 7 | - [Installation](#installation) 8 | - [Usage](#usage) 9 | - [License](#license) 10 | 11 | ## Installation 12 | 13 | To use Libft in your C projects, follow these steps: 14 | 15 | 1. Clone the Libft repository to your local machine: 16 | ```bash 17 | https://github.com/LeeSinLiang/Libft 18 | ``` 19 | 20 | 2. Compile the library by running the Makefile: 21 | ```bash 22 | cd Libft 23 | make 24 | ``` 25 | 26 | 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. 27 | 28 | 4. Finally, compile your C program with the Libft library: 29 | ```bash 30 | gcc my_program.c -L. -lft 31 | ``` 32 | 33 | ## Usage 34 | 35 | Libft provides a wide range of functions categorized into different sections, including string manipulation, memory allocation, linked lists, and more. 36 | 37 | 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: 38 | 39 | ```c 40 | #include 41 | 42 | int ft_atoi(const char *str); 43 | 44 | int main() 45 | { 46 | char *str = "123456789"; 47 | printf("%d", ft_atoi(str)); 48 | return (0); 49 | } 50 | ``` 51 | 52 | For more details on the available functions and their usage, refer to the individual c files and their corresponding comments. 53 | 54 | ## License 55 | 56 | MIT License 57 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/README.md: -------------------------------------------------------------------------------- 1 | # Libft 2 | 3 | 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. 4 | 5 | ## Table of Contents 6 | 7 | - [Installation](#installation) 8 | - [Usage](#usage) 9 | - [License](#license) 10 | 11 | ## Installation 12 | 13 | To use Libft in your C projects, follow these steps: 14 | 15 | 1. Clone the Libft repository to your local machine: 16 | ```bash 17 | https://github.com/LeeSinLiang/Libft 18 | ``` 19 | 20 | 2. Compile the library by running the Makefile: 21 | ```bash 22 | cd Libft 23 | make 24 | ``` 25 | 26 | 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. 27 | 28 | 4. Finally, compile your C program with the Libft library: 29 | ```bash 30 | gcc my_program.c -L. -lft 31 | ``` 32 | 33 | ## Usage 34 | 35 | Libft provides a wide range of functions categorized into different sections, including string manipulation, memory allocation, linked lists, and more. 36 | 37 | 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: 38 | 39 | ```c 40 | #include 41 | 42 | int ft_atoi(const char *str); 43 | 44 | int main() 45 | { 46 | char *str = "123456789"; 47 | printf("%d", ft_atoi(str)); 48 | return (0); 49 | } 50 | ``` 51 | 52 | For more details on the available functions and their usage, refer to the individual c files and their corresponding comments. 53 | 54 | ## License 55 | 56 | MIT License 57 | -------------------------------------------------------------------------------- /lib/libft/src/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 14:04:49 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 14:14:49 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement ft_itoa function: convert int to string 14 | 15 | #include "libft.h" 16 | 17 | static int ft_len(int n) 18 | { 19 | int len; 20 | 21 | len = 0; 22 | if (n <= 0) 23 | len++; 24 | while (n != 0) 25 | { 26 | n /= 10; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | static int ft_abs(int n) 33 | { 34 | if (n < 0) 35 | return (-n); 36 | return (n); 37 | } 38 | 39 | char *ft_itoa(int n) 40 | { 41 | int len; 42 | char *str; 43 | 44 | len = ft_len(n); 45 | str = (char *)malloc((len + 1) * 1); 46 | if (!str) 47 | return (0); 48 | str[len] = '\0'; 49 | len--; 50 | if (n == 0) 51 | str[0] = '0'; 52 | if (n < 0) 53 | str[0] = '-'; 54 | while (n != 0) 55 | { 56 | str[len] = ft_abs(n % 10) + '0'; 57 | n /= 10; 58 | len--; 59 | } 60 | return (str); 61 | } 62 | 63 | // #include 64 | // int main(int argc, char **argv) 65 | // { 66 | // if (argc == 2) 67 | // { 68 | // printf("%s\n", ft_itoa(atoi(argv[1]))); 69 | // } 70 | // return (0); 71 | // } 72 | -------------------------------------------------------------------------------- /lib/libft/src/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 12:27:24 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 12:54:04 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memmove function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_memmove(void *dest, const void *src, size_t n) 18 | { 19 | unsigned char *ptr_dest; 20 | unsigned char *ptr_src; 21 | size_t i; 22 | 23 | ptr_dest = (unsigned char *)dest; 24 | ptr_src = (unsigned char *)src; 25 | i = 0; 26 | if (ptr_dest > ptr_src) 27 | { 28 | while (n--) 29 | ptr_dest[n] = ptr_src[n]; 30 | } 31 | else 32 | { 33 | while (i < n) 34 | { 35 | ptr_dest[i] = ptr_src[i]; 36 | i++; 37 | } 38 | } 39 | return (dest); 40 | } 41 | 42 | // test ft_memmove function 43 | 44 | // #include 45 | 46 | // int main(void) 47 | // { 48 | // char str1[50] = "Hello"; 49 | // char str2[50] = "WorldHi"; 50 | // ft_memmove(str1, str2, 3 * sizeof(char)); 51 | // printf("After memmove(): %s|\n", str1); 52 | // char str3[50] = "Hello"; 53 | // char str4[50] = "WorldHi"; 54 | // memmove(str3, str4, 3 * sizeof(char)); 55 | // printf("After memmove(): %s|", str3); 56 | // return (0); 57 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 14:04:49 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/03 14:14:49 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement ft_itoa function: convert int to string 14 | 15 | #include "libft.h" 16 | 17 | static int ft_len(int n) 18 | { 19 | int len; 20 | 21 | len = 0; 22 | if (n <= 0) 23 | len++; 24 | while (n != 0) 25 | { 26 | n /= 10; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | static int ft_abs(int n) 33 | { 34 | if (n < 0) 35 | return (-n); 36 | return (n); 37 | } 38 | 39 | char *ft_itoa(int n) 40 | { 41 | int len; 42 | char *str; 43 | 44 | len = ft_len(n); 45 | str = (char *)malloc((len + 1) * 1); 46 | if (!str) 47 | return (0); 48 | str[len] = '\0'; 49 | len--; 50 | if (n == 0) 51 | str[0] = '0'; 52 | if (n < 0) 53 | str[0] = '-'; 54 | while (n != 0) 55 | { 56 | str[len] = ft_abs(n % 10) + '0'; 57 | n /= 10; 58 | len--; 59 | } 60 | return (str); 61 | } 62 | 63 | // #include 64 | // int main(int argc, char **argv) 65 | // { 66 | // if (argc == 2) 67 | // { 68 | // printf("%s\n", ft_itoa(atoi(argv[1]))); 69 | // } 70 | // return (0); 71 | // } 72 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 12:27:24 by sinlee #+# #+# */ 9 | /* Updated: 2023/04/28 12:54:04 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implementation of memmove function 14 | 15 | #include "libft.h" 16 | 17 | void *ft_memmove(void *dest, const void *src, size_t n) 18 | { 19 | unsigned char *ptr_dest; 20 | unsigned char *ptr_src; 21 | size_t i; 22 | 23 | ptr_dest = (unsigned char *)dest; 24 | ptr_src = (unsigned char *)src; 25 | i = 0; 26 | if (ptr_dest > ptr_src) 27 | { 28 | while (n--) 29 | ptr_dest[n] = ptr_src[n]; 30 | } 31 | else 32 | { 33 | while (i < n) 34 | { 35 | ptr_dest[i] = ptr_src[i]; 36 | i++; 37 | } 38 | } 39 | return (dest); 40 | } 41 | 42 | // test ft_memmove function 43 | 44 | // #include 45 | 46 | // int main(void) 47 | // { 48 | // char str1[50] = "Hello"; 49 | // char str2[50] = "WorldHi"; 50 | // ft_memmove(str1, str2, 3 * sizeof(char)); 51 | // printf("After memmove(): %s|\n", str1); 52 | // char str3[50] = "Hello"; 53 | // char str4[50] = "WorldHi"; 54 | // memmove(str3, str4, 3 * sizeof(char)); 55 | // printf("After memmove(): %s|", str3); 56 | // return (0); 57 | // } -------------------------------------------------------------------------------- /srcs/stack/misc_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* misc_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/05 08:52:19 by codespace #+# #+# */ 9 | /* Updated: 2023/07/16 04:11:08 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int max(int a, int b) 16 | { 17 | if (a > b) 18 | return (a); 19 | return (b); 20 | } 21 | 22 | int min(int a, int b) 23 | { 24 | if (a < b) 25 | return (a); 26 | return (b); 27 | } 28 | 29 | int stack_len(t_node *stack) 30 | { 31 | int len; 32 | t_node *tmp; 33 | 34 | len = 0; 35 | tmp = last_first_node(stack, false); 36 | while (tmp) 37 | { 38 | len++; 39 | tmp = tmp->next; 40 | } 41 | return (len); 42 | } 43 | 44 | int ft_strcmp(const char *s1, const char *s2) 45 | { 46 | while ((*s1 || *s2)) 47 | { 48 | if (*s1++ != *s2++) 49 | return (*(unsigned char *)--s1 - *(unsigned char *)--s2); 50 | } 51 | return (0); 52 | } 53 | 54 | void check_exit_else_exec(t_node **stack_a, t_node **stack_b, char *line) 55 | { 56 | if (stack_a == NULL || stack_b == NULL) 57 | ft_printf("%s\n", line); 58 | else if (is_sorted(*stack_a, false) && stack_len(*stack_b) == 0) 59 | { 60 | clear_lst_node(*stack_a); 61 | clear_lst_node(*stack_b); 62 | exit(0); 63 | } 64 | else if (line != NULL) 65 | ft_printf("%s\n", line); 66 | } 67 | -------------------------------------------------------------------------------- /lib/libft/src/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 11:08:15 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:16:15 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // Function name: ft_strjoin 14 | // Prototype: char: *ft_strjoin(char const *s1, char const *s2); 15 | // Parameters: 16 | // s1: The prefix string. s2: The suffix string. 17 | // Return value: The new string. NULL if the allocation fails. 18 | // External functs: malloc 19 | // Description: 20 | // Allocates (with malloc(3)) and returns a new string, 21 | // which is the result of the concatenation of ’s1’ and ’s2’. 22 | 23 | #include "libft.h" 24 | 25 | char *ft_strjoin(char const *s1, char const *s2) 26 | { 27 | char *str; 28 | int i; 29 | 30 | i = -1; 31 | str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); 32 | if (!str) 33 | return (0); 34 | while (s1[++i]) 35 | str[i] = s1[i]; 36 | i = -1; 37 | while (s2[++i]) 38 | str[ft_strlen(s1) + i] = s2[i]; 39 | str[ft_strlen(s1) + i] = '\0'; 40 | return (str); 41 | } 42 | 43 | // test ft_strjoin function. check strlcat const 44 | // #include 45 | 46 | // int main() 47 | // { 48 | // char *s1 = "tripouille"; 49 | // char *s2 = "42"; 50 | // char *s3 = ft_strjoin("", "42"); 51 | // printf("%s|\n", s3); 52 | // printf("%d", strcmp(s3, "42")); 53 | // // printf("%s|\n", strjoin("", "42")); 54 | // return (0); 55 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 11:08:15 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:16:15 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // Function name: ft_strjoin 14 | // Prototype: char: *ft_strjoin(char const *s1, char const *s2); 15 | // Parameters: 16 | // s1: The prefix string. s2: The suffix string. 17 | // Return value: The new string. NULL if the allocation fails. 18 | // External functs: malloc 19 | // Description: 20 | // Allocates (with malloc(3)) and returns a new string, 21 | // which is the result of the concatenation of ’s1’ and ’s2’. 22 | 23 | #include "libft.h" 24 | 25 | char *ft_strjoin(char const *s1, char const *s2) 26 | { 27 | char *str; 28 | int i; 29 | 30 | i = -1; 31 | str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); 32 | if (!str) 33 | return (0); 34 | while (s1[++i]) 35 | str[i] = s1[i]; 36 | i = -1; 37 | while (s2[++i]) 38 | str[ft_strlen(s1) + i] = s2[i]; 39 | str[ft_strlen(s1) + i] = '\0'; 40 | return (str); 41 | } 42 | 43 | // test ft_strjoin function. check strlcat const 44 | // #include 45 | 46 | // int main() 47 | // { 48 | // char *s1 = "tripouille"; 49 | // char *s2 = "42"; 50 | // char *s3 = ft_strjoin("", "42"); 51 | // printf("%s|\n", s3); 52 | // printf("%d", strcmp(s3, "42")); 53 | // // printf("%s|\n", strjoin("", "42")); 54 | // return (0); 55 | // } -------------------------------------------------------------------------------- /lib/ft_printf/src/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/19 10:03:13 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/19 14:34:20 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement printf function 14 | 15 | #include "ft_printf.h" 16 | 17 | static void ft_printf_check(char c, va_list *args, int *i, int *len) 18 | { 19 | if (c == 'c') 20 | ft_putchar_len(va_arg(*args, int), len); 21 | else if (c == 's') 22 | ft_putstr_len(va_arg(*args, char *), len); 23 | else if (c == 'p') 24 | ft_putpointer_len(va_arg(*args, uintptr_t), len); 25 | else if (c == 'd' || c == 'i') 26 | ft_putnbr_len(va_arg(*args, int), len); 27 | else if (c == 'u') 28 | ft_putnbr_u_len(va_arg(*args, unsigned int), len); 29 | else if (c == 'x') 30 | ft_putnbr_hexa_len(va_arg(*args, unsigned int), len, false); 31 | else if (c == 'X') 32 | ft_putnbr_hexa_len(va_arg(*args, unsigned int), len, true); 33 | else if (c == '%') 34 | ft_putchar_len('%', len); 35 | else 36 | (*i)--; 37 | } 38 | 39 | int ft_printf(const char *str, ...) 40 | { 41 | va_list args; 42 | int i; 43 | int len; 44 | 45 | va_start(args, str); 46 | len = 0; 47 | i = 0; 48 | while (str[i] != '\0') 49 | { 50 | if (str[i] == '%') 51 | { 52 | i++; 53 | ft_printf_check(str[i], &args, &i, &len); 54 | } 55 | else 56 | ft_putchar_len(str[i], &len); 57 | i++; 58 | } 59 | va_end(args); 60 | return (len); 61 | } 62 | -------------------------------------------------------------------------------- /lib/libft/src/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 10:20:56 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:54:36 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // Function name: ft_substr 14 | // Prototype: 15 | // char *ft_substr(char const *s, unsigned int start, 16 | // size_t len); 17 | // Parameters: 18 | // s: The string from which to create the substring. 19 | // start: The start index of the substring in the string ’s’. 20 | // len: The maximum length of the substring. 21 | // Return value: 22 | // The substring. 23 | // NULL if the allocation fails. 24 | // External functs: 25 | // malloc 26 | // Description: 27 | // Allocates (with malloc(3)) and returns a substring from the string ’s’. 28 | // The substring begins at index ’start’ and is of maximum size ’len’. 29 | 30 | #include "libft.h" 31 | 32 | char *ft_substr(char const *s, unsigned int start, size_t len) 33 | { 34 | char *substr; 35 | 36 | if (start > ft_strlen(s)) 37 | { 38 | substr = malloc(sizeof(char) * 1); 39 | substr[0] = '\0'; 40 | } 41 | else 42 | { 43 | if (len > ft_strlen(s + start)) 44 | len = ft_strlen(s + start); 45 | substr = malloc(sizeof(char) * (len + 1)); 46 | if (substr == 0) 47 | return (NULL); 48 | else 49 | ft_strlcpy(substr, s + start, len + 1); 50 | } 51 | return (substr); 52 | } 53 | 54 | // #include 55 | // int main() 56 | // { 57 | // printf("%s\n", ft_substr("123456789", 2, 5)); 58 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 10:20:56 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 14:54:36 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // Function name: ft_substr 14 | // Prototype: 15 | // char *ft_substr(char const *s, unsigned int start, 16 | // size_t len); 17 | // Parameters: 18 | // s: The string from which to create the substring. 19 | // start: The start index of the substring in the string ’s’. 20 | // len: The maximum length of the substring. 21 | // Return value: 22 | // The substring. 23 | // NULL if the allocation fails. 24 | // External functs: 25 | // malloc 26 | // Description: 27 | // Allocates (with malloc(3)) and returns a substring from the string ’s’. 28 | // The substring begins at index ’start’ and is of maximum size ’len’. 29 | 30 | #include "libft.h" 31 | 32 | char *ft_substr(char const *s, unsigned int start, size_t len) 33 | { 34 | char *substr; 35 | 36 | if (start > ft_strlen(s)) 37 | { 38 | substr = malloc(sizeof(char) * 1); 39 | substr[0] = '\0'; 40 | } 41 | else 42 | { 43 | if (len > ft_strlen(s + start)) 44 | len = ft_strlen(s + start); 45 | substr = malloc(sizeof(char) * (len + 1)); 46 | if (substr == 0) 47 | return (NULL); 48 | else 49 | ft_strlcpy(substr, s + start, len + 1); 50 | } 51 | return (substr); 52 | } 53 | 54 | // #include 55 | // int main() 56 | // { 57 | // printf("%s\n", ft_substr("123456789", 2, 5)); 58 | // } -------------------------------------------------------------------------------- /srcs/checker.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 11:28:28 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 14:49:19 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // checker for push_swap program 14 | 15 | #include "checker.h" 16 | 17 | void checker(t_node **stack_a, t_node **stack_b) 18 | { 19 | char *line; 20 | char **split; 21 | 22 | line = get_next_line(0); 23 | while (line) 24 | { 25 | split = ft_split(line, '\n'); 26 | if (ft_strchr(line, '\n') == 0 || split[0] == 0) 27 | ft_error(*stack_a); 28 | free(line); 29 | if (execute(stack_a, stack_b, split[0], true) == false) 30 | ft_error(*stack_a); 31 | free(split[0]); 32 | free(split); 33 | line = get_next_line(0); 34 | } 35 | if (is_sorted(*stack_a, false) && *stack_b == NULL) 36 | ft_printf("OK\n"); 37 | else 38 | ft_printf("KO\n"); 39 | } 40 | 41 | int main(int argc, char **argv) 42 | { 43 | int i; 44 | t_node *stack_a; 45 | t_node *stack_b; 46 | 47 | i = 0; 48 | stack_a = NULL; 49 | stack_b = NULL; 50 | if (argc == 2) 51 | argv = ft_split(argv[1], ' '); 52 | if (argc == 2) 53 | i = -1; 54 | if (argc > 1) 55 | { 56 | while (argv[++i]) 57 | { 58 | if (is_valid(stack_a, argv[i]) == false) 59 | ft_error(stack_a); 60 | stack_a = add_node(stack_a, ft_atoi(argv[i])); 61 | } 62 | if (stack_len(stack_a) == 0) 63 | ft_error(stack_a); 64 | checker(&stack_a, &stack_b); 65 | clear_lst_node(stack_a); 66 | clear_lst_node(stack_b); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /lib/ft_printf/README.md: -------------------------------------------------------------------------------- 1 | # ft_printf 2 | 3 | This project is a custom implementation of the `printf` function in the C programming language. 4 | 5 | ## Introduction 6 | 7 | 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. 8 | 9 | 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. 10 | 11 | ## Features 12 | 13 | The `ft_printf` function in this project supports the following features: 14 | 15 | - Printing formatted output to the standard output stream. 16 | - Handling different conversion specifiers, such as `%d`, `%s`, `%c`, `%u`, `%p`, and more. 17 | 18 | ## Getting Started 19 | 20 | To compile and use the `ft_printf` function, follow these steps: 21 | 22 | 1. Clone the repository to your local machine using the following command: 23 | 24 | ```shell 25 | git clone https://github.com/LeeSinLiang/ft_printf.git 26 | ``` 27 | 28 | 2. Navigate to the project directory: 29 | 30 | ```shell 31 | cd ft_printf 32 | ``` 33 | 34 | 3. Compile the project using Makefile: 35 | 36 | ```shell 37 | make 38 | ``` 39 | 40 | 4. Start using the `ft_printf` function in your code as you would with the standard `printf` function. For example: 41 | 42 | ```c 43 | int ft_printf(const char *str, ...); 44 | 45 | int main() { 46 | ft_printf("Hello, %s!\n", "world"); 47 | return 0; 48 | } 49 | ``` 50 | 51 | Compile your code with the `libftprintf.a` library: 52 | 53 | ```shell 54 | gcc -Wall -Wextra -Werror -I includes main.c -L. -lftprintf -o main 55 | ``` 56 | 57 | Run the executable: 58 | 59 | ```shell 60 | ./main 61 | ``` 62 | 63 | Output: 64 | 65 | ``` 66 | Hello, world! 67 | ``` 68 | 69 | **Note: libft is included to utilize several functions such as `ft_itoa`, `ft_putchar_fd`, `ft_putstr_fd` and more.** 70 | 71 | **More details of libft: https://github.com/LeeSinLiang/libft** 72 | -------------------------------------------------------------------------------- /lib/libft/src/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 11:27:47 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:11:55 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // Function name: ft_strtrim 14 | // Prototype: char *ft_strtrim(char const *s1, char const *set); 15 | // Parameters: 16 | // s1: The string to be trimmed. 17 | // set: The reference set of characters to trim. 18 | // Return value: The trimmed string. NULL if the allocation fails. 19 | // External functs: malloc 20 | // Description: 21 | // Allocates (with malloc(3)) and returns a copy of ’s1’ 22 | // with the characters specified in ’set’ removed 23 | // from the beginning and the end of the string. 24 | 25 | #include "libft.h" 26 | 27 | char *ft_strtrim(char const *s1, char const *set) 28 | { 29 | size_t i; 30 | size_t j; 31 | char *str; 32 | 33 | str = 0; 34 | if (set == 0) 35 | return (ft_strdup((char *)s1)); 36 | if (s1 != 0) 37 | { 38 | i = 0; 39 | j = ft_strlen(s1); 40 | while (s1[i] && ft_strchr(set, s1[i])) 41 | i++; 42 | while (s1[j - 1] && ft_strchr(set, s1[j - 1]) && j > i) 43 | j--; 44 | str = (char *)malloc(sizeof(char) * (j - i + 1)); 45 | if (str) 46 | ft_strlcpy(str, &s1[i], j - i + 1); 47 | } 48 | return (str); 49 | } 50 | 51 | // test ft_strtrim function 52 | 53 | // #include 54 | 55 | // int main(int argc, char **argv) 56 | // { 57 | // // printf("%s|\n", ft_strtrim(argv[1], argv[2])); 58 | // printf("%s|", ft_strtrim("basic test", " ")); 59 | // return (0); 60 | // } -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 11:27:47 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:11:55 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // Function name: ft_strtrim 14 | // Prototype: char *ft_strtrim(char const *s1, char const *set); 15 | // Parameters: 16 | // s1: The string to be trimmed. 17 | // set: The reference set of characters to trim. 18 | // Return value: The trimmed string. NULL if the allocation fails. 19 | // External functs: malloc 20 | // Description: 21 | // Allocates (with malloc(3)) and returns a copy of ’s1’ 22 | // with the characters specified in ’set’ removed 23 | // from the beginning and the end of the string. 24 | 25 | #include "libft.h" 26 | 27 | char *ft_strtrim(char const *s1, char const *set) 28 | { 29 | size_t i; 30 | size_t j; 31 | char *str; 32 | 33 | str = 0; 34 | if (set == 0) 35 | return (ft_strdup((char *)s1)); 36 | if (s1 != 0) 37 | { 38 | i = 0; 39 | j = ft_strlen(s1); 40 | while (s1[i] && ft_strchr(set, s1[i])) 41 | i++; 42 | while (s1[j - 1] && ft_strchr(set, s1[j - 1]) && j > i) 43 | j--; 44 | str = (char *)malloc(sizeof(char) * (j - i + 1)); 45 | if (str) 46 | ft_strlcpy(str, &s1[i], j - i + 1); 47 | } 48 | return (str); 49 | } 50 | 51 | // test ft_strtrim function 52 | 53 | // #include 54 | 55 | // int main(int argc, char **argv) 56 | // { 57 | // // printf("%s|\n", ft_strtrim(argv[1], argv[2])); 58 | // printf("%s|", ft_strtrim("basic test", " ")); 59 | // return (0); 60 | // } -------------------------------------------------------------------------------- /lib/libft/src/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 09:32:57 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 13:45:45 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement strnstr function 14 | 15 | #include "libft.h" 16 | #include 17 | 18 | static bool check_occur(char *str, char *to_find, size_t len) 19 | { 20 | size_t i; 21 | 22 | i = 0; 23 | while (to_find[i] != '\0' && len--) 24 | { 25 | if (str[i] != to_find[i]) 26 | return (false); 27 | i++; 28 | } 29 | if (to_find[i] != '\0') 30 | return (false); 31 | return (true); 32 | } 33 | 34 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 35 | { 36 | char *haystack_cpy; 37 | 38 | haystack_cpy = (char *)haystack; 39 | if (!needle[0]) 40 | return (haystack_cpy); 41 | while (*haystack_cpy && len > 0) 42 | { 43 | if (!check_occur(haystack_cpy, (char *)needle, len)) 44 | { 45 | haystack_cpy++; 46 | len--; 47 | } 48 | else 49 | return (haystack_cpy); 50 | } 51 | return (0); 52 | } 53 | 54 | // test ft_strnstr by comparing with strnstr function 55 | 56 | // #include 57 | // #include 58 | 59 | // int main(int argc, char **argv) 60 | // { 61 | // char *str1; 62 | // char *str2; 63 | // int n; 64 | 65 | // str1 = argv[1]; 66 | // str2 = argv[2]; 67 | // n = atoi(argv[3]); 68 | // printf("%s\n", ft_strnstr(str1, str2, n)); 69 | // printf("%s\n", strnstr(str1, str2, n)); 70 | // // char haystack[30] = "aaabcabcd"; 71 | // // printf("%s\n", ft_strnstr(haystack, "abcd", 9)); 72 | // // printf("%s\n", strnstr(haystack, "abcd", 9)); 73 | // return (0); 74 | // } 75 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/03 09:32:57 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 13:45:45 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | // implement strnstr function 14 | 15 | #include "libft.h" 16 | #include 17 | 18 | static bool check_occur(char *str, char *to_find, size_t len) 19 | { 20 | size_t i; 21 | 22 | i = 0; 23 | while (to_find[i] != '\0' && len--) 24 | { 25 | if (str[i] != to_find[i]) 26 | return (false); 27 | i++; 28 | } 29 | if (to_find[i] != '\0') 30 | return (false); 31 | return (true); 32 | } 33 | 34 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 35 | { 36 | char *haystack_cpy; 37 | 38 | haystack_cpy = (char *)haystack; 39 | if (!needle[0]) 40 | return (haystack_cpy); 41 | while (*haystack_cpy && len > 0) 42 | { 43 | if (!check_occur(haystack_cpy, (char *)needle, len)) 44 | { 45 | haystack_cpy++; 46 | len--; 47 | } 48 | else 49 | return (haystack_cpy); 50 | } 51 | return (0); 52 | } 53 | 54 | // test ft_strnstr by comparing with strnstr function 55 | 56 | // #include 57 | // #include 58 | 59 | // int main(int argc, char **argv) 60 | // { 61 | // char *str1; 62 | // char *str2; 63 | // int n; 64 | 65 | // str1 = argv[1]; 66 | // str2 = argv[2]; 67 | // n = atoi(argv[3]); 68 | // printf("%s\n", ft_strnstr(str1, str2, n)); 69 | // printf("%s\n", strnstr(str1, str2, n)); 70 | // // char haystack[30] = "aaabcabcd"; 71 | // // printf("%s\n", ft_strnstr(haystack, "abcd", 9)); 72 | // // printf("%s\n", strnstr(haystack, "abcd", 9)); 73 | // return (0); 74 | // } 75 | -------------------------------------------------------------------------------- /srcs/misc/check.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 14:33:27 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 14:31:39 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void ft_error(t_node *stack_a) 16 | { 17 | clear_lst_node(stack_a); 18 | ft_printf("Error\n"); 19 | exit(EXIT_FAILURE); 20 | } 21 | 22 | static bool is_space(char c) 23 | { 24 | char *str; 25 | 26 | str = "\t\n\v\f\r "; 27 | while (*str) 28 | { 29 | if (*str == c) 30 | return (true); 31 | str++; 32 | } 33 | return (false); 34 | } 35 | 36 | int ft_atoi_ps(char *str, t_node *stack_a) 37 | { 38 | long long int ans; 39 | int result; 40 | 41 | ans = 0; 42 | result = 1; 43 | while (is_space(*str)) 44 | str++; 45 | if (*str == '-') 46 | result *= -1; 47 | if (*str == '+' || *str == '-') 48 | str++; 49 | if (!*str) 50 | ft_error(stack_a); 51 | while (*str) 52 | { 53 | if (!ft_isdigit(*str)) 54 | ft_error(stack_a); 55 | ans = (ans * 10) + (*str - 48); 56 | str++; 57 | } 58 | if ((result * ans) > 2147483647 || (result * ans) < -2147483648) 59 | ft_error(stack_a); 60 | return (result * ans); 61 | } 62 | 63 | bool is_valid(t_node *stack_a, char *str) 64 | { 65 | t_node *tmp; 66 | int nbr; 67 | 68 | tmp = last_first_node(stack_a, false); 69 | nbr = ft_atoi_ps(str, stack_a); 70 | while (tmp) 71 | { 72 | if (tmp->content == nbr) 73 | return (false); 74 | tmp = tmp->next; 75 | } 76 | return (true); 77 | } 78 | 79 | void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]) 80 | { 81 | pos[0] = stack_len(*stack_a) - pos[0]; 82 | if (pos[1] != 0) 83 | pos[1] = stack_len(*stack_b) - pos[1]; 84 | } 85 | -------------------------------------------------------------------------------- /lib/ft_printf/src/ft_nbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_nbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/19 11:16:41 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/19 14:57:06 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | void ft_putnbr_len(int n, int *len) 16 | { 17 | char *nbr; 18 | 19 | nbr = ft_itoa(n); 20 | ft_putstr_len(nbr, len); 21 | free(nbr); 22 | } 23 | 24 | void ft_putnbr_u_len(unsigned int n, int *len) 25 | { 26 | if (n >= 10) 27 | ft_putnbr_u_len(n / 10, len); 28 | ft_putchar_len((n % 10) + '0', len); 29 | } 30 | 31 | static char *ft_nbr_base_len(unsigned int nbr, char *base) 32 | { 33 | unsigned int length; 34 | char *ans; 35 | int attr[2]; 36 | char display[11]; 37 | 38 | attr[0] = 0; 39 | attr[1] = 0; 40 | ans = malloc(11); 41 | length = (unsigned int)ft_strlen(base); 42 | while (nbr >= length) 43 | { 44 | display[attr[0]++] = base[(nbr % length)]; 45 | nbr /= length; 46 | } 47 | display[attr[0]] = base[nbr]; 48 | while (attr[0] >= 0) 49 | ans[attr[1]++] = display[attr[0]--]; 50 | ans[attr[1]] = '\0'; 51 | return (ans); 52 | } 53 | 54 | void ft_putnbr_hexa_len(unsigned int nbr, int *len, bool up) 55 | { 56 | char *ans; 57 | 58 | if (up == true) 59 | ans = ft_nbr_base_len(nbr, "0123456789ABCDEF"); 60 | else 61 | ans = ft_nbr_base_len(nbr, "0123456789abcdef"); 62 | ft_putstr_len(ans, len); 63 | free(ans); 64 | } 65 | 66 | void ft_putpointer_len(uintptr_t pointer, int *len) 67 | { 68 | char ans[25]; 69 | int i; 70 | char *base; 71 | 72 | base = "0123456789abcdef"; 73 | i = 0; 74 | ft_putstr_len("0x", len); 75 | if (pointer == 0) 76 | { 77 | ft_putchar_len('0', len); 78 | return ; 79 | } 80 | while (pointer != 0) 81 | { 82 | ans[i++] = base[pointer % 16]; 83 | pointer /= 16; 84 | } 85 | while (i--) 86 | ft_putchar_len(ans[i], len); 87 | } 88 | -------------------------------------------------------------------------------- /lib/libft/src/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 11:43:18 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 10:09:26 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | #include 15 | 16 | char *extract_and_ret(char *str, int mode, char *remain_str) 17 | { 18 | int index; 19 | char *extract; 20 | 21 | index = gnl_strchr(str, '\n'); 22 | if (index != -1 && index != BUFFER_SIZE - 1) 23 | { 24 | if (mode == 1 || mode == 2) 25 | { 26 | extract = gnl_substr(str, index + 1, gnl_strlen(str) + 1); 27 | if (remain_str != NULL && gnl_strlen(remain_str) > 0 && mode == 1) 28 | extract = gnl_strjoin(remain_str, extract); 29 | free(remain_str); 30 | return (extract); 31 | } 32 | else 33 | return (gnl_substr(str, 0, index + 1)); 34 | } 35 | else if (gnl_strlen(str) > 0 && mode == 0) 36 | return (gnl_substr(str, 0, gnl_strlen(str) + 1)); 37 | else 38 | { 39 | if (mode == 2) 40 | free(str); 41 | return (NULL); 42 | } 43 | } 44 | 45 | char *get_next_line(int fd) 46 | { 47 | int read_size; 48 | char *buffer; 49 | static char *remain[1024]; 50 | char *buff_all; 51 | 52 | if (fd < 0 || BUFFER_SIZE <= 0) 53 | return (false); 54 | read_size = 1; 55 | buffer = malloc((BUFFER_SIZE + 1) * sizeof(char)); 56 | buff_all = extract_and_ret(remain[fd], 0, remain[fd]); 57 | remain[fd] = extract_and_ret(remain[fd], 2, remain[fd]); 58 | while (gnl_strchr(buff_all, '\n') == -1 && read_size != 0) 59 | { 60 | read_size = read(fd, buffer, BUFFER_SIZE); 61 | if (read_size <= 0) 62 | break ; 63 | buffer[read_size] = '\0'; 64 | buff_all = gnl_strjoin(buff_all, extract_and_ret(buffer, 0, 65 | remain[fd])); 66 | remain[fd] = extract_and_ret(buffer, 1, remain[fd]); 67 | } 68 | free(buffer); 69 | return (buff_all); 70 | } 71 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 11:43:18 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 10:09:26 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | #include 15 | 16 | char *extract_and_ret(char *str, int mode, char *remain_str) 17 | { 18 | int index; 19 | char *extract; 20 | 21 | index = gnl_strchr(str, '\n'); 22 | if (index != -1 && index != BUFFER_SIZE - 1) 23 | { 24 | if (mode == 1 || mode == 2) 25 | { 26 | extract = gnl_substr(str, index + 1, gnl_strlen(str) + 1); 27 | if (remain_str != NULL && gnl_strlen(remain_str) > 0 && mode == 1) 28 | extract = gnl_strjoin(remain_str, extract); 29 | free(remain_str); 30 | return (extract); 31 | } 32 | else 33 | return (gnl_substr(str, 0, index + 1)); 34 | } 35 | else if (gnl_strlen(str) > 0 && mode == 0) 36 | return (gnl_substr(str, 0, gnl_strlen(str) + 1)); 37 | else 38 | { 39 | if (mode == 2) 40 | free(str); 41 | return (NULL); 42 | } 43 | } 44 | 45 | char *get_next_line(int fd) 46 | { 47 | int read_size; 48 | char *buffer; 49 | static char *remain[1024]; 50 | char *buff_all; 51 | 52 | if (fd < 0 || BUFFER_SIZE <= 0) 53 | return (false); 54 | read_size = 1; 55 | buffer = malloc((BUFFER_SIZE + 1) * sizeof(char)); 56 | buff_all = extract_and_ret(remain[fd], 0, remain[fd]); 57 | remain[fd] = extract_and_ret(remain[fd], 2, remain[fd]); 58 | while (gnl_strchr(buff_all, '\n') == -1 && read_size != 0) 59 | { 60 | read_size = read(fd, buffer, BUFFER_SIZE); 61 | if (read_size <= 0) 62 | break ; 63 | buffer[read_size] = '\0'; 64 | buff_all = gnl_strjoin(buff_all, extract_and_ret(buffer, 0, 65 | remain[fd])); 66 | remain[fd] = extract_and_ret(buffer, 1, remain[fd]); 67 | } 68 | free(buffer); 69 | return (buff_all); 70 | } 71 | -------------------------------------------------------------------------------- /lib/libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sinlee +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/07/16 04:12:09 by sinlee #+# #+# # 9 | # Updated: 2023/07/17 14:16:20 by sinlee ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | SRCS_DIR = src/ 14 | SRCS = ft_atoi.c \ 15 | ft_bzero.c \ 16 | ft_calloc.c \ 17 | ft_isalnum.c \ 18 | ft_isalpha.c \ 19 | ft_isascii.c \ 20 | ft_isdigit.c \ 21 | ft_isprint.c \ 22 | ft_itoa.c \ 23 | ft_memchr.c \ 24 | ft_memcmp.c \ 25 | ft_memcpy.c \ 26 | ft_memmove.c \ 27 | ft_memset.c \ 28 | ft_putchar_fd.c \ 29 | ft_putendl_fd.c \ 30 | ft_putnbr_fd.c \ 31 | ft_putstr_fd.c \ 32 | ft_split.c \ 33 | ft_strchr.c \ 34 | ft_strdup.c \ 35 | ft_strjoin.c \ 36 | ft_strlcat.c \ 37 | ft_strlcpy.c \ 38 | ft_strlen.c \ 39 | ft_strmapi.c \ 40 | ft_strncmp.c \ 41 | ft_strnstr.c \ 42 | ft_strrchr.c \ 43 | ft_strtrim.c \ 44 | ft_substr.c \ 45 | ft_striteri.c \ 46 | ft_tolower.c \ 47 | ft_toupper.c \ 48 | get_next_line.c \ 49 | get_next_line_utils.c 50 | 51 | SRCS_B = ft_lstadd_back.c \ 52 | ft_lstadd_front.c \ 53 | ft_lstclear.c \ 54 | ft_lstdelone.c \ 55 | ft_lstiter.c \ 56 | ft_lstlast.c \ 57 | ft_lstmap.c \ 58 | ft_lstnew.c \ 59 | ft_lstsize.c 60 | 61 | 62 | CFILES = $(addprefix $(SRCS_DIR), $(SRCS)) 63 | CFILES_B = $(addprefix $(SRCS_DIR), $(SRCS_B)) 64 | 65 | OBJS = ${CFILES:.c=.o} 66 | OBJS_B = ${CFILES_B:.c=.o} 67 | INCS = ./includes 68 | NAME = libft.a 69 | LIBC = ar rcs 70 | LIBR = ranlib 71 | CC = gcc 72 | RM = rm -f 73 | CFLAGS = -Wall -Wextra -Werror -I$(INCS) 74 | 75 | 76 | all: ${NAME} 77 | 78 | $(NAME): ${OBJS} ${OBJS_B} 79 | ${LIBC} $(NAME) ${OBJS} ${OBJS_B} 80 | 81 | #for 42 bonus 82 | bonus: ${OBJS} ${OBJS_B} 83 | ${LIBC} $(NAME) ${OBJS_B} 84 | 85 | clean: 86 | ${RM} ${OBJS} ${OBJS_B} 87 | 88 | fclean: clean 89 | ${RM} $(NAME) $(bonus) 90 | 91 | re: fclean all 92 | 93 | .PHONY: all bonus clean fclean re -------------------------------------------------------------------------------- /lib/ft_printf/libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sinlee +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/07/16 04:12:09 by sinlee #+# #+# # 9 | # Updated: 2023/07/17 14:16:52 by sinlee ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | SRCS_DIR = src/ 14 | SRCS = ft_atoi.c \ 15 | ft_bzero.c \ 16 | ft_calloc.c \ 17 | ft_isalnum.c \ 18 | ft_isalpha.c \ 19 | ft_isascii.c \ 20 | ft_isdigit.c \ 21 | ft_isprint.c \ 22 | ft_itoa.c \ 23 | ft_memchr.c \ 24 | ft_memcmp.c \ 25 | ft_memcpy.c \ 26 | ft_memmove.c \ 27 | ft_memset.c \ 28 | ft_putchar_fd.c \ 29 | ft_putendl_fd.c \ 30 | ft_putnbr_fd.c \ 31 | ft_putstr_fd.c \ 32 | ft_split.c \ 33 | ft_strchr.c \ 34 | ft_strdup.c \ 35 | ft_strjoin.c \ 36 | ft_strlcat.c \ 37 | ft_strlcpy.c \ 38 | ft_strlen.c \ 39 | ft_strmapi.c \ 40 | ft_strncmp.c \ 41 | ft_strnstr.c \ 42 | ft_strrchr.c \ 43 | ft_strtrim.c \ 44 | ft_substr.c \ 45 | ft_striteri.c \ 46 | ft_tolower.c \ 47 | ft_toupper.c \ 48 | get_next_line.c \ 49 | get_next_line_utils.c 50 | 51 | SRCS_B = ft_lstadd_back.c \ 52 | ft_lstadd_front.c \ 53 | ft_lstclear.c \ 54 | ft_lstdelone.c \ 55 | ft_lstiter.c \ 56 | ft_lstlast.c \ 57 | ft_lstmap.c \ 58 | ft_lstnew.c \ 59 | ft_lstsize.c 60 | 61 | 62 | CFILES = $(addprefix $(SRCS_DIR), $(SRCS)) 63 | CFILES_B = $(addprefix $(SRCS_DIR), $(SRCS_B)) 64 | 65 | OBJS = ${CFILES:.c=.o} 66 | OBJS_B = ${CFILES_B:.c=.o} 67 | INCS = ./includes 68 | NAME = libft.a 69 | LIBC = ar rcs 70 | LIBR = ranlib 71 | CC = gcc 72 | RM = rm -f 73 | CFLAGS = -Wall -Wextra -Werror -I$(INCS) 74 | 75 | 76 | all: ${NAME} 77 | 78 | $(NAME): ${OBJS} ${OBJS_B} 79 | ${LIBC} $(NAME) ${OBJS} ${OBJS_B} 80 | 81 | #for 42 bonus 82 | bonus: ${OBJS} ${OBJS_B} 83 | ${LIBC} $(NAME) ${OBJS_B} 84 | 85 | clean: 86 | ${RM} ${OBJS} ${OBJS_B} 87 | 88 | fclean: clean 89 | ${RM} $(NAME) $(bonus) 90 | 91 | re: fclean all 92 | 93 | .PHONY: all bonus clean fclean re -------------------------------------------------------------------------------- /srcs/algorithm/sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/13 11:59:58 by codespace #+# #+# */ 9 | /* Updated: 2023/07/16 04:11:09 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | // identify the position of the target in stack_a 16 | int find_target(t_node *stack_from, t_node *stack_to) 17 | { 18 | t_node *tmp; 19 | int target; 20 | 21 | tmp = last_first_node(stack_to, false); 22 | target = tmp->content; 23 | if (stack_from->content > min_max_pos(stack_to, true, false)) 24 | return (min_max_pos(stack_to, false, true)); 25 | while (tmp) 26 | { 27 | if ((tmp->content < target && tmp->content > stack_from->content) 28 | || (tmp->content > stack_from->content 29 | && target < stack_from->content)) 30 | target = tmp->content; 31 | tmp = tmp->next; 32 | } 33 | return (node_index(stack_to, target)); 34 | } 35 | 36 | void target_push(t_node *stack, int pos) 37 | { 38 | int len; 39 | 40 | stack = last_first_node(stack, false); 41 | len = stack_len(stack); 42 | if (pos <= len / 2) 43 | { 44 | while (pos--) 45 | execute(&stack, NULL, "ra", false); 46 | } 47 | else 48 | { 49 | pos = len - pos; 50 | while (pos--) 51 | execute(&stack, NULL, "rra", false); 52 | } 53 | } 54 | 55 | void exec_smt(t_node **stack_a, t_node **stack_b, int pos[2], int mode) 56 | { 57 | if (mode == 0) 58 | { 59 | multi_execute(stack_a, stack_b, "rr", min(pos[0], pos[1])); 60 | if (max(pos[0], pos[1]) == pos[0] && pos[0] != pos[1]) 61 | multi_execute(stack_a, stack_a, "ra", pos[0] - pos[1]); 62 | else if (max(pos[0], pos[1]) == pos[1] && pos[0] != pos[1]) 63 | multi_execute(stack_a, stack_b, "rb", pos[1] - pos[0]); 64 | } 65 | else if (mode == 1) 66 | { 67 | multi_execute(stack_a, stack_b, "rrr", min(pos[0], pos[1])); 68 | if (max(pos[0], pos[1]) == pos[0] && pos[0] != pos[1]) 69 | multi_execute(stack_a, stack_a, "rra", pos[0] - pos[1]); 70 | else if (max(pos[0], pos[1]) == pos[1] && pos[0] != pos[1] 71 | && pos[1] != 0) 72 | multi_execute(stack_a, stack_b, "rrb", pos[1] - pos[0]); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /lib/libft/src/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 11:43:22 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 10:08:41 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | #include 15 | 16 | size_t gnl_strlen(const char *str) 17 | { 18 | size_t len; 19 | 20 | len = 0; 21 | if (str == 0) 22 | return (0); 23 | while (*str++) 24 | len++; 25 | return (len); 26 | } 27 | 28 | int gnl_strchr(char *s, char c) 29 | { 30 | int i; 31 | 32 | i = 0; 33 | if (!s) 34 | return (-1); 35 | while (s[i]) 36 | { 37 | if (s[i] == c) 38 | return (i); 39 | i++; 40 | } 41 | return (-1); 42 | } 43 | 44 | size_t gnl_strlcpy(char *dest, const char *src, size_t size) 45 | { 46 | size_t src_len; 47 | size_t i; 48 | 49 | src_len = gnl_strlen(src); 50 | if (size == 0) 51 | return (src_len); 52 | i = 0; 53 | while (i < size - 1 && src[i]) 54 | { 55 | dest[i] = src[i]; 56 | ++i; 57 | } 58 | dest[i] = '\0'; 59 | return (src_len); 60 | } 61 | 62 | char *gnl_strjoin(char *s1, char *s2) 63 | { 64 | char *str; 65 | int i; 66 | 67 | i = -1; 68 | str = malloc(sizeof(char) * (gnl_strlen(s1) + gnl_strlen(s2) + 1)); 69 | if (!str) 70 | return (0); 71 | if (s1) 72 | { 73 | while (s1[++i]) 74 | str[i] = s1[i]; 75 | i = -1; 76 | } 77 | if (s2) 78 | { 79 | while (s2[++i]) 80 | str[gnl_strlen(s1) + i] = s2[i]; 81 | } 82 | free(s2); 83 | str[gnl_strlen(s1) + i] = '\0'; 84 | free(s1); 85 | return (str); 86 | } 87 | 88 | char *gnl_substr(char *s, unsigned int start, size_t len) 89 | { 90 | char *substr; 91 | 92 | if (start > gnl_strlen(s)) 93 | { 94 | substr = malloc(sizeof(char) * 1); 95 | substr[0] = '\0'; 96 | } 97 | else 98 | { 99 | if (len > gnl_strlen(s + start)) 100 | len = gnl_strlen(s + start); 101 | if (len == 0) 102 | return (NULL); 103 | substr = malloc(sizeof(char) * (len + 1)); 104 | if (substr == 0) 105 | return (NULL); 106 | else 107 | gnl_strlcpy(substr, s + start, len + 1); 108 | } 109 | return (substr); 110 | } 111 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/15 11:43:22 by codespace #+# #+# */ 9 | /* Updated: 2023/07/17 10:08:41 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | #include 15 | 16 | size_t gnl_strlen(const char *str) 17 | { 18 | size_t len; 19 | 20 | len = 0; 21 | if (str == 0) 22 | return (0); 23 | while (*str++) 24 | len++; 25 | return (len); 26 | } 27 | 28 | int gnl_strchr(char *s, char c) 29 | { 30 | int i; 31 | 32 | i = 0; 33 | if (!s) 34 | return (-1); 35 | while (s[i]) 36 | { 37 | if (s[i] == c) 38 | return (i); 39 | i++; 40 | } 41 | return (-1); 42 | } 43 | 44 | size_t gnl_strlcpy(char *dest, const char *src, size_t size) 45 | { 46 | size_t src_len; 47 | size_t i; 48 | 49 | src_len = gnl_strlen(src); 50 | if (size == 0) 51 | return (src_len); 52 | i = 0; 53 | while (i < size - 1 && src[i]) 54 | { 55 | dest[i] = src[i]; 56 | ++i; 57 | } 58 | dest[i] = '\0'; 59 | return (src_len); 60 | } 61 | 62 | char *gnl_strjoin(char *s1, char *s2) 63 | { 64 | char *str; 65 | int i; 66 | 67 | i = -1; 68 | str = malloc(sizeof(char) * (gnl_strlen(s1) + gnl_strlen(s2) + 1)); 69 | if (!str) 70 | return (0); 71 | if (s1) 72 | { 73 | while (s1[++i]) 74 | str[i] = s1[i]; 75 | i = -1; 76 | } 77 | if (s2) 78 | { 79 | while (s2[++i]) 80 | str[gnl_strlen(s1) + i] = s2[i]; 81 | } 82 | free(s2); 83 | str[gnl_strlen(s1) + i] = '\0'; 84 | free(s1); 85 | return (str); 86 | } 87 | 88 | char *gnl_substr(char *s, unsigned int start, size_t len) 89 | { 90 | char *substr; 91 | 92 | if (start > gnl_strlen(s)) 93 | { 94 | substr = malloc(sizeof(char) * 1); 95 | substr[0] = '\0'; 96 | } 97 | else 98 | { 99 | if (len > gnl_strlen(s + start)) 100 | len = gnl_strlen(s + start); 101 | if (len == 0) 102 | return (NULL); 103 | substr = malloc(sizeof(char) * (len + 1)); 104 | if (substr == 0) 105 | return (NULL); 106 | else 107 | gnl_strlcpy(substr, s + start, len + 1); 108 | } 109 | return (substr); 110 | } 111 | -------------------------------------------------------------------------------- /lib/libft/src/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/13 14:51:39 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:15:30 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static bool check_seperator(char c_str, char c) 16 | { 17 | if (c_str == c) 18 | return (true); 19 | return (false); 20 | } 21 | 22 | static int count_strings(char *str, char c) 23 | { 24 | int count; 25 | 26 | count = 0; 27 | while (*str) 28 | { 29 | while (*str && check_seperator(*str, c)) 30 | str++; 31 | if (*str) 32 | count++; 33 | while (*str && !check_seperator(*str, c)) 34 | str++; 35 | } 36 | return (count); 37 | } 38 | 39 | // count string before character c 40 | static int ft_strclen(char *str, char c) 41 | { 42 | int i; 43 | 44 | i = 0; 45 | while (str[i] && !check_seperator(str[i], c)) 46 | i++; 47 | return (i); 48 | } 49 | 50 | static char *word(char *str, char c) 51 | { 52 | int i; 53 | int len; 54 | char *word; 55 | 56 | i = 0; 57 | len = ft_strclen(str, c); 58 | word = (char *)malloc((len + 1) * 1); 59 | while (i < len) 60 | { 61 | word[i] = str[i]; 62 | i++; 63 | } 64 | word[i] = '\0'; 65 | return (word); 66 | } 67 | 68 | char **ft_split(char const *s, char c) 69 | { 70 | char **arr; 71 | char *str; 72 | int i; 73 | 74 | i = 0; 75 | if (!s) 76 | return (0); 77 | str = (char *)s; 78 | arr = (char **)malloc((count_strings(str, c) + 1) * 8); 79 | while (*str) 80 | { 81 | while (*str && check_seperator(*str, c)) 82 | str++; 83 | if (*str) 84 | { 85 | arr[i] = word(str, c); 86 | i++; 87 | } 88 | while (*str && !check_seperator(*str, c)) 89 | str++; 90 | } 91 | arr[i] = 0; 92 | return (arr); 93 | } 94 | 95 | // #include 96 | // int main(int argc, char **argv) 97 | // { 98 | // int index; 99 | // char **split; 100 | 101 | // split = ft_split(argv[1], argv[2][0]); 102 | // index = 0; 103 | // printf("tab start\n"); 104 | // while (split[index]) 105 | // { 106 | // printf("tab[%d]: $%s$\n", index, split[index]); 107 | // fflush(stdout); 108 | // index++; 109 | // } 110 | // printf("tab end\n"); 111 | // } 112 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/src/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/13 14:51:39 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 15:15:30 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static bool check_seperator(char c_str, char c) 16 | { 17 | if (c_str == c) 18 | return (true); 19 | return (false); 20 | } 21 | 22 | static int count_strings(char *str, char c) 23 | { 24 | int count; 25 | 26 | count = 0; 27 | while (*str) 28 | { 29 | while (*str && check_seperator(*str, c)) 30 | str++; 31 | if (*str) 32 | count++; 33 | while (*str && !check_seperator(*str, c)) 34 | str++; 35 | } 36 | return (count); 37 | } 38 | 39 | // count string before character c 40 | static int ft_strclen(char *str, char c) 41 | { 42 | int i; 43 | 44 | i = 0; 45 | while (str[i] && !check_seperator(str[i], c)) 46 | i++; 47 | return (i); 48 | } 49 | 50 | static char *word(char *str, char c) 51 | { 52 | int i; 53 | int len; 54 | char *word; 55 | 56 | i = 0; 57 | len = ft_strclen(str, c); 58 | word = (char *)malloc((len + 1) * 1); 59 | while (i < len) 60 | { 61 | word[i] = str[i]; 62 | i++; 63 | } 64 | word[i] = '\0'; 65 | return (word); 66 | } 67 | 68 | char **ft_split(char const *s, char c) 69 | { 70 | char **arr; 71 | char *str; 72 | int i; 73 | 74 | i = 0; 75 | if (!s) 76 | return (0); 77 | str = (char *)s; 78 | arr = (char **)malloc((count_strings(str, c) + 1) * 8); 79 | while (*str) 80 | { 81 | while (*str && check_seperator(*str, c)) 82 | str++; 83 | if (*str) 84 | { 85 | arr[i] = word(str, c); 86 | i++; 87 | } 88 | while (*str && !check_seperator(*str, c)) 89 | str++; 90 | } 91 | arr[i] = 0; 92 | return (arr); 93 | } 94 | 95 | // #include 96 | // int main(int argc, char **argv) 97 | // { 98 | // int index; 99 | // char **split; 100 | 101 | // split = ft_split(argv[1], argv[2][0]); 102 | // index = 0; 103 | // printf("tab start\n"); 104 | // while (split[index]) 105 | // { 106 | // printf("tab[%d]: $%s$\n", index, split[index]); 107 | // fflush(stdout); 108 | // index++; 109 | // } 110 | // printf("tab end\n"); 111 | // } 112 | -------------------------------------------------------------------------------- /srcs/stack/stack_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/04 05:29:26 by codespace #+# #+# */ 9 | /* Updated: 2023/07/16 04:11:06 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void print_stack(t_node *stack, char *str, bool advanced, bool to_first) 16 | { 17 | t_node *tmp; 18 | 19 | if (to_first == true) 20 | tmp = last_first_node(stack, false); 21 | else 22 | tmp = stack; 23 | ft_printf("----Printing Stack %s (Top to Down)-------\n", str); 24 | while (tmp) 25 | { 26 | ft_printf("%d ", tmp->content); 27 | if (advanced) 28 | { 29 | if (tmp->prev) 30 | ft_printf("Previous: %d ", tmp->prev->content); 31 | else 32 | ft_printf("Previous: NULL "); 33 | if (tmp->next) 34 | ft_printf("Next: %d\n", tmp->next->content); 35 | else 36 | ft_printf("Next: NULL\n"); 37 | } 38 | tmp = tmp->next; 39 | } 40 | ft_printf("\n"); 41 | } 42 | 43 | t_node *create_node(int content) 44 | { 45 | t_node *node; 46 | 47 | node = (t_node *)malloc(sizeof(t_node)); 48 | if (node == NULL) 49 | return (NULL); 50 | node->content = content; 51 | node->prev = NULL; 52 | node->next = NULL; 53 | return (node); 54 | } 55 | 56 | void clear_lst_node(t_node *node) 57 | { 58 | t_node *temp; 59 | 60 | if (!node) 61 | return ; 62 | node = last_first_node(node, false); 63 | while (node) 64 | { 65 | temp = node->next; 66 | free(node); 67 | node = temp; 68 | } 69 | } 70 | 71 | t_node *add_node(t_node *node, int content) 72 | { 73 | t_node *new; 74 | 75 | new = create_node(content); 76 | if (!new) 77 | return (NULL); 78 | if (!node) 79 | return (new); 80 | node->next = new; 81 | new->prev = node; 82 | return (new); 83 | } 84 | 85 | // function that returns either last or first node, 86 | // depending on the 'is_last' variable. if is_last == true, is last node 87 | t_node *last_first_node(t_node *node, bool is_last) 88 | { 89 | if (!node) 90 | return (NULL); 91 | if (!is_last) 92 | { 93 | while (node->prev) 94 | node = node->prev; 95 | return (node); 96 | } 97 | else 98 | { 99 | while (node->next) 100 | node = node->next; 101 | return (node); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /lib/libft/includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 09:52:13 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:08:47 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | typedef struct s_list 24 | { 25 | void *content; 26 | struct s_list *next; 27 | } t_list; 28 | 29 | int ft_isalpha(int c); 30 | int ft_isdigit(int c); 31 | int ft_isalnum(int c); 32 | int ft_isascii(int c); 33 | int ft_isprint(int c); 34 | size_t ft_strlen(const char *str); 35 | void *ft_memset(void *ptr, int value, size_t num); 36 | void ft_bzero(void *s, size_t n); 37 | void *ft_memcpy(void *dst, const void *src, size_t n); 38 | void *ft_memmove(void *dest, const void *src, size_t n); 39 | size_t ft_strlcpy(char *dest, const char *src, size_t size); 40 | size_t ft_strlcat(char *dest, const char *src, size_t size); 41 | int ft_toupper(int c); 42 | int ft_tolower(int c); 43 | char *ft_strchr(const char *s, int c); 44 | char *ft_strrchr(const char *s, int c); 45 | int ft_strncmp(const char *s1, const char *s2, size_t n); 46 | void *ft_memchr(const void *s, int c, size_t n); 47 | int ft_memcmp(const void *s1, const void *s2, size_t n); 48 | char *ft_strnstr(const char *haystack, const char *needle, size_t len); 49 | int ft_atoi(const char *str); 50 | void *ft_calloc(size_t count, size_t size); 51 | char *ft_strdup(const char *s1); 52 | char *ft_substr(char const *s, unsigned int start, size_t len); 53 | char *ft_strjoin(char const *s1, char const *s2); 54 | char *ft_strtrim(char const *s1, char const *set); 55 | char **ft_split(char const *s, char c); 56 | char *ft_itoa(int n); 57 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 58 | void ft_striteri(char *s, void (*f)(unsigned int, char*)); 59 | void ft_putchar_fd(char c, int fd); 60 | void ft_putstr_fd(char *s, int fd); 61 | void ft_putendl_fd(char *s, int fd); 62 | void ft_putnbr_fd(int n, int fd); 63 | t_list *ft_lstnew(void *content); 64 | void ft_lstadd_front(t_list **lst, t_list *new); 65 | int ft_lstsize(t_list *lst); 66 | t_list *ft_lstlast(t_list *lst); 67 | void ft_lstadd_back(t_list **lst, t_list *new); 68 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 69 | void ft_lstclear(t_list **lst, void (*del)(void *)); 70 | void ft_lstiter(t_list *lst, void (*f)(void *)); 71 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 72 | #endif 73 | -------------------------------------------------------------------------------- /lib/ft_printf/libft/includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/04/28 09:52:13 by sinlee #+# #+# */ 9 | /* Updated: 2023/05/05 17:08:47 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | typedef struct s_list 24 | { 25 | void *content; 26 | struct s_list *next; 27 | } t_list; 28 | 29 | int ft_isalpha(int c); 30 | int ft_isdigit(int c); 31 | int ft_isalnum(int c); 32 | int ft_isascii(int c); 33 | int ft_isprint(int c); 34 | size_t ft_strlen(const char *str); 35 | void *ft_memset(void *ptr, int value, size_t num); 36 | void ft_bzero(void *s, size_t n); 37 | void *ft_memcpy(void *dst, const void *src, size_t n); 38 | void *ft_memmove(void *dest, const void *src, size_t n); 39 | size_t ft_strlcpy(char *dest, const char *src, size_t size); 40 | size_t ft_strlcat(char *dest, const char *src, size_t size); 41 | int ft_toupper(int c); 42 | int ft_tolower(int c); 43 | char *ft_strchr(const char *s, int c); 44 | char *ft_strrchr(const char *s, int c); 45 | int ft_strncmp(const char *s1, const char *s2, size_t n); 46 | void *ft_memchr(const void *s, int c, size_t n); 47 | int ft_memcmp(const void *s1, const void *s2, size_t n); 48 | char *ft_strnstr(const char *haystack, const char *needle, size_t len); 49 | int ft_atoi(const char *str); 50 | void *ft_calloc(size_t count, size_t size); 51 | char *ft_strdup(const char *s1); 52 | char *ft_substr(char const *s, unsigned int start, size_t len); 53 | char *ft_strjoin(char const *s1, char const *s2); 54 | char *ft_strtrim(char const *s1, char const *set); 55 | char **ft_split(char const *s, char c); 56 | char *ft_itoa(int n); 57 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 58 | void ft_striteri(char *s, void (*f)(unsigned int, char*)); 59 | void ft_putchar_fd(char c, int fd); 60 | void ft_putstr_fd(char *s, int fd); 61 | void ft_putendl_fd(char *s, int fd); 62 | void ft_putnbr_fd(int n, int fd); 63 | t_list *ft_lstnew(void *content); 64 | void ft_lstadd_front(t_list **lst, t_list *new); 65 | int ft_lstsize(t_list *lst); 66 | t_list *ft_lstlast(t_list *lst); 67 | void ft_lstadd_back(t_list **lst, t_list *new); 68 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 69 | void ft_lstclear(t_list **lst, void (*del)(void *)); 70 | void ft_lstiter(t_list *lst, void (*f)(void *)); 71 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 72 | #endif 73 | -------------------------------------------------------------------------------- /srcs/algorithm/sort_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sort_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/05 08:37:28 by codespace #+# #+# */ 9 | /* Updated: 2023/07/16 04:11:10 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | bool is_sorted(t_node *stack, bool reverse) 16 | { 17 | t_node *tmp; 18 | 19 | tmp = last_first_node(stack, false); 20 | if (stack_len(stack) == 0 || !stack) 21 | return (false); 22 | while (tmp->next) 23 | { 24 | if (tmp->content > tmp->next->content && reverse == false) 25 | return (false); 26 | if (tmp->content < tmp->next->content && reverse == true) 27 | return (false); 28 | tmp = tmp->next; 29 | } 30 | return (true); 31 | } 32 | 33 | // retrieve the first index found that matches the target 34 | int node_index(t_node *stack, int target) 35 | { 36 | t_node *tmp; 37 | int index; 38 | 39 | index = 0; 40 | tmp = last_first_node(stack, false); 41 | if (stack_len(stack) == 0 || !stack) 42 | return (-1); 43 | while (tmp) 44 | { 45 | if (tmp->content == target) 46 | return (index); 47 | tmp = tmp->next; 48 | index++; 49 | } 50 | return (-1); 51 | } 52 | 53 | // attr[0] = traverse_index, attr[1] = target_index 54 | int min_max_pos(t_node *stack, bool max, bool pos) 55 | { 56 | int target; 57 | int attr[2]; 58 | t_node *tmp; 59 | 60 | tmp = last_first_node(stack, false); 61 | attr[0] = -1; 62 | attr[1] = 0; 63 | target = tmp->content; 64 | while (++attr[0] < stack_len(stack)) 65 | { 66 | if ((tmp->content > target && max == true) || (tmp->content < target 67 | && max == false)) 68 | { 69 | target = tmp->content; 70 | attr[1] = attr[0]; 71 | } 72 | tmp = tmp->next; 73 | } 74 | if (pos == true) 75 | return (attr[1]); 76 | else 77 | return (target); 78 | } 79 | 80 | void min_max_push(t_node *stack, bool max) 81 | { 82 | int pos; 83 | 84 | pos = min_max_pos(stack, max, true); 85 | target_push(stack, pos); 86 | } 87 | 88 | int find_min_index(t_node *stack_a, t_node *stack_b, int len) 89 | { 90 | int min; 91 | int index[2]; 92 | t_node *tmp; 93 | 94 | tmp = last_first_node(stack_a, false); 95 | min = execute_calc(stack_a, stack_b, len, false); 96 | index[0] = 0; 97 | index[1] = 0; 98 | while (tmp) 99 | { 100 | if (execute_calc(tmp, stack_b, len, false) < min) 101 | { 102 | min = execute_calc(tmp, stack_b, len, false); 103 | index[1] = index[0]; 104 | } 105 | tmp = tmp->next; 106 | index[0]++; 107 | } 108 | return (index[1]); 109 | } 110 | -------------------------------------------------------------------------------- /includes/push_swap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* push_swap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/04 02:39:31 by sinlee #+# #+# */ 9 | /* Updated: 2023/07/16 04:11:27 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef PUSH_SWAP_H 14 | # define PUSH_SWAP_H 15 | 16 | # include "ft_printf.h" 17 | # include "libft.h" 18 | # include 19 | 20 | typedef struct s_node 21 | { 22 | int content; 23 | struct s_node *prev; 24 | struct s_node *next; 25 | } t_node; 26 | 27 | // Stack Utilties 28 | t_node *last_first_node(t_node *node, bool is_last); 29 | t_node *add_node(t_node *node, int content); 30 | t_node *create_node(int content); 31 | void clear_lst_node(t_node *node); 32 | void print_stack(t_node *stack, char *str, bool advanced, 33 | bool to_first); 34 | int stack_len(t_node *stack); 35 | int min(int a, int b); 36 | int max(int a, int b); 37 | 38 | // push swap operations 39 | bool swap(t_node **stack); 40 | bool push(t_node **stack_from, t_node **stack_to); 41 | bool rotate(t_node **stack, bool reverse); 42 | bool execute(t_node **stack_a, t_node **stack_b, char *line, 43 | bool s_print); 44 | int execute_calc(t_node *stack_a, t_node *stack_b, int len, 45 | bool return_pos_b); 46 | int reverse_calc(t_node *stack_a, t_node *stack_b, int len, 47 | bool return_pos_b); 48 | bool multi_execute(t_node **stack_a, t_node **stack_b, 49 | char *line, int n); 50 | void check_exit_else_exec(t_node **stack_a, t_node **stack_b, 51 | char *line); 52 | void exec_smt(t_node **stack_a, t_node **stack_b, int pos[2], 53 | int mode); 54 | void execute_ps(t_node **stack_a, t_node **stack_b, int pos[2], 55 | int mode); 56 | void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]); 57 | 58 | // sorting Utilities 59 | void push_swap(t_node *stack_a, t_node *stack_b, int len); 60 | bool is_sorted(t_node *stack, bool reverse); 61 | int min_max_pos(t_node *stack, bool max, bool pos); 62 | int find_target(t_node *stack_from, t_node *stack_to); 63 | void target_push(t_node *stack, int pos); 64 | void min_max_push(t_node *stack, bool max); 65 | int calc(t_node *stack_a, t_node *stack_b, int len, 66 | bool return_pos_b); 67 | int lcm(int pos[2], int len_a, int len_b, bool return_move); 68 | int node_index(t_node *stack, int target); 69 | int find_min_index(t_node *stack_a, t_node *stack_b, int len); 70 | void sort_three(t_node *stack_a); 71 | void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]); 72 | 73 | // generic Utilities 74 | int ft_strcmp(const char *s1, const char *s2); 75 | void ft_error(t_node *stack_a); 76 | int ft_atoi_ps(char *str, t_node *stack_a); 77 | bool is_valid(t_node *stack_a, char *str); 78 | 79 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Push Swap: The most efficient yet inefficient algorithm.

2 |

3 | 4 |

5 |

6 | Code language count 7 | GitHub top language 8 |
9 | A journey to sort arrays efficiently. 10 |

11 | 12 | **Disclaimer: This implementation is purely for personal entertainment, by no means it's suitable for production use cases.** 13 | ## Description 14 | - Push Swap is a sorting algorithm implemented in C. It efficiently sorts a stack of integers using a series of predefined operations. 15 | - The algorithm aims to minimize the number of operations required to sort the stack, achieving an efficient sorting process. 16 | 17 | ## Implementation 18 | - The push_swap algorithm can be implemented using different data structures such as stacks (Last In First Out) or linked lists. For this project, a doubly linked list data structure is recommended for efficient stack manipulation. 19 | 20 | - Create a doubly linked list structure to represent stack nodes, with each node containing an integer value and pointers to the next and previous nodes. 21 | - Implement functions for stack initialization, stack operations (push, pop, swap, rotate, and reverse rotate), and stack sorting. 22 | - For my approach, I implemented a function to calculate the minimum number of moves required to push an element from stack A to stack B. This function will determine the optimal moves to push to stack B during the sorting process before the actual sorting operation begins. 23 | 24 | ## Sorting Algorithms 25 | - The push_swap algorithm uses different sorting techniques based on the number of elements in the stack: 26 | - A sorting algorithm based on permutations is used for small stack sizes (typically 3 or less). This involves checking all possible permutations of the stack and determining the minimum number of operations required to sort it. 27 | - For larger stack sizes, more efficient sorting algorithms such as quicksort or mergesort can be implemented. These algorithms divide the stack into smaller sub-stacks, sort them recursively, and then combine them to achieve the final sorted order. 28 | 29 | ## Usage 30 | - To compile the push_swap program, run: 31 | ``` 32 | make 33 | ``` 34 | - To use the push_swap program, provide a series of integers as arguments: 35 | ``` 36 | ./push_swap 37 | ``` 38 | For example: 39 | ``` 40 | ./push_swap 5 2 7 1 4 41 | ``` 42 | - The program will output a list of operations to sort the input stack. A two/three-letter code represents each operation. The goal is to minimize the number of operations required to sort the stack efficiently. 43 | 44 | ## Resources 45 | To get started with this project, you can refer to the following resources: 46 | - Doubly Linked List in C: [GeeksforGeeks](https://www.geeksforgeeks.org/doubly-linked-list/) 47 | - Sorting Algorithms: [GeeksforGeeks - Sorting](https://www.geeksforgeeks.org/sorting-algorithms/) 48 | - Quicksort Algorithm: [Wikipedia](https://en.wikipedia.org/wiki/Quicksort) 49 | - Mergesort Algorithm: [Wikipedia](https://en.wikipedia.org/wiki/Merge_sort) 50 | 51 | Feel free to explore additional resources and implement further optimizations to enhance the efficiency of your push_swap algorithm. 52 | 53 | ## Author 54 | Created by Lee Sin Liang 55 | 56 | ## Acknowledgements 57 | This project was completed as part of the curriculum at 42 School. 58 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sinlee +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/07/16 04:09:49 by codespace #+# #+# # 9 | # Updated: 2023/07/17 14:37:20 by sinlee ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Compiler 14 | NAME = push_swap 15 | CC = gcc 16 | RM = rm -f 17 | INCLUDE = -Iincludes -Ilib/ft_printf/includes -Ilib/libft/includes 18 | CFLAGS = -Wall -Wextra -Werror -ggdb -fsanitize=address 19 | 20 | # Source files 21 | ALGO_DIR = algorithm 22 | STACK_DIR = stack 23 | MISC_DIR = misc 24 | BONUS = checker 25 | SRCS_DIR = srcs 26 | MAIN_FILES = main.c $(ALGO_DIR)/push_swap.c 27 | 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 28 | SRC_1 = $(addprefix $(SRCS_DIR)/,$(MAIN_FILES)) 29 | SRC_2 = $(addprefix $(SRCS_DIR)/,$(SRCS_FILES)) 30 | BONUS_SRC = srcs/checker.c 31 | 32 | # Object files 33 | OBJ_DIR = obj/ 34 | OBJ_1 = ${SRC_1:.c=.o} 35 | OBJ_2 = ${SRC_2:.c=.o} 36 | BONUS_OBJ =${BONUS_SRC:.c=.o} 37 | 38 | # Libraries 39 | LIBFT_DIR = lib/libft 40 | LIBFT = $(LIBFT_DIR)/libft.a 41 | 42 | PRINTF_DIR = lib/ft_printf 43 | PRINTF = $(PRINTF_DIR)/libftprintf.a 44 | 45 | LIBS = -L$(LIBFT_DIR) -L$(PRINTF_DIR) -lft -lftprintf 46 | 47 | # Colors and text formatting 48 | RESET = \033[0m 49 | BOLD = \033[1m 50 | DIM = \033[2m 51 | UNDERLINE = \033[4m 52 | BLINK = \033[5m 53 | INVERT = \033[7m 54 | LIGHT_BLUE = \033[94m 55 | YELLOW = \033[93m 56 | 57 | # Makefile rules 58 | # @${CC} -c $(CFLAGS) $(INCLUDE) $< -o ${<:.c=.o} 59 | .c.o: 60 | @echo "$(BOLD)$(YELLOW)Compiling $<...$(RESET)" 61 | @$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ 62 | 63 | ${NAME}: ${OBJ_1} ${OBJ_2} $(LIBFT) $(PRINTF) 64 | @echo "$(BOLD)$(LIGHT_BLUE)Linking objects...$(RESET)" 65 | @$(CC) $(CFLAGS) $(INCLUDES) ${OBJ_1} ${OBJ_2} $(LIBS) -o $(NAME) 66 | @echo "$(BOLD)$(LIGHT_BLUE)$(NAME) created successfully!$(RESET)" 67 | @echo "$(BOLD)Copyright Reserved. Lee Sin Liang.$(RESET)" 68 | 69 | ${BONUS}: ${OBJ_2} ${BONUS_OBJ} $(LIBFT) $(PRINTF) 70 | @echo "$(BOLD)$(LIGHT_BLUE)Linking objects...$(RESET)" 71 | @${CC} ${CFLAGS} ${BONUS_OBJ} ${OBJ_2} $(LIBS) -o ${BONUS} ${INCLUDE} 72 | @echo "$(BOLD)$(LIGHT_BLUE)$(NAME) created successfully!$(RESET)" 73 | @echo "$(BOLD)Copyright Reserved. Lee Sin Liang.$(RESET)" 74 | 75 | $(LIBFT): 76 | @echo "$(BOLD)$(LIGHT_BLUE)Building libft...$(RESET)" 77 | @make -C $(LIBFT_DIR) -s 78 | 79 | $(PRINTF): 80 | @echo "$(BOLD)$(LIGHT_BLUE)Building ft_printf...$(RESET)" 81 | @make -C $(PRINTF_DIR) -s 82 | 83 | all: ${NAME} 84 | 85 | bonus: ${BONUS} 86 | 87 | clean: 88 | @echo "$(BOLD)$(LIGHT_BLUE)Cleaning objects...$(RESET)" 89 | @${RM} ${OBJ_1} ${OBJ_2} ${BONUS_OBJ} ${NAME} ${BONUS} 90 | @make -C $(LIBFT_DIR) clean -s 91 | @make -C $(PRINTF_DIR) clean -s 92 | 93 | fclean: clean 94 | @echo "$(BOLD)$(LIGHT_BLUE)Cleaning $(NAME)...$(RESET)" 95 | @${RM} ${OBJ_1} ${OBJ_2} ${BONUS_OBJ} ${NAME} ${BONUS} 96 | @make -C $(LIBFT_DIR) fclean -s 97 | @make -C $(PRINTF_DIR) fclean -s 98 | 99 | re: clean all 100 | 101 | .PHONY: all bonus clean fclean re bonus 102 | -------------------------------------------------------------------------------- /srcs/stack/operations.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* operations.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sinlee +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/04 03:27:49 by sinlee #+# #+# */ 9 | /* Updated: 2023/07/17 11:18:47 by sinlee ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | // double pointer array to get mem address of node 16 | bool swap(t_node **stack) 17 | { 18 | t_node *tmp; 19 | 20 | *stack = last_first_node(*stack, false); 21 | if (stack == NULL || *stack == NULL || (*stack)->next == NULL) 22 | return (true); 23 | tmp = (*stack)->next; 24 | (*stack)->next = tmp->next; 25 | if (tmp->next != NULL) 26 | tmp->next->prev = *stack; 27 | tmp->next = *stack; 28 | tmp->prev = NULL; 29 | (*stack)->prev = tmp; 30 | *stack = tmp; 31 | return (true); 32 | } 33 | 34 | bool push(t_node **stack_from, t_node **stack_to) 35 | { 36 | t_node *tmp; 37 | 38 | if (stack_from == NULL || *stack_from == NULL) 39 | return (true); 40 | *stack_from = last_first_node(*stack_from, false); 41 | *stack_to = last_first_node(*stack_to, false); 42 | tmp = (*stack_from)->next; 43 | if (tmp != NULL) 44 | tmp->prev = NULL; 45 | (*stack_from)->next = *stack_to; 46 | if (*stack_to != NULL) 47 | (*stack_to)->prev = *stack_from; 48 | *stack_to = *stack_from; 49 | *stack_from = tmp; 50 | return (true); 51 | } 52 | 53 | bool rotate(t_node **stack, bool reverse) 54 | { 55 | t_node *tmp; 56 | 57 | *stack = last_first_node(*stack, false); 58 | if (stack == NULL || *stack == NULL || (*stack)->next == NULL) 59 | return (true); 60 | tmp = *stack; 61 | if (!reverse) 62 | { 63 | *stack = (*stack)->next; 64 | (*stack)->prev = NULL; 65 | tmp->next = NULL; 66 | tmp->prev = last_first_node(*stack, true); 67 | last_first_node(*stack, true)->next = tmp; 68 | } 69 | else 70 | { 71 | *stack = last_first_node(*stack, true); 72 | (*stack)->prev->next = NULL; 73 | (*stack)->prev = NULL; 74 | (*stack)->next = tmp; 75 | tmp->prev = *stack; 76 | } 77 | return (true); 78 | } 79 | 80 | bool execute(t_node **stack_a, t_node **stack_b, char *line, bool s_print) 81 | { 82 | if (!s_print) 83 | ft_printf("%s\n", line); 84 | if (ft_strcmp(line, "sa") == 0) 85 | return (swap(stack_a)); 86 | else if (ft_strcmp(line, "sb") == 0) 87 | return (swap(stack_b)); 88 | else if (ft_strcmp(line, "ss") == 0) 89 | return (swap(stack_a) && swap(stack_b)); 90 | else if (ft_strcmp(line, "pa") == 0) 91 | return (push(stack_b, stack_a)); 92 | else if (ft_strcmp(line, "pb") == 0) 93 | return (push(stack_a, stack_b)); 94 | else if (ft_strcmp(line, "ra") == 0) 95 | return (rotate(stack_a, false)); 96 | else if (ft_strcmp(line, "rb") == 0) 97 | return (rotate(stack_b, false)); 98 | else if (ft_strcmp(line, "rr") == 0) 99 | return (rotate(stack_a, false) && rotate(stack_b, false)); 100 | else if (ft_strcmp(line, "rra") == 0) 101 | return (rotate(stack_a, true)); 102 | else if (ft_strcmp(line, "rrb") == 0) 103 | return (rotate(stack_b, true)); 104 | else if (ft_strcmp(line, "rrr") == 0) 105 | return (rotate(stack_a, true) && rotate(stack_b, true)); 106 | return (false); 107 | } 108 | 109 | bool multi_execute(t_node **stack_a, t_node **stack_b, char *line, int n) 110 | { 111 | while (n--) 112 | { 113 | if (!execute(stack_a, stack_b, line, false)) 114 | return (false); 115 | } 116 | return (true); 117 | } 118 | --------------------------------------------------------------------------------