From c8afa2fd391390880dde85992111da9d2f1d416f Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Sun, 2 Apr 2023 22:39:12 +0200 Subject: [PATCH] IT'S WORKING, only norminette left! --- ex00/Makefile | 20 ++++-- ex00/argparse.c | 4 +- ex00/ft_array.c | 93 ++++++++++++++++++++++++++++ ex00/ft_math.c | 20 ++++++ ex00/ft_ntow.c | 127 ++++++++++++++++++++++++++++++++++++-- ex00/ft_strlib.c | 42 ++++++------- ex00/ft_strlib2.c | 40 ++++++++++++ ex00/include/ft_array.h | 21 +++++++ ex00/include/ft_math.h | 18 ++++++ ex00/include/ft_ntow.h | 4 +- ex00/include/ft_strlib.h | 12 ++-- ex00/include/ft_strlib2.h | 20 ++++++ ex00/main.c | 11 +++- 13 files changed, 386 insertions(+), 46 deletions(-) create mode 100644 ex00/ft_array.c create mode 100644 ex00/ft_math.c create mode 100644 ex00/ft_strlib2.c create mode 100644 ex00/include/ft_array.h create mode 100644 ex00/include/ft_math.h create mode 100644 ex00/include/ft_strlib2.h diff --git a/ex00/Makefile b/ex00/Makefile index 1ffc835..27e595a 100644 --- a/ex00/Makefile +++ b/ex00/Makefile @@ -1,5 +1,6 @@ SRC = main.c \ ft_strlib.c \ + ft_strlib2.c \ ft_io.c \ argparse.c \ dictparse.c \ @@ -8,8 +9,11 @@ SRC = main.c \ printing.c \ ft_convert.c \ ft_ntow.c \ + ft_array.c \ + ft_math.c \ HEADERS = ft_strlib.h \ + ft_strlib2.h \ ft_io.h \ colors.h \ argparse.h \ @@ -19,6 +23,8 @@ HEADERS = ft_strlib.h \ printing.h \ ft_convert.h \ ft_ntow.h \ + ft_array.h \ + ft_math.h \ OBJDIR = obj INCDIR = include @@ -87,12 +93,14 @@ CLR_RST = && :  run: @clear - $(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS) - $(YLW) ./$(NAME) 1 $(CLR_RST) $(SUCCESS) - $(YLW) ./$(NAME) "./dicts/numbers2.dict" 1 $(CLR_RST) $(SUCCESS) - $(YLW) ./$(NAME) 1 "./dicts/numbers2.dict" $(CLR_RST) $(FAIL) - $(YLW) ./$(NAME) 1 "./dicts/numb.dict" $(CLR_RST) $(FAIL) - $(YLW) ./$(NAME) 1 2 3 $(CLR_RST) $(FAIL) + $(YLW) ./$(NAME) "./dicts/numbers2.dict" 12345678 $(CLR_RST) $(SUCCESS) + $(YLW) ./$(NAME) "./dicts/numbers2.dict" 123456 $(CLR_RST) $(SUCCESS) + $(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS) + $(YLW) ./$(NAME) 1 $(CLR_RST) $(SUCCESS) + $(YLW) ./$(NAME) "./dicts/numbers2.dict" 1 $(CLR_RST) $(SUCCESS) + $(YLW) ./$(NAME) 1 "./dicts/numbers2.dict" $(CLR_RST) $(FAIL) + $(YLW) ./$(NAME) 1 "./dicts/numb.dict" $(CLR_RST) $(FAIL) + $(YLW) ./$(NAME) 1 2 3 $(CLR_RST) $(FAIL) valgrind: re $(CYN) $(VALGRIND) ./$(NAME) 1 $(CLR_RST) $(SUCCESS_VALG) diff --git a/ex00/argparse.c b/ex00/argparse.c index 259ea5b..6e3dae0 100644 --- a/ex00/argparse.c +++ b/ex00/argparse.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 15:12:53 by tischmid #+# #+# */ -/* Updated: 2023/04/01 21:58:44 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 21:23:58 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int parse_args(int argc, char **argv, char **path, char **str_nbr) { if (argc > 3) return (0); - *path = "./numbers.dict"; + *path = "./dicts/numbers.dict"; if (argc < 2) { *str_nbr = "123"; diff --git a/ex00/ft_array.c b/ex00/ft_array.c new file mode 100644 index 0000000..e5e96ef --- /dev/null +++ b/ex00/ft_array.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 22:34:51 by tischmid #+# #+# */ +/* Updated: 2023/04/02 22:37:10 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_array.h" +#include "ft_strlib.h" +#include + +char **partition_right(char *str, int part_size) +{ + int strlen; + int first_part_size; + int idx; + char **arr; + + strlen = ft_strlen(str); + arr = malloc(sizeof(char *) * (strlen + 1)); + first_part_size = strlen % part_size; + idx = 0; + if (first_part_size) + { + arr[idx] = malloc(sizeof(char) * (first_part_size + 1)); + ft_strlcpy(arr[idx], str, first_part_size + 1); + str += first_part_size; + ++idx; + } + while (*str) + { + arr[idx] = malloc(sizeof(char) * (part_size + 1)); + ft_strlcpy(arr[idx], str, part_size + 1); + str += part_size; + ++idx; + } + arr[idx] = NULL; + return (arr); +} + +int arr_len(char **arr) +{ + int size; + + size = 0; + while (*arr++) + ++size; + return (size); +} + +void free_arr(char **arr) +{ + int i; + + i = -1; + while (arr[++i]) + free(arr[i]); + free(arr); +} + +char *ft_strjoin(int size, char **strs, char *sep) +{ + int total_len; + int sep_len; + int idx; + char *joined_str; + + total_len = 0; + sep_len = ft_strlen(sep); + idx = -1; + while (++idx < size) + total_len += ft_strlen(strs[idx]) + sep_len; + total_len -= sep_len; + if (total_len < 1) + joined_str = malloc(sizeof(char) * 1); + else + joined_str = malloc(sizeof(char) * (total_len + 1)); + idx = -1; + joined_str[0] = 0; + while (++idx < size) + { + ft_strcat(joined_str, strs[idx]); + if (idx < size - 1) + ft_strcat(joined_str, sep); + } + return (joined_str); +} + diff --git a/ex00/ft_math.c b/ex00/ft_math.c new file mode 100644 index 0000000..6aaf23d --- /dev/null +++ b/ex00/ft_math.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_math.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 22:37:55 by tischmid #+# #+# */ +/* Updated: 2023/04/02 22:38:10 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int power(int nb, int exp) +{ + if (exp < 1) + return (exp == 0); + return (nb * power(nb, exp - 1)); +} diff --git a/ex00/ft_ntow.c b/ex00/ft_ntow.c index cfc398c..3875b2d 100644 --- a/ex00/ft_ntow.c +++ b/ex00/ft_ntow.c @@ -6,17 +6,134 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 20:26:44 by tischmid #+# #+# */ -/* Updated: 2023/04/02 20:29:01 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 22:37:52 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_ntow.h" #include "ft_linked_list.h" +#include "ft_strlib.h" +#include "dictparse.h" +#include "ft_convert.h" +#include "ft_array.h" +#include "ft_math.h" +#include #include -char *num_to_words(t_map_entry *map) +char *triplet_to_word(char *triplet, t_map_entry *map) { - (void)map; - printf("%s\n", ll_map_get_value(map, "17")); - return ("one hundred"); + char *triplet_word; + int triplet_len; + char *map_value; + int idx; + int positional_value; + int digit; + char *digit_str; + char *lookup_num; + + triplet_word = malloc(sizeof(char) * MAX_LINE_LENGTH * 5); + ft_strcpy(triplet_word, ""); + if (ft_atoi(triplet) < 20) + { + map_value = ll_map_get_value(map, triplet); + if (map_value == NULL) + return (ft_strcpy(triplet_word, "MISSING_KEY")); + ft_strcat(triplet_word, map_value); + return (triplet_word); + } + else + { + triplet_len = ft_strlen(triplet); + idx = 0; + while (triplet_len--) + { + lookup_num = malloc(sizeof(char) * MAX_DIGITS); + digit = triplet[idx] - '0'; + positional_value = power(10, triplet_len) * digit; + ft_itoa(lookup_num, positional_value); + digit_str = malloc(sizeof(char) * 2); + ft_itoa(digit_str, digit); + map_value = ll_map_get_value(map, digit_str); + free(digit_str); + if (map_value == NULL) + { + free(lookup_num); + return (ft_strcpy(triplet_word, "MISSING_KEY")); + } + if (triplet_len == 2) + map_value = ll_map_get_value(map, "100"); + else + map_value = ll_map_get_value(map, lookup_num); + free(lookup_num); + if (map_value == NULL) + return (ft_strcpy(triplet_word, "MISSING_KEY")); + if (triplet_len == 2) + { + digit_str = malloc(sizeof(char) * 2); + ft_itoa(digit_str, digit); + ft_strcat(triplet_word, ll_map_get_value(map, digit_str)); + ft_strcat(triplet_word, " "); + free(digit_str); + } + ft_strcat(triplet_word, map_value); + if (triplet_len != 0) + ft_strcat(triplet_word, " "); + ++idx; + } + } + return (triplet_word); +} + +void num_to_words_free(char **triplets, char **parts, char *thousands_power) +{ + free_arr(triplets); + free_arr(parts); + free(thousands_power); +} + +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; + + (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, " "); + num_to_words_free(triplets, parts, thousands_power); + return (result); } diff --git a/ex00/ft_strlib.c b/ex00/ft_strlib.c index 7bedeec..5b00703 100644 --- a/ex00/ft_strlib.c +++ b/ex00/ft_strlib.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 07:13:31 by tischmid #+# #+# */ -/* Updated: 2023/04/02 18:38:52 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 21:17:42 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,31 +33,29 @@ char *ft_strcpy(char *dest, char *src) return (o_dest); } -int ft_strlen(char *str) +char *ft_strcat(char *dest, char *src) { - int i; + char *orig_s1; - i = 0; - while (*str++) - ++i; - return (i); + orig_s1 = dest; + while (*dest) + ++dest; + while (*src) + *dest++ = *src++; + *dest = 0; + return (orig_s1); } -int ft_isspace(char c) +unsigned int ft_strlcpy(char *dest, char *src, unsigned int size) { - return (c == ' ' - || c == '\t' - || c == '\v' - || c == '\n' - || c == '\r' - || c == '\f'); -} + char *orig_src; -char *last_non_whitespace(char *ptr) -{ - while (*ptr) - ++ptr; - while (ft_isspace(*ptr)) - --ptr; - return (ptr); + orig_src = src; + while (size-- > 1 && *src) + *dest++ = *src++; + *dest = 0; + size = 0; + while (*orig_src++) + ++size; + return (size); } diff --git a/ex00/ft_strlib2.c b/ex00/ft_strlib2.c new file mode 100644 index 0000000..d697f9c --- /dev/null +++ b/ex00/ft_strlib2.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlib2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 20:49:31 by tischmid #+# #+# */ +/* Updated: 2023/04/02 20:49:40 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (*str++) + ++i; + return (i); +} + +int ft_isspace(char c) +{ + return (c == ' ' + || c == '\t' + || c == '\v' + || c == '\n' + || c == '\r' + || c == '\f'); +} + +char *last_non_whitespace(char *ptr) +{ + while (*ptr) + ++ptr; + while (ft_isspace(*ptr)) + --ptr; + return (ptr); +} diff --git a/ex00/include/ft_array.h b/ex00/include/ft_array.h new file mode 100644 index 0000000..9b31edb --- /dev/null +++ b/ex00/include/ft_array.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_array.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 22:35:29 by tischmid #+# #+# */ +/* Updated: 2023/04/02 22:35:45 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_ARRAY_H +# define FT_ARRAY_H + +char **partition_right(char *str, int part_size); +int arr_len(char **arr); +void free_arr(char **arr); +char *ft_strjoin(int size, char **strs, char *sep); + +#endif diff --git a/ex00/include/ft_math.h b/ex00/include/ft_math.h new file mode 100644 index 0000000..6355bad --- /dev/null +++ b/ex00/include/ft_math.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_math.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 22:38:13 by tischmid #+# #+# */ +/* Updated: 2023/04/02 22:38:24 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MATH_H +# define FT_MATH_H + +int power(int nb, int exp); + +#endif diff --git a/ex00/include/ft_ntow.h b/ex00/include/ft_ntow.h index 76e192c..01da233 100644 --- a/ex00/include/ft_ntow.h +++ b/ex00/include/ft_ntow.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 20:21:19 by tischmid #+# #+# */ -/* Updated: 2023/04/02 20:29:19 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 20:32:16 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,6 @@ # define FT_NTOW_H # include "ft_linked_list.h" -char *num_to_words(t_map_entry *map); +char *num_to_words(char *str_nbr, t_map_entry *map); #endif diff --git a/ex00/include/ft_strlib.h b/ex00/include/ft_strlib.h index 01560ea..143a22b 100644 --- a/ex00/include/ft_strlib.h +++ b/ex00/include/ft_strlib.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/01 07:14:29 by tischmid #+# #+# */ -/* Updated: 2023/04/02 18:39:09 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 21:18:30 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,11 @@ # define FT_STRLIB_H # include # include "colors.h" +# include "ft_strlib2.h" -char *ft_strcpy(char *dest, char *src); -int ft_strcmp(char *s1, char *s2); -int ft_strlen(char *str); -char *last_non_whitespace(char *ptr); -int ft_isspace(char c); +char *ft_strcpy(char *dest, char *src); +int ft_strcmp(char *s1, char *s2); +unsigned int ft_strlcpy(char *dest, char *src, unsigned int size); +char *ft_strcat(char *dest, char *src); #endif diff --git a/ex00/include/ft_strlib2.h b/ex00/include/ft_strlib2.h new file mode 100644 index 0000000..3f12f7d --- /dev/null +++ b/ex00/include/ft_strlib2.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlib2.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 20:50:20 by tischmid #+# #+# */ +/* Updated: 2023/04/02 20:50:35 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STRLIB2_H +# define FT_STRLIB2_H + +char *last_non_whitespace(char *ptr); +int ft_strlen(char *str); +int ft_isspace(char c); + +#endif diff --git a/ex00/main.c b/ex00/main.c index 682ba42..a769d34 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 20:21:54 by tischmid ### ########.fr */ +/* Updated: 2023/04/02 22:33:19 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "ft_convert.h" #include "ft_ntow.h" #include +#include int main(int argc, char **argv) { @@ -30,14 +31,18 @@ int main(int argc, char **argv) map = ll_map_new_entry("about", "Dict for the data from *.dict file", NULL); if (!parse_dict(path, map)) return (puterr("Dict Error\n", 2)); - numberwords = num_to_words(map); - if (*numberwords) + numberwords = num_to_words(str_nbr, map); + if (numberwords != NULL) { ft_putstr(numberwords); ft_putchar('\n'); + free(numberwords); } else + { + free(numberwords); return (puterr("Dict Error\n", 3)); + } ll_clear(map, 1); return (0); }