├── .gitignore ├── libft ├── libft.a ├── bitwise │ ├── check_bit.c │ ├── check_mask_or.c │ ├── set_bit.c │ ├── not_bit.c │ ├── check_mask_not.c │ ├── check_mask_and.c │ └── print_bits.c ├── check │ ├── ft_isascii.c │ ├── ft_isprint.c │ ├── ft_isdigit.c │ ├── ft_isalpha.c │ ├── ft_isalnum.c │ ├── ft_isspace.c │ ├── ft_str_is_printable.c │ └── ft_str_is_uppercase.c ├── put │ ├── ft_putchar_fd.c │ ├── ft_putendl_fd.c │ ├── ft_putstr_fd.c │ └── ft_putnbr_fd.c ├── printf │ ├── pf_putchar_fd.c │ ├── pf_strlen.c │ ├── pf_putstr_fd.c │ ├── pf_putnbr_fd.c │ ├── Makefile │ ├── pf_putnbr_base.c │ ├── ft_printf.h │ └── ft_printf.c ├── str │ ├── ft_tolower.c │ ├── ft_toupper.c │ ├── ft_strfree.c │ ├── ft_strlen.c │ ├── ft_chr_count.c │ ├── ft_striteri.c │ ├── ft_strchr.c │ ├── ft_strrchr.c │ ├── ft_strdup.c │ ├── ft_strlcpy.c │ ├── ft_chrpos.c │ ├── ft_strmapi.c │ ├── ft_strjoin.c │ ├── ft_strncmp.c │ ├── ft_strjoinfree.c │ ├── ft_strstr.c │ ├── ft_strnstr.c │ ├── ft_unsplit.c │ ├── ft_strlcat.c │ ├── ft_strget_btwn.c │ ├── ft_substr.c │ ├── ft_strtrim.c │ ├── ft_strrep.c │ ├── ft_strbrk.c │ └── ft_split.c ├── mem │ ├── ft_bzero.c │ ├── ft_memset.c │ ├── ft_memcpy.c │ ├── ft_calloc.c │ ├── ft_memchr.c │ ├── ft_memmove.c │ └── ft_memcmp.c ├── tree │ ├── tree_del_node.c │ ├── tree_post_order.c │ ├── tree_pre_order.c │ ├── tree_new_node.c │ └── tree_in_order.c ├── lst │ ├── ft_lstadd_front_bonus.c │ ├── ft_lstdelone_bonus.c │ ├── ft_lstsize_bonus.c │ ├── ft_lstlast_bonus.c │ ├── ft_lstiter_bonus.c │ ├── ft_lstnew_bonus.c │ ├── ft_lstadd_back_bonus.c │ ├── ft_lstclear_bonus.c │ └── ft_lstmap_bonus.c ├── arr │ ├── ft_sarrsize.c │ ├── ft_sarrprint.c │ ├── ft_sarrrep.c │ ├── ft_sarrfree.c │ ├── ft_sarrcpy.c │ ├── ft_sarrcmp.c │ ├── ft_sarrrmi.c │ └── ft_sarradd.c ├── to │ ├── ft_atod.c │ ├── ft_atoi_btwn.c │ ├── ft_atoi_pos.c │ ├── ft_atoi.c │ └── ft_itoa.c ├── gnl │ ├── get_next_line.h │ ├── get_next_line.c │ └── get_next_line_utils.c ├── Makefile └── libft.h ├── .vscode └── settings.json ├── MiniShellExample ├── testing.sh ├── actions.c ├── main.c ├── example.h └── ainit.c ├── PushSwapExample ├── testing.sh ├── example.h ├── main.c ├── actions.c └── ainit.c ├── So_LongExample ├── testing.sh ├── main.c ├── actions.c ├── example.h └── ainit.c ├── So_LongFileExample ├── testing.sh ├── map.ber ├── actions.c ├── print_actions.c ├── example.h ├── main.c └── ainit.c ├── TreeMSExample ├── testing.sh ├── inorder.c ├── tokenizer │ ├── ta_actions.c │ └── ta_ainit.c ├── main.c ├── organizer │ ├── oa_ainit.c │ └── oa_actions.c └── example.h ├── automata ├── automata.h └── automata.c └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | -------------------------------------------------------------------------------- /libft/libft.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JorgeVJ/automatas/HEAD/libft/libft.a -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "example.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /MiniShellExample/testing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Example using a range of numbers 4 | while true 5 | do 6 | gcc main.c ../automata/automata.c ainit.c actions.c ../libft/libft.a && ./a.out 7 | sleep 2 8 | clear 9 | done 10 | -------------------------------------------------------------------------------- /PushSwapExample/testing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Example using a range of numbers 4 | while true 5 | do 6 | gcc main.c ../automata/automata.c ainit.c actions.c ../libft/libft.a && ./a.out 7 | sleep 2 8 | clear 9 | done 10 | -------------------------------------------------------------------------------- /So_LongExample/testing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Example using a range of numbers 4 | while true 5 | do 6 | gcc main.c ../automata/automata.c ainit.c actions.c ../libft/libft.a && ./a.out 7 | sleep 2 8 | clear 9 | done 10 | -------------------------------------------------------------------------------- /So_LongFileExample/testing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Example using a range of numbers 4 | while true 5 | do 6 | gcc main.c ../automata/automata.c ainit.c print_actions.c actions.c ../libft/libft.a && ./a.out 7 | sleep 2 8 | clear 9 | done 10 | -------------------------------------------------------------------------------- /So_LongFileExample/map.ber: -------------------------------------------------------------------------------- 1 | 1111111111111111111111111111111111 2 | 1E0000000000000C00000C000000000001 3 | 1010010100100000101001000000010101 4 | 1010010010101010001001000000010101 5 | 1P0000000C00C0000000000000000000C1 6 | 1111111111111111111111111111111111 7 | -------------------------------------------------------------------------------- /TreeMSExample/testing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Example using a range of numbers 4 | while true 5 | do 6 | gcc main.c inorder.c ../automata/automata.c tokenizer/ta_ainit.c tokenizer/ta_actions.c organizer/oa_ainit.c organizer/oa_actions.c ../libft/libft.a && ./a.out 7 | sleep 2 8 | clear 9 | done 10 | -------------------------------------------------------------------------------- /libft/bitwise/check_bit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_bit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/15 22:25:36 by jvasquez #+# #+# */ 9 | /* Updated: 2022/11/22 18:27:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int check_bit(int n, int bit) 14 | { 15 | return (n >> bit & 1); 16 | } 17 | -------------------------------------------------------------------------------- /libft/check/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/13 19:45:03 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/21 13:43:21 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_isascii(int c) 14 | { 15 | if (c >= 0 && c <= 127) 16 | return (1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /libft/check/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/13 19:51:25 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/29 12:21:38 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_isprint(int c) 14 | { 15 | if (c > 31 && c < 127) 16 | return (1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /libft/put/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:23:34 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/23 21:24:07 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/printf/pf_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pf_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:23:34 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/19 12:01:15 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int pf_putchar_fd(char c, int fd) 16 | { 17 | return (write(fd, &c, 1)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/str/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 14:04:47 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/17 14:05:00 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 'A' && c <= 'Z') 18 | c += 32; 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/str/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 14:06:43 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/17 14:07:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 'a' && c <= 'z') 18 | c -= 32; 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/check/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/27 14:49:03 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 14:49:31 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= '0' && c <= '9') 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /libft/put/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:29:32 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/23 21:30:13 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | ft_putstr_fd(s, fd); 18 | ft_putchar_fd('\n', fd); 19 | } 20 | -------------------------------------------------------------------------------- /libft/str/ft_strfree.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strfree.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/24 19:57:26 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/24 19:57:27 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strfreel(char **str) 16 | { 17 | if (!*str) 18 | return ; 19 | free(*str); 20 | *str = NULL; 21 | } 22 | -------------------------------------------------------------------------------- /libft/check/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/13 18:04:58 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 14:50:46 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 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 | -------------------------------------------------------------------------------- /libft/mem/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 17:57:32 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/17 21:38:45 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | void ft_bzero(void *str, size_t n) 15 | { 16 | size_t i; 17 | 18 | i = -1; 19 | while (++i < n) 20 | ((unsigned char *)str)[i] = '\0'; 21 | } 22 | -------------------------------------------------------------------------------- /libft/str/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/18 02:57:12 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/17 13:53:45 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | size_t ft_strlen(const char *str) 16 | { 17 | size_t n; 18 | 19 | n = 0; 20 | while (*(str++) != '\0') 21 | n++; 22 | return (n); 23 | } 24 | -------------------------------------------------------------------------------- /libft/printf/pf_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pf_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/18 02:57:12 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/16 16:40:29 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | size_t pf_strlen(const char *str) 16 | { 17 | size_t n; 18 | 19 | n = 0; 20 | while (*(str++) != '\0') 21 | n++; 22 | return (n); 23 | } 24 | -------------------------------------------------------------------------------- /libft/tree/tree_del_node.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tree_del_node.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/18 20:17:31 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/18 20:17:31 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void tree_del_node(t_tree *node, void (*del)(void*)) 16 | { 17 | if (!node || !del) 18 | return ; 19 | del(node->data); 20 | free(node); 21 | } 22 | -------------------------------------------------------------------------------- /libft/lst/ft_lstadd_front_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 18:04:13 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/24 19:37:13 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | if (lst && new) 18 | { 19 | new->next = *lst; 20 | *lst = new; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /libft/lst/ft_lstdelone_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 21:48:30 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 16:01:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void*)) 16 | { 17 | if (!lst || !del) 18 | return ; 19 | del(lst->content); 20 | free(lst); 21 | } 22 | -------------------------------------------------------------------------------- /libft/put/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:26:55 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/23 21:28:25 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char const *s, int fd) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | if (s) 21 | while (s[++i]) 22 | write(fd, &s[i], 1); 23 | } 24 | -------------------------------------------------------------------------------- /libft/bitwise/check_mask_or.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_mask_or.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/31 20:52:26 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/31 20:52:27 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Checks if any bit in "with" is in mask. 16 | int check_mask_or(int mask, int with) 17 | { 18 | if (mask & with) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/bitwise/set_bit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* set_bit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/15 22:25:12 by jvasquez #+# #+# */ 9 | /* Updated: 2022/11/15 22:25:29 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int set_bit(int *n, int bit, int val) 14 | { 15 | if (*n >> bit & 1 && !val) 16 | *n = *n - (1 << bit); 17 | else if (!(*n >> bit) && val) 18 | *n = *n + (1 << bit); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /libft/check/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/13 18:04:58 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/13 19:27:41 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 18 | || (c >= '0' && c <= '9')) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/bitwise/not_bit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* not_bit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/24 20:00:21 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/24 20:00:22 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int not_bit(int *n, int bit) 14 | { 15 | if (bit >= 32 || bit < 0) 16 | return (0); 17 | if (*n >> bit & 1) 18 | *n = *n - (1 << bit); 19 | else 20 | *n = *n + (1 << bit); 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /libft/bitwise/check_mask_not.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_mask_not.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/31 20:52:31 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/31 20:52:32 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Checks if any bit in "with" is in mask. 16 | int check_mask_not(int mask, int with) 17 | { 18 | if ((~mask & with) == with) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/check/ft_isspace.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isspace.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/17 12:02:35 by jvasquez #+# #+# */ 9 | /* Updated: 2023/12/17 12:02:53 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_isspace(char c) 14 | { 15 | int b; 16 | 17 | b = 0; 18 | if (c == ' ' || c == '\t' || c == '\n') 19 | b = 1; 20 | else if (c == '\r' || c == '\v' || c == '\f') 21 | b = 1; 22 | return (b); 23 | } 24 | -------------------------------------------------------------------------------- /libft/str/ft_chr_count.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_chr_count.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/06 11:34:49 by jvasquez #+# #+# */ 9 | /* Updated: 2022/09/06 11:37:48 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_chr_count(char *str, char c) 14 | { 15 | int i; 16 | int count; 17 | 18 | i = -1; 19 | count = 0; 20 | while (str[++i]) 21 | { 22 | if (str[i] == c) 23 | count++; 24 | } 25 | return (count); 26 | } 27 | -------------------------------------------------------------------------------- /libft/str/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:37:38 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/23 21:42:37 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | unsigned int i; 18 | 19 | if (!s || !f) 20 | return ; 21 | i = -1; 22 | while (s[++i]) 23 | f(i, s + i); 24 | } 25 | -------------------------------------------------------------------------------- /libft/check/ft_str_is_printable.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_str_is_printable.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/20 13:26:55 by jvasquez #+# #+# */ 9 | /* Updated: 2022/05/25 18:42:24 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_str_is_printable(char *str) 16 | { 17 | while (*str != '\0') 18 | { 19 | if (!(*str >= 32 && *str <= 126)) 20 | return (0); 21 | str++; 22 | } 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /libft/check/ft_str_is_uppercase.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_str_is_uppercase.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/20 13:30:02 by jvasquez #+# #+# */ 9 | /* Updated: 2022/05/25 18:42:02 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_str_is_uppercase(char *str) 16 | { 17 | while (*str != '\0') 18 | { 19 | if (!(*str >= 'A' && *str <= 'Z')) 20 | return (0); 21 | str++; 22 | } 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /libft/bitwise/check_mask_and.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_mask_and.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/31 20:52:19 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/31 20:52:21 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Checks if every bit in "with" is in the same state in "mask". 16 | int check_mask_and(int mask, int with) 17 | { 18 | if ((mask & with) == with) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /libft/str/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 14:09:00 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/11 16:52:29 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | char *ft_strchr(const char *str, int c) 14 | { 15 | while (*str != '\0') 16 | { 17 | if (*str == (char)c) 18 | return ((char *)str); 19 | str++; 20 | } 21 | if (c == '\0') 22 | return ((char *)str); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 16:25:29 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 16:25:32 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Return the array dimention 16 | int ft_sarrsize(char **arr) 17 | { 18 | int size; 19 | 20 | if (!arr) 21 | return (0); 22 | size = 0; 23 | while (arr[size]) 24 | size++; 25 | return (size); 26 | } 27 | -------------------------------------------------------------------------------- /libft/lst/ft_lstsize_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 21:09:17 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/24 21:42:13 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | int i; 18 | 19 | if (!lst) 20 | return (0); 21 | i = 1; 22 | while (lst->next) 23 | { 24 | i++; 25 | lst = lst->next; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /libft/mem/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 17:41:30 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/17 19:04:52 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include 13 | 14 | void *ft_memset(void *str, int c, size_t len) 15 | { 16 | size_t i; 17 | unsigned char *cstr; 18 | 19 | cstr = (unsigned char *)str; 20 | i = -1; 21 | while (++i < len) 22 | cstr[i] = c; 23 | return (cstr); 24 | } 25 | -------------------------------------------------------------------------------- /libft/tree/tree_post_order.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tree_post_order.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/18 20:17:16 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/18 20:17:16 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void tree_post_order(t_tree *tree, void (*ft)(void*)) 16 | { 17 | if (tree != NULL) 18 | { 19 | tree_post_order(tree->left, ft); 20 | tree_post_order(tree->right, ft); 21 | ft(tree->data); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /libft/tree/tree_pre_order.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tree_pre_order.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/18 20:17:21 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/18 20:17:21 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void tree_pre_order(t_tree *tree, void (*ft)(void*)) 16 | { 17 | if (tree != NULL) 18 | { 19 | ft(tree->data); 20 | tree_pre_order(tree->left, ft); 21 | tree_pre_order(tree->right, ft); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /libft/bitwise/print_bits.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_bits.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/24 20:02:24 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/24 20:02:25 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void print_bits(int n) 16 | { 17 | int i; 18 | 19 | i = 32; 20 | while (i--) 21 | { 22 | ft_printf("%d", (n >> i) & 1); 23 | if (!(i % 4)) 24 | write(1, " ", 1); 25 | } 26 | write (1, "\n", 1); 27 | } 28 | -------------------------------------------------------------------------------- /libft/lst/ft_lstlast_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 21:02:47 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/24 21:09:25 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | if (lst == NULL) 18 | return (0); 19 | while (lst) 20 | { 21 | if (lst->next == NULL) 22 | return (lst); 23 | lst = lst->next; 24 | } 25 | return (lst); 26 | } 27 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 17:41:53 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 17:41:54 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Prints the entire array 16 | int ft_sarrprint(char **arr) 17 | { 18 | int i; 19 | 20 | if (!arr || !arr[0]) 21 | return (0); 22 | i = -1; 23 | while (arr[++i]) 24 | ft_printf("%s\n", arr[i]); 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /libft/printf/pf_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pf_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:26:55 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/18 17:22:06 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int pf_putstr_fd(char const *s, int fd) 16 | { 17 | int i; 18 | 19 | if (!s) 20 | return (write(fd, "(null)", 6)); 21 | i = -1; 22 | if (s) 23 | while (s[++i]) 24 | write(fd, &s[i], 1); 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /libft/lst/ft_lstiter_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/27 12:31:25 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 16:01:02 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | t_list *temp; 18 | 19 | if (!lst || !f) 20 | return ; 21 | temp = lst; 22 | while (temp) 23 | { 24 | f(temp->content); 25 | temp = temp->next; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /libft/tree/tree_new_node.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tree_new_node.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/18 20:17:01 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/18 20:17:02 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_tree *tree_new_node(void *data) 16 | { 17 | t_tree *node; 18 | 19 | node = malloc(sizeof(t_tree)); 20 | ft_bzero(node, sizeof(t_tree)); 21 | node->data = data; 22 | node->left = NULL; 23 | node->right = NULL; 24 | return (node); 25 | } 26 | -------------------------------------------------------------------------------- /libft/lst/ft_lstnew_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 15:30:43 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/24 16:20:14 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | t_list *ft_lstnew(void *content) 17 | { 18 | t_list *elt; 19 | 20 | elt = (t_list *)malloc(sizeof(*elt)); 21 | if (!elt) 22 | return (NULL); 23 | elt->content = content; 24 | elt->next = NULL; 25 | return (elt); 26 | } 27 | -------------------------------------------------------------------------------- /libft/str/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 15:20:56 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/11 16:54:01 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | #include 14 | 15 | char *ft_strrchr(const char *str, int c) 16 | { 17 | size_t i; 18 | 19 | i = ft_strlen(str) + 1; 20 | while (i >= 1) 21 | { 22 | i--; 23 | if (str[i] == (char)c) 24 | return ((char *)&str[i]); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /libft/lst/ft_lstadd_back_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 19:19:33 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/24 21:08:52 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *temp; 18 | 19 | if (lst && new) 20 | { 21 | if (*lst == NULL) 22 | *lst = new; 23 | else 24 | { 25 | temp = ft_lstlast(*(lst)); 26 | temp->next = new; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /libft/lst/ft_lstclear_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/24 21:42:46 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 12:14:52 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void*)) 16 | { 17 | t_list *tmp; 18 | 19 | if (!lst || !del || !*lst) 20 | return ; 21 | while (*lst) 22 | { 23 | tmp = (*lst)->next; 24 | (*del)((*lst)->content); 25 | free(*lst); 26 | *lst = tmp; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /libft/mem/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 18:05:05 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 15:30:34 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | void *ft_memcpy(void *dest, const void *src, size_t n) 15 | { 16 | size_t j; 17 | 18 | if (!src && !dest) 19 | return (NULL); 20 | j = 0; 21 | while (j < n) 22 | { 23 | ((unsigned char *)dest)[j] = ((unsigned char *)src)[j]; 24 | j++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /libft/str/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/21 18:44:14 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/21 18:47:31 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *str) 16 | { 17 | size_t len; 18 | char *temp; 19 | 20 | len = ft_strlen(str); 21 | temp = (char *)malloc(sizeof(char) * (len + 1)); 22 | if (!temp) 23 | return (NULL); 24 | ft_strlcpy(temp, (char *)str, len + 1); 25 | return (temp); 26 | } 27 | -------------------------------------------------------------------------------- /libft/mem/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/21 15:43:11 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/19 16:15:22 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t count, size_t size) 16 | { 17 | void *temp; 18 | size_t n; 19 | 20 | n = size * count; 21 | if (n < count || n < size) 22 | return (NULL); 23 | temp = malloc(n); 24 | if (temp == (void *)0) 25 | return (NULL); 26 | ft_bzero(temp, n); 27 | return (temp); 28 | } 29 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrrep.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrrep.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 21:25:47 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 21:25:48 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Replace one item at the given index 16 | char **ft_sarrrep(char **arr, int index, char *str) 17 | { 18 | int size; 19 | 20 | size = ft_sarrsize(arr); 21 | if (size < index || index < 0) 22 | return (arr); 23 | free(arr[index]); 24 | arr[index] = ft_strdup(str); 25 | return (arr); 26 | } 27 | -------------------------------------------------------------------------------- /libft/str/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/23 12:12:56 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/21 20:38:26 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | size_t ft_strlcpy(char *dest, char *src, size_t size) 15 | { 16 | size_t j; 17 | 18 | if (size == 0) 19 | return (ft_strlen(src)); 20 | j = 0; 21 | while (src[j] != '\0' && j < size - 1) 22 | { 23 | dest[j] = src[j]; 24 | j++; 25 | } 26 | dest[j] = '\0'; 27 | return (ft_strlen(src)); 28 | } 29 | -------------------------------------------------------------------------------- /libft/str/ft_chrpos.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_chrpos.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/02 16:18:55 by jvasquez #+# #+# */ 9 | /* Updated: 2023/10/02 16:18:56 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_chrpos(char *str, int chr, int start) 16 | { 17 | int i; 18 | int len; 19 | 20 | len = (int)ft_strlen(str); 21 | if (!str || chr < 0 || start < 0) 22 | return (-1); 23 | i = start - 1; 24 | while (++i < len && str[i]) 25 | if (str[i] == chr) 26 | return (i); 27 | return (-1); 28 | } 29 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrfree.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrfree.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 16:48:58 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 16:49:00 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Free one string array 16 | void ft_sarrfree(char ***arr) 17 | { 18 | int i; 19 | 20 | if (!arr || !arr[0]) 21 | return ; 22 | i = -1; 23 | while (arr[0][++i]) 24 | { 25 | if (arr[0][i]) 26 | { 27 | free(arr[0][i]); 28 | arr[0][i] = NULL; 29 | } 30 | } 31 | free (*arr); 32 | *arr = NULL; 33 | } 34 | -------------------------------------------------------------------------------- /libft/put/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:31:07 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/11 16:24:36 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n == -2147483648) 18 | ft_putstr_fd("-2147483648", fd); 19 | else 20 | { 21 | if (n < 0) 22 | { 23 | ft_putchar_fd('-', fd); 24 | n = n * -1; 25 | } 26 | if (n > 9) 27 | ft_putnbr_fd(n / 10, fd); 28 | ft_putchar_fd((n % 10) + '0', fd); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /libft/mem/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 16:39:47 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/11 16:44:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | void *ft_memchr(const void *str, int c, size_t size) 15 | { 16 | size_t i; 17 | unsigned char *aux; 18 | unsigned char n; 19 | 20 | i = 0; 21 | aux = (unsigned char *)str; 22 | n = (unsigned char)c; 23 | while (size > i) 24 | { 25 | if (aux[i] == n) 26 | return ((void *)aux + i); 27 | i++; 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 16:21:10 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 16:22:14 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Copy one string array 16 | char **ft_sarrcpy(char **arr) 17 | { 18 | int i; 19 | char **new_arr; 20 | 21 | if (!arr) 22 | return (NULL); 23 | new_arr = malloc(sizeof(char *) * (ft_sarrsize(arr) + 1)); 24 | i = -1; 25 | while (arr[++i]) 26 | new_arr[i] = ft_strdup(arr[i]); 27 | new_arr[i] = NULL; 28 | return (new_arr); 29 | } 30 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 19:27:15 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 19:27:16 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /*Compare a string with all the items in the array 16 | return the index or -1 if there's no match*/ 17 | int ft_sarrcmp(char **arr, char *str) 18 | { 19 | int i; 20 | 21 | if (!str || !arr) 22 | return (-1); 23 | i = -1; 24 | while (arr[++i]) 25 | { 26 | if (!ft_strncmp(arr[i], str, ft_strlen(str))) 27 | return (i); 28 | } 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /libft/str/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 20:58:47 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/24 14:38:00 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *sptr; 18 | int index; 19 | 20 | if (!s || !f) 21 | return (NULL); 22 | sptr = malloc(ft_strlen(s) + 1); 23 | if (!sptr) 24 | return (NULL); 25 | index = -1; 26 | while (s[++index]) 27 | sptr[index] = f(index, s[index]); 28 | sptr[index] = '\0'; 29 | return (sptr); 30 | } 31 | -------------------------------------------------------------------------------- /libft/to/ft_atod.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atod.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/17 12:07:13 by jvasquez #+# #+# */ 9 | /* Updated: 2023/12/17 12:07:14 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | double ft_atod(char *str) 16 | { 17 | int pos; 18 | int in; 19 | double de; 20 | int i; 21 | 22 | de = 0; 23 | pos = 0; 24 | in = ft_atoi_pos(str, &pos); 25 | if (str[pos++] == '.') 26 | { 27 | i = 0; 28 | de = 1; 29 | while (ft_isdigit(str[pos + i++])) 30 | de *= 10; 31 | de = ft_atoi_pos(str, &pos) / de; 32 | } 33 | return ((double)in + de); 34 | } 35 | -------------------------------------------------------------------------------- /libft/str/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/21 21:34:24 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/23 20:42:43 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | size_t st_len; 18 | char *joined; 19 | 20 | if (!s1 || !s2) 21 | return (NULL); 22 | st_len = ft_strlen(s1) + ft_strlen(s2) + 1; 23 | joined = malloc(st_len); 24 | if (!joined) 25 | return (NULL); 26 | ft_strlcpy(joined, (char *)s1, st_len); 27 | ft_strlcat(joined, s2, st_len); 28 | return (joined); 29 | } 30 | -------------------------------------------------------------------------------- /libft/to/ft_atoi_btwn.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi_btwn.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/05 08:27:01 by jvasquez #+# #+# */ 9 | /* Updated: 2023/10/05 08:27:03 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Get a string inside the chars given and return 17 | * the integer of char numbers in the string. 18 | */ 19 | int ft_atoi_btwn(char *str, char ini, char end) 20 | { 21 | char *number; 22 | int n; 23 | 24 | number = ft_strget_btwn(str, 0, ini, end); 25 | if (number) 26 | n = atoi(number); 27 | else 28 | n = 0; 29 | free (number); 30 | return (n); 31 | } 32 | -------------------------------------------------------------------------------- /libft/printf/pf_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pf_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 21:31:07 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/18 17:55:04 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int pf_putnbr_fd(int n, int fd) 16 | { 17 | long int nbr; 18 | 19 | nbr = (long int)n; 20 | if (nbr < 0) 21 | { 22 | pf_putchar_fd('-', fd); 23 | nbr = nbr * -1; 24 | } 25 | if (nbr > 9) 26 | pf_putnbr_fd(nbr / 10, fd); 27 | pf_putchar_fd((nbr % 10) + '0', fd); 28 | if (n < 0) 29 | return (pf_nbrlen(nbr) + 1); 30 | else 31 | return (pf_nbrlen(nbr)); 32 | } 33 | -------------------------------------------------------------------------------- /libft/str/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/23 16:25:36 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/21 20:50:21 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | int ft_strncmp(const char *s1, const char *s2, size_t n) 15 | { 16 | size_t i; 17 | 18 | i = 0; 19 | while ((s1[i] != '\0' || s2[i] != '\0') && (s1[i] == s2[i]) && i < n) 20 | { 21 | if (s1[i] != s2[i]) 22 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 23 | i++; 24 | } 25 | if (i != n) 26 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /libft/to/ft_atoi_pos.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi_pos.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/06 11:19:28 by jvasquez #+# #+# */ 9 | /* Updated: 2022/09/23 13:53:03 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_atoi_pos(char *str, int *pos) 14 | { 15 | unsigned long int sum; 16 | int sign; 17 | 18 | sum = 0; 19 | sign = 1; 20 | if (str[*pos] == '-' || str[*pos] == '+') 21 | { 22 | if (str[*pos] == '-') 23 | sign *= -1; 24 | *pos += 1; 25 | } 26 | while (str[*pos] >= '0' && str[*pos] <= '9') 27 | { 28 | sum = - '0' + sum * 10 + str[*pos]; 29 | *pos += 1; 30 | } 31 | return (sign * sum); 32 | } 33 | -------------------------------------------------------------------------------- /libft/lst/ft_lstmap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/27 12:58:52 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 15:58:52 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 16 | { 17 | t_list *n_list; 18 | t_list *n_node; 19 | 20 | n_list = NULL; 21 | while (lst) 22 | { 23 | n_node = ft_lstnew(f(lst->content)); 24 | if (!n_node) 25 | { 26 | ft_lstclear(&n_list, del); 27 | return (NULL); 28 | } 29 | ft_lstadd_back(&n_list, n_node); 30 | lst = lst->next; 31 | } 32 | return (n_list); 33 | } 34 | -------------------------------------------------------------------------------- /libft/str/ft_strjoinfree.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoinfree.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/15 13:47:43 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/15 13:51:09 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoinfree(char *s1, char *s2, int which) 16 | { 17 | char *joined; 18 | 19 | if (!s1 && !s2) 20 | return (NULL); 21 | if (!s1) 22 | return (s2); 23 | if (!s2) 24 | return (s1); 25 | joined = ft_strjoin(s1, s2); 26 | if (!which || which == 2) 27 | { 28 | free(s1); 29 | s1 = NULL; 30 | } 31 | if (which) 32 | { 33 | free(s2); 34 | s2 = NULL; 35 | } 36 | return (joined); 37 | } 38 | -------------------------------------------------------------------------------- /libft/mem/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 19:18:15 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/13 10:32:19 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | void *ft_memmove(void *dest, const void *src, size_t n) 15 | { 16 | size_t j; 17 | char *csrc; 18 | char *cdest; 19 | 20 | cdest = (char *)dest; 21 | csrc = (char *)src; 22 | if (dest == src) 23 | return (dest); 24 | else if (dest < src) 25 | { 26 | j = 0; 27 | while (j < n) 28 | { 29 | cdest[j] = csrc[j]; 30 | j++; 31 | } 32 | } 33 | else 34 | while (n--) 35 | cdest[n] = csrc[n]; 36 | return (dest); 37 | } 38 | -------------------------------------------------------------------------------- /libft/str/ft_strstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/23 16:58:31 by jvasquez #+# #+# */ 9 | /* Updated: 2022/05/31 20:45:25 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | * Find a string into another string and returns te pointer. 15 | */ 16 | char *ft_strstr(char *str, char *to_find) 17 | { 18 | int i; 19 | int b; 20 | 21 | i = 0; 22 | if (to_find[0] == '\0') 23 | return (str); 24 | while (str[i] != '\0') 25 | { 26 | b = 0; 27 | while (str[i + b] && to_find[b] == str[i + b]) 28 | { 29 | if (to_find[b + 1] == '\0') 30 | return (&str[i]); 31 | b++; 32 | } 33 | i++; 34 | } 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /libft/mem/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 16:49:01 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/27 15:37:30 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | int ft_memcmp(const char *s1, const char *s2, size_t n) 15 | { 16 | size_t i; 17 | const unsigned char *cs1; 18 | const unsigned char *cs2; 19 | 20 | cs1 = (const unsigned char *)s1; 21 | cs2 = (const unsigned char *)s2; 22 | i = 0; 23 | while ((cs1[i] == cs2[i]) && i < n - 1) 24 | { 25 | if (cs1[i] != cs2[i]) 26 | return (cs1[i] - cs2[i]); 27 | i++; 28 | } 29 | if (i != n) 30 | return (cs1[i] - cs2[i]); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /libft/str/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/17 15:43:02 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/11 17:08:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 15 | { 16 | size_t i; 17 | size_t b; 18 | 19 | i = 0; 20 | if (!needle) 21 | return ((char *)haystack); 22 | while (haystack[i] != '\0' && i < len) 23 | { 24 | b = 0; 25 | while (haystack[i + b] && needle[b] == haystack[i + b] && i + b < len) 26 | { 27 | if (needle[b + 1] == '\0') 28 | return ((char *)&haystack[i]); 29 | b++; 30 | } 31 | i++; 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /libft/printf/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jvasquez +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2022/06/13 17:31:12 by jvasquez #+# #+# # 9 | # Updated: 2022/08/10 15:42:10 by jvasquez ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libftprintf.a 14 | CFLAGS = -Wall -Wextra -Werror 15 | FUNCTIONS = pf_strlen.c \ 16 | pf_putchar_fd.c \ 17 | pf_putstr_fd.c \ 18 | pf_putnbr_fd.c \ 19 | ft_printf.c \ 20 | pf_putnbr_base.c \ 21 | 22 | #DFUNCTIONS = $(addprefix $(DIR), ${FUNCTIONS}) 23 | OBJS = ${FUNCTIONS:.c=.o} 24 | 25 | ${NAME}: ${OBJS} 26 | ar rcs ${NAME} ${OBJS} 27 | 28 | all: $(NAME) 29 | 30 | clean: 31 | rm -f $(OBJS) 32 | 33 | fclean: clean 34 | rm -f $(NAME) 35 | 36 | re: fclean all 37 | -------------------------------------------------------------------------------- /libft/arr/ft_sarrrmi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarrrmi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 17:23:02 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 17:23:04 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Remove one item given an index 16 | char **ft_sarrrmi(int index, char **arr) 17 | { 18 | int i; 19 | int j; 20 | char **new_arr; 21 | 22 | i = ft_sarrsize(arr); 23 | if (index > i || index < 0) 24 | return (arr); 25 | new_arr = malloc(sizeof(char *) * (i)); 26 | i = -1; 27 | j = 0; 28 | while (arr[++i]) 29 | { 30 | if (i != index) 31 | new_arr[j++] = ft_strdup(arr[i]); 32 | } 33 | new_arr[j] = NULL; 34 | ft_sarrfree(&arr); 35 | return (new_arr); 36 | } 37 | -------------------------------------------------------------------------------- /libft/str/ft_unsplit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_unsplit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/19 14:28:09 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/19 14:28:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Return all the items in a array string joined and separated 16 | //by the string given. 17 | char *ft_unsplit(char **sarr, char *sep) 18 | { 19 | int i; 20 | char *unsplit; 21 | 22 | if (!sarr) 23 | return (NULL); 24 | unsplit = ft_strdup(sarr[0]); 25 | i = 0; 26 | while (sarr[++i]) 27 | { 28 | if (!sep) 29 | unsplit = ft_strjoin(unsplit, sarr[i]); 30 | else 31 | unsplit = ft_strjoin(unsplit, ft_strjoin(sep, sarr[i])); 32 | } 33 | return (unsplit); 34 | } 35 | -------------------------------------------------------------------------------- /libft/str/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/23 17:13:59 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/21 19:59:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include "libft.h" 13 | 14 | size_t ft_strlcat(char *dest, const char *src, size_t dsize) 15 | { 16 | size_t i; 17 | size_t d_len; 18 | size_t s_len; 19 | 20 | d_len = ft_strlen(dest); 21 | s_len = ft_strlen((char *)src); 22 | if (d_len > dsize || dsize == 0) 23 | return (s_len + dsize); 24 | i = 0; 25 | while (((unsigned char *)src)[i] && d_len + i < (dsize - 1)) 26 | { 27 | dest[i + d_len] = ((unsigned char *)src)[i]; 28 | i++; 29 | } 30 | dest[i + d_len] = '\0'; 31 | return (d_len + s_len); 32 | } 33 | -------------------------------------------------------------------------------- /libft/str/ft_strget_btwn.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strget_btwn.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/05 08:27:47 by jvasquez #+# #+# */ 9 | /* Updated: 2023/10/05 08:27:49 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Get the string between the chars given, if chars not found returns NULL. 17 | */ 18 | char *ft_strget_btwn(char *str, int start, char ini, char end) 19 | { 20 | char *group; 21 | int left; 22 | int right; 23 | 24 | if (!str) 25 | return (NULL); 26 | group = NULL; 27 | left = ft_chrpos(str, ini, start); 28 | right = ft_chrpos(str, end, left + 1); 29 | if (left != -1 && right > left) 30 | group = ft_substr(str, left + 1, right - left - 1); 31 | return (group); 32 | } 33 | -------------------------------------------------------------------------------- /libft/str/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/21 21:13:19 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/11 18:27:09 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(const char *str, unsigned int start, size_t len) 16 | { 17 | char *temp; 18 | unsigned int i; 19 | 20 | if (!str) 21 | return (NULL); 22 | if (start > ft_strlen(str) || !*str) 23 | return (ft_strdup("")); 24 | if (ft_strlen(str) + 1 <= (len + start)) 25 | len = ft_strlen(str) - start; 26 | temp = ft_calloc(sizeof(char), len + 1); 27 | if (!temp) 28 | return (NULL); 29 | i = -1; 30 | while (++i < len) 31 | temp[i] = str[start + i]; 32 | return (temp); 33 | } 34 | -------------------------------------------------------------------------------- /libft/tree/tree_in_order.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tree_in_order.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/18 20:17:08 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 21:42:40 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void tree_in_order(t_tree *tree, void (*ft)(void*)) 16 | { 17 | if (tree != NULL) 18 | { 19 | tree_in_order(tree->left, ft); 20 | ft(tree->data); 21 | tree_in_order(tree->right, ft); 22 | } 23 | } 24 | 25 | void tree_in_order_context(t_tree *tree, void (*ft)(void*, void*), void *context) 26 | { 27 | if (tree != NULL) 28 | { 29 | tree_in_order_context(tree->left, ft, context); 30 | ft(tree->data, context); 31 | tree_in_order_context(tree->right, ft, context); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /automata/automata.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* automata.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:35:05 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 21:53:48 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | typedef struct s_automata 14 | { 15 | void *data; 16 | char **alphabet; 17 | char **errors; 18 | char *str; 19 | int state; // Current State 20 | int ostate; // Old/Previous State 21 | int errorlen; 22 | int i; 23 | int j; 24 | void (*fsa[20])(struct s_automata *a, void *data); 25 | void (*fta[20][20])(struct s_automata *a, void *data); 26 | int (*get_state)(int state, int abc_idx); 27 | } t_automata; 28 | 29 | int evaluate(t_automata *a); 30 | void evaluate_file(t_automata *a, char *dir, void (*f)(t_automata *a, int state)); 31 | -------------------------------------------------------------------------------- /libft/arr/ft_sarradd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sarradd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/20 16:41:24 by jvasquez #+# #+# */ 9 | /* Updated: 2023/01/20 16:41:25 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | //Adds an item to a string array 16 | char **ft_sarradd(char **arr, char *string) 17 | { 18 | int size; 19 | char **new_arr; 20 | 21 | if (!string) 22 | return (arr); 23 | size = ft_sarrsize(arr) + 2; 24 | new_arr = malloc(sizeof(char *) * (size)); 25 | size = 0; 26 | if (arr) 27 | { 28 | while (arr[size]) 29 | { 30 | new_arr[size] = ft_strdup(arr[size]); 31 | size++; 32 | } 33 | ft_sarrfree(&arr); 34 | } 35 | new_arr[size++] = ft_strdup(string); 36 | new_arr[size] = NULL; 37 | return (new_arr); 38 | } 39 | -------------------------------------------------------------------------------- /MiniShellExample/actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:48:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | void get_token(t_automata *a, void *data) 20 | { 21 | char *cmdline; 22 | t_data *info; 23 | 24 | info = (t_data *)data; 25 | cmdline = ft_substr(a->str, a->j, a->i - a->j); 26 | a->j = a->i; 27 | info->tokens = ft_sarradd(info->tokens, cmdline); 28 | free (cmdline); 29 | } 30 | -------------------------------------------------------------------------------- /libft/str/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/23 16:26:42 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/21 21:55:54 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strtrim(char const *str, char const *set) 16 | { 17 | size_t len; 18 | int p_ini; 19 | int p_fin; 20 | char *trim; 21 | 22 | if (!str) 23 | return (0); 24 | trim = ""; 25 | len = ft_strlen(str); 26 | p_ini = 0; 27 | while (ft_strchr(set, str[p_ini]) && str[p_ini]) 28 | p_ini++; 29 | p_fin = len - 1; 30 | while (ft_strchr(set, str[p_fin]) && p_fin > 0) 31 | p_fin--; 32 | if (p_fin < 0 || p_ini > p_fin) 33 | trim = ft_strdup(""); 34 | else 35 | trim = ft_substr(str, p_ini, p_fin - p_ini + 1); 36 | return (trim); 37 | } 38 | -------------------------------------------------------------------------------- /libft/str/ft_strrep.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrep.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/04 22:19:42 by jvasquez #+# #+# */ 9 | /* Updated: 2023/10/04 22:19:44 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Find old string into str string and replace the old match for the new one. 17 | */ 18 | char *ft_strrep(char *str, char *old, char *new) 19 | { 20 | char *found; 21 | char *first; 22 | char *last; 23 | 24 | if (!str || !old || !new || !ft_strstr(str, old)) 25 | return (ft_strdup(str)); 26 | found = ft_strstr(str, old); 27 | first = ft_substr(str, 0, found - str); 28 | last = ft_substr(str, found - str + ft_strlen(old), 29 | ft_strlen(found) - ft_strlen(old)); 30 | return (ft_strjoinfree(first, ft_strjoinfree(new, last, 1), 2)); 31 | } 32 | -------------------------------------------------------------------------------- /libft/gnl/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/28 14:30:24 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/16 12:11:56 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | # define GET_NEXT_LINE_H 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include "../libft.h" 22 | 23 | # ifndef BUFFER_SIZE 24 | # define BUFFER_SIZE 1 25 | # endif 26 | 27 | char *get_next_line(int fd); 28 | ssize_t ft_bufjoin(char **buf, int fd); 29 | void ft_cleanbuf(char **buf); 30 | char *ft_getline(char **buf, int fd); 31 | char *ft_fillbuf(ssize_t *size, int fd); 32 | void *ft_strfree(char **buf); 33 | //int ft_strlen(char *str); 34 | #endif 35 | -------------------------------------------------------------------------------- /PushSwapExample/example.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* example.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/01 09:09:03 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 20:17:34 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include "../libft/libft.h" 16 | #include "../automata/automata.h" 17 | 18 | typedef struct s_data 19 | { 20 | t_tree *numbers; 21 | } t_data; 22 | 23 | typedef enum e_states 24 | { 25 | EMPTY, 26 | NEGATIVE, 27 | INVALID, 28 | DIGIT, 29 | SPACES_BTW 30 | } t_states; 31 | 32 | void alphabet_init(t_automata *a); 33 | void errors_init(t_automata *a); 34 | 35 | /* Single Actions */ 36 | void sactions_init(t_automata *a); 37 | void getnum(t_automata *a, void *data); 38 | 39 | /* Transition Actions */ 40 | void tactions_init(t_automata *a); 41 | 42 | int get_state(int i, int j); -------------------------------------------------------------------------------- /So_LongExample/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 18:51:13 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 18:51:17 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | void automata_init(t_automata *a, void *data) 16 | { 17 | alphabet_init(a); 18 | errors_init(a); 19 | sactions_init(a); 20 | tactions_init(a); 21 | a->data = data; 22 | a->get_state = get_state; 23 | } 24 | 25 | int main(void) 26 | { 27 | t_automata a; 28 | t_data info; 29 | 30 | ft_bzero(&a, sizeof(t_automata)); 31 | ft_bzero(&info, sizeof(t_data)); 32 | automata_init(&a, &info); 33 | a.str = ft_strdup("111CPEE1"); 34 | printf("%s\n", a.str); 35 | evaluate(&a); 36 | printf("there are:\n%d player\n%d exit\n%d collectible\n", 37 | info.player_count, info.exit_count, info.collect_count); 38 | return (0); 39 | } -------------------------------------------------------------------------------- /libft/printf/pf_putnbr_base.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pf_putnbr_base.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/25 15:59:15 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/20 18:21:43 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include "ft_printf.h" 16 | 17 | int pf_putnbr_base(unsigned long long nbr, char *base, int type) 18 | { 19 | int i; 20 | size_t b; 21 | char c[500]; 22 | int s; 23 | unsigned long long n; 24 | 25 | if (type == 1) 26 | n = (unsigned int)nbr; 27 | else 28 | n = (unsigned long long)nbr; 29 | b = pf_strlen(base); 30 | s = 1; 31 | if (n == 0) 32 | return (write(1, "0", 1)); 33 | i = 0; 34 | while (n && b) 35 | { 36 | c[i] = base[n % b]; 37 | n /= b; 38 | i++; 39 | } 40 | s = i; 41 | while (--i >= 0 && b > 1) 42 | write(1, &c[i], 1); 43 | return (s); 44 | } 45 | -------------------------------------------------------------------------------- /libft/printf/ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/07/18 21:24:25 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/19 10:47:24 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | 24 | int ft_printf(char const *str, ...); 25 | int pf_putnbr_base(unsigned long long nbr, char *base, int type); 26 | int pf_nbrlen(long int n); 27 | int pf_putnbr_fd(int n, int fd); 28 | int pf_putunsigned(unsigned long long nbr); 29 | size_t pf_putptr(unsigned long long ptr); 30 | int pf_putchar_fd(char c, int fd); 31 | int pf_putstr_fd(char const *s, int fd); 32 | size_t pf_strlen(const char *str); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /PushSwapExample/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 18:51:13 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 20:07:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | void printtree(void *data) 16 | { 17 | printf("%d\n", *(int *)(data)); 18 | } 19 | 20 | void automata_init(t_automata *a, void *data) 21 | { 22 | alphabet_init(a); 23 | errors_init(a); 24 | sactions_init(a); 25 | tactions_init(a); 26 | a->data = data; 27 | a->get_state = get_state; 28 | } 29 | 30 | int main(void) 31 | { 32 | t_automata a; 33 | t_data info; 34 | 35 | ft_bzero(&a, sizeof(t_automata)); 36 | ft_bzero(&info, sizeof(t_data)); 37 | automata_init(&a, &info); 38 | a.str = ft_strdup(" 0 1 -123 45 42 -4 31 -24 1 0 -849 54 81 29 5"); 39 | evaluate(&a); 40 | tree_in_order(info.numbers, printtree); 41 | return (0); 42 | } -------------------------------------------------------------------------------- /libft/str/ft_strbrk.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strbrk.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/02/24 19:58:38 by jvasquez #+# #+# */ 9 | /* Updated: 2023/02/24 19:58:39 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | // Divide a string in two strings at the breakpoint 16 | char **ft_strbrk(char *str, int b_point) 17 | { 18 | char **half; 19 | int len; 20 | 21 | half = NULL; 22 | len = ft_strlen(str); 23 | if (b_point >= len || b_point <= 0) 24 | { 25 | half = malloc(sizeof(char *) * 2); 26 | half[0] = ft_calloc(sizeof(char), len + 1); 27 | ft_strlcpy(half[0], str, len + 1); 28 | half[1] = NULL; 29 | } 30 | else 31 | { 32 | half = malloc(sizeof(char *) * 3); 33 | half[0] = ft_calloc(sizeof(char), b_point + 1); 34 | ft_strlcpy(half[0], str, b_point + 1); 35 | half[1] = ft_substr(str, b_point, len - b_point); 36 | half[2] = NULL; 37 | } 38 | return (half); 39 | } 40 | -------------------------------------------------------------------------------- /So_LongExample/actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:48:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | void count_player(t_automata *a, void *data) 20 | { 21 | t_data *info; 22 | 23 | info = (t_data *)data; 24 | info->player_count++; 25 | } 26 | 27 | void count_exit(t_automata *a, void *data) 28 | { 29 | t_data *info; 30 | 31 | info = (t_data *)data; 32 | info->exit_count++; 33 | } 34 | 35 | void count_collectible(t_automata *a, void *data) 36 | { 37 | t_data *info; 38 | 39 | info = (t_data *)data; 40 | info->collect_count++; 41 | } -------------------------------------------------------------------------------- /MiniShellExample/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 18:51:13 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 18:51:17 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | void automata_init(t_automata *a, void *data) 16 | { 17 | alphabet_init(a); 18 | errors_init(a); 19 | sactions_init(a); 20 | tactions_init(a); 21 | a->data = data; 22 | a->get_state = get_state; 23 | } 24 | 25 | int main(void) 26 | { 27 | t_automata a; 28 | t_data info; 29 | int finalstate; 30 | 31 | ft_bzero(&a, sizeof(t_automata)); 32 | ft_bzero(&info, sizeof(t_data)); 33 | automata_init(&a, &info); 34 | a.str = ft_strdup(" echo 'hola mundo' | cat | grep a > file1 && cat file1"); 35 | printf("%s\n", a.str); 36 | finalstate = evaluate(&a); 37 | if (finalstate > a.errorlen) 38 | get_token(&a, &info); 39 | printf("%d\n", finalstate); 40 | ft_sarrprint(info.tokens); 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /So_LongExample/example.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* example.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/01 09:04:39 by jvasquez #+# #+# */ 9 | /* Updated: 2023/12/01 09:04:40 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include "../libft/libft.h" 16 | #include "../automata/automata.h" 17 | 18 | typedef struct s_data 19 | { 20 | int player_count; 21 | int exit_count; 22 | int collect_count; 23 | } t_data; 24 | 25 | typedef enum e_states 26 | { 27 | FOUND_1, 28 | FOUND_P, 29 | FOUND_E, 30 | FOUND_C, 31 | FOUND_0, 32 | INVALID 33 | } t_states; 34 | 35 | void alphabet_init(t_automata *a); 36 | void errors_init(t_automata *a); 37 | 38 | /* Single Actions */ 39 | void sactions_init(t_automata *a); 40 | void count_player(t_automata *a, void *data); 41 | void count_exit(t_automata *a, void *data); 42 | void count_collectible(t_automata *a, void *data); 43 | 44 | /* Transition Actions */ 45 | void tactions_init(t_automata *a); 46 | -------------------------------------------------------------------------------- /libft/gnl/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/28 14:29:09 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/16 17:16:15 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | char *ft_fillbuf(ssize_t *size, int fd) 16 | { 17 | char *buf; 18 | 19 | buf = malloc(BUFFER_SIZE + 1); 20 | if (!buf) 21 | return (NULL); 22 | *size = read(fd, buf, BUFFER_SIZE); 23 | if (!*size) 24 | return (ft_strfree(&buf)); 25 | buf[*size] = '\0'; 26 | return (buf); 27 | } 28 | 29 | char *get_next_line(int fd) 30 | { 31 | static char *files[4096]; 32 | ssize_t size; 33 | 34 | if (BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0) 35 | return (NULL); 36 | if (!files[fd]) 37 | { 38 | files[fd] = ft_fillbuf(&size, fd); 39 | if (!files[fd]) 40 | return (NULL); 41 | } 42 | if (!files[fd][0]) 43 | if (!ft_bufjoin(&files[fd], fd)) 44 | return (ft_strfree(&files[fd])); 45 | return (ft_getline(&files[fd], fd)); 46 | } 47 | -------------------------------------------------------------------------------- /libft/to/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/05/24 17:39:19 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/28 13:23:03 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int is_sc(char c) 16 | { 17 | int b; 18 | 19 | b = 0; 20 | if (c == ' ' || c == '\t' || c == '\n') 21 | b = 1; 22 | else if (c == '\r' || c == '\v' || c == '\f') 23 | b = 1; 24 | return (b); 25 | } 26 | 27 | int ft_atoi(const char *str) 28 | { 29 | unsigned long int sum; 30 | int sign; 31 | 32 | sum = 0; 33 | sign = 1; 34 | while (is_sc(*str)) 35 | str++; 36 | if (*str == '-' || *str == '+') 37 | { 38 | if (*str == '-') 39 | sign *= -1; 40 | str++; 41 | } 42 | while (*str >= '0' && *str <= '9') 43 | { 44 | sum = - '0' + sum * 10 + *str; 45 | str++; 46 | } 47 | if (sum > 9223372036854775807 && sign == 1) 48 | return (-1); 49 | else if (sum > 9223372036854775807 && sign == -1) 50 | return (0); 51 | return (sign * sum); 52 | } 53 | -------------------------------------------------------------------------------- /MiniShellExample/example.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* example.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/01 09:17:13 by jvasquez #+# #+# */ 9 | /* Updated: 2023/12/01 09:17:14 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include "../libft/libft.h" 16 | #include "../automata/automata.h" 17 | 18 | typedef struct s_data 19 | { 20 | char **tokens; 21 | } t_data; 22 | 23 | typedef enum e_states 24 | { 25 | EMPTY, 26 | DQUOTES, 27 | SQUOTES, 28 | PIPE, 29 | OR, 30 | LESS, 31 | HEREDOC, 32 | GREATER, 33 | APPEND, 34 | AMPER, 35 | AND, 36 | INVALID, 37 | SPACES_NW, 38 | SPACES_BTW, 39 | NOT_OPERATOR 40 | } t_states; 41 | 42 | void alphabet_init(t_automata *a); 43 | void errors_init(t_automata *a); 44 | int get_state(int i, int j); 45 | 46 | /* Single Actions */ 47 | void sactions_init(t_automata *a); 48 | void get_token(t_automata *a, void *data); 49 | // void count_player(t_automata *a, void *data); 50 | 51 | /* Transition Actions */ 52 | void tactions_init(t_automata *a); 53 | -------------------------------------------------------------------------------- /So_LongFileExample/actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:48:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | void count_player(t_automata *a, void *data) 20 | { 21 | t_data *info; 22 | 23 | info = (t_data *)data; 24 | info->player_count++; 25 | if (info->player_count > 1) 26 | exit(EXIT_FAILURE); 27 | } 28 | 29 | void count_exit(t_automata *a, void *data) 30 | { 31 | t_data *info; 32 | 33 | info = (t_data *)data; 34 | info->exit_count++; 35 | if (info->exit_count > 1) 36 | exit(EXIT_FAILURE); 37 | } 38 | 39 | void count_collectible(t_automata *a, void *data) 40 | { 41 | t_data *info; 42 | 43 | info = (t_data *)data; 44 | info->collect_count++; 45 | } 46 | -------------------------------------------------------------------------------- /TreeMSExample/inorder.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* inorder.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/03/28 22:08:39 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/30 13:09:22 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | void print_outfiles(void *data) 16 | { 17 | t_file *file; 18 | 19 | file = (t_file *)data; 20 | if (file->type == APPEND) 21 | printf("archivo para append: %s\n", file->name); 22 | if (file->type == WRITE) 23 | printf("archivo para write: %s\n", file->name); 24 | } 25 | 26 | void printtree(void *data) 27 | { 28 | t_token *token; 29 | 30 | token = (t_token *)data; 31 | printf("line: %s\n", token->line); 32 | printf("cmd: %s\n", token->cmd); 33 | printf("infile:\n"); 34 | ft_sarrprint(token->infile); 35 | printf("args:\n"); 36 | ft_sarrprint(token->args); 37 | printf("outfiles:\n"); 38 | ft_lstiter(token->outfiles, print_outfiles); 39 | printf("\n\n"); 40 | } 41 | 42 | void set_token_type(void *data) 43 | { 44 | t_token *token; 45 | 46 | token = (t_token *)data; 47 | if (ft_strlen(token->line) == 1 && token->line[0] == '|') 48 | token->type = TOK_OPR; 49 | } -------------------------------------------------------------------------------- /So_LongFileExample/print_actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:48:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | void print_empty(t_automata *a, void *data) 20 | { 21 | t_data *info; 22 | 23 | info = (t_data *)data; 24 | // Get img and put img on screen; 25 | printf("0"); 26 | } 27 | 28 | void print_wall(t_automata *a, void *data) 29 | { 30 | t_data *info; 31 | 32 | info = (t_data *)data; 33 | // Get img and put img on screen; 34 | printf("1"); 35 | } 36 | 37 | void print_exit(t_automata *a, void *data) 38 | { 39 | t_data *info; 40 | 41 | info = (t_data *)data; 42 | // Get img and put img on screen; 43 | printf("E"); 44 | } 45 | 46 | void print_collectible(t_automata *a, void *data) 47 | { 48 | t_data *info; 49 | 50 | info = (t_data *)data; 51 | // Get img and put img on screen; 52 | printf("C"); 53 | } 54 | -------------------------------------------------------------------------------- /libft/to/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/22 17:42:46 by jvasquez #+# #+# */ 9 | /* Updated: 2022/06/28 13:14:03 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static long int ft_exp(int base, int exp) 16 | { 17 | long int sol; 18 | 19 | sol = base; 20 | while (--exp) 21 | sol = sol * base; 22 | return (sol); 23 | } 24 | 25 | static char *ft_ini(char *num, long int *n, int *digits) 26 | { 27 | if (*n < 0) 28 | { 29 | *digits += 1; 30 | *n *= -1; 31 | num = malloc(*digits + 1); 32 | if (!num) 33 | return (NULL); 34 | num[0] = '-'; 35 | } 36 | else 37 | { 38 | num = malloc(*digits + 1); 39 | if (!num) 40 | return (NULL); 41 | num[0] = '0'; 42 | } 43 | num[*digits] = '\0'; 44 | return (num); 45 | } 46 | 47 | char *ft_itoa(int n) 48 | { 49 | int digits; 50 | int i; 51 | long int cn; 52 | char *num; 53 | 54 | cn = n; 55 | digits = 1; 56 | while (cn / (ft_exp(10, digits))) 57 | digits++; 58 | num = NULL; 59 | num = ft_ini(num, &cn, &digits); 60 | if (!num) 61 | return (NULL); 62 | i = 0; 63 | while (cn) 64 | { 65 | num[digits - ++i] = '0' + cn % 10; 66 | cn = cn / 10; 67 | } 68 | return (num); 69 | } 70 | -------------------------------------------------------------------------------- /automata/automata.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* automata.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/25 18:53:38 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 22:20:55 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../libft/libft.h" 14 | #include "automata.h" 15 | 16 | int idx(char *alphabet[], char c) 17 | { 18 | int i; 19 | 20 | i = -1; 21 | while (alphabet[++i]) 22 | if (ft_chrpos(alphabet[i], c, 0) != -1) 23 | return (i); 24 | return (i); 25 | } 26 | 27 | int evaluate(t_automata *a) 28 | { 29 | a->ostate = 0; 30 | a->i = -1; 31 | while (++a->i < ft_strlen(a->str)) 32 | { 33 | a->state = a->get_state(a->state, idx(a->alphabet, a->str[a->i])); 34 | if (a->fsa[a->state]) 35 | a->fsa[a->state](a, a->data); 36 | if (a->fta[a->ostate][a->state]) 37 | a->fta[a->ostate][a->state](a, a->data); 38 | a->ostate = a->state; 39 | } 40 | return (a->state); 41 | } 42 | 43 | void evaluate_file(t_automata *a, char *dir, 44 | void (*f)(t_automata *a, int state)) 45 | { 46 | int file; 47 | char *line; 48 | 49 | file = open(dir, O_RDONLY); 50 | line = get_next_line(file); 51 | while (line) 52 | { 53 | a->str = line; 54 | a->state = 0; 55 | f(a, evaluate(a)); 56 | free (line); 57 | line = get_next_line(file); 58 | } 59 | close(file); 60 | } 61 | -------------------------------------------------------------------------------- /So_LongFileExample/example.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* example.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/01 09:04:39 by jvasquez #+# #+# */ 9 | /* Updated: 2023/12/01 09:04:40 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include "../libft/libft.h" 16 | #include "../automata/automata.h" 17 | 18 | typedef struct s_data 19 | { 20 | int player_count; 21 | int exit_count; 22 | int collect_count; 23 | char **map; 24 | } t_data; 25 | 26 | typedef enum e_states 27 | { 28 | EMPTY, 29 | INVALID, 30 | FOUND_P, 31 | FOUND_E, 32 | FOUND_C, 33 | FOUND_0, 34 | FOUND_FIRST, 35 | FOUND_MID, 36 | FOUND_END, 37 | } t_states; 38 | 39 | void alphabet_init(t_automata *a); 40 | void errors_init(t_automata *a); 41 | int get_state(int i, int j); 42 | 43 | /* Single Actions */ 44 | void sactions_init(t_automata *a); 45 | void count_player(t_automata *a, void *data); 46 | void count_exit(t_automata *a, void *data); 47 | void count_collectible(t_automata *a, void *data); 48 | 49 | void print_empty(t_automata *a, void *data); 50 | void print_wall(t_automata *a, void *data); 51 | void print_exit(t_automata *a, void *data); 52 | void print_collectible(t_automata *a, void *data); 53 | 54 | /* Transition Actions */ 55 | void tactions_init(t_automata *a); 56 | -------------------------------------------------------------------------------- /PushSwapExample/actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:48:10 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | void numtotree(t_data *data, t_tree *tree, int n) 20 | { 21 | int *num; 22 | 23 | num = malloc(sizeof(int)); 24 | *num = n; 25 | if (!tree) 26 | data->numbers = tree_new_node(num); 27 | else if (*(int *)(tree->data) == n) 28 | printf("Numero duplicado %d\n",n); 29 | else if (*(int *)(tree->data) < n && tree->right == NULL) 30 | tree->right = tree_new_node(num); 31 | else if (*(int *)(tree->data) > n && tree->left == NULL) 32 | tree->left = tree_new_node(num); 33 | else if (*(int *)(tree->data) < n) 34 | numtotree(data, tree->right, n); 35 | else if (*(int *)(tree->data) > n) 36 | numtotree(data, tree->left, n); 37 | } 38 | 39 | void getnum(t_automata *a, void *data) 40 | { 41 | int numint; 42 | t_tree *tree; 43 | 44 | tree = (t_tree *)((t_data *)data)->numbers; 45 | numint = ft_atoi(a->str + a->j); 46 | a->j = a->i; 47 | numtotree((t_data *)data, tree, numint); 48 | } 49 | -------------------------------------------------------------------------------- /libft/str/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/22 14:43:26 by jvasquez #+# #+# */ 9 | /* Updated: 2022/11/17 00:38:26 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static unsigned int ini_move(const char *str, char delimiter, unsigned int *ini) 16 | { 17 | if (*ini > ft_strlen(str)) 18 | *ini = ft_strlen(str); 19 | while (str[*ini] == delimiter) 20 | *ini += 1; 21 | return (*ini); 22 | } 23 | 24 | static int w_count(char **split, const char *str, char delimiter) 25 | { 26 | size_t pos; 27 | unsigned int ini; 28 | size_t c_word; 29 | 30 | ini = 0; 31 | ini = ini_move(str, delimiter, &ini); 32 | pos = ini; 33 | c_word = 0; 34 | while (str[pos]) 35 | { 36 | if (str[pos + 1] == delimiter || str[pos + 1] == '\0') 37 | { 38 | if (str[ini] != delimiter && str[ini] != '\0') 39 | { 40 | if (split && ft_strlen(str) == 0) 41 | split[c_word] = NULL; 42 | else if (split) 43 | split[c_word] = ft_substr(str, ini, pos - ini + 1); 44 | c_word++; 45 | } 46 | ini = pos + 2; 47 | } 48 | pos++; 49 | } 50 | return (c_word); 51 | } 52 | 53 | char **ft_split(const char *str, char delimiter) 54 | { 55 | size_t c_word; 56 | char **split; 57 | 58 | if (!str) 59 | return (NULL); 60 | split = NULL; 61 | c_word = w_count(split, str, delimiter); 62 | split = ft_calloc(1, sizeof(char *) * (c_word + 1)); 63 | if (!split) 64 | return (NULL); 65 | w_count(split, str, delimiter); 66 | return (split); 67 | } 68 | -------------------------------------------------------------------------------- /PushSwapExample/ainit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ainit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:34:39 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 20:07:46 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /** 16 | * Alphabet definitions 17 | **/ 18 | void alphabet_init(t_automata *a) 19 | { 20 | a->alphabet = ft_sarradd(NULL, " "); 21 | a->alphabet = ft_sarradd(a->alphabet, "-"); 22 | a->alphabet = ft_sarradd(a->alphabet, "0123456789"); 23 | } 24 | 25 | int get_state(int i, int j) 26 | { 27 | int states[][4] = { 28 | // \s, -, D, ^ 29 | {0, 1, 3, 2}, // Empty input 30 | {2, 2, 3, 2}, // Negative sign found 31 | {2, 2, 2, 2}, // Invalid Character 32 | {4, 2, 3, 2}, // Digit found 33 | {4, 1, 3, 2}, // Space after Digits 34 | }; 35 | return (states[i][j]); 36 | } 37 | 38 | /** 39 | * Error strings to print when automata finish on a 40 | * non ending state. 41 | **/ 42 | void errors_init(t_automata *a) 43 | { 44 | a->errors = NULL; 45 | a->errors = ft_sarradd(NULL, "Empty string."); 46 | a->errors = ft_sarradd(a->errors, "Negative sign without a number."); 47 | a->errors = ft_sarradd(a->errors, "Invalid characters"); 48 | a->errorlen = ft_sarrsize(a->errors); 49 | } 50 | 51 | /** 52 | * Simple actions, they trigger when entering a state. 53 | **/ 54 | void sactions_init(t_automata *a) 55 | { 56 | } 57 | 58 | /** 59 | * Transition actions, they trigger when going 60 | * from one state to another. 61 | **/ 62 | void tactions_init(t_automata *a) 63 | { 64 | a->fta[DIGIT][SPACES_BTW] = getnum; 65 | } 66 | -------------------------------------------------------------------------------- /So_LongFileExample/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 18:51:13 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 18:51:17 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | void automata_init(t_automata *a, void *data) 16 | { 17 | alphabet_init(a); 18 | errors_init(a); 19 | sactions_init(a); 20 | tactions_init(a); 21 | a->data = data; 22 | a->get_state = get_state; 23 | } 24 | 25 | void state_handle(t_automata *a, int state) 26 | { 27 | if (a->state < a->errorlen) 28 | { 29 | printf("%s\n", a->errors[a->state]); 30 | exit (EXIT_FAILURE); 31 | } 32 | ((t_data *)a->data)->map = ft_sarradd(((t_data *)a->data)->map, a->str); 33 | } 34 | 35 | int main(void) 36 | { 37 | t_automata a; 38 | t_data info; 39 | 40 | ft_bzero(&a, sizeof(t_automata)); 41 | ft_bzero(&info, sizeof(t_data)); 42 | automata_init(&a, &info); 43 | evaluate_file(&a, "map.ber", state_handle); 44 | if (!info.player_count || !info.exit_count) 45 | exit (EXIT_FAILURE); 46 | printf("there are:\n%d player\n%d exit\n%d collectible\n", 47 | info.player_count, info.exit_count, info.collect_count); 48 | 49 | /* Changing actions to print background */ 50 | a.fsa[FOUND_0] = print_empty; 51 | a.fsa[FOUND_P] = print_empty; 52 | a.fsa[FOUND_E] = print_exit; 53 | a.fsa[FOUND_C] = print_collectible; 54 | a.fsa[FOUND_FIRST] = print_wall; 55 | a.fsa[FOUND_MID] = print_wall; 56 | a.fsa[FOUND_END] = print_wall; 57 | 58 | /* Using the automata to print map */ 59 | int i = -1; 60 | while (info.map[++i]) 61 | { 62 | a.str = info.map[i]; 63 | a.i = 0; 64 | evaluate(&a); 65 | printf("\n"); 66 | } 67 | return (0); 68 | } 69 | -------------------------------------------------------------------------------- /TreeMSExample/tokenizer/ta_actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ta_actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 22:05:49 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | /* Reserve memory for the new token */ 20 | t_token *new_token(char *cmdline) 21 | { 22 | t_token *token; 23 | 24 | token = malloc(sizeof(t_token)); 25 | ft_bzero(token, sizeof(t_token)); 26 | token->line = cmdline; 27 | return token; 28 | } 29 | 30 | /* Adds a token to the tree, so it can be execute inorder */ 31 | void add_token(t_tree *tree, char *cmdline) 32 | { 33 | if (!tree->left) 34 | { 35 | tree->left = tree_new_node(tree->data); 36 | tree->data = new_token(cmdline); 37 | } 38 | else if (!tree->right) 39 | tree->right = tree_new_node(new_token(cmdline)); 40 | else 41 | add_token(tree->right, cmdline); 42 | } 43 | 44 | /* Get the cmd line between pipes and also get the pipe into a new token */ 45 | void get_token(t_automata *a, void *data) 46 | { 47 | char *cmdline; 48 | char *trim; 49 | t_data *info; 50 | t_tree *tree; 51 | 52 | info = (t_data *)data; 53 | cmdline = ft_substr(a->str, a->j, a->i - a->j); 54 | trim = ft_strtrim(cmdline, " "); 55 | free (cmdline); 56 | a->j = a->i; 57 | if (!info->tree) 58 | info->tree = tree_new_node(new_token(trim)); 59 | else 60 | add_token(info->tree, trim); 61 | } 62 | 63 | /* A function to be executed when falling into INVALID state */ 64 | void tokenizer_print_error(t_automata *a, void *data) 65 | { 66 | printf("Error found at pos: %d", a->i); 67 | exit(EXIT_FAILURE); 68 | } 69 | -------------------------------------------------------------------------------- /libft/printf/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/07/04 11:45:00 by jvasquez #+# #+# */ 9 | /* Updated: 2022/09/05 14:56:06 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int pf_putunsigned(unsigned long long nbr) 16 | { 17 | return (pf_putnbr_base(nbr, "0123456789", 0)); 18 | } 19 | 20 | int pf_nbrlen(long int n) 21 | { 22 | int i; 23 | 24 | if (n == 0) 25 | return (1); 26 | i = 1; 27 | while (n && i++) 28 | n /= 10; 29 | return (--i); 30 | } 31 | 32 | size_t pf_putptr(unsigned long long ptr) 33 | { 34 | size_t size; 35 | 36 | write(1, "0x", 2); 37 | size = pf_putnbr_base(ptr, "0123456789abcdef", 0) + 2; 38 | return (size); 39 | } 40 | 41 | int pf_switchcase(va_list args, const char c) 42 | { 43 | int size; 44 | 45 | size = 0; 46 | if (c == 'c') 47 | size = pf_putchar_fd(va_arg(args, int), 1); 48 | if (c == 's') 49 | size = pf_putstr_fd(va_arg(args, char *), 1); 50 | if (c == 'd') 51 | size = pf_putnbr_fd(va_arg(args, int), 1); 52 | if (c == 'i') 53 | size = pf_putnbr_fd(va_arg(args, int), 1); 54 | if (c == 'u') 55 | size = pf_putunsigned(va_arg(args, unsigned int)); 56 | if (c == 'x') 57 | size = pf_putnbr_base(va_arg(args, int), "0123456789abcdef", 1); 58 | if (c == 'X') 59 | size = pf_putnbr_base(va_arg(args, int), "0123456789ABCDEF", 1); 60 | if (c == 'p') 61 | size = pf_putptr(va_arg(args, unsigned long long)); 62 | if (c == '%') 63 | size = write(1, "%", 1); 64 | return (size); 65 | } 66 | 67 | int ft_printf(char const *str, ...) 68 | { 69 | size_t i; 70 | size_t size; 71 | va_list args; 72 | 73 | va_start(args, str); 74 | size = 0; 75 | i = -1; 76 | while (str[++i]) 77 | { 78 | if (str[i] == '%') 79 | { 80 | size += pf_switchcase(args, str[i + 1]); 81 | i += 2; 82 | } 83 | if (str[i] == '%') 84 | i--; 85 | else if (pf_strlen(str) > i) 86 | size += write(1, &str[i], 1); 87 | } 88 | va_end(args); 89 | return (size); 90 | } 91 | -------------------------------------------------------------------------------- /So_LongExample/ainit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ainit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:34:39 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:34:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /** 16 | * Alphabet definitions 17 | **/ 18 | void alphabet_init(t_automata *a) 19 | { 20 | a->alphabet = ft_sarradd(NULL, "1"); 21 | a->alphabet = ft_sarradd(a->alphabet, "P"); 22 | a->alphabet = ft_sarradd(a->alphabet, "E"); 23 | a->alphabet = ft_sarradd(a->alphabet, "C"); 24 | a->alphabet = ft_sarradd(a->alphabet, "0"); 25 | } 26 | 27 | int get_state(int i, int j) 28 | { 29 | int states[][6] = { 30 | // 1, P, E, C, 0, ^ 31 | {6, 1, 1, 1, 1, 1}, // 0 Empty input 32 | {1, 1, 1, 1, 1, 1}, // 1 Invalid Character 33 | {8, 2, 3, 4, 5, 1}, // 2 Player Found 34 | {8, 2, 3, 4, 5, 1}, // 3 Exit Found 35 | {8, 2, 3, 4, 5, 1}, // 4 Collectible Found 36 | {8, 2, 3, 4, 5, 1}, // 5 Blank Space found 37 | {7, 2, 3, 4, 5, 1}, // 6 First Wall 38 | {8, 2, 3, 4, 5, 1}, // 7 Mid Wall 39 | {8, 2, 3, 4, 5, 1}, // 8 End Wall 40 | }; 41 | 42 | return (states[i][j]); 43 | } 44 | 45 | /** 46 | * Error strings to print when automata finish on a 47 | * non ending state. 48 | **/ 49 | void errors_init(t_automata *a) 50 | { 51 | a->errors = NULL; 52 | a->errors = ft_sarradd(NULL, "Empty string."); 53 | a->errors = ft_sarradd(a->errors, "Invalid character"); 54 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 55 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 56 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 57 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 58 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 59 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 60 | a->errorlen = ft_sarrsize(a->errors); 61 | } 62 | 63 | /** 64 | * Simple actions, they trigger when entering a state. 65 | **/ 66 | void sactions_init(t_automata *a) 67 | { 68 | a->fsa[FOUND_P] = count_player; 69 | a->fsa[FOUND_E] = count_exit; 70 | a->fsa[FOUND_C] = count_collectible; 71 | } 72 | 73 | /** 74 | * Transition actions, they trigger when going 75 | * from one state to another. 76 | **/ 77 | void tactions_init(t_automata *a) 78 | { 79 | } 80 | -------------------------------------------------------------------------------- /So_LongFileExample/ainit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ainit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:34:39 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:34:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /** 16 | * Alphabet definitions 17 | **/ 18 | void alphabet_init(t_automata *a) 19 | { 20 | a->alphabet = ft_sarradd(NULL, "1"); 21 | a->alphabet = ft_sarradd(a->alphabet, "P"); 22 | a->alphabet = ft_sarradd(a->alphabet, "E"); 23 | a->alphabet = ft_sarradd(a->alphabet, "C"); 24 | a->alphabet = ft_sarradd(a->alphabet, "0"); 25 | a->alphabet = ft_sarradd(a->alphabet, "\n"); 26 | } 27 | 28 | int get_state(int i, int j) 29 | { 30 | const int states[][7] = { 31 | // 1, P, E, C, 0,\n, ^ 32 | {6, 1, 1, 1, 1, 1, 1}, // 0 Empty input 33 | {1, 1, 1, 1, 1, 1, 1}, // 1 Invalid Character 34 | {8, 2, 3, 4, 5, 1, 1}, // 2 Player Found 35 | {8, 2, 3, 4, 5, 1, 1}, // 3 Exit Found 36 | {8, 2, 3, 4, 5, 1, 1}, // 4 Collectible Found 37 | {8, 2, 3, 4, 5, 1, 1}, // 5 Blank Space found 38 | {7, 2, 3, 4, 5, 1, 1}, // 6 First Wall 39 | {8, 2, 3, 4, 5, 1, 1}, // 7 Mid Wall 40 | {8, 2, 3, 4, 5, 8, 1}, // 8 End Wall 41 | }; 42 | 43 | return (states[i][j]); 44 | } 45 | 46 | /** 47 | * Error strings to print when automata finish on a 48 | * non ending state. 49 | **/ 50 | void errors_init(t_automata *a) 51 | { 52 | a->errors = NULL; 53 | a->errors = ft_sarradd(NULL, "Empty string."); 54 | a->errors = ft_sarradd(a->errors, "Invalid character"); 55 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 56 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 57 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 58 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 59 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 60 | a->errors = ft_sarradd(a->errors, "Non ending wall"); 61 | a->errorlen = ft_sarrsize(a->errors); 62 | } 63 | 64 | /** 65 | * Simple actions, they trigger when entering a state. 66 | **/ 67 | void sactions_init(t_automata *a) 68 | { 69 | a->fsa[FOUND_P] = count_player; 70 | a->fsa[FOUND_E] = count_exit; 71 | a->fsa[FOUND_C] = count_collectible; 72 | } 73 | 74 | /** 75 | * Transition actions, they trigger when going 76 | * from one state to another. 77 | **/ 78 | void tactions_init(t_automata *a) 79 | { 80 | } 81 | -------------------------------------------------------------------------------- /TreeMSExample/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 18:51:13 by jvasquez #+# #+# */ 9 | /* Updated: 2024/04/12 19:15:57 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /* Function to iterate the Tree to evaluate and organize the tokens */ 16 | void evaluate_token(void *data, void *context) 17 | { 18 | int finalstate; 19 | t_token *token; 20 | t_data *info; 21 | 22 | token = (t_token *)data; 23 | info = (t_data *)context; 24 | 25 | info->ctoken = token; // Set the token pointer to access easily from organize_token function 26 | info->organizer->str = token->line; // Set the organizer string to be analized 27 | info->organizer->i = 0; // Reset the automata data 28 | info->organizer->j = 0; 29 | info->organizer->state = 0; 30 | info->organizer->ostate = 0; 31 | info->memory = 0; 32 | 33 | finalstate = evaluate(info->organizer); 34 | if (finalstate > info->organizer->errorlen) 35 | organize_token(info->organizer, info); 36 | else 37 | printf("%s\n", info->organizer->errors[finalstate]); 38 | } 39 | 40 | int main(void) 41 | { 42 | t_automata tokenizer; 43 | t_automata organizer; 44 | t_data info; 45 | int finalstate; 46 | 47 | ft_bzero(&tokenizer, sizeof(t_automata)); 48 | ft_bzero(&organizer, sizeof(t_automata)); 49 | ft_bzero(&info, sizeof(t_data)); 50 | tokenizer_automata_init(&tokenizer, &info); 51 | organizer_automata_init(&organizer, &info); 52 | 53 | /* Evaluates the first automata and check if the last state is valid */ 54 | tokenizer.str = ft_strdup(" cat -e < infile1 < infile2 > outfile1 >> outfile2 > outfile3 | cmd <> iofile | cmd2 | cmd3"); 55 | // printf("%s\n", tokenizer.str); 56 | 57 | finalstate = evaluate(&tokenizer); 58 | if (finalstate > tokenizer.errorlen) 59 | get_token(&tokenizer, &info); 60 | else 61 | printf("%s\n", tokenizer.errors[finalstate]); 62 | 63 | // printf("\nImprimir árbol tokenizado\n"); 64 | // tree_in_order(info.tree, printtree); 65 | 66 | 67 | /* Set token type to CMD or OPERATOR */ 68 | tree_in_order(info.tree, set_token_type); 69 | 70 | 71 | /* Evaluates the second automata and check if the last state is valid */ 72 | info.organizer = &organizer; 73 | tree_in_order_context(info.tree, evaluate_token, &info); 74 | 75 | printf("\nImprimir arbol organizado\n"); 76 | tree_in_order(info.tree, printtree); 77 | return (0); 78 | } -------------------------------------------------------------------------------- /libft/gnl/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/28 15:17:45 by jvasquez #+# #+# */ 9 | /* Updated: 2022/07/16 17:16:16 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | void *ft_strfree(char **buf) 16 | { 17 | free (buf[0]); 18 | buf[0] = NULL; 19 | return (NULL); 20 | } 21 | 22 | /*int ft_strlen(char *str) 23 | { 24 | int j; 25 | 26 | j = 0; 27 | while (str[j] != '\0') 28 | j++; 29 | return (j); 30 | }*/ 31 | 32 | ssize_t ft_bufjoin(char **buf, int fd) 33 | { 34 | char *joined; 35 | int i; 36 | int j; 37 | char *str; 38 | ssize_t size; 39 | 40 | size = 0; 41 | str = ft_fillbuf(&size, fd); 42 | if (!str ||!size) 43 | return (size); 44 | joined = malloc(sizeof(char) * (size + ft_strlen(buf[0]) + 1)); 45 | if (!joined) 46 | return (size); 47 | i = -1; 48 | while (buf[0][++i] != '\0') 49 | joined[i] = buf[0][i]; 50 | j = -1; 51 | while (str[++j] != '\0') 52 | joined[i++] = str[j]; 53 | joined[i] = '\0'; 54 | free (str); 55 | str = NULL; 56 | free (buf[0]); 57 | buf[0] = joined; 58 | return (size); 59 | } 60 | 61 | void ft_cleanbuf(char **buf) 62 | { 63 | char *str; 64 | unsigned int i; 65 | unsigned int j; 66 | 67 | i = 0; 68 | while (buf[0][i] != '\0' && buf[0][i] != '\n') 69 | i++; 70 | if (buf[0][i] == '\0') 71 | { 72 | free (buf[0]); 73 | buf[0] = NULL; 74 | return ; 75 | } 76 | str = malloc(sizeof(char) * (ft_strlen(buf[0]) - i + 1)); 77 | if (!str) 78 | return ; 79 | j = 0; 80 | while (buf[0][++i] != '\0') 81 | str[j++] = buf[0][i]; 82 | str[j] = '\0'; 83 | free (buf[0]); 84 | *buf = str; 85 | } 86 | 87 | char *ft_getline(char **buf, int fd) 88 | { 89 | char *line; 90 | unsigned int i; 91 | unsigned int j; 92 | 93 | i = 0; 94 | while (buf[0][i] != '\n' && buf[0][i] != '\0') 95 | { 96 | if (buf[0][i + 1] == '\0') 97 | ft_bufjoin(buf, fd); 98 | i++; 99 | } 100 | if (buf[0][i] == '\n') 101 | line = (char *)malloc(sizeof(char) * (i + 2)); 102 | else 103 | line = (char *)malloc(sizeof(char) * (i + 1)); 104 | if (!line) 105 | return (NULL); 106 | j = -1; 107 | while (++j < i) 108 | line[j] = buf[0][j]; 109 | if (buf[0][i] == '\n') 110 | line[i++] = '\n'; 111 | line[i] = '\0'; 112 | ft_cleanbuf(buf); 113 | return (line); 114 | } 115 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # automatas 2 | 3 | This program implements an automaton using a finite state machine (FSM). The main 4 | file, `automata.c`, contains functions for evaluating a string based on the FSM, 5 | and `automata.h` defines the structures and functions related to the automaton. 6 | 7 | 8 | ### Automata Structure 9 | 10 | ```c 11 | typedef struct s_automata 12 | { 13 | // Pointer to a desired structure to save / read data. 14 | void *data; 15 | 16 | // Array of strings with chars to be recognized. 17 | char **alphabet; 18 | 19 | // Array of strings describing the error states. 20 | char **errors; 21 | 22 | // String to be analize. 23 | char *str; 24 | 25 | // Current State. 26 | int state; 27 | 28 | // Old/Previous State. 29 | int ostate; 30 | 31 | // Error states quantity. 32 | int errorlen; 33 | 34 | // Current parsing string index. 35 | int i; 36 | 37 | // Auxiliar index to make easier getting substrings. 38 | int j; 39 | 40 | // Array of functions, those will be activated just if a function is assigned to a state. 41 | void (*fsa[20])(struct s_automata *a, void *data); 42 | 43 | // Matrix of functions, those will be activated just if a function is assigned to it (two states needed). 44 | void (*fta[20][20])(struct s_automata *a, void *data); 45 | 46 | // A function just to get a state from the state matrix. 47 | int (*get_state)(int state, int abc_idx); 48 | } t_automata; 49 | 50 | 51 | ### Usage 52 | 53 | To use the automaton, follow these steps: 54 | 55 | 1. Include the required header file: 56 | 57 | ```c 58 | #include "automata.h" 59 | 60 | 2. Initialize: 61 | ```c 62 | t_automata a; 63 | 64 | automata_init(&a, data); 65 | alphabet_init(&a); 66 | errors_init(&a); 67 | sactions_init(&a); 68 | tactions_init(&a); 69 | a.str = "your_input_string"; 70 | 71 | 3. Use the evaluate function: 72 | ```c 73 | int final_state = evaluate(&a); 74 | 75 | 76 | ### Examples 77 | 78 | There are some examples inspired on 42 projects. 79 | 80 | **So_Long:** A map parsing where each line must start and finish with '1' and only a few allowed characters are permitted. 81 | 82 | **PushSwap:** A program designed to sort given numbers, therefore, the parser should only accept digits, spaces and the minus sign. 83 | 84 | **MiniShell:** Usually uses two automatons to parse the input, this example shows demonstrates a naive approach to real parsing, 85 | it needs improvement. 86 | 87 | 88 | ### Links 89 | 90 | The [presentation]([URL](https://docs.google.com/presentation/d/1ufalJ-XsxPrYjmpsoG6mrgfZn7XBrcOB6rXR-9xw56w/edit#slide=id.g1ec4f24304a_0_235)https://docs.google.com/presentation/d/1ufalJ-XsxPrYjmpsoG6mrgfZn7XBrcOB6rXR-9xw56w/edit#slide=id.g1ec4f24304a_0_235) given in 42 where these examples where shown and eplained. 91 | 92 | Some [videos](https://www.youtube.com/watch?v=0x_dbHi7dEY&list=PLrsxkaDs20ZTCxLHHe66YHhuOxkwJnSMH&index=1) with theoretical content. 93 | 94 | A [file](https://holub.com/goodies/compiler/compilerDesignInC.pdf) about Compiler Design in C, useful for those working on the MiniShell project. 95 | 96 | -------------------------------------------------------------------------------- /TreeMSExample/tokenizer/ta_ainit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ta_ainit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:34:39 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 22:23:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../example.h" 14 | 15 | /** 16 | * Alphabet definitions 17 | **/ 18 | void tokenizer_alphabet_init(t_automata *a) 19 | { 20 | a->alphabet = ft_sarradd(NULL, " "); 21 | a->alphabet = ft_sarradd(a->alphabet, "|"); 22 | a->alphabet = ft_sarradd(a->alphabet, "\""); 23 | a->alphabet = ft_sarradd(a->alphabet, "\'"); 24 | } 25 | 26 | int tokenizer_get_state(int i, int j) 27 | { 28 | const int states[][8] = { 29 | // \s, |, ", ', ^ 30 | { 0, 3, 1, 2, 7}, // 0 Empty input 31 | { 1, 1, 6, 1, 1}, // 1 Open double quotes 32 | { 2, 2, 2, 6, 2}, // 2 Open single quotes 33 | { 5, 4, 4, 4, 7}, // 3 Pipe open 34 | { 4, 4, 4, 4, 4}, // 4 Invalid input 35 | { 5, 4, 1, 2, 7}, // 5 Spaces without words 36 | { 6, 3, 1, 2, 7}, // 6 Spaces between words 37 | { 6, 3, 1, 2, 7}, // 7 Not operators 38 | }; 39 | return (states[i][j]); 40 | } 41 | 42 | /** 43 | * Error strings to print when automata finish on a 44 | * non ending state. 45 | **/ 46 | void tokenizer_errors_init(t_automata *a) 47 | { 48 | a->errors = ft_sarradd(NULL, "Empty string."); 49 | a->errors = ft_sarradd(a->errors, "Open double quotes"); 50 | a->errors = ft_sarradd(a->errors, "Open single quotes"); 51 | a->errors = ft_sarradd(a->errors, "Pipe open"); 52 | a->errors = ft_sarradd(a->errors, "Invalid input"); 53 | a->errorlen = ft_sarrsize(a->errors); 54 | } 55 | 56 | /** 57 | * Simple actions, they trigger when entering a state. 58 | **/ 59 | void tokenizer_sactions_init(t_automata *a) 60 | { 61 | a->fsa[TA_INVALID] = tokenizer_print_error; 62 | } 63 | 64 | /** 65 | * Transition actions, they trigger when going 66 | * from one state to another. 67 | **/ 68 | void tokenizer_tactions_init(t_automata *a) 69 | { 70 | a->fta[TA_SPACES_NW][TA_PIPE] = get_token; 71 | a->fta[TA_SPACES_BTW][TA_PIPE] = get_token; 72 | a->fta[TA_NOT_OPERATOR][TA_PIPE] = get_token; 73 | a->fta[TA_PIPE][TA_NOT_OPERATOR] = get_token; 74 | a->fta[TA_PIPE][TA_SPACES_NW] = get_token; 75 | } 76 | 77 | /** 78 | * Initialize all the automata components 79 | **/ 80 | void tokenizer_automata_init(t_automata *a, void *data) 81 | { 82 | tokenizer_alphabet_init(a); 83 | tokenizer_errors_init(a); 84 | tokenizer_sactions_init(a); 85 | tokenizer_tactions_init(a); 86 | a->data = data; 87 | a->get_state = tokenizer_get_state; 88 | } -------------------------------------------------------------------------------- /TreeMSExample/organizer/oa_ainit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* oa_ainit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:34:39 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 23:33:09 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../example.h" 14 | 15 | /** 16 | * Alphabet definitions 17 | **/ 18 | void organizer_alphabet_init(t_automata *a) 19 | { 20 | a->alphabet = ft_sarradd(NULL, " "); 21 | a->alphabet = ft_sarradd(a->alphabet, "\""); 22 | a->alphabet = ft_sarradd(a->alphabet, "\'"); 23 | a->alphabet = ft_sarradd(a->alphabet, "<"); 24 | a->alphabet = ft_sarradd(a->alphabet, ">"); 25 | } 26 | 27 | int organizer_get_state(int i, int j) 28 | { 29 | const int states[][8] = { 30 | // \s, ", ', <, >, ^ 31 | { 0, 1, 2, 6, 8, 10}, // 0 Empty input 32 | { 1, 5, 1, 1, 1, 1}, // 1 Open double quotes 33 | { 2, 2, 5, 2, 2, 2}, // 2 Open single quotes 34 | { 3, 3, 3, 3, 3, 3}, // 3 Invalid input 35 | { 4, 1, 2, 6, 8, 10}, // 4 Spaces without words 36 | { 5, 1, 2, 6, 8, 10}, // 5 Spaces between words 37 | { 4, 1, 2, 7, 8, 10}, // 6 lower found 38 | { 4, 1, 2, 3, 3, 10}, // 7 heredoc 39 | { 4, 1, 2, 3, 9, 10}, // 8 greater found 40 | { 4, 1, 2, 3, 3, 10}, // 9 append 41 | { 5, 1, 2, 6, 8, 10}, // 10 Not operators 42 | }; 43 | return (states[i][j]); 44 | } 45 | 46 | /** 47 | * Error strings to print when automata finish on a 48 | * non ending state. 49 | **/ 50 | void organizer_errors_init(t_automata *a) 51 | { 52 | a->errors = ft_sarradd(NULL, "Empty string."); 53 | a->errors = ft_sarradd(a->errors, "Open double quotes"); 54 | a->errors = ft_sarradd(a->errors, "Open single quotes"); 55 | a->errors = ft_sarradd(a->errors, "Invalid input"); 56 | a->errorlen = ft_sarrsize(a->errors); 57 | } 58 | 59 | /** 60 | * Simple actions, they trigger when entering a state. 61 | **/ 62 | void organizer_sactions_init(t_automata *a) 63 | { 64 | a->fsa[OA_INVALID] = organizer_print_error; 65 | a->fsa[OA_LOWER] = save_in_memory; 66 | a->fsa[OA_GREATER] = save_in_memory; 67 | a->fsa[OA_HEREDOC] = save_in_memory; 68 | a->fsa[OA_APPEND] = save_in_memory; 69 | } 70 | 71 | /** 72 | * Transition actions, they trigger when going 73 | * from one state to another. 74 | **/ 75 | void organizer_tactions_init(t_automata *a) 76 | { 77 | const int states = 11; 78 | int state; 79 | 80 | state = -1; 81 | while (++state < states) 82 | { 83 | a->fta[OA_LOWER][state] = skip_op; 84 | a->fta[OA_GREATER][state] = skip_op; 85 | a->fta[OA_HEREDOC][state] = skip_op; 86 | a->fta[OA_APPEND][state] = skip_op; 87 | a->fta[OA_NOT_OPERATOR][state] = organize_token; 88 | } 89 | a->fta[OA_SQUOTES][OA_SPACES_BTW] = organize_token; 90 | a->fta[OA_DQUOTES][OA_SPACES_BTW] = organize_token; 91 | a->fta[OA_NOT_OPERATOR][OA_NOT_OPERATOR] = NULL; 92 | } 93 | 94 | /** 95 | * Initialize all the automata components 96 | **/ 97 | void organizer_automata_init(t_automata *a, void *data) 98 | { 99 | organizer_alphabet_init(a); 100 | organizer_errors_init(a); 101 | organizer_sactions_init(a); 102 | organizer_tactions_init(a); 103 | a->data = data; 104 | a->get_state = organizer_get_state; 105 | } 106 | -------------------------------------------------------------------------------- /TreeMSExample/example.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* example.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/12/01 08:08:26 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/30 13:02:16 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include "../libft/libft.h" 16 | #include "../automata/automata.h" 17 | 18 | /* Token Types */ 19 | typedef enum e_toktype 20 | { 21 | TOK_CMD, 22 | TOK_OPR 23 | } t_toktype; 24 | 25 | typedef enum e_filetype 26 | { 27 | APPEND, 28 | WRITE 29 | } t_filetype; 30 | 31 | typedef struct s_file 32 | { 33 | char *name; 34 | t_filetype type; 35 | } t_file; 36 | 37 | typedef struct s_token 38 | { 39 | t_toktype type; 40 | char *line; 41 | char *cmd; 42 | char **args; 43 | char **infile; 44 | t_list *outfiles; 45 | } t_token; 46 | 47 | typedef struct s_data 48 | { 49 | t_tree *tree; 50 | t_automata *organizer; // Pointer to organizer to simplify recoursive calls 51 | t_token *ctoken; // Pointer to current organizing token to simplify recoursive calls 52 | int memory; // 32 bits to save some information for the automata 53 | } t_data; 54 | 55 | /* States for tokenizer automata */ 56 | typedef enum e_tstates 57 | { 58 | TA_EMPTY, 59 | TA_DQUOTES, 60 | TA_SQUOTES, 61 | TA_PIPE, 62 | TA_INVALID, 63 | TA_SPACES_NW, 64 | TA_SPACES_BTW, 65 | TA_NOT_OPERATOR 66 | } t_tstates; 67 | 68 | /* States for organizer automata */ 69 | typedef enum e_ostates 70 | { 71 | OA_EMPTY, 72 | OA_DQUOTES, 73 | OA_SQUOTES, 74 | OA_INVALID, 75 | OA_SPACES_NW, 76 | OA_SPACES_BTW, 77 | OA_LOWER, 78 | OA_HEREDOC, 79 | OA_GREATER, 80 | OA_APPEND, 81 | OA_NOT_OPERATOR, 82 | } t_ostates; 83 | 84 | /* States for organizer automata */ 85 | typedef enum e_memorybits 86 | { 87 | CMD_TAKEN, 88 | REDIR_TAKEN, 89 | MEM_LOWER, 90 | MEM_HEREDOC, 91 | MEM_GREATER, 92 | MEM_APPEND, 93 | } t_memorybits; 94 | 95 | /* Tree functions */ 96 | void printtree(void *data); 97 | void set_token_type(void *data); 98 | 99 | /* ------ TOKENIZER FUNCTIONS ------ */ 100 | /* Initialization */ 101 | void tokenizer_automata_init(t_automata *a, void *data); 102 | void tokenizer_alphabet_init(t_automata *a); 103 | void tokenizer_errors_init(t_automata *a); 104 | 105 | /* Single Actions */ 106 | void tokenizer_sactions_init(t_automata *a); 107 | void tokenizer_print_error(t_automata *a, void *data); 108 | 109 | /* Transition Actions */ 110 | void tokenizer_tactions_init(t_automata *a); 111 | void get_token(t_automata *a, void *data); 112 | 113 | int tokenizer_get_state(int i, int j); 114 | 115 | 116 | /* ------ ORGANIZER FUNCTIONS ------ */ 117 | void organizer_automata_init(t_automata *a, void *data); 118 | void organizer_alphabet_init(t_automata *a); 119 | void organizer_errors_init(t_automata *a); 120 | 121 | /* Single Actions */ 122 | void organizer_sactions_init(t_automata *a); 123 | void organizer_print_error(t_automata *a, void *data); 124 | void save_in_memory(t_automata *a, void *data); 125 | 126 | /* Transition Actions */ 127 | void organizer_tactions_init(t_automata *a); 128 | void organize_token(t_automata *a, void *data); 129 | void skip_op(t_automata *a, void *data); 130 | 131 | int organizer_get_state(int i, int j); 132 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jvasquez +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2022/06/13 17:31:12 by jvasquez #+# #+# # 9 | # Updated: 2024/03/28 22:52:04 by jvasquez ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | INCLUDES = . 15 | CFLAGS = -Wall -Wextra -Werror -I $(INCLUDES) 16 | DIR_LST = ./lst/ 17 | DIR_MEM = ./mem/ 18 | DIR_PUT = ./put/ 19 | DIR_CHECK= ./check/ 20 | DIR_BW = ./bitwise/ 21 | DIR_TO = ./to/ 22 | DIR_STR = ./str/ 23 | DIR_PF = ./printf/ 24 | DIR_GNL = ./gnl/ 25 | DIR_ARR = ./arr/ 26 | DIR_TREE = ./tree/ 27 | 28 | GNL = get_next_line.c \ 29 | get_next_line_utils.c 30 | 31 | PF = ft_printf.c \ 32 | pf_putnbr_fd.c \ 33 | pf_putstr_fd.c \ 34 | pf_strlen.c \ 35 | pf_putchar_fd.c \ 36 | pf_putnbr_base.c 37 | 38 | PUT = ft_putchar_fd.c \ 39 | ft_putstr_fd.c \ 40 | ft_putendl_fd.c \ 41 | ft_putnbr_fd.c 42 | 43 | TO = ft_atoi.c \ 44 | ft_itoa.c \ 45 | ft_atoi_pos.c \ 46 | ft_atoi_btwn.c \ 47 | ft_atod.c 48 | 49 | MEM = ft_bzero.c \ 50 | ft_calloc.c \ 51 | ft_memchr.c \ 52 | ft_memcmp.c \ 53 | ft_memcpy.c \ 54 | ft_memmove.c \ 55 | ft_memset.c 56 | 57 | BW = check_bit.c \ 58 | set_bit.c \ 59 | not_bit.c \ 60 | print_bits.c \ 61 | check_mask_and.c \ 62 | check_mask_not.c \ 63 | check_mask_or.c 64 | 65 | STR = ft_strlen.c \ 66 | ft_strlcat.c \ 67 | ft_strchr.c \ 68 | ft_strrchr.c \ 69 | ft_strncmp.c \ 70 | ft_strnstr.c \ 71 | ft_strdup.c \ 72 | ft_strjoin.c \ 73 | ft_strjoinfree.c \ 74 | ft_split.c \ 75 | ft_unsplit.c \ 76 | ft_strtrim.c \ 77 | ft_strmapi.c \ 78 | ft_striteri.c \ 79 | ft_strlcpy.c \ 80 | ft_toupper.c \ 81 | ft_tolower.c \ 82 | ft_substr.c \ 83 | ft_strbrk.c \ 84 | ft_strrep.c \ 85 | ft_strfree.c \ 86 | ft_chr_count.c \ 87 | ft_strstr.c \ 88 | ft_strrep.c \ 89 | ft_chrpos.c \ 90 | ft_strget_btwn.c 91 | 92 | CHECK = ft_isalpha.c \ 93 | ft_isdigit.c \ 94 | ft_isalnum.c \ 95 | ft_isascii.c \ 96 | ft_isprint.c \ 97 | ft_isspace.c 98 | 99 | LST = ft_lstnew_bonus.c \ 100 | ft_lstadd_front_bonus.c \ 101 | ft_lstadd_back_bonus.c \ 102 | ft_lstlast_bonus.c \ 103 | ft_lstsize_bonus.c \ 104 | ft_lstdelone_bonus.c \ 105 | ft_lstclear_bonus.c \ 106 | ft_lstiter_bonus.c \ 107 | ft_lstmap_bonus.c 108 | 109 | ARR = ft_sarrcpy.c \ 110 | ft_sarrsize.c \ 111 | ft_sarradd.c \ 112 | ft_sarrfree.c \ 113 | ft_sarrrmi.c \ 114 | ft_sarrprint.c \ 115 | ft_sarrcmp.c \ 116 | ft_sarrrep.c 117 | 118 | TREE = tree_in_order.c \ 119 | tree_pre_order.c \ 120 | tree_new_node.c \ 121 | tree_del_node.c \ 122 | tree_post_order.c 123 | 124 | OBJS = $(addprefix $(DIR_LST), ${LST:.c=.o}) 125 | OBJS += $(addprefix $(DIR_RGX), ${RGX:.c=.o}) 126 | OBJS += $(addprefix $(DIR_MEM), ${MEM:.c=.o}) 127 | OBJS += $(addprefix $(DIR_PUT), ${PUT:.c=.o}) 128 | OBJS += $(addprefix $(DIR_CHECK), ${CHECK:.c=.o}) 129 | OBJS += $(addprefix $(DIR_BW), ${BW:.c=.o}) 130 | OBJS += $(addprefix $(DIR_TO), ${TO:.c=.o}) 131 | OBJS += $(addprefix $(DIR_STR), ${STR:.c=.o}) 132 | OBJS += $(addprefix $(DIR_PF), ${PF:.c=.o}) 133 | OBJS += $(addprefix $(DIR_GNL), ${GNL:.c=.o}) 134 | OBJS += $(addprefix $(DIR_ARR), ${ARR:.c=.o}) 135 | OBJS += $(addprefix $(DIR_TREE), ${TREE:.c=.o}) 136 | 137 | # pwd 138 | # gcc $(CFLAGS) -c $< -o ${<:.c=.o} 139 | 140 | ${NAME}: ${OBJS} 141 | ar rcs ${NAME} ${OBJS} 142 | 143 | all: $(NAME) 144 | 145 | clean: 146 | rm -f $(OBJS) 147 | 148 | fclean: clean 149 | rm -f $(NAME) 150 | 151 | re: fclean all 152 | -------------------------------------------------------------------------------- /MiniShellExample/ainit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ainit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:34:39 by jvasquez #+# #+# */ 9 | /* Updated: 2023/11/26 16:34:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "example.h" 14 | 15 | /** 16 | * Alphabet definitions 17 | **/ 18 | void alphabet_init(t_automata *a) 19 | { 20 | a->alphabet = ft_sarradd(NULL, " "); 21 | a->alphabet = ft_sarradd(a->alphabet, "|"); 22 | a->alphabet = ft_sarradd(a->alphabet, "<"); 23 | a->alphabet = ft_sarradd(a->alphabet, ">"); 24 | a->alphabet = ft_sarradd(a->alphabet, "&"); 25 | a->alphabet = ft_sarradd(a->alphabet, "\""); 26 | a->alphabet = ft_sarradd(a->alphabet, "\'"); 27 | } 28 | 29 | int get_state(int i, int j) 30 | { 31 | const int states[][8] = { 32 | // \S, |, <, >, &, ", ', 33 | { 0, 11, 11, 11, 11, 1, 2, 14}, // 0 Empty input 34 | { 1, 1, 1, 1, 1, 13, 1, 1}, // 1 Open double quotes 35 | { 2, 2, 2, 2, 2, 2, 13, 2}, // 2 Open single quotes 36 | {12, 4, 5, 11, 11, 1, 2, 14}, // 3 Pipe open 37 | {12, 11, 11, 11, 11, 1, 2, 14}, // 4 Or open 38 | {12, 11, 6, 11, 11, 1, 2, 14}, // 5 Less open 39 | {12, 11, 11, 11, 11, 1, 2, 14}, // 6 Heredoc open 40 | {12, 11, 11, 8, 11, 1, 2, 14}, // 7 Greater open 41 | {12, 11, 11, 11, 11, 1, 2, 14}, // 8 Append open 42 | {12, 11, 11, 11, 10, 1, 2, 11}, // 9 & Found 43 | {12, 11, 11, 11, 11, 1, 2, 14}, // 10 And open 44 | {11, 11, 11, 11, 11, 11, 11, 11}, // 11 Invalid input 45 | {12, 1, 1, 1, 1, 1, 2, 14}, // 12 Spaces without words 46 | {13, 3, 5, 7, 9, 1, 2, 14}, // 13 Spaces between words 47 | {13, 3, 5, 7, 9, 1, 2, 14}, // 14 Not operators 48 | }; 49 | 50 | return (states[i][j]); 51 | } 52 | 53 | /** 54 | * Error strings to print when automata finish on a 55 | * non ending state. 56 | **/ 57 | void errors_init(t_automata *a) 58 | { 59 | a->errors = NULL; 60 | a->errors = ft_sarradd(NULL, "Empty string."); 61 | a->errors = ft_sarradd(a->errors, "Open double quotes"); 62 | a->errors = ft_sarradd(a->errors, "Open single quotes"); 63 | a->errors = ft_sarradd(a->errors, "Pipe open"); 64 | a->errors = ft_sarradd(a->errors, "Or open"); 65 | a->errors = ft_sarradd(a->errors, "Less open"); 66 | a->errors = ft_sarradd(a->errors, "Heredoc open"); 67 | a->errors = ft_sarradd(a->errors, "Greater open"); 68 | a->errors = ft_sarradd(a->errors, "Append open"); 69 | a->errors = ft_sarradd(a->errors, "& Found"); 70 | a->errors = ft_sarradd(a->errors, "And open"); 71 | a->errors = ft_sarradd(a->errors, "Invalid input"); 72 | a->errorlen = ft_sarrsize(a->errors); 73 | } 74 | 75 | /** 76 | * Simple actions, they trigger when entering a state. 77 | **/ 78 | void sactions_init(t_automata *a) 79 | { 80 | } 81 | 82 | /** 83 | * Transition actions, they trigger when going 84 | * from one state to another. 85 | **/ 86 | void tactions_init(t_automata *a) 87 | { 88 | a->fta[NOT_OPERATOR][PIPE] = get_token; 89 | a->fta[NOT_OPERATOR][LESS] = get_token; 90 | a->fta[NOT_OPERATOR][GREATER] = get_token; 91 | a->fta[NOT_OPERATOR][AMPER] = get_token; 92 | a->fta[SPACES_BTW][PIPE] = get_token; 93 | a->fta[SPACES_BTW][LESS] = get_token; 94 | a->fta[SPACES_BTW][GREATER] = get_token; 95 | a->fta[SPACES_BTW][AMPER] = get_token; 96 | a->fta[PIPE][SPACES_NW] = get_token; 97 | a->fta[OR][SPACES_NW] = get_token; 98 | a->fta[AND][SPACES_NW] = get_token; 99 | a->fta[LESS][SPACES_NW] = get_token; 100 | a->fta[HEREDOC][SPACES_NW] = get_token; 101 | a->fta[GREATER][SPACES_NW] = get_token; 102 | a->fta[APPEND][SPACES_NW] = get_token; 103 | a->fta[PIPE][NOT_OPERATOR] = get_token; 104 | a->fta[OR][NOT_OPERATOR] = get_token; 105 | a->fta[AND][NOT_OPERATOR] = get_token; 106 | a->fta[LESS][NOT_OPERATOR] = get_token; 107 | a->fta[HEREDOC][NOT_OPERATOR] = get_token; 108 | a->fta[GREATER][NOT_OPERATOR] = get_token; 109 | a->fta[APPEND][NOT_OPERATOR] = get_token; 110 | } 111 | -------------------------------------------------------------------------------- /TreeMSExample/organizer/oa_actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* oa_actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/26 16:48:08 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/30 13:08:28 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../example.h" 14 | 15 | /* ************************************************************************** */ 16 | /* This file will contain all the custom actions refered to the project */ 17 | /* ************************************************************************** */ 18 | 19 | /* Organize the cmd, the args, infile and outfile from the line into the token */ 20 | void organize_token(t_automata *a, void *data) 21 | { 22 | char *substr; 23 | char *trim; 24 | t_data *info; 25 | t_token *token; 26 | 27 | info = (t_data *)data; 28 | token = info->ctoken; 29 | if (token->type == TOK_OPR) 30 | { // If token is an operator there's nothing to do 31 | return ; 32 | } 33 | 34 | if (a->str[a->i] == '\'' || a->str[a->i] == '\"') 35 | substr = ft_substr(a->str, a->j, a->i - a->j + 1); 36 | else 37 | substr = ft_substr(a->str, a->j, a->i - a->j); 38 | trim = ft_strtrim(substr, " "); 39 | free (substr); 40 | 41 | if (check_bit(info->memory, MEM_HEREDOC)) 42 | { // If HEREDOC have been readed, the substring is assigned to infile 43 | token->infile = ft_sarradd(token->infile, ft_strdup("HeredocFileName")); 44 | set_bit(&info->memory, MEM_HEREDOC, 0); 45 | set_bit(&info->memory, MEM_LOWER, 0); 46 | set_bit(&info->memory, REDIR_TAKEN, 1); 47 | } 48 | if (check_bit(info->memory, MEM_LOWER)) 49 | { // If LOWER have been readed, the substring is assigned to infile 50 | token->infile = ft_sarradd(token->infile, trim); 51 | set_bit(&info->memory, MEM_LOWER, 0); 52 | set_bit(&info->memory, REDIR_TAKEN, 1); 53 | } 54 | if (check_bit(info->memory, REDIR_TAKEN)) 55 | set_bit(&info->memory, REDIR_TAKEN, 0); 56 | else if (check_bit(info->memory, MEM_APPEND)) 57 | { // If APPEND have been readed, the substring is assigned to outfile 58 | t_file *file; 59 | file = malloc(sizeof(t_file)); 60 | file->name = ft_strdup(trim); 61 | file->type = APPEND; 62 | ft_lstadd_back(&token->outfiles, ft_lstnew(file)); 63 | set_bit(&info->memory, MEM_APPEND, 0); 64 | set_bit(&info->memory, MEM_GREATER, 0); 65 | } 66 | else if (check_bit(info->memory, MEM_GREATER)) 67 | { // If GREATER have been readed, the substring is assigned to outfile 68 | t_file *file; 69 | file = malloc(sizeof(t_file)); 70 | file->name = ft_strdup(trim); 71 | file->type = WRITE; 72 | ft_lstadd_back(&token->outfiles, ft_lstnew(file)); 73 | set_bit(&info->memory, MEM_GREATER, 0); 74 | } 75 | else if (!check_bit(info->memory, CMD_TAKEN)) 76 | { // Check if the command has been taken 77 | token->cmd = ft_strdup(trim); 78 | token->args = ft_sarradd(token->args, trim); 79 | set_bit(&info->memory, CMD_TAKEN, 1); 80 | printf("CMD: %s\n", token->cmd); 81 | } 82 | else if (check_bit(info->memory, CMD_TAKEN)) 83 | { // Take the substr as argument 84 | token->args = ft_sarradd(token->args, trim); 85 | } 86 | a->j = a->i; 87 | free (trim); 88 | } 89 | 90 | /* Saves the operators are not neede so this function save the current operator in memory */ 91 | void save_in_memory(t_automata *a, void *data) 92 | { 93 | t_data *info; 94 | 95 | info = (t_data *)data; 96 | if (a->state == OA_LOWER) 97 | set_bit(&info->memory, MEM_LOWER, 1); 98 | else if (a->state == OA_HEREDOC) 99 | set_bit(&info->memory, MEM_HEREDOC, 1); 100 | else if (a->state == OA_GREATER) 101 | set_bit(&info->memory, MEM_GREATER, 1); 102 | else if (a->state == OA_APPEND) 103 | set_bit(&info->memory, MEM_APPEND, 1); 104 | a->j = a->i; 105 | } 106 | 107 | /* Skip the operator */ 108 | void skip_op(t_automata *a, void *data) 109 | { 110 | a->j = a->i; 111 | } 112 | 113 | /* A function to be executed when falling into INVALID state */ 114 | void organizer_print_error(t_automata *a, void *data) 115 | { 116 | printf("Error found at pos: %d", a->i); 117 | exit(EXIT_FAILURE); 118 | } 119 | -------------------------------------------------------------------------------- /libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jvasquez +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/13 17:28:16 by jvasquez #+# #+# */ 9 | /* Updated: 2024/03/28 22:56:42 by jvasquez ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include "printf/ft_printf.h" 21 | # include "gnl/get_next_line.h" 22 | 23 | typedef struct s_list 24 | { 25 | void *content; 26 | struct s_list *next; 27 | } t_list; 28 | 29 | typedef struct s_tree { 30 | void *data; 31 | struct s_tree *left; 32 | struct s_tree *right; 33 | } t_tree; 34 | 35 | /* Check functions */ 36 | int ft_isalpha(int c); 37 | int ft_isdigit(int c); 38 | int ft_isalnum(int c); 39 | int ft_isascii(int c); 40 | int ft_isprint(int c); 41 | int ft_isspace(char c); 42 | 43 | /* Bitwise functions */ 44 | int set_bit(int *n, int bit, int val); 45 | int check_bit(int n, int bit); 46 | int not_bit(int *n, int bit); 47 | void print_bits(int n); 48 | int check_mask_and(int mask, int with); 49 | int check_mask_not(int mask, int with); 50 | int check_mask_or(int mask, int with); 51 | 52 | /* String functions */ 53 | size_t ft_strlen(const char *str); 54 | void ft_strfreel(char **str); 55 | char **ft_strbrk(char *str, int b_point); 56 | size_t ft_strlcpy(char *dest, char *src, size_t size); 57 | size_t ft_strlcat(char *dest, const char *src, size_t size); 58 | char *ft_strchr(const char *str, int c); 59 | int ft_chr_count(char *str, char c); 60 | char *ft_strrchr(const char *str, int c); 61 | int ft_strncmp(const char *s1, const char *s2, size_t n); 62 | char *ft_strnstr(const char *haystack, const char *needle, size_t len); 63 | char *ft_strstr(char *str, char *to_find); 64 | char *ft_strrep(char *str, char *old, char *new); 65 | char *ft_strget_btwn(char *str, int start, char ini, char end); 66 | char *ft_strdup(const char *str); 67 | char *ft_substr(const char *str, unsigned int start, size_t len); 68 | char *ft_strjoin(char const *s1, char const *s2); 69 | char *ft_strjoinfree(char *s1, char *s2, int which); 70 | char **ft_split(const char *str, char delimiter); 71 | char *ft_unsplit(char **sarr, char *sep); 72 | char *ft_strtrim(char const *str, char const *set); 73 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 74 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 75 | 76 | /* Memory functions */ 77 | void ft_bzero(void *str, size_t n); 78 | void *ft_memset(void *str, int c, size_t len); 79 | void *ft_memcpy(void *dest, const void *src, size_t n); 80 | void *ft_memmove(void *dest, const void *src, size_t n); 81 | void *ft_memchr(const void *str, int c, size_t n); 82 | int ft_memcmp(const char *s1, const char *s2, size_t n); 83 | void *ft_calloc(size_t count, size_t size); 84 | 85 | /* Char functions */ 86 | int ft_toupper(int c); 87 | int ft_tolower(int c); 88 | int ft_chrpos(char *str, int chr, int start); 89 | 90 | /* Print functions */ 91 | void ft_putchar_fd(char c, int fd); 92 | void ft_putstr_fd(char const *s, int fd); 93 | void ft_putendl_fd(char *s, int fd); 94 | void ft_putnbr_fd(int n, int fd); 95 | 96 | /* List functions */ 97 | t_list *ft_lstnew(void *content); 98 | void ft_lstadd_front(t_list **lst, t_list *new); 99 | void ft_lstadd_back(t_list **lst, t_list *new); 100 | t_list *ft_lstlast(t_list *lst); 101 | int ft_lstsize(t_list *lst); 102 | void ft_lstdelone(t_list *lst, void (*del)(void*)); 103 | void ft_lstclear(t_list **lst, void (*del)(void*)); 104 | void ft_lstiter(t_list *lst, void (*f)(void *)); 105 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 106 | 107 | /* To functions */ 108 | int ft_atoi(const char *str); 109 | char *ft_itoa(int n); 110 | int ft_atoi_pos(char *str, int *pos); 111 | int ft_atoi_btwn(char *str, char ini, char end); 112 | double ft_atod(char *str); 113 | 114 | /* String Array functions */ 115 | int ft_sarrsize(char **arr); 116 | int ft_sarrprint(char **arr); 117 | int ft_sarrcmp(char **arr, char *str); 118 | void ft_sarrfree(char ***arr); 119 | char **ft_sarrcpy(char **arr); 120 | char **ft_sarrrmi(int index, char **arr); 121 | char **ft_sarradd(char **arr, char *string); 122 | char **ft_sarrrep(char **arr, int index, char *str); 123 | 124 | /* Tree functions */ 125 | t_tree *tree_new_node(void *data); 126 | void tree_in_order(t_tree *tree, void (*ft)(void*)); 127 | void tree_pre_order(t_tree *tree, void (*ft)(void*)); 128 | void tree_post_order(t_tree *tree, void (*ft)(void*)); 129 | void tree_del_node(t_tree *node, void (*del)(void*)); 130 | void tree_in_order_context(t_tree *tree, void (*ft)(void*, void*), void *context); 131 | 132 | #endif 133 | --------------------------------------------------------------------------------