diff --git a/ex00/argparse.c b/ex00/argparse.c new file mode 100644 index 0000000..60c1593 --- /dev/null +++ b/ex00/argparse.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* argparse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/01 15:12:53 by tischmid #+# #+# */ +/* Updated: 2023/04/01 15:13:56 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "argparse.h" +#include "ft_io.h" + +int parse_args(int argc, char **argv, char **path, char **str_nbr) +{ + if (argc > 3) + return (0); + *path = "./numbers.dict"; + if (argc < 2) + { + // TODO: Bonus: Use read to take nbr from stdin + *str_nbr = "123"; + } + else + *str_nbr = argv[1]; + if (argc == 3) + { + if (!file_exists(argv[1])) + return (0); + *path = argv[1]; + *str_nbr = argv[2]; + } + return (1); +} diff --git a/ex00/ft_io.c b/ex00/ft_io.c index 44ae344..3f09fdc 100644 --- a/ex00/ft_io.c +++ b/ex00/ft_io.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 08:51:59 by tischmid #+# #+# */ -/* Updated: 2023/04/01 08:58:01 by tischmid ### ########.fr */ +/* Updated: 2023/04/01 14:57:53 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,9 @@ int file_exists(char *path) { - int fd_num_dict = open(path, O_RDONLY, 0); + int fd_num_dict; + + fd_num_dict = open(path, O_RDONLY, 0); if (fd_num_dict < 0) return (0); if (close(fd_num_dict) < 0) diff --git a/ex00/ft_linked_list.c b/ex00/ft_linked_list.c new file mode 100644 index 0000000..90ed8cf --- /dev/null +++ b/ex00/ft_linked_list.c @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_linked_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */ +/* Updated: 2023/04/01 15:25:50 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_linked_list.h" diff --git a/ex00/include/argparse.h b/ex00/include/argparse.h new file mode 100644 index 0000000..7ea0326 --- /dev/null +++ b/ex00/include/argparse.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* argparse.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/01 15:13:05 by tischmid #+# #+# */ +/* Updated: 2023/04/01 15:13:21 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARGPARSE_H +# define ARGPARSE_H + +int parse_args(int argc, char **argv, char **path, char **str_nbr); + +#endif diff --git a/ex00/include/ft_linked_list.h b/ex00/include/ft_linked_list.h new file mode 100644 index 0000000..eb66481 --- /dev/null +++ b/ex00/include/ft_linked_list.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_linked_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */ +/* Updated: 2023/04/01 15:26:10 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LINKED_LIST_H +# define FT_LINKED_LIST_H + +typedef struct s_map_entry +{ + char *key; + char *value; + struct s_map_entry *next; +} t_map_entry; + +#endif diff --git a/ex00/main.c b/ex00/main.c index af118ed..527e897 100644 --- a/ex00/main.c +++ b/ex00/main.c @@ -6,32 +6,120 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */ -/* Updated: 2023/04/01 14:59:26 by tischmid ### ########.fr */ +/* Updated: 2023/04/01 16:35:17 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" #include "ft_lib.h" -#include "ft_io.h" +#include "argparse.h" +#include "ft_linked_list.h" -int parse_args(int argc, char **argv) +#include +#include + +int ft_strcmp(char *s1, char *s2) { - if (argc > 3) - return (0); - if (argc == 3) - if (!file_exists(argv[1])) - return (0); - if (argc < 2) + while (*s1 && *s2 && *s1 == *s2) { - // TODO: Bonus: Use read to take nbr from stdin - return (0); + ++s1; + ++s2; + } + return (*s1 - *s2); +} + +t_map_entry *ll_map_new_node(char *key, char *value) +{ + t_map_entry *new_node; + + new_node = (t_map_entry *) malloc(sizeof(t_map_entry)); + new_node->key = key; + new_node->value = value; + return (new_node); +} + +char *ll_map_get(t_map_entry *head, char *key) +{ + t_map_entry *current; + + if (head != NULL) + { + current = head; + while (current != NULL) + { + if (!ft_strcmp(current->key, key)) + return (current->value); + current = current->next; + } + } + return (NULL); +} + +void ll_map_push(t_map_entry *head, char *key, char *value) +{ + t_map_entry *current; + + current = head; + if (head != NULL) + { + while (current->next != NULL) + current = current->next; + current->next = ll_map_new_node(key, value); + } + else + ft_putstr("head is NULL\n"); +} + +t_map_entry *ll_pop_last(t_map_entry *head) +{ + t_map_entry *current; + t_map_entry *retval; + + if (head != NULL) + { + if (head->next == NULL) + { + retval = ll_map_new_node(head->key, head->value); + free(head); + return (retval); + } + + current = head; + while (current->next->next != NULL) + current = current->next; + + retval = ll_map_new_node(current->next->key, current->next->value); + free(current->next); + current->next = NULL; + return (retval); + } + else + return (NULL); +} + +void ll_clear(t_map_entry *head) +{ + if (head != NULL) + { + while (head->next != NULL) + free(ll_pop_last(head)); + free(ll_pop_last(head)); } - return (1); } int main(int argc, char **argv) { - if (!parse_args(argc, argv)) + char *str_nbr; + char *path; + // t_map_entry *map; + + if (!parse_args(argc, argv, &path, &str_nbr)) return (puterr("Error\n", 1)); + // if (!parse_dict(path, map)) + // return (puterr("Dict Error\n", 2)); + t_map_entry *head = ll_map_new_node("hello", "world"); + ll_map_push(head, "hello2", "world2"); + // printf("%s\n", ll_map_get(head, "hello2")); + ll_clear(head); return (0); } diff --git a/ex00/obj/argparse.o b/ex00/obj/argparse.o new file mode 100644 index 0000000..1630c0e Binary files /dev/null and b/ex00/obj/argparse.o differ diff --git a/ex00/obj/main.o b/ex00/obj/main.o index 6270259..becf24d 100644 Binary files a/ex00/obj/main.o and b/ex00/obj/main.o differ diff --git a/ex00/rush-02 b/ex00/rush-02 index 986b59d..d743da1 100755 Binary files a/ex00/rush-02 and b/ex00/rush-02 differ