From 8bfb047332afcd72bbb4c375cf73a6512cac26bd Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Sun, 2 Apr 2023 23:21:26 +0200 Subject: [PATCH] ALMOST --- ex00/Makefile | 5 +- ex00/argparse.c | 2 +- ex00/dictparse.c | 2 +- ex00/ft_array.c | 3 +- ex00/ft_linked_list.c | 16 ++---- ex00/ft_ntow.c | 79 +++++++++++++-------------- ex00/include/ft_linked_list.h | 4 +- ex00/include/ft_linked_list_helpers.h | 4 +- ex00/main.c | 2 +- 9 files changed, 54 insertions(+), 63 deletions(-) diff --git a/ex00/Makefile b/ex00/Makefile index 27e595a..97246be 100644 --- a/ex00/Makefile +++ b/ex00/Makefile @@ -69,7 +69,7 @@ $(NAME): $(OBJ) $(OBJ): | $(OBJDIR) $(OBJDIR)/%.o: %.c $(DEPS) - @norminette $< >/dev/null || { printf '\033[101;37m%s\033[m\n' "!Norminette Failed>>>"; norminette $<; printf '\033[101;37m%s\033[m\n' "<</dev/null || { printf '\033[101;37m%s\033[m\n' "!Norminette Failed>>>"; norminette $<; printf '\033[101;37m%s\033[m\n' "<< +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 15:12:53 by tischmid #+# #+# */ -/* Updated: 2023/04/02 21:23:58 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:03:12 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ex00/dictparse.c b/ex00/dictparse.c index d730af8..e6d9bae 100644 --- a/ex00/dictparse.c +++ b/ex00/dictparse.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 21:45:39 by tischmid #+# #+# */ -/* Updated: 2023/04/02 19:19:53 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:08:01 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ex00/ft_array.c b/ex00/ft_array.c index e5e96ef..09910f4 100644 --- a/ex00/ft_array.c +++ b/ex00/ft_array.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 22:34:51 by tischmid #+# #+# */ -/* Updated: 2023/04/02 22:37:10 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:09:10 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,4 +90,3 @@ char *ft_strjoin(int size, char **strs, char *sep) } return (joined_str); } - diff --git a/ex00/ft_linked_list.c b/ex00/ft_linked_list.c index ada5156..7fc022f 100644 --- a/ex00/ft_linked_list.c +++ b/ex00/ft_linked_list.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 15:25:42 by tischmid #+# #+# */ -/* Updated: 2023/04/02 20:01:34 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:08:50 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,16 +49,10 @@ void ll_map_push(t_map_entry *head, char *key, char *value) if (head != NULL) { - current = ll_map_get(head, key); - if (current == NULL) - { - current = head; - while (current->next != NULL) - current = current->next; - current->next = ll_map_new_entry(key, value, NULL); - } - else - ll_map_set_value(head, key, value); + current = head; + while (current->next != NULL) + current = current->next; + current->next = ll_map_new_entry(key, value, NULL); } } diff --git a/ex00/ft_ntow.c b/ex00/ft_ntow.c index 3875b2d..8184e0a 100644 --- a/ex00/ft_ntow.c +++ b/ex00/ft_ntow.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 20:26:44 by tischmid #+# #+# */ -/* Updated: 2023/04/02 22:37:52 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:21:07 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,14 @@ #include #include +int num_to_words_free(char **triplets, char **parts, char *thousands_power) +{ + free_arr(triplets); + free_arr(parts); + free(thousands_power); + return (0); +} + char *triplet_to_word(char *triplet, t_map_entry *map) { char *triplet_word; @@ -84,56 +92,47 @@ char *triplet_to_word(char *triplet, t_map_entry *map) return (triplet_word); } -void num_to_words_free(char **triplets, char **parts, char *thousands_power) +int set_parts(char **parts, char **triplets, char *k_power, t_map_entry *map) { - free_arr(triplets); - free_arr(parts); - free(thousands_power); + char *thousands_power_word; + int idx; + int size; + char *triplet_word; + + size = arr_len(triplets); + idx = 0; + while (size--) + { + ft_itoa(k_power, power(1000, size)); + thousands_power_word = ll_map_get_value(map, k_power); + if (!thousands_power_word) + return (num_to_words_free(triplets, parts, k_power)); + triplet_word = triplet_to_word(triplets[idx], map); + if (!ft_strcmp(triplet_word, "MISSING_KEY")) + return (num_to_words_free(triplets, parts, k_power)); + parts[idx] = malloc(sizeof(char) * MAX_LINE_LENGTH * 6); + ft_strcpy(parts[idx], triplet_word); + if (size != 0) + ft_strcat(ft_strcat(parts[idx++], " "), thousands_power_word); + free(triplet_word); + } + parts[idx] = NULL; + return (1); } char *num_to_words(char *str_nbr, t_map_entry *map) { char **triplets; - int arr_size; - int size; - int idx; - char *thousands_power; - char *thousands_power_word; char **parts; - char *triplet_word; char *result; + char *thousands_power; - (void)map; triplets = partition_right(str_nbr, 3); - arr_size = arr_len(triplets); thousands_power = malloc(sizeof(char) * MAX_LINE_LENGTH); - idx = 0; - parts = malloc(sizeof(char *) * (arr_size + 1)); - size = arr_size; - while (size--) - { - ft_itoa(thousands_power, power(1000, size)); - thousands_power_word = ll_map_get_value(map, thousands_power); - if (!thousands_power_word) - { - num_to_words_free(triplets, parts, thousands_power); - return (NULL); - } - triplet_word = triplet_to_word(triplets[idx], map); - if (!ft_strcmp(triplet_word, "MISSING_KEY")) - { - num_to_words_free(triplets, parts, thousands_power); - return (NULL); - } - parts[idx] = malloc(sizeof(char) * MAX_LINE_LENGTH * 6); - ft_strcpy(parts[idx], triplet_word); - if (size != 0) - ft_strcat(ft_strcat(parts[idx], " "), thousands_power_word); - free(triplet_word); - ++idx; - } - parts[idx] = NULL; - result = ft_strjoin(arr_size, parts, " "); + parts = malloc(sizeof(char *) * (arr_len(triplets) + 1)); + if (!set_parts(parts, triplets, thousands_power, map)) + return (NULL); + result = ft_strjoin(arr_len(parts), parts, " "); num_to_words_free(triplets, parts, thousands_power); return (result); } diff --git a/ex00/include/ft_linked_list.h b/ex00/include/ft_linked_list.h index 8e84af5..4f7e262 100644 --- a/ex00/include/ft_linked_list.h +++ b/ex00/include/ft_linked_list.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */ -/* Updated: 2023/04/02 20:07:49 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:09:41 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ typedef struct s_map_entry struct s_map_entry *next; } t_map_entry; -typedef enum +typedef enum e_ret_flag { POP_NO_RETURN_VAL } t_ret_flag; diff --git a/ex00/include/ft_linked_list_helpers.h b/ex00/include/ft_linked_list_helpers.h index d49bf9d..c8e888b 100644 --- a/ex00/include/ft_linked_list_helpers.h +++ b/ex00/include/ft_linked_list_helpers.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 15:25:59 by tischmid #+# #+# */ -/* Updated: 2023/04/02 20:08:35 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:09:21 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,5 +20,3 @@ void free_entry(t_map_entry *entry); int ll_map_set_value(t_map_entry *head, char *key, char *value); #endif - - diff --git a/ex00/main.c b/ex00/main.c index a769d34..fe5e372 100644 --- a/ex00/main.c +++ b/ex00/main.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */ -/* Updated: 2023/04/02 22:33:19 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 23:04:46 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */