From b5efa9ea07ee15a2fc814aba099b979340e8eafa Mon Sep 17 00:00:00 2001 From: Aleksei Chubukov Date: Sun, 3 Dec 2023 23:36:55 +0400 Subject: [PATCH] unfinished --- ex00/digits_to_words.c | 3 +- ex00/digits_to_words.h | 4 ++- ex00/ft_open.c | 79 +++++++++++++++++++++++++++++++++++------- ex00/ft_utils.c | 13 +++++-- ex00/ft_utils.h | 9 +++-- ex00/main.c | 43 +++++++++++++++-------- ex00/main.h | 3 +- tests/Makefile | 4 +-- tests/test_ft_utils.c | 8 +---- 9 files changed, 121 insertions(+), 45 deletions(-) diff --git a/ex00/digits_to_words.c b/ex00/digits_to_words.c index 04fe191..3cd31ef 100644 --- a/ex00/digits_to_words.c +++ b/ex00/digits_to_words.c @@ -6,7 +6,7 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 23:40:54 by achubuko #+# #+# */ -/* Updated: 2023/12/03 17:26:57 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 22:21:52 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #include "digits_to_words.h" @@ -14,4 +14,3 @@ void print_digit_words(char *str) { } - diff --git a/ex00/digits_to_words.h b/ex00/digits_to_words.h index 0ef434a..219e38d 100644 --- a/ex00/digits_to_words.h +++ b/ex00/digits_to_words.h @@ -6,11 +6,13 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 23:35:43 by achubuko #+# #+# */ -/* Updated: 2023/12/03 17:26:46 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 22:02:54 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DIGITS_TO_WORDS_H # define DIGITS_TO_WORDS_H +# include "ft_utils.h" +# include "digits_to_words.h" void print_digit_words(char *str); #endif diff --git a/ex00/ft_open.c b/ex00/ft_open.c index ade2bb7..eaa3ffb 100644 --- a/ex00/ft_open.c +++ b/ex00/ft_open.c @@ -6,22 +6,77 @@ /* By: asargsya +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/03 19:08:11 by asargsya #+# #+# */ -/* Updated: 2023/12/03 20:17:25 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 23:36:29 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_open.h" -void ft_dict_open(char *path) +t_file *ft_open(char *path) { - int fd; - char buf[1024]; - int buflen; + t_file *file; - fd = open(path, O_RDONLY); - buflen = read(fd, buf, 1024); - while (buflen > 0) - { - write(1, buf, buflen); - } - close(fd); + file = malloc(sizeof(t_file)); + file->descriptor = open(path, O_RDONLY); + file->read_window_size = 4096; + file->read_window = malloc(sizeof(char) * file->read_window_size); + file->read_size = 0; + file->read_window_cursor = 0; + file->buffer = 0; + file->buffer_size = 0; + return (file); +} + +void ft_growbuffer(t_file *f, ssize_t grow) +{ + char *prev; + char *cur; + ssize_t pos; + + prev = f->buffer; + cur = malloc(sizeof(char) * (f->buffer_size + grow)); + pos = 0; + while (prev && cur && pos < f->buffer_size) + { + cur[pos] = prev[pos]; + pos++; + } + while (cur && pos < f->buffer_size + grow) + { + cur[pos] = 0; + pos++; + } + free(prev); + f->buffer = cur; +} + +ssize_t ft_churn(t_file *f) +{ + f->read_size = read(f->descriptor, f->read_window, f->read_window_size); + f->read_window_cursor = 0; + return (f->read_size); +} + +ssize_t ft_buffer_line(t_file *f) +{ + char *c; + ssize_t len; + + ft_growbuffer(f, 100); + len = 0; + c = 0; + while (1) + { + if (f->read_window_cursor >= f->read_size) + { + ft_churn(f); + } + f->buffer[len] = f->read_window[f->read_window_cursor]; + if (len + 1 >= f->buffer_size) + ft_growbuffer(f, 100); + if (f->read_window[f->read_window_cursor] == '\n') + return (len); + len++; + f->read_window_cursor++; + } + return (0); } diff --git a/ex00/ft_utils.c b/ex00/ft_utils.c index a23aadf..5df9c19 100644 --- a/ex00/ft_utils.c +++ b/ex00/ft_utils.c @@ -6,14 +6,21 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 21:56:30 by achubuko #+# #+# */ -/* Updated: 2023/12/03 18:14:54 by asargsya ### ########.fr */ +/* Updated: 2023/12/03 21:58:36 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_utils.h" -uint32_t ft_atoui32(char *str) +t_dec ft_atoui32(char *str) { - return (*str - '0'); + t_dec dec; + + dec.significant = 0; + dec.base = 1; + while (*str) + { + } + return (dec); } size_t ft_strlen(char *s) diff --git a/ex00/ft_utils.h b/ex00/ft_utils.h index f82d180..9cd71e2 100644 --- a/ex00/ft_utils.h +++ b/ex00/ft_utils.h @@ -6,7 +6,7 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 21:57:06 by achubuko #+# #+# */ -/* Updated: 2023/12/03 18:08:38 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 21:46:56 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_UTILS_H @@ -14,7 +14,12 @@ # include # include -uint32_t ft_atoui32(char *str); +typedef struct s_dec +{ + int significant; + int base; +} t_dec; +t_dec ft_atoui32(char *str); size_t ft_strlen(char *s); int ft_str_is_numeric(char *str); #endif diff --git a/ex00/main.c b/ex00/main.c index c08984f..8d73edc 100644 --- a/ex00/main.c +++ b/ex00/main.c @@ -6,30 +6,43 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 21:01:21 by achubuko #+# #+# */ -/* Updated: 2023/12/03 18:09:34 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 22:21:05 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -int main(int argc, char **argv) +int invalid_arg_num(char *arg) { - if (argc <= 1 || argc > 3) + if (ft_str_is_numeric(arg) == 0) { - write(1, "Error: program requires 1 or 2 arguments passed!", 48); - return (1); + write(1, "Argument #1 must be a number!\n", 29); + return (3); } - if (ft_str_is_numeric(argv[1]) == 1) + else if (ft_str_is_numeric(arg) == -1) { - print_digit_words(argv[1]); - } - else if (ft_str_is_numeric(argv[1]) == -1) - { - write(1, "Argument #1 must be positive!", 28); + write(1, "Argument #1 must be positive!\n", 29); return (2); } else - { - write(1, "Argument #1 must be a number!", 28); - return (3); - } + return (0); +} + +int main(int argc, char **argv) +{ + int chk; + t_dict *d; + + if (argc <= 1 || argc > 3) + { + write(1, "Error: program requires 1 or 2 arguments passed!\n", 49); + return (1); + } + d = dict_create_default("numbers.dict"); + if (argc == 3) + dict_update(d, argv[2]); + chk = invalid_arg_num(argv[1]); + if (!chk) + print_digit_words(argv[1]); + else + return (chk); } diff --git a/ex00/main.h b/ex00/main.h index 95af0c0..efeaa15 100644 --- a/ex00/main.h +++ b/ex00/main.h @@ -6,7 +6,7 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 21:14:25 by achubuko #+# #+# */ -/* Updated: 2023/12/02 23:43:11 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 22:19:00 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MAIN_H @@ -15,4 +15,5 @@ # include # include "digits_to_words.h" # include "ft_utils.h" +# include "dict_struct.h" #endif diff --git a/tests/Makefile b/tests/Makefile index 0074e26..a81bcdd 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,10 +6,10 @@ # By: achubuko +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/12/02 22:04:03 by achubuko #+# #+# # -# Updated: 2023/12/02 23:31:12 by achubuko ### ########.fr # +# Updated: 2023/12/03 22:46:50 by achubuko ### ########.fr # # # # **************************************************************************** # -NAME := test_ft_utils +NAME := $(foreach f,$(wildcard test*.c),$(subst .c,,$f)) _CFLAGS = -Wall -Wextra -Werror CFLAGS = -g CFLAGS := $(_CFLAGS) $(CFLAGS) -I../ex00 -I../contrib/Unity/src/ diff --git a/tests/test_ft_utils.c b/tests/test_ft_utils.c index 19bf57c..f9e2362 100644 --- a/tests/test_ft_utils.c +++ b/tests/test_ft_utils.c @@ -6,7 +6,7 @@ /* By: achubuko +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/02 22:12:42 by achubuko #+# #+# */ -/* Updated: 2023/12/03 18:44:35 by achubuko ### ########.fr */ +/* Updated: 2023/12/03 22:45:24 by achubuko ### ########.fr */ /* */ /* ************************************************************************** */ #include "unity.h" @@ -44,11 +44,6 @@ void test_ft_strlen(void) TEST_ASSERT_EQUAL_INT(strlen(f), ft_strlen(f)); } -void test_ft_atoui32_42(void) -{ - TEST_ASSERT_EQUAL_UINT32(42, ft_atoui32("42")); -} - int main(void) { UNITY_BEGIN(); @@ -56,6 +51,5 @@ int main(void) RUN_TEST(test_ft_str_is_numeric_num); RUN_TEST(test_ft_str_is_numeric_nonnum); RUN_TEST(test_ft_str_is_numeric_neg); - RUN_TEST(test_ft_atoui32_42); return (UNITY_END()); }