From 35bc48a3f2c9907c8f2f3d2083e1d64b5ab00115 Mon Sep 17 00:00:00 2001 From: Nicolas TORO Date: Mon, 17 Jun 2024 16:13:07 +0200 Subject: [PATCH] [+] Add all files --- Makefile | 160 +++ README.md | 120 +- include/my.h | 95 ++ include/myflags.h | 63 + include/myformats.h | 112 ++ include/setting_up.h | 28 + lib/my/Makefile | 105 ++ lib/my/flags/Makefile | 61 + lib/my/flags/find_format.c | 154 +++ lib/my/flags/flag_a.c | 190 +++ lib/my/flags/flag_b.c | 26 + lib/my/flags/flag_biga.c | 190 +++ lib/my/flags/flag_bigd.c | 16 + lib/my/flags/flag_bige.c | 99 ++ lib/my/flags/flag_bigf.c | 36 + lib/my/flags/flag_bigg.c | 177 +++ lib/my/flags/flag_bigs.c | 25 + lib/my/flags/flag_bigx.c | 27 + lib/my/flags/flag_c.c | 23 + lib/my/flags/flag_d.c | 21 + lib/my/flags/flag_e.c | 99 ++ lib/my/flags/flag_f.c | 36 + lib/my/flags/flag_g.c | 177 +++ lib/my/flags/flag_i.c | 21 + lib/my/flags/flag_m.c | 17 + lib/my/flags/flag_n.c | 67 + lib/my/flags/flag_o.c | 26 + lib/my/flags/flag_p.c | 25 + lib/my/flags/flag_percent.c | 17 + lib/my/flags/flag_s.c | 21 + lib/my/flags/flag_u.c | 21 + lib/my/flags/flag_x.c | 27 + lib/my/flags/format_it_char.c | 28 + lib/my/flags/format_it_double.c | 27 + lib/my/flags/format_it_int.c | 27 + lib/my/flags/format_it_str.c | 26 + lib/my/flags/get_format.c | 77 ++ lib/my/flags/my.h | 95 ++ lib/my/flags/my_show_formating.c | 31 + lib/my/flags/myflags.h | 63 + lib/my/flags/myformats.h | 112 ++ lib/my/flags/mylist.h | 34 + lib/my/flags/precise_it_double.c | 26 + lib/my/flags/precise_it_int.c | 26 + lib/my/flags/specifier_base.c | 46 + lib/my/flags/specifier_int.c | 42 + lib/my/flags/sub_format_char.c | 74 ++ lib/my/flags/sub_format_double.c | 87 ++ lib/my/flags/sub_format_int.c | 96 ++ lib/my/flags/sub_format_str.c | 67 + lib/my/my.h | 95 ++ lib/my/my_array_of_array_len.c | 18 + lib/my/my_char_isprintable.c | 15 + lib/my/my_compute_factorial_rec.c | 17 + lib/my/my_compute_power_rec.c | 21 + lib/my/my_compute_power_rec_size_t.c | 23 + lib/my/my_compute_square_root.c | 22 + lib/my/my_concat_params.c | 27 + lib/my/my_convert_base.c | 55 + lib/my/my_convert_base_size_t.c | 89 ++ lib/my/my_convert_base_unsigned.c | 87 ++ lib/my/my_find_prime_sup.c | 35 + lib/my/my_getnbr.c | 76 ++ lib/my/my_getnbr_base.c | 61 + lib/my/my_is_prime.c | 21 + lib/my/my_isneg.c | 19 + lib/my/my_params_to_array.c | 28 + lib/my/my_print_combn.c | 65 + lib/my/my_print_params.c | 16 + lib/my/my_printf.c | 98 ++ lib/my/my_put_nbr.c | 42 + lib/my/my_putchar.c | 13 + lib/my/my_putnbr_base.c | 42 + lib/my/my_putstr.c | 19 + lib/my/my_putstr_error.c | 19 + lib/my/my_putstr_sized.c | 17 + lib/my/my_rev_params.c | 16 + lib/my/my_revstr.c | 22 + lib/my/my_round_float_str.c | 46 + lib/my/my_show_param_array.c | 24 + lib/my/my_show_word_array.c | 21 + lib/my/my_showmem.c | 90 ++ lib/my/my_showstr.c | 40 + lib/my/my_sort_int_array.c | 28 + lib/my/my_sort_params.c | 67 + lib/my/my_str_isalpha.c | 20 + lib/my/my_str_islower.c | 24 + lib/my/my_str_isnum.c | 20 + lib/my/my_str_isprintable.c | 19 + lib/my/my_str_isupper.c | 24 + lib/my/my_str_nbr.c | 43 + lib/my/my_str_nbr_base_long_long_int.c | 39 + lib/my/my_str_nbr_base_unsigned.c | 39 + lib/my/my_str_nbr_base_unsigned_long.c | 39 + lib/my/my_str_nbr_base_unsigned_short.c | 39 + lib/my/my_str_nbr_base_unsigned_short_short.c | 39 + lib/my/my_str_nbr_base_unsigned_size_t.c | 40 + lib/my/my_str_nbr_long_long.c | 43 + lib/my/my_str_nbr_short.c | 43 + lib/my/my_str_nbr_short_short.c | 44 + lib/my/my_str_nbr_size_t.c | 35 + lib/my/my_str_nbr_unsigned.c | 34 + lib/my/my_str_nbr_unsigned_long.c | 35 + lib/my/my_str_to_word_array.c | 100 ++ lib/my/my_strcapitalize.c | 22 + lib/my/my_strcat.c | 26 + lib/my/my_strcmp.c | 22 + lib/my/my_strcpy.c | 18 + lib/my/my_strdup.c | 23 + lib/my/my_strlen.c | 16 + lib/my/my_strlowcase.c | 21 + lib/my/my_strncat.c | 22 + lib/my/my_strncmp.c | 22 + lib/my/my_strncpy.c | 23 + lib/my/my_strstr.c | 22 + lib/my/my_strupcase.c | 21 + lib/my/my_swap.c | 15 + src/create_map.c | 38 + src/edit_map.c | 61 + src/error_handling.c | 112 ++ src/get_size.c | 72 + src/main.c | 18 + src/place_square.c | 90 ++ tests/test_my_printf.c | 1169 +++++++++++++++++ tests/test_mylib.c | 411 ++++++ 125 files changed, 7621 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 include/my.h create mode 100644 include/myflags.h create mode 100644 include/myformats.h create mode 100644 include/setting_up.h create mode 100644 lib/my/Makefile create mode 100644 lib/my/flags/Makefile create mode 100644 lib/my/flags/find_format.c create mode 100644 lib/my/flags/flag_a.c create mode 100644 lib/my/flags/flag_b.c create mode 100644 lib/my/flags/flag_biga.c create mode 100644 lib/my/flags/flag_bigd.c create mode 100644 lib/my/flags/flag_bige.c create mode 100644 lib/my/flags/flag_bigf.c create mode 100644 lib/my/flags/flag_bigg.c create mode 100644 lib/my/flags/flag_bigs.c create mode 100644 lib/my/flags/flag_bigx.c create mode 100644 lib/my/flags/flag_c.c create mode 100644 lib/my/flags/flag_d.c create mode 100644 lib/my/flags/flag_e.c create mode 100644 lib/my/flags/flag_f.c create mode 100644 lib/my/flags/flag_g.c create mode 100644 lib/my/flags/flag_i.c create mode 100644 lib/my/flags/flag_m.c create mode 100644 lib/my/flags/flag_n.c create mode 100644 lib/my/flags/flag_o.c create mode 100644 lib/my/flags/flag_p.c create mode 100644 lib/my/flags/flag_percent.c create mode 100644 lib/my/flags/flag_s.c create mode 100644 lib/my/flags/flag_u.c create mode 100644 lib/my/flags/flag_x.c create mode 100644 lib/my/flags/format_it_char.c create mode 100644 lib/my/flags/format_it_double.c create mode 100644 lib/my/flags/format_it_int.c create mode 100644 lib/my/flags/format_it_str.c create mode 100644 lib/my/flags/get_format.c create mode 100644 lib/my/flags/my.h create mode 100644 lib/my/flags/my_show_formating.c create mode 100644 lib/my/flags/myflags.h create mode 100644 lib/my/flags/myformats.h create mode 100644 lib/my/flags/mylist.h create mode 100644 lib/my/flags/precise_it_double.c create mode 100644 lib/my/flags/precise_it_int.c create mode 100644 lib/my/flags/specifier_base.c create mode 100644 lib/my/flags/specifier_int.c create mode 100644 lib/my/flags/sub_format_char.c create mode 100644 lib/my/flags/sub_format_double.c create mode 100644 lib/my/flags/sub_format_int.c create mode 100644 lib/my/flags/sub_format_str.c create mode 100644 lib/my/my.h create mode 100644 lib/my/my_array_of_array_len.c create mode 100644 lib/my/my_char_isprintable.c create mode 100644 lib/my/my_compute_factorial_rec.c create mode 100644 lib/my/my_compute_power_rec.c create mode 100644 lib/my/my_compute_power_rec_size_t.c create mode 100644 lib/my/my_compute_square_root.c create mode 100644 lib/my/my_concat_params.c create mode 100644 lib/my/my_convert_base.c create mode 100644 lib/my/my_convert_base_size_t.c create mode 100644 lib/my/my_convert_base_unsigned.c create mode 100644 lib/my/my_find_prime_sup.c create mode 100644 lib/my/my_getnbr.c create mode 100644 lib/my/my_getnbr_base.c create mode 100644 lib/my/my_is_prime.c create mode 100644 lib/my/my_isneg.c create mode 100644 lib/my/my_params_to_array.c create mode 100644 lib/my/my_print_combn.c create mode 100644 lib/my/my_print_params.c create mode 100644 lib/my/my_printf.c create mode 100644 lib/my/my_put_nbr.c create mode 100644 lib/my/my_putchar.c create mode 100644 lib/my/my_putnbr_base.c create mode 100644 lib/my/my_putstr.c create mode 100644 lib/my/my_putstr_error.c create mode 100644 lib/my/my_putstr_sized.c create mode 100644 lib/my/my_rev_params.c create mode 100644 lib/my/my_revstr.c create mode 100644 lib/my/my_round_float_str.c create mode 100644 lib/my/my_show_param_array.c create mode 100644 lib/my/my_show_word_array.c create mode 100644 lib/my/my_showmem.c create mode 100644 lib/my/my_showstr.c create mode 100644 lib/my/my_sort_int_array.c create mode 100644 lib/my/my_sort_params.c create mode 100644 lib/my/my_str_isalpha.c create mode 100644 lib/my/my_str_islower.c create mode 100644 lib/my/my_str_isnum.c create mode 100644 lib/my/my_str_isprintable.c create mode 100644 lib/my/my_str_isupper.c create mode 100644 lib/my/my_str_nbr.c create mode 100644 lib/my/my_str_nbr_base_long_long_int.c create mode 100644 lib/my/my_str_nbr_base_unsigned.c create mode 100644 lib/my/my_str_nbr_base_unsigned_long.c create mode 100644 lib/my/my_str_nbr_base_unsigned_short.c create mode 100644 lib/my/my_str_nbr_base_unsigned_short_short.c create mode 100644 lib/my/my_str_nbr_base_unsigned_size_t.c create mode 100644 lib/my/my_str_nbr_long_long.c create mode 100644 lib/my/my_str_nbr_short.c create mode 100644 lib/my/my_str_nbr_short_short.c create mode 100644 lib/my/my_str_nbr_size_t.c create mode 100644 lib/my/my_str_nbr_unsigned.c create mode 100644 lib/my/my_str_nbr_unsigned_long.c create mode 100644 lib/my/my_str_to_word_array.c create mode 100644 lib/my/my_strcapitalize.c create mode 100644 lib/my/my_strcat.c create mode 100644 lib/my/my_strcmp.c create mode 100644 lib/my/my_strcpy.c create mode 100644 lib/my/my_strdup.c create mode 100644 lib/my/my_strlen.c create mode 100644 lib/my/my_strlowcase.c create mode 100644 lib/my/my_strncat.c create mode 100644 lib/my/my_strncmp.c create mode 100644 lib/my/my_strncpy.c create mode 100644 lib/my/my_strstr.c create mode 100644 lib/my/my_strupcase.c create mode 100644 lib/my/my_swap.c create mode 100644 src/create_map.c create mode 100644 src/edit_map.c create mode 100644 src/error_handling.c create mode 100644 src/get_size.c create mode 100644 src/main.c create mode 100644 src/place_square.c create mode 100644 tests/test_my_printf.c create mode 100644 tests/test_mylib.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..df46ab0 --- /dev/null +++ b/Makefile @@ -0,0 +1,160 @@ +## +## EPITECH PROJECT, 2023 +## settingup +## File description: +## The main Makefile of our project +## + +NAME = setting_up + +MAIN = src/create_map.c \ + src/edit_map.c \ + src/error_handling.c \ + src/get_size.c \ + src/place_square.c \ + src/main.c + +SRC = ./lib/my/my_array_of_array_len.c \ + ./lib/my/my_compute_factorial_rec.c \ + ./lib/my/my_compute_power_rec.c \ + ./lib/my/my_compute_square_root.c \ + ./lib/my/my_concat_params.c \ + ./lib/my/my_convert_base.c \ + ./lib/my/my_convert_base_unsigned.c \ + ./lib/my/my_find_prime_sup.c \ + ./lib/my/my_getnbr.c \ + ./lib/my/my_getnbr_base.c \ + ./lib/my/my_isneg.c \ + ./lib/my/my_is_prime.c \ + ./lib/my/my_params_to_array.c \ + ./lib/my/my_print_combn.c \ + ./lib/my/my_print_params.c \ + ./lib/my/my_put_nbr.c \ + ./lib/my/my_putchar.c \ + ./lib/my/my_putnbr_base.c \ + ./lib/my/my_putstr.c \ + ./lib/my/my_putstr_error.c \ + ./lib/my/my_putstr_sized.c \ + ./lib/my/my_rev_params.c \ + ./lib/my/my_revstr.c \ + ./lib/my/my_show_param_array.c \ + ./lib/my/my_show_word_array.c \ + ./lib/my/my_showmem.c \ + ./lib/my/my_showstr.c \ + ./lib/my/my_sort_int_array.c \ + ./lib/my/my_sort_params.c \ + ./lib/my/my_str_isalpha.c \ + ./lib/my/my_str_islower.c \ + ./lib/my/my_str_isnum.c \ + ./lib/my/my_str_isprintable.c \ + ./lib/my/my_str_isupper.c \ + ./lib/my/my_str_nbr.c \ + ./lib/my/my_str_nbr_base_unsigned.c \ + ./lib/my/my_str_nbr_base_long_long_int.c \ + ./lib/my/my_str_nbr_unsigned.c \ + ./lib/my/my_str_nbr_unsigned_long.c \ + ./lib/my/my_str_nbr_long_long.c \ + ./lib/my/my_str_nbr_size_t.c \ + ./lib/my/my_str_to_word_array.c \ + ./lib/my/my_strcapitalize.c \ + ./lib/my/my_strcat.c \ + ./lib/my/my_strcmp.c \ + ./lib/my/my_strcpy.c \ + ./lib/my/my_strdup.c \ + ./lib/my/my_strlen.c \ + ./lib/my/my_strlowcase.c \ + ./lib/my/my_strncat.c \ + ./lib/my/my_strncmp.c \ + ./lib/my/my_strncpy.c \ + ./lib/my/my_strstr.c \ + ./lib/my/my_strupcase.c \ + ./lib/my/my_swap.c \ + ./lib/my/flags/flag_c.c \ + ./lib/my/flags/flag_s.c \ + ./lib/my/flags/flag_d.c \ + ./lib/my/flags/flag_i.c \ + ./lib/my/flags/flag_p.c \ + ./lib/my/flags/flag_o.c \ + ./lib/my/flags/flag_u.c \ + ./lib/my/flags/flag_x.c \ + ./lib/my/flags/flag_bigx.c \ + ./lib/my/flags/flag_e.c \ + ./lib/my/flags/flag_bige.c \ + ./lib/my/flags/flag_f.c \ + ./lib/my/flags/flag_bigf.c \ + ./lib/my/flags/flag_g.c \ + ./lib/my/flags/flag_bigg.c \ + ./lib/my/flags/flag_a.c \ + ./lib/my/flags/flag_biga.c \ + ./lib/my/flags/flag_n.c \ + ./lib/my/flags/flag_m.c \ + ./lib/my/flags/flag_b.c \ + ./lib/my/flags/flag_bigs.c \ + ./lib/my/flags/flag_bigd.c \ + ./lib/my/flags/flag_percent.c \ + ./lib/my/flags/find_format.c \ + ./lib/my/flags/get_format.c \ + ./lib/my/flags/my_show_formating.c \ + ./lib/my/flags/sub_format_double.c \ + ./lib/my/flags/format_it_int.c \ + ./lib/my/flags/format_it_double.c \ + ./lib/my/flags/precise_it_int.c \ + ./lib/my/flags/precise_it_double.c \ + ./lib/my/flags/format_it_str.c \ + ./lib/my/flags/format_it_char.c \ + ./lib/my/flags/sub_format_int.c \ + ./lib/my/flags/sub_format_str.c \ + ./lib/my/flags/sub_format_char.c \ + ./lib/my/flags/specifier_int.c \ + ./lib/my/flags/specifier_base.c \ + ./lib/my/my_printf.c + +MAIN-OBJ = $(MAIN:.c=.o) + +OBJ = $(SRC:.c=.o) + +CFLAGS += -Werror -Wextra -Wpedantic -O3 +CFLAGS += -I./include/ + +LDFLAGS += -L./lib/my -lmy + +TEST_FLAGS += --coverage -lcriterion -lgcov + +VALGRIND_FLAGS += -g3 + +all: libmy.a $(NAME) + +libmy.a: + @make -C./lib/my/ + +$(NAME): $(MAIN-OBJ) + @gcc -o $(NAME) $(MAIN-OBJ) $(CFLAGS) $(LDFLAGS) + +test: re + @./$(NAME) 6 "..oo." + +valgrind: fclean libmy.a $(MAIN-OBJ) + @gcc -o $(NAME) $(MAIN-OBJ) $(VALGRIND_FLAGS) $(CFLAGS) $(LDFLAGS) + @valgrind -s ./$(NAME) 6 "..oo." + +clean: + @rm -f $(OBJ) + @rm -f $(MAIN-OBJ) + +fclean: clean + @make fclean -C./lib/my/ + rm -rf ./lib/libmy.a + rm -rf libmy.a + rm -rf a.out + rm -rf unit_tests* + rm -rf vgcore* + rm -rf $(NAME) + +re: fclean all + +unit_tests: re + @gcc -o unit_tests $(SRC) tests/*.c $(LDFLAGS) $(TEST_FLAGS) + +tests_run: unit_tests + @./unit_tests + @gcovr diff --git a/README.md b/README.md index 0fe74d9..8e684b7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,118 @@ -# setting_up -A algorithmic project in C consisting in finding the largest square in a map as quickly as possible. +# Setting Up ๐Ÿ“ + +## Table of contents ๐Ÿ“‘ +- [Description](https://github.com/toro-nicolas/setting-up/blob/main/README.md#description-) +- [Usage](https://github.com/toro-nicolas/setting-up/blob/main/README.md#usage-%EF%B8%8F) +- [Result](https://github.com/toro-nicolas/setting-up/blob/main/README.md#result-) +- [Compilation](https://github.com/toro-nicolas/setting-up/blob/main/README.md#compilation-%EF%B8%8F) +- [Code mandatory](https://github.com/toro-nicolas/setting-up/blob/main/README.md#code-mandatory-) +- [What's next ?](https://github.com/toro-nicolas/setting-up/blob/main/README.md#whats-next--) +- [Contributors](https://github.com/toro-nicolas/setting-up/blob/main/README.md#contributors-) + + +## Description ๐Ÿ“ +The **Setting Up** is a project carried out in **solo**, during our **1st year** in [**EPITECH**](https://www.epitech.eu/) Grand Ecole program. +Its purpose is to create in [**C**](https://en.wikipedia.org/wiki/C_(programming_language)) a program to **find the largest square in a zone**. + + +## Usage โš”๏ธ +You can run the setting-up like this : +- Map resolution : +```sh +./setting-up exemple_files/maps/intermediate_map_100_100 +``` +- Map generation and resolution : +```sh +./setting_up 10000 "......o.." +``` + + +## Result ๐Ÿšฉ +The result of this project is a **perfect Setting Up**. +If you discover a **problem** or an **error**, don't hesitate to **create an issue** and **report it** to us as soon as possible. + + +### my.epitech.eu result + +| Category | Percentage | Numbers of tests | Crash | +|-----------------------------------------|:----------:|:----------------:|:--------:| +| Algorithm app. - Generating column | 100% | 8/8 | No | +| Algorithm app. - Generating edges | 100% | 4/4 | No | +| Algorithm app. - Generating line | 100% | 7/7 | No | +| Algorithm app. - Generating square | 100% | 3/3 | No | +| Algorithm app. - Opening column | 100% | 7/7 | No | +| Algorithm app. - Opening edges | 100% | 7/7 | No | +| Algorithm app. - Opening line | 100% | 7/7 | No | +| Algorithm app. - Opening rectangle | 100% | 7/7 | No | +| Basics | 100% | 7/7 | No | +| Data structure - Generating | 100% | 7/7 | No | +| Data structure - Opening | 100% | 7/7 | No | +| Optimization - Generating 100 to 500 | 100% | 7/7 | No | +| Optimization - Generating 1000 to 2000 | 100% | 7/7 | No | +| Optimization - Generating 5000 to 10000 | 100% | 7/7 | No | +| Optimization - Opening 100 to 500 | 100% | 7/7 | No | +| Optimization - Opening 1000 to 2000 | 100% | 7/7 | No | +| Optimization - Opening 5000 to 10000 | 100% | 7/7 | No | +| Parsing - Opening | 100% | 7/7 | No | +| Robustness - Generating empty map | 100% | 7/7 | No | +| Robustness - Generating filled map | 100% | 7/7 | No | +| Robustness - Generating valid board | 100% | 7/7 | No | +| Robustness - Opening empty map | 100% | 7/7 | No | +| Robustness - Opening filled map | 100% | 7/7 | No | +| Robustness - Opening valid file | 100% | 7/7 | No | +| **Results** | **100%** | **29/29** | **No** | + + +### Tests and code coverage +**Unit tests** were performed using [criterion](https://criterion.readthedocs.io/en/master/intro.html). +In this project, **only** the **library code is covered**. +Unit tests are still to be performed, but a large part of the code is already covered. + +You can compile the project and run the unit tests with this command : +```sh +make tests_run +``` + + +## Compilation ๐Ÿ› ๏ธ +You can compile the project with this command : +```sh +make +``` + +If you want clean the project, you can run this command : +```sh +make fclean +``` + +You can clean and compile the project with ```make re```. + +You can compile the unit tests with this command : +```sh +make unit_tests +``` + + +## Code mandatory ๐Ÿ“ฆ +- You'll need to create a branch where you'll push your code. Once you've completed your tasks on this branch, we'll work together to merge it and check that everything works. +- Every function you add must be code-style. +- Before merging, you'll need to check that all unit tests pass by running ```make tests_run```. +- Each commit will contain ```[+]``` or ```[-]``` or ```[~]``` followed by a message + - ```[+]``` : Add feature + - ```[-]``` : Delete feature + - ```[~]``` : Edit feature + +**Of course, in exceptional cases, we may depart from these rules.** + + +## What's next ? ๐Ÿš€ +- Add help section +- Add a graphical visualization +- Add github actions +- Improve the memory management +- Add a documentation for each function +- Add unit tests on each piece of code + + +## Contributors ๐Ÿ‘ค +This project was carried out alone by [**Nicolas TORO**](https://github.com/toro-nicolas). diff --git a/include/my.h b/include/my.h new file mode 100644 index 0000000..92c63df --- /dev/null +++ b/include/my.h @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2023 +** Libmy +** File description: +** Store libmy functions prototypes +*/ + +#include + +#ifndef MY_H_ + #define MY_H_ + #define ABS(value) ((value < 0) ? - value : value) + +struct info_param { + int length; + char *str; + char *copy; + char **word_array; +}; + +int my_printf(char const *format, ...); +void my_putchar(char c); +int my_isneg(int nb); +int my_put_nbr(int nb); +void my_swap(int *a, int *b); +int my_putstr(char const *str); +int my_strlen(char const *str); +int my_getnbr(char const *str); +void my_sort_int_array(int *tab, int size); +int my_compute_power_rec(int nb, int power); +int my_compute_square_root(int nb); +int my_is_prime(int nb); +int my_find_prime_sup(int nb); +char *my_strcpy(char *dest, char const *src); +char *my_strncpy(char *dest, char const *src, int n); +char *my_revstr(char *str); +char *my_strstr(char *str, char const *to_find); +int my_strcmp(char const *s1, char const *s2); +int my_strncmp(char const *s1, char const *s2, int n); +char *my_strupcase(char *str); +char *my_strlowcase(char *str); +char *my_strcapitalize(char *str); +int my_str_isalpha(char const *str); +int my_str_isnum(char const *str); +int my_str_islower(char const *str); +int my_str_isupper(char const *str); +int my_str_isprintable(char const *str); +int my_showstr(char const *str); +int my_showmem(char const *str, int size); +char *my_strcat(char *dest, char const *src); +char *my_strncat(char *dest, char const *src, int nb); + +int my_print_combn(int n); +int my_compute_factorial_rec(int nb); +int my_putnbr_base(int nbr, char const *base); +int my_getnbr_base(char const *str, char const *base); +void my_print_params(int argc, char **argv); +void my_rev_params(int argc, char **argv); +void my_sort_params(int argc, char **argv); +char *my_strdup(const char *src); +char *my_concat_params(int argc, char **argv); +int my_show_word_array(char *const *tab); +char **my_str_to_word_array(char const *str); +char *my_convert_base(char const *nbr, + char const *base_from, char const *base_to); +char *my_convert_base_unsigned(char const *nbr, + char const *base_from, char const *base_to); +char *my_convert_base_size_t(char const *nbr, + char const *base_from, char const *base_to); +struct info_param *my_params_to_array(int ac, char **av); +int my_show_param_array(struct info_param const *par); +int my_array_of_array_len(char **array); +int my_putstr_error(char const *str); +int my_putstr_sized(char const *str, int size); +size_t my_compute_power_rec_size_t(int nb, int p); +void my_round_float_str(char *float_nb, char last_char, int i, int enable); +int my_char_isprintable(char const c); + +char *my_str_nbr(int nb); +char *my_str_nbr_short(short int nb); +char *my_str_nbr_short_short(signed char nb); +char *my_str_nbr_base_unsigned(unsigned int nbr, char const *base); +char *my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base); +char *my_str_nbr_base_unsigned_short_short(unsigned char nbr, + char const *base); +char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base); +char *my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base); +char *my_str_nbr_base_long_long_int(long long int nbr, char const *base); + +char *my_str_nbr_unsigned(unsigned int nb); +char *my_str_nbr_unsigned_long(unsigned long int nb); +char *my_str_nbr_long_long(long long nb); +char *my_str_nbr_size_t(size_t nb); + +#endif /* MY_H_ */ diff --git a/include/myflags.h b/include/myflags.h new file mode 100644 index 0000000..f54ca1a --- /dev/null +++ b/include/myflags.h @@ -0,0 +1,63 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Header of all flags +*/ + +#include +#include "myformats.h" + +#ifndef MYFLAGS_H_ + #define MYFLAGS_H_ + +int flag_c(va_list list, formating_t *); +int flag_s(va_list list, formating_t *); +int flag_d(va_list list, formating_t *); +int flag_i(va_list list, formating_t *); +int flag_p(va_list list, formating_t *); +int flag_o(va_list list, formating_t *); +int flag_u(va_list list, formating_t *); +int flag_x(va_list list, formating_t *); +int flag_bigx(va_list list, formating_t *); +int flag_e(va_list list, formating_t *); +int flag_bige(va_list list, formating_t *); +int flag_f(va_list list, formating_t *); +int flag_bigf(va_list list, formating_t *); +int flag_g(va_list list, formating_t *); +int flag_bigg(va_list list, formating_t *); +int flag_a(va_list list, formating_t *); +int flag_biga(va_list list, formating_t *); +int flag_n(va_list list, formating_t *, int len); +int flag_m(va_list list, formating_t *); +int flag_b(va_list list, formating_t *); +int flag_bigs(va_list list, formating_t *); +int flag_bigd(va_list list, formating_t *); +int flag_percent(va_list list, formating_t *); + +int (*FLAGS[])(va_list, formating_t *) = { + &flag_d, + &flag_i, + &flag_c, + &flag_s, + &flag_p, + &flag_o, + &flag_u, + &flag_x, + &flag_bigx, + &flag_e, + &flag_bige, + &flag_f, + &flag_bigf, + &flag_g, + &flag_bigg, + &flag_a, + &flag_biga, + &flag_m, + &flag_b, + &flag_bigs, + &flag_bigd, + &flag_percent +}; + +#endif /* MYFLAGS_H_ */ diff --git a/include/myformats.h b/include/myformats.h new file mode 100644 index 0000000..d380088 --- /dev/null +++ b/include/myformats.h @@ -0,0 +1,112 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Header of all formats +*/ + +#include +#include + +#ifndef MYFORMATS_H_ + #define MYFORMATS_H_ + + #define FORMATAGE "-+ #0" + #define WIDTH "0123456789" + #define PRECISION "." + #define NUMBERS "0123456789" + +typedef struct formating { + int id_ft; + int id_wd; + int id_prc; + int id_nb; + int id_sp; + int nb_format; + char final_format[10]; + char next_chara; + va_list *liste; + int index_user; + int id_us; + char flag; +} formating_t; + +typedef struct flags { + char *str; + int index_flag; +} flags_t; + +typedef struct user { + char const *str; + int total_len; + int i; +} user_t; + +formating_t *find_first(user_t *user, int *i, + formating_t *formating); +formating_t *find_width(user_t *user, int *i, + formating_t *formating); +formating_t *find_precision(user_t *user, int *i, + formating_t *formating); +formating_t *find_numbers(user_t *user, int *i, + formating_t *formating); +formating_t *find_specifier(user_t *user, int *i, + formating_t *formating); + +void format_first(struct user *, struct flags *, + struct formating *, int *i); +void format_width(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void format_precision(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void format_numbers(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void format_specifier(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void my_show_formating(formating_t *formating); + +void format_plus_double(char *str_finale, formating_t *formating, double nb); +void format_moins_double(char *str_finale, formating_t *formating, double nb); +void format_esp_double(char *str_finale, formating_t *formating, double nb); +void format_hash_double(char *str_finale, formating_t *formating, double nb); +void format_zero_double(char *str_finale, formating_t *formating, double nb); +void format_it_double(char *str_finale, + formating_t *formating, double partie_d); +void precise_it_double(char *str_finale, + formating_t *formating, double partie_d); + +void format_plus_int(char *str_finale, + formating_t *formating, size_t nb); +void format_moins_int(char *str_finale, + formating_t *formating, size_t nb); +void format_esp_int(char *str_finale, + formating_t *formating, size_t nb); +void format_hash_int(char *str_finale, + formating_t *formating, size_t nb); +void format_zero_int(char *str_finale, + formating_t *formating, size_t nb); +void format_it_int(char *str_finale, + formating_t *formating, size_t nb); +void precise_it_int(char *str_finale, + formating_t *formating, size_t nb); + +void format_plus_char(char *str_finale, formating_t *formating, char c); +void format_moins_char(char *str_finale, formating_t *formating, char c); +void format_esp_char(char *str_finale, formating_t *formating, char c); +void format_hash_char(char *str_finale, formating_t *formating, char c); +void format_zero_char(char *str_finale, formating_t *formating, char c); +void format_it_char(char *str_finale, + formating_t *formating, char c); + +void format_plus_str(char *str_finale, formating_t *formating); +void format_moins_str(char *str_finale, formating_t *formating); +void format_esp_str(char *str_finale, formating_t *formating); +void format_hash_str(char *str_finale, formating_t *formating); +void format_zero_str(char *str_finale, formating_t *formating); +void format_it_str(char *str_finale, + formating_t *formating); + +char *specify_it_int(formating_t *formating, size_t temp); +char *specify_it_base(formating_t *formating, size_t temp, char *); + +#endif /* MYFORMATS_H_ */ diff --git a/include/setting_up.h b/include/setting_up.h new file mode 100644 index 0000000..9abb41d --- /dev/null +++ b/include/setting_up.h @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The setting_up.h +*/ + +#include +#include +#include +#include +#include +#include "../lib/my/flags/my.h" + +#ifndef SETTING_UP_H + #define SETTING_UP_H + +int search_error(int argc, char **argv); +int print_map(char **map, int nb_rows, int nb_cols); +int place_square(char *map, int nb_rows, int nb_cols); +int search_file_error(int file, int infos_status, struct stat *file_infos); +void get_size(char *map_str, struct stat *infos, int *rows, int *columns); +int search_map_str_error(char *map_str, int rows, int columns); +int search_map_error(char **map, int rows, int columns); +int edit_map(char *file_path); +int create_map(int map_size, char *pattern); + +#endif /* SETTING_UP_H */ diff --git a/lib/my/Makefile b/lib/my/Makefile new file mode 100644 index 0000000..67d2c2b --- /dev/null +++ b/lib/my/Makefile @@ -0,0 +1,105 @@ +## +## EPITECH PROJECT, 2023 +## Libmy +## File description: +## The Makefile of the libmy +## + +NAME = libmy.a + +SRCS = my_array_of_array_len.c \ + my_char_isprintable.c \ + my_compute_factorial_rec.c \ + my_compute_power_rec.c \ + my_compute_power_rec_size_t.c \ + my_compute_square_root.c \ + my_concat_params.c \ + my_convert_base.c \ + my_convert_base_unsigned.c \ + my_convert_base_size_t.c \ + my_find_prime_sup.c \ + my_getnbr.c \ + my_getnbr_base.c \ + my_isneg.c \ + my_is_prime.c \ + my_params_to_array.c \ + my_print_combn.c \ + my_print_params.c \ + my_put_nbr.c \ + my_putchar.c \ + my_putnbr_base.c \ + my_putstr.c \ + my_putstr_error.c \ + my_putstr_sized.c \ + my_rev_params.c \ + my_revstr.c \ + my_round_float_str.c \ + my_show_param_array.c \ + my_show_word_array.c \ + my_showmem.c \ + my_showstr.c \ + my_sort_int_array.c \ + my_sort_params.c \ + my_str_isalpha.c \ + my_str_islower.c \ + my_str_isnum.c \ + my_str_isprintable.c \ + my_str_isupper.c \ + my_str_nbr.c \ + my_str_nbr_short.c \ + my_str_nbr_short_short.c \ + my_str_nbr_base_unsigned.c \ + my_str_nbr_base_long_long_int.c \ + my_str_nbr_unsigned.c \ + my_str_nbr_unsigned_long.c \ + my_str_nbr_base_unsigned_short.c\ + my_str_nbr_base_unsigned_short_short.c\ + my_str_nbr_base_unsigned_size_t.c\ + my_str_nbr_base_unsigned_long.c \ + my_str_nbr_long_long.c \ + my_str_nbr_size_t.c \ + my_str_to_word_array.c \ + my_strcapitalize.c \ + my_strcat.c \ + my_strcmp.c \ + my_strcpy.c \ + my_strdup.c \ + my_strlen.c \ + my_strlowcase.c \ + my_strncat.c \ + my_strncmp.c \ + my_strncpy.c \ + my_strstr.c \ + my_strupcase.c \ + my_swap.c \ + my_printf.c \ + +OBJS = $(SRCS:.c=.o) +OBJS += $(wildcard flags/*.o) + +HEADER = my.h + +CFLAGS = -Werror -Wall -Wextra -Wpedantic + +all: allflags $(NAME) copy + +allflags: + @make -C./flags/ + +$(NAME): $(OBJS) + @ar rc $(NAME) $(OBJS) + +clean: + rm -rf $(OBJS) + +fclean: clean + rm -rf $(NAME) + +re: fclean all + +copy: + @cp $(NAME) ../$(NAME) + @cp $(HEADER) ../../include/$(HEADER) + @cp $(HEADER) flags/$(HEADER) + @cp flags/myflags.h ../../include/ + @cp flags/myformats.h ../../include/ diff --git a/lib/my/flags/Makefile b/lib/my/flags/Makefile new file mode 100644 index 0000000..0a88da2 --- /dev/null +++ b/lib/my/flags/Makefile @@ -0,0 +1,61 @@ +## +## EPITECH PROJECT, 2023 +## starting with makefile +## File description: +## starting with makefile +## + +SRC = flag_c.c \ + flag_s.c \ + flag_d.c \ + flag_i.c \ + flag_p.c \ + flag_o.c \ + flag_u.c \ + flag_x.c \ + flag_bigx.c \ + flag_e.c \ + flag_bige.c \ + flag_f.c \ + flag_bigf.c \ + flag_g.c \ + flag_bigg.c \ + flag_a.c \ + flag_biga.c \ + flag_n.c \ + flag_m.c \ + flag_b.c \ + flag_bigs.c \ + flag_bigd.c \ + flag_percent.c \ + find_format.c \ + get_format.c \ + my_show_formating.c \ + sub_format_double.c \ + format_it_double.c \ + precise_it_double.c \ + sub_format_int.c \ + format_it_int.c \ + precise_it_int.c \ + format_it_char.c \ + sub_format_char.c \ + format_it_str.c \ + sub_format_str.c \ + specifier_int.c \ + specifier_base.c \ + +OBJ = $(SRC:.c=.o) + +NAME = my_flags + +all: $(NAME) + +$(NAME): $(OBJ) + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all diff --git a/lib/my/flags/find_format.c b/lib/my/flags/find_format.c new file mode 100644 index 0000000..8956b78 --- /dev/null +++ b/lib/my/flags/find_format.c @@ -0,0 +1,154 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The file that contains functions for find all format +*/ + +#include "my.h" +#include "myformats.h" + +formating_t *find_specifier(user_t *user, + int *i, formating_t *formating) +{ + char str[5] = {0}; + formating_t *copy2 = formating; + char *specifier[] = {"l", "ll", "h", "hh", "j", "z", "t", "L", NULL}; + + str[0] = user->str[*i]; + if (user->str[*i + 1] == 'l' || user->str[*i + 1] == 'h') + str[1] = user->str[*i + 1]; + for (; specifier[copy2->id_sp] != NULL; (copy2->id_sp)++) { + if (my_strcmp(str, specifier[copy2->id_sp]) == 0) { + my_strcat((copy2->final_format), str); + return copy2; + } + } + formating = copy2; + formating->id_sp = -1; + return formating; +} + +static int check_star_prc(user_t *user, + formating_t *formating, formating_t *copy2, int nb) +{ + char str[1000] = {0}; + + if (formating->index_user == '*') { + nb = va_arg(*(formating->liste), int); + my_strcat(str, my_str_nbr(nb)); + my_strcat((copy2->final_format), str); + formating->id_nb = nb; + return 1; + } + return 0; +} + +formating_t *find_numbers(user_t *user, int *i, + formating_t *formating) +{ + char str[1000] = {0}; + formating_t *copy2 = formating; + int nb = 0; + + for (; NUMBERS[copy2->id_nb] != '\0'; (copy2->id_nb)++) { + formating->index_user = user->str[*i]; + if (check_star_prc(user, formating, copy2, nb) == 1) + return copy2; + if (user->str[*i] == NUMBERS[copy2->id_nb]) { + nb = my_getnbr(&user->str[*i]); + my_strcat(str, my_str_nbr(nb)); + my_strcat((copy2->final_format), str); + formating->id_nb = nb; + return copy2; + } + } + formating = copy2; + formating->id_nb = -1; + return formating; +} + +formating_t *find_precision(user_t *user, + int *i, formating_t *formating) +{ + char str[5] = {0}; + formating_t *copy2 = formating; + + for (; PRECISION[copy2->id_prc] != '\0'; (copy2->id_prc)++) { + if (user->str[*i] == PRECISION[copy2->id_prc]) { + str[0] = user->str[*i]; + my_strcat((copy2->final_format), str); + return copy2; + } + } + formating = copy2; + formating->id_prc = -1; + return formating; +} + +static int check_star_wd(user_t *user, + formating_t *formating, formating_t *copy2, int nb) +{ + char str[1000] = {0}; + + if (formating->index_user == '*') { + nb = va_arg(*(formating->liste), int); + my_strcat(str, my_str_nbr(nb)); + my_strcat((copy2->final_format), str); + formating->id_wd = nb; + return 1; + } + return 0; +} + +formating_t *find_width(user_t *user, + int *i, formating_t *formating) +{ + char str[1000] = {0}; + formating_t *copy2 = formating; + int nb = 0; + + for (; WIDTH[copy2->id_wd] != '\0'; (copy2->id_wd)++) { + formating->index_user = user->str[*i]; + if (check_star_wd(user, formating, copy2, nb) == 1) + return copy2; + if (user->str[*i] == WIDTH[copy2->id_wd]) { + nb = ABS(my_getnbr(&(user->str[*i]))); + my_strcat(str, my_str_nbr(nb)); + my_strcat((copy2->final_format), str); + formating->id_wd = nb; + return copy2; + } + } + formating = copy2; + formating->id_wd = -1; + return formating; +} + +static void check_nextesp(formating_t *copy2, formating_t *formating) +{ + if (formating->next_chara == '+') + my_strcat(copy2->final_format, "+"); + if (formating->next_chara == '-') + my_strcat(copy2->final_format, "-"); +} + +formating_t *find_first(user_t *user, + int *i, formating_t *formating) +{ + char str[5] = {0}; + formating_t *copy2 = formating; + + for (; FORMATAGE[copy2->id_ft] != '\0'; (copy2->id_ft)++) { + if (user->str[*i] == FORMATAGE[copy2->id_ft]) { + str[0] = user->str[*i]; + formating->next_chara = (user->str[*i + 1]); + my_strcat((copy2->final_format), str); + check_nextesp(copy2, formating); + return copy2; + } + } + formating = copy2; + formating->id_ft = -1; + return formating; +} diff --git a/lib/my/flags/flag_a.c b/lib/my/flags/flag_a.c new file mode 100644 index 0000000..1cf9e17 --- /dev/null +++ b/lib/my/flags/flag_a.c @@ -0,0 +1,190 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %E (a double) +*/ + +#include +#include +#include "my.h" +#include "myformats.h" + +static char *my_get_exposant_neg(int *nbr, char *converted_nb, + char *exposant_temp) +{ + char *signe = malloc(sizeof(char) * 2); + + if (converted_nb[0] < '4' + && my_strcmp(converted_nb, "3ff0000000000000") <= 0) { + signe[0] = '\0'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", + "0123456789")) - 0x400; + } else { + signe[0] = '+'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", + "0123456789")) - 0x400; + } + signe[1] = '\0'; + return my_strdup(signe); +} + +static char *my_get_exposant_pos(int *nbr, char *converted_nb, + char *exposant_temp) +{ + char *signe = malloc(sizeof(char) * 2); + + if (converted_nb[0] < 'c' + && my_strcmp(converted_nb, "bff0000000000000") <= 0) { + signe[0] = '\0'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", + "0123456789")) - 0xC00; + } else { + signe[0] = '+'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", + "0123456789")) - 0xC00; + } + signe[1] = '\0'; + return my_strdup(signe); +} + +static char *my_get_exposant(char *converted_nb, char *signe) +{ + char *exposant = malloc(sizeof(char) * 6); + char exposant_temp[4]; + int nbr = 0; + + exposant[0] = 'p'; + exposant_temp[0] = converted_nb[0]; + exposant_temp[1] = converted_nb[1]; + exposant_temp[2] = converted_nb[2]; + exposant_temp[3] = '\0'; + if (my_strcmp(signe, "-") != 0) { + my_strcat(exposant, my_get_exposant_neg(&nbr, converted_nb, + exposant_temp)); + } else { + my_strcat(exposant, my_get_exposant_pos(&nbr, converted_nb, + exposant_temp)); + } + my_strcat(exposant, my_str_nbr(nbr + 1)); + my_strcat(exposant, "\0"); + return exposant; +} + +static char *my_get_decimal_part(char *converted_nb, int *detected) +{ + char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); + int chara = 3; + + for (; chara < my_strlen(converted_nb); chara++) { + if (converted_nb[chara] == '0' && *detected == -1) + *detected = chara - 3; + if (converted_nb[chara] != '0') + *detected = -1; + decimal_part[chara - 3] = converted_nb[chara]; + } + decimal_part[*detected] = '\0'; + return decimal_part; +} + +static char *my_get_sign(char *converted_nb) +{ + if (converted_nb[0] > '7') + return "-"; + else + return ""; +} + +static void precise_zero(char *float_nb, int precision) +{ + if (precision < 1) { + my_strcat(float_nb, "0x0p+0"); + } else { + my_strcat(float_nb, "0x0."); + for (int i = 0; i < precision; i++) + my_strcat(float_nb, "0"); + my_strcat(float_nb, "p+0"); + } +} + +static void my_round(char *float_nb, int precision, + char last_char, int i) +{ + if (last_char == '\0') { + for (; i < precision; i++) + my_strcat(float_nb, "0"); + return; + } + if (float_nb[3 + i] == '.') { + float_nb[2] = float_nb[2] + 1; + return; + } + if (last_char > '7' && float_nb[3 + i] == '9') { + float_nb[3 + i] = 'a'; + return; + } + if (last_char > '7' && float_nb[3 + i] == 'f') + float_nb[3 + i] = '0'; + if (last_char > '7' && float_nb[3 + i] != '0') + float_nb[3 + i] = float_nb[3 + i] + 1; + if (float_nb[3 + i] == '0') + my_round(float_nb, precision, last_char, i - 1); +} + +static void precise_it_a(char *float_nb, formating_t *formating, + char *decimal_part, int detected) +{ + char temp[2]; + int i = 0; + + if (formating->id_nb == 0 + || (formating->id_prc == 0 && formating->id_nb == -1)) + return; + if (formating->id_nb == -1 && detected != 0) { + my_strcat(float_nb, "."); + my_strcat(float_nb, decimal_part); + return; + } + if (formating->id_nb > 0) { + my_strcat(float_nb, "."); + for (; i < formating->id_nb && decimal_part[i] != '\0'; i++) { + temp[0] = decimal_part[i]; + my_strcat(float_nb, temp); + } + my_round(float_nb, formating->id_nb, decimal_part[i], i); + return; + } +} + +static char *my_get_str_float_hexa(double nb, formating_t *formating) +{ + char *converted_nb = my_convert_base_size_t( + my_str_nbr_size_t(*(size_t *)&nb), + "0123456789", "0123456789abcdef"); + char *float_nb = malloc(sizeof(char) * (my_strlen(converted_nb) + 6)); + int detected = -1; + char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); + + my_strcat(float_nb, my_get_sign(converted_nb)); + if (my_strcmp(converted_nb, "0") == 0) { + precise_zero(float_nb, formating->id_nb); + return float_nb; + } + my_strcat(float_nb, "0x1"); + decimal_part = my_get_decimal_part(converted_nb, &detected); + precise_it_a(float_nb, formating, decimal_part, detected); + my_strcat(float_nb, my_get_exposant(converted_nb, + my_get_sign(converted_nb))); + free(decimal_part); + return float_nb; +} + +int flag_a(va_list list, formating_t *formating) +{ + double temp_double = va_arg(list, double); + char *convert_base = my_get_str_float_hexa(temp_double, formating); + + format_it_double(convert_base, formating, temp_double); + my_putstr(convert_base); + return my_strlen(convert_base); +} diff --git a/lib/my/flags/flag_b.c b/lib/my/flags/flag_b.c new file mode 100644 index 0000000..2fba8ac --- /dev/null +++ b/lib/my/flags/flag_b.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %b (an int in binary) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_b(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *convert_base = specify_it_base(formating, temp, "01"); + + precise_it_int(convert_base, formating, temp); + if (formating->id_ft == 3) { + my_revstr(convert_base); + my_strcat(convert_base, "b0"); + my_revstr(convert_base); + } + format_it_int(convert_base, formating, temp); + my_putstr(convert_base); + return my_strlen(convert_base); +} diff --git a/lib/my/flags/flag_biga.c b/lib/my/flags/flag_biga.c new file mode 100644 index 0000000..db59dbe --- /dev/null +++ b/lib/my/flags/flag_biga.c @@ -0,0 +1,190 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %A (a double in hexadecimal) +*/ + +#include +#include +#include "my.h" +#include "myformats.h" + +static char *my_get_exposant_neg(int *nbr, char *converted_nb, + char *exposant_temp) +{ + char *signe = malloc(sizeof(char) * 2); + + if (converted_nb[0] < '4' + && my_strcmp(converted_nb, "3FF0000000000000") <= 0) { + signe[0] = '\0'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", + "0123456789")) - 0x400; + } else { + signe[0] = '+'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", + "0123456789")) - 0x400; + } + signe[1] = '\0'; + return my_strdup(signe); +} + +static char *my_get_exposant_pos(int *nbr, char *converted_nb, + char *exposant_temp) +{ + char *signe = malloc(sizeof(char) * 2); + + if (converted_nb[0] < 'C' + && my_strcmp(converted_nb, "BFF0000000000000") <= 0) { + signe[0] = '\0'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", + "0123456789")) - 0xC00; + } else { + signe[0] = '+'; + *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", + "0123456789")) - 0xC00; + } + signe[1] = '\0'; + return my_strdup(signe); +} + +static char *my_get_exposant(char *converted_nb, char *signe) +{ + char *exposant = malloc(sizeof(char) * 6); + char exposant_temp[4]; + int nbr = 0; + + exposant[0] = 'P'; + exposant_temp[0] = converted_nb[0]; + exposant_temp[1] = converted_nb[1]; + exposant_temp[2] = converted_nb[2]; + exposant_temp[3] = '\0'; + if (my_strcmp(signe, "-") != 0) { + my_strcat(exposant, my_get_exposant_neg(&nbr, converted_nb, + exposant_temp)); + } else { + my_strcat(exposant, my_get_exposant_pos(&nbr, converted_nb, + exposant_temp)); + } + my_strcat(exposant, my_str_nbr(nbr + 1)); + my_strcat(exposant, "\0"); + return exposant; +} + +static char *my_get_decimal_part(char *converted_nb, int *detected) +{ + char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); + int chara = 3; + + for (; chara < my_strlen(converted_nb); chara++) { + if (converted_nb[chara] == '0' && *detected == -1) + *detected = chara - 3; + if (converted_nb[chara] != '0') + *detected = -1; + decimal_part[chara - 3] = converted_nb[chara]; + } + decimal_part[*detected] = '\0'; + return decimal_part; +} + +static char *my_get_sign(char *converted_nb) +{ + if (converted_nb[0] > '7') + return "-"; + else + return ""; +} + +static void precise_zero(char *float_nb, int precision) +{ + if (precision < 1) { + my_strcat(float_nb, "0X0P+0"); + } else { + my_strcat(float_nb, "0X0."); + for (int i = 0; i < precision; i++) + my_strcat(float_nb, "0"); + my_strcat(float_nb, "P+0"); + } +} + +static void my_round(char *float_nb, int precision, + char last_char, int i) +{ + if (last_char == '\0') { + for (; i < precision; i++) + my_strcat(float_nb, "0"); + return; + } + if (float_nb[3 + i] == '.') { + float_nb[2] = float_nb[2] + 1; + return; + } + if (last_char > '7' && float_nb[3 + i] == '9') { + float_nb[3 + i] = 'A'; + return; + } + if (last_char > '7' && float_nb[3 + i] == 'F') + float_nb[3 + i] = '0'; + if (last_char > '7' && float_nb[3 + i] != '0') + float_nb[3 + i] = float_nb[3 + i] + 1; + if (float_nb[3 + i] == '0') + my_round(float_nb, precision, last_char, i - 1); +} + +static void precise_it_biga(char *float_nb, formating_t *formating, + char *decimal_part, int detected) +{ + char temp[2]; + int i = 0; + + if (formating->id_nb == 0 + || (formating->id_prc == 0 && formating->id_nb == -1)) + return; + if (formating->id_nb == -1 && detected != 0) { + my_strcat(float_nb, "."); + my_strcat(float_nb, decimal_part); + return; + } + if (formating->id_nb > 0) { + my_strcat(float_nb, "."); + for (; i < formating->id_nb && decimal_part[i] != '\0'; i++) { + temp[0] = decimal_part[i]; + my_strcat(float_nb, temp); + } + my_round(float_nb, formating->id_nb, decimal_part[i], i); + return; + } +} + +static char *my_get_str_float_hexa(double nb, formating_t *formating) +{ + char *converted_nb = my_convert_base_size_t( + my_str_nbr_size_t(*(size_t *)&nb), + "0123456789", "0123456789ABCDEF"); + char *float_nb = malloc(sizeof(char) * (my_strlen(converted_nb) + 6)); + int detected = -1; + char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); + + my_strcat(float_nb, my_get_sign(converted_nb)); + if (my_strcmp(converted_nb, "0") == 0) { + precise_zero(float_nb, formating->id_nb); + return float_nb; + } + my_strcat(float_nb, "0X1"); + decimal_part = my_get_decimal_part(converted_nb, &detected); + precise_it_biga(float_nb, formating, decimal_part, detected); + my_strcat(float_nb, my_get_exposant(converted_nb, + my_get_sign(converted_nb))); + free(decimal_part); + return float_nb; +} + +int flag_biga(va_list list, formating_t *formating) +{ + double temp_double = va_arg(list, double); + char *convert_base = my_get_str_float_hexa(temp_double, formating); + + format_it_double(convert_base, formating, temp_double); + my_putstr(convert_base); + return my_strlen(convert_base); +} diff --git a/lib/my/flags/flag_bigd.c b/lib/my/flags/flag_bigd.c new file mode 100644 index 0000000..314246d --- /dev/null +++ b/lib/my/flags/flag_bigd.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %d (an int) +*/ + +#include +#include "my.h" +#include "mylist.h" +#include "myformats.h" + +int flag_bigd(va_list list, formating_t *formating) +{ + return 0; +} diff --git a/lib/my/flags/flag_bige.c b/lib/my/flags/flag_bige.c new file mode 100644 index 0000000..acb7c47 --- /dev/null +++ b/lib/my/flags/flag_bige.c @@ -0,0 +1,99 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %E (a double) +*/ + +#include +#include +#include +#include +#include "my.h" +#include "myformats.h" + +static char *my_get_str_float(double nb, formating_t *formating) +{ + long long int partie_entiere = ABS((int)nb); + double partie_d = ABS(nb - partie_entiere); + char *str_finale = malloc(sizeof(char) * 1000000); + + if (nb < 0){ + my_strcat(str_finale, "-"); + nb = -nb; + } + my_strcat(str_finale, my_str_nbr_long_long(partie_entiere)); + precise_it_double(str_finale, formating, partie_d); + return my_strdup(str_finale); +} + +static char *my_get_power(int exposant) +{ + char *power; + + if (ABS(exposant) >= 100) { + power = malloc(sizeof(char) * 5); + } else { + power = malloc(sizeof(char) * 4); + } + if (exposant >= 0) { + my_strcat(power, "+"); + } else { + my_strcat(power, "-"); + } + if (ABS(exposant) < 10) + my_strcat(power, "0"); + my_strcat(power, my_str_nbr(ABS(exposant))); + my_strcat(power, "\0"); + return power; +} + +static void condition(double nb, int *exposant, + double *temp_nb_double, int *temp_nb) +{ + if (ABS(nb) > 1) { + for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { + *exposant = *exposant + 1; + *temp_nb_double = *temp_nb_double / 10; + } + } else { + while (*temp_nb % 10 == 0) { + *exposant = *exposant - 1; + *temp_nb_double = *temp_nb_double * 10; + *temp_nb = *temp_nb_double; + } + } +} + +static char *my_get_str_float_scientific(double nb, formating_t *formating) +{ + char *float_nb; + int signe = 0; + int exposant = 0; + int temp_nb = nb; + double temp_nb_double = nb; + + if (nb < 0) + signe = 1; + float_nb = malloc(sizeof(char) * (16 + signe)); + if (nb == 0.0) { + my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); + my_strcat(float_nb, "E+00\0"); + return my_strdup(float_nb); + } + condition(nb, &exposant, &temp_nb_double, &temp_nb); + my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); + my_strcat(float_nb, "E"); + my_strcat(float_nb, my_get_power(exposant)); + return my_strdup(float_nb); +} + +int flag_bige(va_list list, formating_t *formating) +{ + double temp_double = va_arg(list, double); + char *temp = my_get_str_float_scientific(temp_double, formating); + + format_it_double(temp, formating, temp_double); + my_putstr(temp); + return my_strlen(temp); +} diff --git a/lib/my/flags/flag_bigf.c b/lib/my/flags/flag_bigf.c new file mode 100644 index 0000000..75a84e3 --- /dev/null +++ b/lib/my/flags/flag_bigf.c @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %F (a double) +*/ + +#include +#include +#include +#include "my.h" +#include "myformats.h" + +static char *my_get_str_float(double nb, formating_t *formating) +{ + long long int partie_entiere = ABS((int)nb); + double partie_d = ABS(nb - partie_entiere); + char *str_finale = malloc(sizeof(char) * 1000000); + + if (nb < 0){ + my_strcat(str_finale, "-"); + nb = -nb; + } + my_strcat(str_finale, my_str_nbr_long_long(partie_entiere)); + precise_it_double(str_finale, formating, partie_d); + format_it_double(str_finale, formating, partie_d); + return my_strdup(str_finale); +} + +int flag_bigf(va_list list, formating_t *formating) +{ + char *temp = my_get_str_float(va_arg(list, double), formating); + + my_putstr(temp); + return my_strlen(temp); +} diff --git a/lib/my/flags/flag_bigg.c b/lib/my/flags/flag_bigg.c new file mode 100644 index 0000000..a190784 --- /dev/null +++ b/lib/my/flags/flag_bigg.c @@ -0,0 +1,177 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %g (a double) +*/ + +#include +#include +#include +#include +#include +#include "my.h" +#include "myformats.h" + +static int check_precision(int precision, int nb) +{ + if (precision - my_strlen(my_str_nbr(ABS(nb))) <= 0) + return 0; + else + return precision - my_strlen(my_str_nbr(ABS(nb))); +} + +static int get_nb_zero(double nb) +{ + int index = 0; + int temp_nb = 0; + double temp_nb_double = nb; + + if (ABS(nb) < 1) { + while (temp_nb % 10 == 0) { + index = index + 1; + temp_nb_double = temp_nb_double * 10; + temp_nb = temp_nb_double; + } + return index - 1; + } + return 0; +} + +static void my_get_decimal_part(char *float_nb, double nb, + formating_t *formating) +{ + size_t precision_size = my_compute_power_rec_size_t(10, check_precision + (formating->id_nb, nb) + get_nb_zero(nb)); + size_t nb_convert = ABS(nb * precision_size); + int i = 0; + char temp[2] = ""; + + if (nb_convert % precision_size != 0) + my_strcat(float_nb, "."); + for (i = 0; i < get_nb_zero(nb - (int)nb); i++) + my_strcat(float_nb, "0"); + for (int j = 0; j < my_strlen(my_str_nbr_long_long(nb_convert)); j++) { + temp[0] = my_str_nbr_long_long(nb_convert % precision_size)[j]; + my_strcat(float_nb, temp); + } + nb_convert = ABS(nb * my_compute_power_rec_size_t(10, check_precision + (formating->id_nb, nb) + get_nb_zero(nb) + 1)); + my_round_float_str(float_nb, 48 + (nb_convert % 10), + my_strlen(float_nb) - 1, 0); +} + +static char *my_get_str_float(double nb, formating_t *formating) +{ + char *float_nb = malloc(sizeof(char) * + my_strlen(my_str_nbr(nb)) + 9 + formating->id_nb); + int i = 0; + + formating->id_nb = (formating->id_prc != -1) ? (formating->id_nb) : 6; + if (nb < 0) + my_strcat(float_nb, "-"); + my_strcat(float_nb, my_str_nbr_long_long(ABS(nb))); + if (formating->id_nb > 0 && check_precision(formating->id_nb, nb) != 0) { + my_get_decimal_part(float_nb, nb, formating); + i = my_strlen(float_nb) - 1; + for (i; (float_nb[i] == '0' || float_nb[i] == '.') && i != 0; i--) + float_nb[i] = '\0'; + } + my_strcat(float_nb, "\0"); + return my_strdup(float_nb); +} + +static char *my_get_power(int index_nb) +{ + char *power; + + if (ABS(index_nb) >= 100) { + power = malloc(sizeof(char) * 5); + } else { + power = malloc(sizeof(char) * 4); + } + if (index_nb >= 0) { + my_strcat(power, "+"); + } else { + my_strcat(power, "-"); + } + if (ABS(index_nb) < 10) + my_strcat(power, "0"); + my_strcat(power, my_str_nbr(ABS(index_nb))); + my_strcat(power, "\0"); + return power; +} + +static void condition(double nb, int *index_nb, + double *temp_nb_double, int *temp_nb) +{ + if (ABS(nb) > 1) { + for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { + *index_nb = *index_nb + 1; + *temp_nb_double = *temp_nb_double / 10; + } + } else { + while (*temp_nb % 10 == 0) { + *index_nb = *index_nb - 1; + *temp_nb_double = *temp_nb_double * 10; + *temp_nb = *temp_nb_double; + } + } +} + +static char *my_get_str_float_scientific(double nb, formating_t *formating) +{ + char *float_nb; + int signe = 0; + int index_nb = 0; + int temp_nb = nb; + double temp_nb_double = nb; + + if (nb < 0) + signe = 1; + float_nb = malloc(sizeof(char) * (13 + signe)); + condition(nb, &index_nb, &temp_nb_double, &temp_nb); + my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); + my_strcat(float_nb, "E"); + my_strcat(float_nb, my_get_power(index_nb)); + return my_strdup(float_nb); +} + +static int my_get_index(double nb) +{ + int index_nb = 0; + int temp_nb = nb; + double temp_nb_double = nb; + + if (nb == 0.0) + return 0; + if (ABS(nb) > 1) { + return my_strlen(my_str_nbr(ABS(nb))); + } else { + while (temp_nb % 10 == 0) { + index_nb = index_nb - 1; + temp_nb_double = temp_nb_double * 10; + temp_nb = temp_nb_double; + } + } + return index_nb; +} + +int flag_bigg(va_list list, formating_t *formating) +{ + double temp_double = va_arg(list, double); + int index = ABS(my_get_index(temp_double)); + char *temp; + + if (temp_double == 0.0) { + temp = "0"; + } else if (index - 3 < 4 && (index <= formating->id_nb || + formating->id_nb == -1)) { + temp = my_get_str_float(temp_double, formating); + } else { + temp = my_get_str_float_scientific(temp_double, formating); + } + format_it_double(temp, formating, temp_double); + my_putstr(temp); + return my_strlen(temp); +} diff --git a/lib/my/flags/flag_bigs.c b/lib/my/flags/flag_bigs.c new file mode 100644 index 0000000..9447038 --- /dev/null +++ b/lib/my/flags/flag_bigs.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %S (an array of string) +*/ + +#include +#include +#include "my.h" +#include "myformats.h" + +int flag_bigs(va_list list, formating_t *formating) +{ + char **temp = va_arg(list, char **); + int len = 0; + int index_temp = 0; + + my_show_word_array(temp); + while (temp[index_temp] != NULL) { + len = len + my_strlen(temp[index_temp]) + 1; + index_temp = index_temp + 1; + } + return len; +} diff --git a/lib/my/flags/flag_bigx.c b/lib/my/flags/flag_bigx.c new file mode 100644 index 0000000..e114ba9 --- /dev/null +++ b/lib/my/flags/flag_bigx.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %X (an int in hexadecimal) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_bigx(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *convert_base = specify_it_base(formating, + temp, "0123456789ABCDEF"); + + precise_it_int(convert_base, formating, temp); + if (formating->id_ft == 3) { + my_revstr(convert_base); + my_strcat(convert_base, "X0"); + my_revstr(convert_base); + } + format_it_int(convert_base, formating, temp); + my_putstr(convert_base); + return my_strlen(convert_base); +} diff --git a/lib/my/flags/flag_c.c b/lib/my/flags/flag_c.c new file mode 100644 index 0000000..cd213e9 --- /dev/null +++ b/lib/my/flags/flag_c.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %c (a char) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_c(va_list list, formating_t *formating) +{ + char temp = va_arg(list, int); + char str[2] = {0}; + char str_finale[100] = {0}; + + str[0] = temp; + my_strcat(str_finale, str); + format_it_char(str_finale, formating, temp); + my_putstr(str_finale); + return my_strlen(str_finale); +} diff --git a/lib/my/flags/flag_d.c b/lib/my/flags/flag_d.c new file mode 100644 index 0000000..29aed17 --- /dev/null +++ b/lib/my/flags/flag_d.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %i (an int) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_d(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *str_finale = specify_it_int(formating, temp); + + precise_it_int(str_finale, formating, temp); + format_it_int(str_finale, formating, temp); + my_putstr(str_finale); + return my_strlen(str_finale); +} diff --git a/lib/my/flags/flag_e.c b/lib/my/flags/flag_e.c new file mode 100644 index 0000000..b0904ed --- /dev/null +++ b/lib/my/flags/flag_e.c @@ -0,0 +1,99 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %E (a double) +*/ + +#include +#include +#include +#include +#include "my.h" +#include "myformats.h" + +static char *my_get_str_float(double nb, formating_t *formating) +{ + long long int partie_entiere = ABS((int)nb); + double partie_d = ABS(nb - partie_entiere); + char *str_finale = malloc(sizeof(char) * 1000000); + + if (nb < 0){ + my_strcat(str_finale, "-"); + nb = -nb; + } + my_strcat(str_finale, my_str_nbr_long_long(partie_entiere)); + precise_it_double(str_finale, formating, partie_d); + return my_strdup(str_finale); +} + +static char *my_get_power(int exposant) +{ + char *power; + + if (ABS(exposant) >= 100) { + power = malloc(sizeof(char) * 5); + } else { + power = malloc(sizeof(char) * 4); + } + if (exposant >= 0) { + my_strcat(power, "+"); + } else { + my_strcat(power, "-"); + } + if (ABS(exposant) < 10) + my_strcat(power, "0"); + my_strcat(power, my_str_nbr(ABS(exposant))); + my_strcat(power, "\0"); + return power; +} + +static void condition(double nb, int *exposant, + double *temp_nb_double, int *temp_nb) +{ + if (ABS(nb) > 1) { + for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { + *exposant = *exposant + 1; + *temp_nb_double = *temp_nb_double / 10; + } + } else { + while (*temp_nb % 10 == 0) { + *exposant = *exposant - 1; + *temp_nb_double = *temp_nb_double * 10; + *temp_nb = *temp_nb_double; + } + } +} + +static char *my_get_str_float_scientific(double nb, formating_t *formating) +{ + char *float_nb; + int signe = 0; + int exposant = 0; + int temp_nb = nb; + double temp_nb_double = nb; + + if (nb < 0) + signe = 1; + float_nb = malloc(sizeof(char) * (16 + signe)); + if (nb == 0.0) { + my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); + my_strcat(float_nb, "e+00\0"); + return my_strdup(float_nb); + } + condition(nb, &exposant, &temp_nb_double, &temp_nb); + my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); + my_strcat(float_nb, "e"); + my_strcat(float_nb, my_get_power(exposant)); + return my_strdup(float_nb); +} + +int flag_e(va_list list, formating_t *formating) +{ + double temp_double = va_arg(list, double); + char *temp = my_get_str_float_scientific(temp_double, formating); + + format_it_double(temp, formating, temp_double); + my_putstr(temp); + return my_strlen(temp); +} diff --git a/lib/my/flags/flag_f.c b/lib/my/flags/flag_f.c new file mode 100644 index 0000000..17fe7f6 --- /dev/null +++ b/lib/my/flags/flag_f.c @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %f (a double) +*/ + +#include +#include +#include +#include "my.h" +#include "myformats.h" + +static char *my_get_str_float(double nb, formating_t *formating) +{ + long long int partie_entiere = ABS((int)nb); + double partie_d = ABS(nb - partie_entiere); + char *str_finale = malloc(sizeof(char) * 1000000); + + if (nb < 0) { + my_strcat(str_finale, "-"); + } + my_strcat(str_finale, my_str_nbr_long_long(partie_entiere)); + precise_it_double(str_finale, formating, partie_d); + format_it_double(str_finale, formating, nb); + return my_strdup(str_finale); +} + +int flag_f(va_list list, formating_t *formating) +{ + double temp_nb = va_arg(list, double); + char *temp = my_get_str_float(temp_nb, formating); + + my_putstr(temp); + return my_strlen(temp); +} diff --git a/lib/my/flags/flag_g.c b/lib/my/flags/flag_g.c new file mode 100644 index 0000000..94256c7 --- /dev/null +++ b/lib/my/flags/flag_g.c @@ -0,0 +1,177 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %g (a double) +*/ + +#include +#include +#include +#include +#include +#include "my.h" +#include "myformats.h" + +static int check_precision(int precision, int nb) +{ + if (precision - my_strlen(my_str_nbr(ABS(nb))) <= 0) + return 0; + else + return precision - my_strlen(my_str_nbr(ABS(nb))); +} + +static int get_nb_zero(double nb) +{ + int index = 0; + int temp_nb = 0; + double temp_nb_double = nb; + + if (ABS(nb) < 1) { + while (temp_nb % 10 == 0) { + index = index + 1; + temp_nb_double = temp_nb_double * 10; + temp_nb = temp_nb_double; + } + return index - 1; + } + return 0; +} + +static void my_get_decimal_part(char *float_nb, double nb, + formating_t *formating) +{ + size_t precision_size = my_compute_power_rec_size_t(10, check_precision + (formating->id_nb, nb) + get_nb_zero(nb)); + size_t nb_convert = ABS(nb * precision_size); + int i = 0; + char temp[2] = ""; + + if (nb_convert % precision_size != 0) + my_strcat(float_nb, "."); + for (i = 0; i < get_nb_zero(nb - (int)nb); i++) + my_strcat(float_nb, "0"); + for (int j = 0; j < my_strlen(my_str_nbr_long_long(nb_convert)); j++) { + temp[0] = my_str_nbr_long_long(nb_convert % precision_size)[j]; + my_strcat(float_nb, temp); + } + nb_convert = ABS(nb * my_compute_power_rec_size_t(10, check_precision + (formating->id_nb, nb) + get_nb_zero(nb) + 1)); + my_round_float_str(float_nb, 48 + (nb_convert % 10), + my_strlen(float_nb) - 1, 0); +} + +static char *my_get_str_float(double nb, formating_t *formating) +{ + char *float_nb = malloc(sizeof(char) * + my_strlen(my_str_nbr(nb)) + 9 + formating->id_nb); + int i = 0; + + formating->id_nb = (formating->id_prc != -1) ? (formating->id_nb) : 6; + if (nb < 0) + my_strcat(float_nb, "-"); + my_strcat(float_nb, my_str_nbr_long_long(ABS(nb))); + if (formating->id_nb > 0 && check_precision(formating->id_nb, nb) != 0) { + my_get_decimal_part(float_nb, nb, formating); + i = my_strlen(float_nb) - 1; + for (i; (float_nb[i] == '0' || float_nb[i] == '.') && i != 0; i--) + float_nb[i] = '\0'; + } + my_strcat(float_nb, "\0"); + return my_strdup(float_nb); +} + +static char *my_get_power(int index_nb) +{ + char *power; + + if (ABS(index_nb) >= 100) { + power = malloc(sizeof(char) * 5); + } else { + power = malloc(sizeof(char) * 4); + } + if (index_nb >= 0) { + my_strcat(power, "+"); + } else { + my_strcat(power, "-"); + } + if (ABS(index_nb) < 10) + my_strcat(power, "0"); + my_strcat(power, my_str_nbr(ABS(index_nb))); + my_strcat(power, "\0"); + return power; +} + +static void condition(double nb, int *index_nb, + double *temp_nb_double, int *temp_nb) +{ + if (ABS(nb) > 1) { + for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { + *index_nb = *index_nb + 1; + *temp_nb_double = *temp_nb_double / 10; + } + } else { + while (*temp_nb % 10 == 0) { + *index_nb = *index_nb - 1; + *temp_nb_double = *temp_nb_double * 10; + *temp_nb = *temp_nb_double; + } + } +} + +static char *my_get_str_float_scientific(double nb, formating_t *formating) +{ + char *float_nb; + int signe = 0; + int index_nb = 0; + int temp_nb = nb; + double temp_nb_double = nb; + + if (nb < 0) + signe = 1; + float_nb = malloc(sizeof(char) * (13 + signe)); + condition(nb, &index_nb, &temp_nb_double, &temp_nb); + my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); + my_strcat(float_nb, "e"); + my_strcat(float_nb, my_get_power(index_nb)); + return my_strdup(float_nb); +} + +static int my_get_index(double nb) +{ + int index_nb = 0; + int temp_nb = nb; + double temp_nb_double = nb; + + if (nb == 0.0) + return 0; + if (ABS(nb) > 1) { + return my_strlen(my_str_nbr(ABS(nb))); + } else { + while (temp_nb % 10 == 0) { + index_nb = index_nb - 1; + temp_nb_double = temp_nb_double * 10; + temp_nb = temp_nb_double; + } + } + return index_nb; +} + +int flag_g(va_list list, formating_t *formating) +{ + double temp_double = va_arg(list, double); + int index = ABS(my_get_index(temp_double)); + char *temp; + + if (temp_double == 0.0) { + temp = "0"; + } else if (index - 3 < 4 && (index <= formating->id_nb || + formating->id_nb == -1)) { + temp = my_get_str_float(temp_double, formating); + } else { + temp = my_get_str_float_scientific(temp_double, formating); + } + format_it_double(temp, formating, temp_double); + my_putstr(temp); + return my_strlen(temp); +} diff --git a/lib/my/flags/flag_i.c b/lib/my/flags/flag_i.c new file mode 100644 index 0000000..d3ca09c --- /dev/null +++ b/lib/my/flags/flag_i.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %i (an int) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_i(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *str_finale = specify_it_int(formating, temp); + + precise_it_int(str_finale, formating, temp); + format_it_int(str_finale, formating, temp); + my_putstr(str_finale); + return my_strlen(str_finale); +} diff --git a/lib/my/flags/flag_m.c b/lib/my/flags/flag_m.c new file mode 100644 index 0000000..14e693b --- /dev/null +++ b/lib/my/flags/flag_m.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %m (an errno) +*/ + +#include +#include +#include +#include "my.h" +#include "myformats.h" + +int flag_m(va_list list, formating_t *formating) +{ + return 0; +} diff --git a/lib/my/flags/flag_n.c b/lib/my/flags/flag_n.c new file mode 100644 index 0000000..50dcded --- /dev/null +++ b/lib/my/flags/flag_n.c @@ -0,0 +1,67 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %n (store a int) +*/ + +#include +#include +#include "my.h" +#include "myformats.h" + +static void signed_char(va_list list, int len) +{ + signed char *variable_char = va_arg(list, signed char *); + + *variable_char = len; +} + +static void short_int(va_list list, int len) +{ + short *variable_short = va_arg(list, short *); + + *variable_short = len; +} + +static void basic_int(va_list list, int len) +{ + int *variable_int = va_arg(list, int *); + + *variable_int = len; +} + +static void long_int(va_list list, int len) +{ + long int *variable_long = va_arg(list, long *); + + *variable_long = len; +} + +static void long_long_int(va_list list, int len) +{ + long long int *variable_long_long = va_arg(list, long long *); + + *variable_long_long = len; +} + +static void basic_size_t(va_list list, int len) +{ + size_t *variable_size_t = va_arg(list, size_t *); + + *variable_size_t = len; +} + +int flag_n(va_list list, formating_t *formating, int len) +{ + void (*specify[])(va_list, int) = {&long_int, &long_long_int, + &short_int, &signed_char, &basic_size_t, + &basic_size_t, &basic_size_t, &basic_size_t}; + + if (formating->id_sp != -1) { + specify[formating->id_sp](list, len); + } else { + basic_int(list, len); + } + return 0; +} diff --git a/lib/my/flags/flag_o.c b/lib/my/flags/flag_o.c new file mode 100644 index 0000000..fb1304c --- /dev/null +++ b/lib/my/flags/flag_o.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %o (an int in octal) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_o(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *convert_base = specify_it_base(formating, temp, "01234567"); + + precise_it_int(convert_base, formating, temp); + if (formating->id_ft == 3 && convert_base[0] != '0') { + my_revstr(convert_base); + my_strcat(convert_base, "0"); + my_revstr(convert_base); + } + format_it_int(convert_base, formating, temp); + my_putstr(convert_base); + return my_strlen(convert_base); +} diff --git a/lib/my/flags/flag_p.c b/lib/my/flags/flag_p.c new file mode 100644 index 0000000..4650223 --- /dev/null +++ b/lib/my/flags/flag_p.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print %p (an address in hexadecimal) +*/ + +#include +#include +#include "my.h" +#include "myformats.h" + +int flag_p(va_list list, formating_t *formating) +{ + unsigned long int temp = va_arg(list, unsigned long int); + char *address_hexa = my_str_nbr_base_unsigned(temp, "0123456789abcdef"); + + precise_it_int(address_hexa, formating, temp); + my_revstr(address_hexa); + my_strcat(address_hexa, "x0"); + my_revstr(address_hexa); + format_it_int(address_hexa, formating, temp); + my_putstr(address_hexa); + return my_strlen(address_hexa); +} diff --git a/lib/my/flags/flag_percent.c b/lib/my/flags/flag_percent.c new file mode 100644 index 0000000..d4105cc --- /dev/null +++ b/lib/my/flags/flag_percent.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print %% +*/ + +#include "myformats.h" +#include "my.h" + +int flag_percent(va_list liste, formating_t *formating) +{ + char str_finale[2] = "%\0"; + + my_putstr(str_finale); + return my_strlen(str_finale); +} diff --git a/lib/my/flags/flag_s.c b/lib/my/flags/flag_s.c new file mode 100644 index 0000000..fde7efc --- /dev/null +++ b/lib/my/flags/flag_s.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %s (a string) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_s(va_list list, formating_t *formating) +{ + char *temp = va_arg(list, char *); + char temp2[5000] = {0}; + + my_strcat(temp2, temp); + format_it_str(temp2, formating); + my_putstr(temp2); + return my_strlen(temp2); +} diff --git a/lib/my/flags/flag_u.c b/lib/my/flags/flag_u.c new file mode 100644 index 0000000..f835e23 --- /dev/null +++ b/lib/my/flags/flag_u.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print %u (an unsigned int) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_u(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *str_finale = specify_it_base(formating, temp, "0123456789"); + + precise_it_int(str_finale, formating, temp); + format_it_int(str_finale, formating, temp); + my_putstr(str_finale); + return my_strlen(str_finale); +} diff --git a/lib/my/flags/flag_x.c b/lib/my/flags/flag_x.c new file mode 100644 index 0000000..3066c25 --- /dev/null +++ b/lib/my/flags/flag_x.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %x (an int in hexadecimal) +*/ + +#include +#include "my.h" +#include "myformats.h" + +int flag_x(va_list list, formating_t *formating) +{ + size_t temp = va_arg(list, size_t); + char *convert_base = specify_it_base(formating, + temp, "0123456789abcdef"); + + precise_it_int(convert_base, formating, temp); + if (formating->id_ft == 3) { + my_revstr(convert_base); + my_strcat(convert_base, "x0"); + my_revstr(convert_base); + } + format_it_int(convert_base, formating, temp); + my_putstr(convert_base); + return my_strlen(convert_base); +} diff --git a/lib/my/flags/format_it_char.c b/lib/my/flags/format_it_char.c new file mode 100644 index 0000000..0ab20c6 --- /dev/null +++ b/lib/my/flags/format_it_char.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %c (a char) +*/ + +#include +#include "my.h" +#include "myformats.h" + +void format_it_char(char *str_finale, + formating_t *formating, char c) +{ + void (*format[])(char *, formating_t *, char c) = + {&format_moins_char, &format_plus_char, + &format_esp_char, &format_hash_char, &format_zero_char}; + + if (formating->id_ft == -1 && formating->id_wd != -1) { + format[2](str_finale, formating, c); + } + for (int i = 0; i < 5; i++) { + if (formating->id_ft == i) { + format[i](str_finale, formating, c); + } + } + return; +} diff --git a/lib/my/flags/format_it_double.c b/lib/my/flags/format_it_double.c new file mode 100644 index 0000000..c52278d --- /dev/null +++ b/lib/my/flags/format_it_double.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** format it +** File description: +** do the function linked to format +*/ + +#include "my.h" +#include "myformats.h" + +void format_it_double(char *str_finale, + formating_t *formating, double nb) +{ + void (*format[])(char *, formating_t *, double) = + {&format_moins_double, &format_plus_double, + &format_esp_double, &format_hash_double, &format_zero_double}; + + if (formating->id_wd != -1 && formating->id_ft == -1) { + format[2](str_finale, formating, nb); + } + for (int i = 0; i < 5; i++) { + if (formating->id_ft == i) { + format[i](str_finale, formating, nb); + } + } + return; +} diff --git a/lib/my/flags/format_it_int.c b/lib/my/flags/format_it_int.c new file mode 100644 index 0000000..b0dc304 --- /dev/null +++ b/lib/my/flags/format_it_int.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** format it +** File description: +** do the function linked to format +*/ + +#include +#include "my.h" +#include "myformats.h" + +void format_it_int(char *str_finale, formating_t *formating, size_t nb) +{ + void (*format[])(char *, formating_t *, size_t) = + {&format_moins_int, &format_plus_int, + &format_esp_int, &format_hash_int, &format_zero_int}; + + if (formating->id_ft == -1 && formating->id_wd != -1) { + format[2](str_finale, formating, nb); + } + for (int i = 0; i < 5; i++) { + if (formating->id_ft == i) { + format[i](str_finale, formating, nb); + } + } + return; +} diff --git a/lib/my/flags/format_it_str.c b/lib/my/flags/format_it_str.c new file mode 100644 index 0000000..386ca37 --- /dev/null +++ b/lib/my/flags/format_it_str.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Flag to print a %c (a char) +*/ + +#include +#include "my.h" +#include "myformats.h" + +void format_it_str(char *str_finale, + formating_t *formating) +{ + void (*format[])(char *, formating_t *) = + {&format_moins_str, &format_plus_str, + &format_esp_str, &format_hash_str, &format_zero_str}; + + if (formating->id_ft == -1 && formating->id_wd != -1) + format[2](str_finale, formating); + for (int i = 0; i < 5; i++) { + if (formating->id_ft == i) + format[i](str_finale, formating); + } + return; +} diff --git a/lib/my/flags/get_format.c b/lib/my/flags/get_format.c new file mode 100644 index 0000000..00144e9 --- /dev/null +++ b/lib/my/flags/get_format.c @@ -0,0 +1,77 @@ +/* +** EPITECH PROJECT, 2023 +** find the first format +** File description: +** find the first format +*/ + +#include "myformats.h" +#include "my.h" + +static void check_operator(formating_t *formating) +{ + if (formating->next_chara == '+' || formating->next_chara == '-') { + formating->nb_format += 2; + } else { + (formating->nb_format) += 1; + } +} + +void format_first(user_t *user, flags_t *flgs, + formating_t *formating, int *copy) +{ + for (; user->str[*copy - 1] != flgs->str[flgs->index_flag]; (*copy)++) { + find_first(user, copy, formating); + if (formating->id_ft != -1) { + check_operator(formating); + break; + } + } +} + +void format_width(user_t *user, flags_t *flgs, + formating_t *formating, int *copy) +{ + for (; user->str[*copy - 1] != flgs->str[flgs->index_flag]; (*copy)++) { + find_width(user, copy, formating); + if (formating->id_wd != -1) { + (formating->nb_format) += my_strlen(my_str_nbr(formating->id_wd)); + break; + } + } +} + +void format_precision(user_t *user, flags_t *flgs, + formating_t *formating, int *copy) +{ + for (; user->str[*copy - 1] != flgs->str[flgs->index_flag]; (*copy)++) { + find_precision(user, copy, formating); + if (formating->id_prc != -1) { + formating->nb_format += 1; + break; + } + } +} + +void format_numbers(user_t *user, flags_t *flgs, + formating_t *formating, int *copy) +{ + for (; user->str[*copy - 1] != flgs->str[flgs->index_flag]; (*copy)++) { + find_numbers(user, copy, formating); + if (formating->id_nb != -1) { + (formating->nb_format) += my_strlen(my_str_nbr(formating->id_nb)); + break; + } + } +} + +void format_specifier(user_t *user, flags_t *flgs, + formating_t *formating, int *copy) +{ + find_specifier(user, copy, formating); + if (formating->id_sp == 1 || formating->id_sp == 3) { + formating->nb_format += 2; + } else if (formating->id_sp != - 1) { + formating->nb_format += 1; + } +} diff --git a/lib/my/flags/my.h b/lib/my/flags/my.h new file mode 100644 index 0000000..92c63df --- /dev/null +++ b/lib/my/flags/my.h @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2023 +** Libmy +** File description: +** Store libmy functions prototypes +*/ + +#include + +#ifndef MY_H_ + #define MY_H_ + #define ABS(value) ((value < 0) ? - value : value) + +struct info_param { + int length; + char *str; + char *copy; + char **word_array; +}; + +int my_printf(char const *format, ...); +void my_putchar(char c); +int my_isneg(int nb); +int my_put_nbr(int nb); +void my_swap(int *a, int *b); +int my_putstr(char const *str); +int my_strlen(char const *str); +int my_getnbr(char const *str); +void my_sort_int_array(int *tab, int size); +int my_compute_power_rec(int nb, int power); +int my_compute_square_root(int nb); +int my_is_prime(int nb); +int my_find_prime_sup(int nb); +char *my_strcpy(char *dest, char const *src); +char *my_strncpy(char *dest, char const *src, int n); +char *my_revstr(char *str); +char *my_strstr(char *str, char const *to_find); +int my_strcmp(char const *s1, char const *s2); +int my_strncmp(char const *s1, char const *s2, int n); +char *my_strupcase(char *str); +char *my_strlowcase(char *str); +char *my_strcapitalize(char *str); +int my_str_isalpha(char const *str); +int my_str_isnum(char const *str); +int my_str_islower(char const *str); +int my_str_isupper(char const *str); +int my_str_isprintable(char const *str); +int my_showstr(char const *str); +int my_showmem(char const *str, int size); +char *my_strcat(char *dest, char const *src); +char *my_strncat(char *dest, char const *src, int nb); + +int my_print_combn(int n); +int my_compute_factorial_rec(int nb); +int my_putnbr_base(int nbr, char const *base); +int my_getnbr_base(char const *str, char const *base); +void my_print_params(int argc, char **argv); +void my_rev_params(int argc, char **argv); +void my_sort_params(int argc, char **argv); +char *my_strdup(const char *src); +char *my_concat_params(int argc, char **argv); +int my_show_word_array(char *const *tab); +char **my_str_to_word_array(char const *str); +char *my_convert_base(char const *nbr, + char const *base_from, char const *base_to); +char *my_convert_base_unsigned(char const *nbr, + char const *base_from, char const *base_to); +char *my_convert_base_size_t(char const *nbr, + char const *base_from, char const *base_to); +struct info_param *my_params_to_array(int ac, char **av); +int my_show_param_array(struct info_param const *par); +int my_array_of_array_len(char **array); +int my_putstr_error(char const *str); +int my_putstr_sized(char const *str, int size); +size_t my_compute_power_rec_size_t(int nb, int p); +void my_round_float_str(char *float_nb, char last_char, int i, int enable); +int my_char_isprintable(char const c); + +char *my_str_nbr(int nb); +char *my_str_nbr_short(short int nb); +char *my_str_nbr_short_short(signed char nb); +char *my_str_nbr_base_unsigned(unsigned int nbr, char const *base); +char *my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base); +char *my_str_nbr_base_unsigned_short_short(unsigned char nbr, + char const *base); +char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base); +char *my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base); +char *my_str_nbr_base_long_long_int(long long int nbr, char const *base); + +char *my_str_nbr_unsigned(unsigned int nb); +char *my_str_nbr_unsigned_long(unsigned long int nb); +char *my_str_nbr_long_long(long long nb); +char *my_str_nbr_size_t(size_t nb); + +#endif /* MY_H_ */ diff --git a/lib/my/flags/my_show_formating.c b/lib/my/flags/my_show_formating.c new file mode 100644 index 0000000..28d6eb9 --- /dev/null +++ b/lib/my/flags/my_show_formating.c @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Prints a formating structure (formating) in stdout +*/ + +#include "my.h" +#include "myformats.h" + +void my_show_formating(formating_t *formating) +{ + my_putstr("Le formatage du flag est le suivant :"); + my_putstr("\nFORMATAGE: "); + my_put_nbr(formating->id_ft); + my_putstr("\nWIDHT: "); + my_put_nbr(formating->id_wd); + my_putstr("\nPRECISION: "); + my_put_nbr(formating->id_prc); + my_putstr("\nPRECISION NUMBER: "); + my_put_nbr(formating->id_nb); + my_putstr("\nSPECIFIER: "); + my_put_nbr(formating->id_sp); + my_putstr("\nFORMAT FINALE: "); + my_putstr(formating->final_format); + my_putstr("\nLE FLAG: "); + my_putchar(formating->flag); + my_putstr("\nNEXT CHARA: "); + my_putchar(formating->next_chara); + my_putchar('\n'); +} diff --git a/lib/my/flags/myflags.h b/lib/my/flags/myflags.h new file mode 100644 index 0000000..f54ca1a --- /dev/null +++ b/lib/my/flags/myflags.h @@ -0,0 +1,63 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Header of all flags +*/ + +#include +#include "myformats.h" + +#ifndef MYFLAGS_H_ + #define MYFLAGS_H_ + +int flag_c(va_list list, formating_t *); +int flag_s(va_list list, formating_t *); +int flag_d(va_list list, formating_t *); +int flag_i(va_list list, formating_t *); +int flag_p(va_list list, formating_t *); +int flag_o(va_list list, formating_t *); +int flag_u(va_list list, formating_t *); +int flag_x(va_list list, formating_t *); +int flag_bigx(va_list list, formating_t *); +int flag_e(va_list list, formating_t *); +int flag_bige(va_list list, formating_t *); +int flag_f(va_list list, formating_t *); +int flag_bigf(va_list list, formating_t *); +int flag_g(va_list list, formating_t *); +int flag_bigg(va_list list, formating_t *); +int flag_a(va_list list, formating_t *); +int flag_biga(va_list list, formating_t *); +int flag_n(va_list list, formating_t *, int len); +int flag_m(va_list list, formating_t *); +int flag_b(va_list list, formating_t *); +int flag_bigs(va_list list, formating_t *); +int flag_bigd(va_list list, formating_t *); +int flag_percent(va_list list, formating_t *); + +int (*FLAGS[])(va_list, formating_t *) = { + &flag_d, + &flag_i, + &flag_c, + &flag_s, + &flag_p, + &flag_o, + &flag_u, + &flag_x, + &flag_bigx, + &flag_e, + &flag_bige, + &flag_f, + &flag_bigf, + &flag_g, + &flag_bigg, + &flag_a, + &flag_biga, + &flag_m, + &flag_b, + &flag_bigs, + &flag_bigd, + &flag_percent +}; + +#endif /* MYFLAGS_H_ */ diff --git a/lib/my/flags/myformats.h b/lib/my/flags/myformats.h new file mode 100644 index 0000000..d380088 --- /dev/null +++ b/lib/my/flags/myformats.h @@ -0,0 +1,112 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Header of all formats +*/ + +#include +#include + +#ifndef MYFORMATS_H_ + #define MYFORMATS_H_ + + #define FORMATAGE "-+ #0" + #define WIDTH "0123456789" + #define PRECISION "." + #define NUMBERS "0123456789" + +typedef struct formating { + int id_ft; + int id_wd; + int id_prc; + int id_nb; + int id_sp; + int nb_format; + char final_format[10]; + char next_chara; + va_list *liste; + int index_user; + int id_us; + char flag; +} formating_t; + +typedef struct flags { + char *str; + int index_flag; +} flags_t; + +typedef struct user { + char const *str; + int total_len; + int i; +} user_t; + +formating_t *find_first(user_t *user, int *i, + formating_t *formating); +formating_t *find_width(user_t *user, int *i, + formating_t *formating); +formating_t *find_precision(user_t *user, int *i, + formating_t *formating); +formating_t *find_numbers(user_t *user, int *i, + formating_t *formating); +formating_t *find_specifier(user_t *user, int *i, + formating_t *formating); + +void format_first(struct user *, struct flags *, + struct formating *, int *i); +void format_width(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void format_precision(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void format_numbers(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void format_specifier(user_t *user, flags_t *flgs, + formating_t *formating, int *i); +void my_show_formating(formating_t *formating); + +void format_plus_double(char *str_finale, formating_t *formating, double nb); +void format_moins_double(char *str_finale, formating_t *formating, double nb); +void format_esp_double(char *str_finale, formating_t *formating, double nb); +void format_hash_double(char *str_finale, formating_t *formating, double nb); +void format_zero_double(char *str_finale, formating_t *formating, double nb); +void format_it_double(char *str_finale, + formating_t *formating, double partie_d); +void precise_it_double(char *str_finale, + formating_t *formating, double partie_d); + +void format_plus_int(char *str_finale, + formating_t *formating, size_t nb); +void format_moins_int(char *str_finale, + formating_t *formating, size_t nb); +void format_esp_int(char *str_finale, + formating_t *formating, size_t nb); +void format_hash_int(char *str_finale, + formating_t *formating, size_t nb); +void format_zero_int(char *str_finale, + formating_t *formating, size_t nb); +void format_it_int(char *str_finale, + formating_t *formating, size_t nb); +void precise_it_int(char *str_finale, + formating_t *formating, size_t nb); + +void format_plus_char(char *str_finale, formating_t *formating, char c); +void format_moins_char(char *str_finale, formating_t *formating, char c); +void format_esp_char(char *str_finale, formating_t *formating, char c); +void format_hash_char(char *str_finale, formating_t *formating, char c); +void format_zero_char(char *str_finale, formating_t *formating, char c); +void format_it_char(char *str_finale, + formating_t *formating, char c); + +void format_plus_str(char *str_finale, formating_t *formating); +void format_moins_str(char *str_finale, formating_t *formating); +void format_esp_str(char *str_finale, formating_t *formating); +void format_hash_str(char *str_finale, formating_t *formating); +void format_zero_str(char *str_finale, formating_t *formating); +void format_it_str(char *str_finale, + formating_t *formating); + +char *specify_it_int(formating_t *formating, size_t temp); +char *specify_it_base(formating_t *formating, size_t temp, char *); + +#endif /* MYFORMATS_H_ */ diff --git a/lib/my/flags/mylist.h b/lib/my/flags/mylist.h new file mode 100644 index 0000000..8f4a7f1 --- /dev/null +++ b/lib/my/flags/mylist.h @@ -0,0 +1,34 @@ +/* +** EPITECH PROJECT, 2023 +** my list +** File description: +** struct of linked list +*/ + +#ifndef MYLIST_H + #define MYLIST_H +typedef struct linked_list { + void *data; + struct linked_list *next; +} linked_list_t; +linked_list_t *my_params_to_list(int ac, char *const *av); +int push_front(linked_list_t **cabine, void *data); +int my_list_size(linked_list_t const *begin); +void my_rev_list(linked_list_t **begin); +int my_show_list(linked_list_t *liste); +int my_apply_on_nodes(linked_list_t *begin, int (*f)(void *)); +int my_apply_on_matching_nodes(linked_list_t *begin, int(*f)(), + void const *data_ref, int (*cmp) ()); +linked_list_t *my_find_node(linked_list_t const *begin, + void const *data, int (*cmp) ()); +int my_delete_nodes(linked_list_t **begin, void const *data_ref, int(*cmp)()); +void my_concat_list(linked_list_t **begin1, linked_list_t *begin2); +void my_sort_list(linked_list_t **begin, int (*cmp) () ); +void sorting_l(linked_list_t **ref, linked_list_t **rest, + char *temp, int(*cmp) ()); +int my_strput(char const *str); +void my_charput(char c); +int my_nbr_put(int nb); +int my_data_put(void *nb); + +#endif diff --git a/lib/my/flags/precise_it_double.c b/lib/my/flags/precise_it_double.c new file mode 100644 index 0000000..4f20f68 --- /dev/null +++ b/lib/my/flags/precise_it_double.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The functions for precise a double +*/ + +#include "my.h" +#include "myformats.h" + +void precise_it_double(char *str_finale, + formating_t *formating, double partie_d) +{ + int precision = (formating->id_prc != -1) ? (formating->id_nb) : 6; + + if (precision != 0) { + my_strcat(str_finale, "."); + for (int i = 0; i < precision; i++) { + partie_d *= 10; + my_strcat(str_finale, my_str_nbr(partie_d)); + partie_d -= (int)partie_d; + } + } + my_round_float_str(str_finale, 48 + (partie_d * 10), + my_strlen(str_finale) - 1, 0); +} diff --git a/lib/my/flags/precise_it_int.c b/lib/my/flags/precise_it_int.c new file mode 100644 index 0000000..5947ebb --- /dev/null +++ b/lib/my/flags/precise_it_int.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The functions for precise an int +*/ + +#include +#include "my.h" +#include "myformats.h" + +void precise_it_int(char *str_finale, + formating_t *formating, size_t nb) +{ + int precision = formating->id_nb; + + if (precision != 0 && my_strlen(str_finale) < precision) { + my_revstr(str_finale); + for (int i = my_strlen(str_finale); i < precision; i++) { + my_strcat(str_finale, "0"); + } + my_revstr(str_finale); + } else if (precision == 0 && nb == '\0') { + str_finale[0] = '\0'; + } +} diff --git a/lib/my/flags/specifier_base.c b/lib/my/flags/specifier_base.c new file mode 100644 index 0000000..7afc996 --- /dev/null +++ b/lib/my/flags/specifier_base.c @@ -0,0 +1,46 @@ +/* +** EPITECH PROJECT, 2023 +** specifier int +** File description: +** specifier int +*/ + +#include "my.h" +#include "myformats.h" + +static char *select_long_long(size_t temp, char *base) +{ + return my_str_nbr_base_unsigned_long((long)temp, base); +} + +static char *select_short(size_t temp, char *base) +{ + return my_str_nbr_base_unsigned_short((unsigned short)temp, base); +} + +static char *select_short_short(size_t temp, char *base) +{ + return my_str_nbr_base_unsigned_short_short((unsigned char)temp, base); +} + +static char *select_size_t(size_t temp, char *base) +{ + return my_str_nbr_base_unsigned_size_t(temp, base); +} + +char *specify_it_base(formating_t *formating, size_t temp, char *base) +{ + char *(*specify[])(size_t, char *) = + {&select_long_long, &select_long_long, + &select_short, &select_short_short, &select_size_t, + &select_size_t, &select_size_t, &select_size_t}; + + if (formating->id_sp != -1) { + return specify[formating->id_sp](temp, base); + } else if (formating->flag == 'b') { + return my_convert_base_unsigned(my_str_nbr_unsigned(temp), + "0123456789", "01"); + } else { + return my_str_nbr_base_unsigned((unsigned)temp, base); + } +} diff --git a/lib/my/flags/specifier_int.c b/lib/my/flags/specifier_int.c new file mode 100644 index 0000000..e1d3ee2 --- /dev/null +++ b/lib/my/flags/specifier_int.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2023 +** specifier int +** File description: +** specifier int +*/ + +#include "my.h" +#include "myformats.h" + +static char *select_long_long(size_t temp) +{ + return my_str_nbr_long_long(temp); +} + +static char *select_short(size_t temp) +{ + return my_str_nbr_short((short int)temp); +} + +static char *select_short_short(size_t temp) +{ + return my_str_nbr_short_short((signed char)temp); +} + +static char *select_size_t(size_t temp) +{ + return my_str_nbr_size_t(temp); +} + +char *specify_it_int(formating_t *formating, size_t temp) +{ + char *(*specify[])(size_t) = {&select_long_long, &select_long_long, + &select_short, &select_short_short, &select_size_t, + &select_size_t, &select_size_t, &select_size_t}; + + if (formating->id_sp != -1) { + return specify[formating->id_sp](temp); + } else { + return my_str_nbr(temp); + } +} diff --git a/lib/my/flags/sub_format_char.c b/lib/my/flags/sub_format_char.c new file mode 100644 index 0000000..7d58d26 --- /dev/null +++ b/lib/my/flags/sub_format_char.c @@ -0,0 +1,74 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The functions for format a char +*/ + +#include "my.h" +#include "myformats.h" + +static void do_next_char(char *str_finale, + formating_t *formating, char c) +{ + void (*format[])(char *, formating_t *, char) = + {&format_moins_char, &format_plus_char, &format_esp_char, + &format_hash_char, &format_zero_char}; + + for (int j = 0; j < 5; j++) { + if (formating->next_chara == FORMATAGE[j]) { + format[j](str_finale, formating, c); + return; + } + } +} + +void format_plus_char(char *str_finale, + formating_t *formating, char c) +{ + format_esp_char(str_finale, formating, c); + return; +} + +void format_moins_char(char *str_finale, + formating_t *formating, char c) +{ + if ((my_strlen(str_finale) < formating->id_wd + && formating->next_chara == '+') + || my_strlen(str_finale) < formating->id_wd) { + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) { + my_strcat(str_finale, " "); + } + } + do_next_char(str_finale, formating, c); +} + +void format_esp_char(char *str_finale, + formating_t *formating, char c) +{ + my_revstr(str_finale); + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) + my_strcat(str_finale, " "); + my_revstr(str_finale); +} + +void format_zero_char(char *str_finale, + formating_t *formating, char c) +{ + long long int i = my_strlen(str_finale); + + my_revstr(str_finale); + for (i;(i < formating->id_wd && formating->next_chara != '+') + || (i < formating->id_wd - 1 && formating->next_chara == '+') + ; i++) + my_strcat(str_finale, "0"); + my_revstr(str_finale); + do_next_char(str_finale, formating, c); +} + +void format_hash_char(char *str_finale, + formating_t *formating, char c) +{ + format_esp_char(str_finale, formating, c); + return; +} diff --git a/lib/my/flags/sub_format_double.c b/lib/my/flags/sub_format_double.c new file mode 100644 index 0000000..59ee80d --- /dev/null +++ b/lib/my/flags/sub_format_double.c @@ -0,0 +1,87 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The functions for format a double +*/ + +#include "my.h" +#include "myformats.h" + +static void do_next(char *str_finale, formating_t *formating, double nb) +{ + void (*format[])(char *, formating_t *, double) = + {&format_moins_double, &format_plus_double, &format_esp_double, + &format_hash_double, &format_zero_double}; + + for (int j = 0; j < 5; j++) { + if (formating->next_chara == FORMATAGE[j]) { + format[j](str_finale, formating, nb); + return; + } + } +} + +static void en_plus(char *str_finale, formating_t *formating) +{ + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) + my_strcat(str_finale, " "); +} + +void format_plus_double(char *str_finale, formating_t *formating, double nb) +{ + if (nb >= 0) { + my_revstr(str_finale); + my_strcat(str_finale, "+"); + if (formating->id_wd != -1) + en_plus(str_finale, formating); + my_revstr(str_finale); + } +} + +void format_moins_double(char *str_finale, formating_t *formating, double nb) +{ + if ((my_strlen(str_finale) < formating->id_wd + && formating->next_chara == '+') + || my_strlen(str_finale) < formating->id_wd) { + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) { + my_strcat(str_finale, " "); + } + } + do_next(str_finale, formating, nb); +} + +void format_esp_double(char *str_finale, formating_t *formating, double nb) +{ + do_next(str_finale, formating, nb); + my_revstr(str_finale); + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) + my_strcat(str_finale, " "); + if (formating->id_wd == -1 && formating->next_chara != '+' + && formating->id_prc == -1) + my_strcat(str_finale, " "); + if (formating->id_wd != -1 + && my_strlen(str_finale) > formating->id_wd + && (formating->flag == 'a' || formating->flag == 'A')) + my_strcat(str_finale, " "); + my_revstr(str_finale); +} + +void format_zero_double(char *str_finale, formating_t *formating, double nb) +{ + long long int i = my_strlen(str_finale); + + my_revstr(str_finale); + for (i;(i < formating->id_wd && formating->next_chara != '+') + || (i < formating->id_wd - 1 && formating->next_chara == '+') + ; i++) + my_strcat(str_finale, "0"); + my_revstr(str_finale); + do_next(str_finale, formating, nb); +} + +void format_hash_double(char *str_finale, formating_t *formating, double nb) +{ + format_esp_double(str_finale, formating, nb); + return; +} diff --git a/lib/my/flags/sub_format_int.c b/lib/my/flags/sub_format_int.c new file mode 100644 index 0000000..22d7e12 --- /dev/null +++ b/lib/my/flags/sub_format_int.c @@ -0,0 +1,96 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The functions for format an int +*/ + +#include +#include "my.h" +#include "myformats.h" + +static void do_next(char *str_finale, formating_t *formating, double nb) +{ + void (*format[])(char *, formating_t *, size_t) = + {&format_moins_int, &format_plus_int, &format_esp_int, + &format_hash_int, &format_zero_int}; + + for (int j = 0; j < 5; j++) { + if (formating->next_chara == FORMATAGE[j]) { + formating->next_chara = 1; + format[j](str_finale, formating, nb); + return; + } + } +} + +static void en_plus(char *str_finale, formating_t *formating) +{ + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) + my_strcat(str_finale, " "); +} + +void format_plus_int(char *str_finale, + formating_t *formating, size_t nb) +{ + if ((formating->flag != 'b') + && (formating->flag != 'x') && (formating->flag != 'X') + && (formating->flag != 'o') && (formating->flag != 'u')){ + my_revstr(str_finale); + my_strcat(str_finale, "+"); + if (formating->id_wd != -1 && formating->next_chara != '-') + en_plus(str_finale, formating); + my_revstr(str_finale); + } + if (formating->next_chara == '+' || formating->next_chara == '-') + do_next(str_finale, formating, nb); +} + +void format_moins_int(char *str_finale, + formating_t *formating, size_t nb) +{ + if (formating->next_chara == '+') + formating->id_wd -= 1; + if ((my_strlen(str_finale) < formating->id_wd + && formating->next_chara == '+') + || my_strlen(str_finale) < formating->id_wd) { + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) { + my_strcat(str_finale, " "); + } + } + if (formating->next_chara == '+' || formating->next_chara == '-') + do_next(str_finale, formating, nb); +} + +void format_esp_int(char *str_finale, formating_t *formating, size_t nb) +{ + if (formating->next_chara == '+' || formating->next_chara == '-') + do_next(str_finale, formating, nb); + my_revstr(str_finale); + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) + my_strcat(str_finale, " "); + my_revstr(str_finale); +} + +void format_zero_int(char *str_finale, + formating_t *formating, size_t nb) +{ + long long int i = my_strlen(str_finale); + + if (formating->next_chara != '-') { + my_revstr(str_finale); + for (i; (i < formating->id_wd && formating->next_chara != '+') + || (i < formating->id_wd - 1 && formating->next_chara == '+'); i++) + my_strcat(str_finale, "0"); + my_revstr(str_finale); + } + if (formating->next_chara == '+' || formating->next_chara == '-') + do_next(str_finale, formating, nb); +} + +void format_hash_int(char *str_finale, + formating_t *formating, size_t nb) +{ + format_esp_int(str_finale, formating, nb); + return; +} diff --git a/lib/my/flags/sub_format_str.c b/lib/my/flags/sub_format_str.c new file mode 100644 index 0000000..5b26851 --- /dev/null +++ b/lib/my/flags/sub_format_str.c @@ -0,0 +1,67 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The functions for format a string +*/ + +#include +#include "my.h" +#include "myformats.h" + +static void do_next_str(char *str_finale, + formating_t *formating) +{ + void (*format[])(char *, formating_t *) = + {&format_moins_str, &format_plus_str, &format_esp_str, + &format_hash_str, &format_zero_str}; + + for (int j = 0; j < 5; j++) { + if (formating->next_chara == FORMATAGE[j]) { + format[j](str_finale, formating); + return; + } + } +} + +void format_plus_str(char *str_finale, + formating_t *formating) +{ + format_esp_str(str_finale, formating); + return; +} + +void format_moins_str(char *str_finale, + formating_t *formating) +{ + if ((my_strlen(str_finale) < formating->id_wd + && formating->next_chara == '+') + || my_strlen(str_finale) < formating->id_wd) { + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) { + my_strcat(str_finale, " "); + } + } + do_next_str(str_finale, formating); +} + +void format_esp_str(char *str_finale, + formating_t *formating) +{ + my_revstr(str_finale); + for (int i = my_strlen(str_finale); i < formating->id_wd; i++) + my_strcat(str_finale, " "); + my_revstr(str_finale); +} + +void format_zero_str(char *str_finale, + formating_t *formating) +{ + format_esp_str(str_finale, formating); +} + +void format_hash_str(char *str_finale, + formating_t *formating) +{ + format_esp_str(str_finale, formating); + return; +} diff --git a/lib/my/my.h b/lib/my/my.h new file mode 100644 index 0000000..92c63df --- /dev/null +++ b/lib/my/my.h @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2023 +** Libmy +** File description: +** Store libmy functions prototypes +*/ + +#include + +#ifndef MY_H_ + #define MY_H_ + #define ABS(value) ((value < 0) ? - value : value) + +struct info_param { + int length; + char *str; + char *copy; + char **word_array; +}; + +int my_printf(char const *format, ...); +void my_putchar(char c); +int my_isneg(int nb); +int my_put_nbr(int nb); +void my_swap(int *a, int *b); +int my_putstr(char const *str); +int my_strlen(char const *str); +int my_getnbr(char const *str); +void my_sort_int_array(int *tab, int size); +int my_compute_power_rec(int nb, int power); +int my_compute_square_root(int nb); +int my_is_prime(int nb); +int my_find_prime_sup(int nb); +char *my_strcpy(char *dest, char const *src); +char *my_strncpy(char *dest, char const *src, int n); +char *my_revstr(char *str); +char *my_strstr(char *str, char const *to_find); +int my_strcmp(char const *s1, char const *s2); +int my_strncmp(char const *s1, char const *s2, int n); +char *my_strupcase(char *str); +char *my_strlowcase(char *str); +char *my_strcapitalize(char *str); +int my_str_isalpha(char const *str); +int my_str_isnum(char const *str); +int my_str_islower(char const *str); +int my_str_isupper(char const *str); +int my_str_isprintable(char const *str); +int my_showstr(char const *str); +int my_showmem(char const *str, int size); +char *my_strcat(char *dest, char const *src); +char *my_strncat(char *dest, char const *src, int nb); + +int my_print_combn(int n); +int my_compute_factorial_rec(int nb); +int my_putnbr_base(int nbr, char const *base); +int my_getnbr_base(char const *str, char const *base); +void my_print_params(int argc, char **argv); +void my_rev_params(int argc, char **argv); +void my_sort_params(int argc, char **argv); +char *my_strdup(const char *src); +char *my_concat_params(int argc, char **argv); +int my_show_word_array(char *const *tab); +char **my_str_to_word_array(char const *str); +char *my_convert_base(char const *nbr, + char const *base_from, char const *base_to); +char *my_convert_base_unsigned(char const *nbr, + char const *base_from, char const *base_to); +char *my_convert_base_size_t(char const *nbr, + char const *base_from, char const *base_to); +struct info_param *my_params_to_array(int ac, char **av); +int my_show_param_array(struct info_param const *par); +int my_array_of_array_len(char **array); +int my_putstr_error(char const *str); +int my_putstr_sized(char const *str, int size); +size_t my_compute_power_rec_size_t(int nb, int p); +void my_round_float_str(char *float_nb, char last_char, int i, int enable); +int my_char_isprintable(char const c); + +char *my_str_nbr(int nb); +char *my_str_nbr_short(short int nb); +char *my_str_nbr_short_short(signed char nb); +char *my_str_nbr_base_unsigned(unsigned int nbr, char const *base); +char *my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base); +char *my_str_nbr_base_unsigned_short_short(unsigned char nbr, + char const *base); +char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base); +char *my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base); +char *my_str_nbr_base_long_long_int(long long int nbr, char const *base); + +char *my_str_nbr_unsigned(unsigned int nb); +char *my_str_nbr_unsigned_long(unsigned long int nb); +char *my_str_nbr_long_long(long long nb); +char *my_str_nbr_size_t(size_t nb); + +#endif /* MY_H_ */ diff --git a/lib/my/my_array_of_array_len.c b/lib/my/my_array_of_array_len.c new file mode 100644 index 0000000..984442a --- /dev/null +++ b/lib/my/my_array_of_array_len.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2023 +** my_array_of_array_len +** File description: +** Returns the lenghts of array of array (array) +*/ + +#include + +int my_array_of_array_len(char **array) +{ + int len = 0; + + while (array[len] != NULL) { + len = len + 1; + } + return len; +} diff --git a/lib/my/my_char_isprintable.c b/lib/my/my_char_isprintable.c new file mode 100644 index 0000000..9652bc6 --- /dev/null +++ b/lib/my/my_char_isprintable.c @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2023 +** my_char_isprintable +** File description: +** Returns 1 if the characters (c) is printable or 0 if not +*/ + +#include "my.h" + +int my_char_isprintable(char const c) +{ + if (c < 32 || c > 126) + return 0; + return 1; +} diff --git a/lib/my/my_compute_factorial_rec.c b/lib/my/my_compute_factorial_rec.c new file mode 100644 index 0000000..d06e801 --- /dev/null +++ b/lib/my/my_compute_factorial_rec.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2023 +** day05 +** File description: +** Task02 +*/ + +int my_compute_factorial_rec(int nb) +{ + if (nb == 0 || nb == 1) { + return 1; + } else if (nb > 1 && nb < 13) { + return nb * my_compute_factorial_rec(nb - 1); + } else { + return 0; + } +} diff --git a/lib/my/my_compute_power_rec.c b/lib/my/my_compute_power_rec.c new file mode 100644 index 0000000..58808f5 --- /dev/null +++ b/lib/my/my_compute_power_rec.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_compute_power_rec +** File description: +** Returns the power (p) of the number (nb) +*/ + +int my_compute_power_rec(int nb, int p) +{ + int result; + + if (p > 0) { + result = nb; + result = result * my_compute_power_rec(nb, p - 1); + } else if (p == 0) { + result = 1; + } else { + result = 0; + } + return result; +} diff --git a/lib/my/my_compute_power_rec_size_t.c b/lib/my/my_compute_power_rec_size_t.c new file mode 100644 index 0000000..af4ae66 --- /dev/null +++ b/lib/my/my_compute_power_rec_size_t.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2023 +** my_compute_power_rec_size_t +** File description: +** Returns the power (p) of the number (nb) +*/ + +#include + +size_t my_compute_power_rec_size_t(int nb, int p) +{ + size_t result; + + if (p > 0) { + result = nb; + result = result * my_compute_power_rec_size_t(nb, p - 1); + } else if (p == 0) { + result = 1; + } else { + result = 0; + } + return result; +} diff --git a/lib/my/my_compute_square_root.c b/lib/my/my_compute_square_root.c new file mode 100644 index 0000000..44bc493 --- /dev/null +++ b/lib/my/my_compute_square_root.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_compute_square_root +** File description: +** square root of int in iterative +*/ + +int my_compute_square_root(int nb) +{ + int i = 1; + + if (nb == 0) + return 0; + while (i * i <= nb) { + if (nb / i == i + && nb % i == 0) { + return i; + } + i++; + } + return 0; +} diff --git a/lib/my/my_concat_params.c b/lib/my/my_concat_params.c new file mode 100644 index 0000000..c7246e7 --- /dev/null +++ b/lib/my/my_concat_params.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** day08 +** File description: +** Task02 +*/ + +#include +#include "my.h" + +char *my_concat_params(int argc, char **argv) +{ + char *result; + int len_result = 0; + int j = 0; + + for (int i = 0; i < argc; i++) { + len_result = len_result + my_strlen(argv[i]); + } + result = malloc(sizeof(char) * (len_result + argc)); + for (j = 0; j < argc; j++) { + result = my_strcat(result, argv[j]); + result = my_strcat(result, "\n"); + } + result[len_result + argc] = '\0'; + return result; +} diff --git a/lib/my/my_convert_base.c b/lib/my/my_convert_base.c new file mode 100644 index 0000000..e500d44 --- /dev/null +++ b/lib/my/my_convert_base.c @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2023 +** my_convert_base +** File description: +** Returns the result of the conversion of number (nbr) +** in a specific base (base_from) to another base (base_to) +*/ + +#include +#include "my.h" + +static char *put_str_nb(int nb, char const *base, int base_len, int negative) +{ + int len_nb = 1; + int temp_nb = nb; + char *nb_str; + int figure_temp = nb; + + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + nb_str = malloc(sizeof(char) * (len_nb + negative)); + if (negative == 1) + nb_str[0] = '-'; + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1 + negative] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +static char *my_setnbr_base(int nbr, char const *base) +{ + int negative = 0; + int base_len = 0; + + if (nbr < 0) { + negative = 1; + nbr = -nbr; + } + while (base[base_len] != '\0') + base_len = base_len + 1; + return put_str_nb(nbr, base, base_len, negative); +} + +char *my_convert_base(char const *nbr, + char const *base_from, char const *base_to) +{ + int number = my_getnbr_base(nbr, base_from); + char *result = my_setnbr_base(number, base_to); + + return result; +} diff --git a/lib/my/my_convert_base_size_t.c b/lib/my/my_convert_base_size_t.c new file mode 100644 index 0000000..c4973b3 --- /dev/null +++ b/lib/my/my_convert_base_size_t.c @@ -0,0 +1,89 @@ +/* +** EPITECH PROJECT, 2023 +** my_convert_base_unsigned_size_t +** File description: +** Returns the result of the conversion of number (nbr) +** in a specific base (base_from) to another base (base_to) +*/ + +#include +#include +#include +#include "my.h" + +static int is_valid(char c, char const *base) +{ + for (int i = 0; i < my_strlen(base); i++) { + if (c == base[i]) + return 1; + } + return 0; +} + +static size_t calculate_base(char const *str, char const *base, + int *index, int *error) +{ + if (is_valid(str[index[0]], base) == 1) { + if (str[index[0]] == base[index[1]]) { + return index[1] * my_compute_power_rec_size_t(my_strlen(base), + my_strlen(str) - index[0] - 1); + } + } else { + *error = 1; + } + return 0; +} + +static size_t my_getnbr_base_size_t(char const *str, char const *base) +{ + size_t result = 0; + int error = 0; + int index[2]; + + for (int i = my_strlen(str) - 1; i >= 0; i--) { + for (int j = 0; j < my_strlen(base) && error == 0; j++) { + index[0] = i; + index[1] = j; + result = result + calculate_base(str, base, index, &error); + } + } + return result; +} + +static char *put_str_nb(size_t nb, char const *base, int base_len) +{ + int len_nb = 1; + size_t temp_nb = nb; + char *nb_str; + size_t figure_temp = nb; + + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + nb_str = malloc(sizeof(char) * len_nb); + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +static char *my_setnbr_base(size_t nbr, char const *base) +{ + int base_len = 0; + + while (base[base_len] != '\0') + base_len = base_len + 1; + return put_str_nb(nbr, base, base_len); +} + +char *my_convert_base_size_t(char const *nbr, + char const *base_from, char const *base_to) +{ + size_t number = my_getnbr_base_size_t(nbr, base_from); + char *result = my_setnbr_base(number, base_to); + + return result; +} diff --git a/lib/my/my_convert_base_unsigned.c b/lib/my/my_convert_base_unsigned.c new file mode 100644 index 0000000..19c65fb --- /dev/null +++ b/lib/my/my_convert_base_unsigned.c @@ -0,0 +1,87 @@ +/* +** EPITECH PROJECT, 2023 +** my_convert_base +** File description: +** Returns the result of the conversion of number (nbr) +** in a specific base (base_from) to another base (base_to) +*/ + +#include +#include "my.h" + +static int is_valid(char c, char const *base) +{ + for (int i = 0; i < my_strlen(base); i++) { + if (c == base[i]) + return 1; + } + return 0; +} + +static int calculate_base(char const *str, char const *base, + int *index, int *error) +{ + if (is_valid(str[index[0]], base) == 1) { + if (str[index[0]] == base[index[1]]) { + return index[1] * my_compute_power_rec(my_strlen(base), + my_strlen(str) - index[0] - 1); + } + } else { + *error = 1; + } + return 0; +} + +static unsigned int my_getnbr_base_unsigned(char const *str, char const *base) +{ + unsigned int result = 0; + int error = 0; + int index[2]; + + for (int i = my_strlen(str) - 1; i >= 0; i--) { + for (int j = 0; j < my_strlen(base) && error == 0; j++) { + index[0] = i; + index[1] = j; + result = result + calculate_base(str, base, index, &error); + } + } + return result; +} + +static char *put_str_nb(unsigned int nb, char const *base, int base_len) +{ + int len_nb = 1; + unsigned int temp_nb = nb; + char *nb_str; + unsigned int figure_temp = nb; + + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + nb_str = malloc(sizeof(char) * len_nb); + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +static char *my_setnbr_base(unsigned int nbr, char const *base) +{ + int base_len = 0; + + while (base[base_len] != '\0') + base_len = base_len + 1; + return put_str_nb(nbr, base, base_len); +} + +char *my_convert_base_unsigned(char const *nbr, + char const *base_from, char const *base_to) +{ + unsigned int number = my_getnbr_base_unsigned(nbr, base_from); + char *result = my_setnbr_base(number, base_to); + + return result; +} diff --git a/lib/my/my_find_prime_sup.c b/lib/my/my_find_prime_sup.c new file mode 100644 index 0000000..43859bb --- /dev/null +++ b/lib/my/my_find_prime_sup.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2023 +** my_find_prime_sup +** File description: +** Returns the next prime number starting form a number (nb) +*/ + +static int is_prime(int nb) +{ + int prime = 1; + int i = 0; + + if (nb <= 0 || nb == 4) { + return 0; + } + for (i = 2; i < nb / 2; i++) { + if (nb % i == 0) { + prime = 0; + } + } + return prime; +} + +int my_find_prime_sup(int nb) +{ + int i = 0; + + if (nb <= 1) { + return 2; + } + while (is_prime(nb + i) == 0) { + i++; + } + return nb + i; +} diff --git a/lib/my/my_getnbr.c b/lib/my/my_getnbr.c new file mode 100644 index 0000000..d045fbb --- /dev/null +++ b/lib/my/my_getnbr.c @@ -0,0 +1,76 @@ +/* +** EPITECH PROJECT, 2023 +** my_getnbr +** File description: +** Returns a int number starting from a char number (str) +*/ + +#include "my.h" + +static int put_number_2(int result, char c) +{ + if (result != 0) { + result = (result * 10) + (c - 48); + } else { + result = c - 48; + } + return result; +} + +static int put_number_1(int *error, int *signe, int result, char c) +{ + if (*signe == -1 && ((result * 10) + (c - 48)) == -2147483648) { + result = -2147483648; + } else if ((result * 10) + (c - 48) >= 0) { + result = put_number_2(result, c); + } else { + *error = 1; + } + return result; +} + +static int char_to_int(int *letter_detected, char c, int *signe) +{ + if (c == '-' || c == '+') { + if (c == '-') { + *signe = - *signe; + } + } else { + *letter_detected = 1; + } + return (0); +} + +static int check_result(int result, int *error, int *signe) +{ + if (*error == 1) { + result = 0; + } else { + result = result * *signe; + } + return result; +} + +int my_getnbr(char const *str) +{ + int result = 0; + int len = 0; + int i = 0; + int signe = 1; + int *ptr_signe = &signe; + int letter_detected = 0; + int *ptr_letter_detected = &letter_detected; + int error = 0; + int *ptr_error = &error; + + len = my_strlen(str); + while (letter_detected == 0 && error == 0 && i < len) { + if (str[i] > 47 && str[i] < 58) { + result = put_number_1(ptr_error, ptr_signe, result, str[i]); + } else { + char_to_int(ptr_letter_detected, str[i], ptr_signe); + } + i++; + } + return check_result(result, ptr_error, ptr_signe); +} diff --git a/lib/my/my_getnbr_base.c b/lib/my/my_getnbr_base.c new file mode 100644 index 0000000..c50d2e2 --- /dev/null +++ b/lib/my/my_getnbr_base.c @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2023 +** my_getnbr_base +** File description: +** Returns a int number starting from a char number (str) +** in the requested base (base) +*/ + +#include "my.h" + +static int is_valid(char c, char const *base) +{ + for (int i = 0; i < my_strlen(base); i++) { + if (c == base[i]) + return 1; + } + if (c == '-' || c == '+') + return 1; + return 0; +} + +static int calculate_base(char const *str, char const *base, + int *index, int **ptr) +{ + if (is_valid(str[index[0]], base) == 1) { + if (str[index[0]] == '-') { + *(ptr[0]) = - *(ptr[0]); + return 0; + } + if (str[index[0]] == base[index[1]]) { + return index[1] * my_compute_power_rec(my_strlen(base), + my_strlen(str) - index[0] - 1); + } + } else { + *(ptr[1]) = 1; + } + return 0; +} + +int my_getnbr_base(char const *str, char const *base) +{ + int result = 0; + int signe_save = 0; + int signe = 1; + int error = 0; + int index[2]; + int *ptr[2]; + + for (int i = my_strlen(str) - 1; i >= 0; i--) { + for (int j = 0; j < my_strlen(base) && error == 0 && signe == 1; j++) { + index[0] = i; + index[1] = j; + ptr[0] = &signe; + ptr[1] = &error; + result = result + calculate_base(str, base, index, ptr); + } + signe_save = signe; + signe = 1; + } + return result * signe_save; +} diff --git a/lib/my/my_is_prime.c b/lib/my/my_is_prime.c new file mode 100644 index 0000000..b98a925 --- /dev/null +++ b/lib/my/my_is_prime.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_is_prime +** File description: +** Returns 1 if the number (nb) is prime and 0 otherwise +*/ + +int my_is_prime(int nb) +{ + int i = 0; + + if (nb <= 1 || nb == 4) { + return 0; + } + for (i = 2; i < nb / 2; i++) { + if (nb % i == 0) { + return 0; + } + } + return 1; +} diff --git a/lib/my/my_isneg.c b/lib/my/my_isneg.c new file mode 100644 index 0000000..3ca5639 --- /dev/null +++ b/lib/my/my_isneg.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2023 +** my_isneg +** File description: +** Prints 'P' if the number (n) is positive +** or 'N' if the number (n) is negative +*/ + +#include "my.h" + +int my_isneg(int n) +{ + if (n < 0) { + my_putchar('N'); + } else { + my_putchar('P'); + } + return 0; +} diff --git a/lib/my/my_params_to_array.c b/lib/my/my_params_to_array.c new file mode 100644 index 0000000..ecf991e --- /dev/null +++ b/lib/my/my_params_to_array.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2023 +** my_params_to_array +** File description: +** Returns a info_params struct of the agrc (ac) and the argv (av) +*/ + +#include +#include "my.h" + +struct info_param *my_params_to_array(int ac, char **av) +{ + struct info_param *result; + int i = 0; + + result = malloc(sizeof(struct info_param) * ac + 1); + for ( ; i < ac; i++) { + result[i].length = my_strlen(av[i]); + result[i].str = av[i]; + result[i].copy = my_strdup(av[i]); + result[i].word_array = my_str_to_word_array(av[i]); + } + result[i].length = 0; + result[i].str = 0; + result[i].copy = 0; + result[i].word_array = 0; + return result; +} diff --git a/lib/my/my_print_combn.c b/lib/my/my_print_combn.c new file mode 100644 index 0000000..0db9189 --- /dev/null +++ b/lib/my/my_print_combn.c @@ -0,0 +1,65 @@ +/* +** EPITECH PROJECT, 2023 +** my_print_combn +** File description: +** Prints all the numbers composed by (n) different digits numbers +** All digits in the number are different and only the smallest +** number composed by those digits are display +*/ + +#include +#include "my.h" + +static char *str_number_to_int(int nb, int n) +{ + int len_nb = 1; + int temp_nb = nb; + char *nb_str; + int figure_temp = nb; + int nb_zero = 0; + + while ((temp_nb / 10) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / 10; + } + nb_str = malloc(sizeof(char) * n); + for ( ; nb_zero < n - len_nb - 1; nb_zero++) + nb_str[nb_zero] = '0'; + for (int i = 0 ; i < n; i++) { + nb_str[n - i - 1 + nb_zero] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[n] = '\0'; + return nb_str; +} + +static int check_printable(char *number, int figure, int *start) +{ + if (number[figure - 1] >= number[figure]) + return 1; + if (figure - 1 == 0) { + if (*start == 1) + *start = 0; + else { + my_putchar(','); + my_putchar(' '); + } + my_putstr(number); + } + return 0; +} + +int my_print_combn(int n) +{ + int stop = 0; + int start = 1; + + for (int number = 0; number < my_compute_power_rec(10, n); number++) { + for (int figure = n - 1; figure >= 0 && stop == 0; figure--) { + stop = check_printable(str_number_to_int(number, n), + figure, &start); + } + stop = 0; + } + return 0; +} diff --git a/lib/my/my_print_params.c b/lib/my/my_print_params.c new file mode 100644 index 0000000..3e2387b --- /dev/null +++ b/lib/my/my_print_params.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2023 +** day07 +** File description: +** Task04 +*/ + +#include "my.h" + +void my_print_params(int argc, char **argv) +{ + for (int i = 0; i < argc; i++) { + my_putstr(argv[i]); + my_putstr("\n"); + } +} diff --git a/lib/my/my_printf.c b/lib/my/my_printf.c new file mode 100644 index 0000000..7fbf7f2 --- /dev/null +++ b/lib/my/my_printf.c @@ -0,0 +1,98 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Prints a string (format) with possible flags and format +** in stdout and returns the lenght the printed string +*/ + +#include +#include "my.h" +#include "./flags/myflags.h" +#include "./flags/myformats.h" + +static void find_format(user_t *user, + flags_t *flgs, formating_t *formating) +{ + int copy = user->i + 1; + + formating->nb_format = 0; + format_first(user, flgs, formating, ©); + copy = user->i + 1 + formating->nb_format; + format_width(user, flgs, formating, ©); + copy = user->i + 1 + formating->nb_format; + format_precision(user, flgs, formating, ©); + copy = user->i + 1 + formating->nb_format; + format_numbers(user, flgs, formating, ©); + copy = user->i + 1 + formating->nb_format; + format_specifier(user, flgs, formating, ©); + copy = user->i + 1 + formating->nb_format; + formating->flag = user->str[copy]; +} + +static void not_a_flag(formating_t *formating, user_t *user) +{ + my_putchar('%'); + (user->total_len) += 1; + if (my_strcmp(formating->final_format, "\0") != 0) { + my_putstr(formating->final_format); + (user->total_len) += my_strlen(formating->final_format); + } + if (formating->flag != '\0') { + my_putchar(formating->flag); + (user->total_len) += 1; + } +} + +static void find_flags(user_t *user, va_list list, + formating_t *formating, int *index) +{ + flags_t flgs = {.str = "dicspouxXeEfFgGaAmbSD%", + .index_flag = 0}; + int copi; + + find_format(user, &flgs, formating); + copi = user->i + my_strlen(formating->final_format) + 1; + user->i = user->i + (my_strlen(formating->final_format)) + 1; + for (; flgs.index_flag < my_strlen(flgs.str); flgs.index_flag++) { + if (user->str[copi] == 'n') { + flag_n(list, formating, user->total_len); + return; + } + if (user->str[copi] == flgs.str[flgs.index_flag]) { + *index = flgs.index_flag; + return; + } + } + not_a_flag(formating, user); + *index = -1; +} + +static void select_display(user_t *user, va_list *liste) +{ + int index_flag = -1; + formating_t formating = {.liste = liste}; + + if (user->str[user->i] == '%') + find_flags(user, *liste, &formating, &index_flag); + if (index_flag != -1) + user->total_len += FLAGS[index_flag](*liste, &formating); + if (user->str[user->i] != '%' && user->str[user->i] != '\0' + && (user->str[user->i] != formating.flag)) { + my_putchar(user->str[user->i]); + (user->total_len)++; + } +} + +int my_printf(char const *format, ...) +{ + va_list liste; + user_t user = {.str = format, .total_len = 0}; + + va_start(liste, format); + for (user.i = 0; user.i < my_strlen(user.str); (user.i)++) { + select_display(&user, &liste); + } + va_end(liste); + return user.total_len; +} diff --git a/lib/my/my_put_nbr.c b/lib/my/my_put_nbr.c new file mode 100644 index 0000000..fbd41c9 --- /dev/null +++ b/lib/my/my_put_nbr.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2023 +** my_put_nbr +** File description: +** Prints a number (nb) in stdout +*/ + +#include "my.h" + +static void put_str_nb(int nb, int len_nb) +{ + char nb_str[len_nb]; + int figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[len_nb] = '\0'; + my_putstr(nb_str); +} + +int my_put_nbr(int nb) +{ + int len_nb = 1; + int temp_nb = nb; + + if (nb == -2147483648) { + my_putstr("-2147483648"); + return 0; + } + if (nb < 0) { + my_putchar('-'); + nb = -nb; + } + while ((temp_nb / 10) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / 10; + } + put_str_nb(nb, len_nb); + return 0; +} diff --git a/lib/my/my_putchar.c b/lib/my/my_putchar.c new file mode 100644 index 0000000..feff7e5 --- /dev/null +++ b/lib/my/my_putchar.c @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2023 +** my_putchar +** File description: +** Prints a char in the stdout +*/ + +#include + +void my_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/lib/my/my_putnbr_base.c b/lib/my/my_putnbr_base.c new file mode 100644 index 0000000..874c095 --- /dev/null +++ b/lib/my/my_putnbr_base.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2023 +** my_putnbr_base +** File description: +** Prints a number (nb) in the requested base (base) in stdout +*/ + +#include "my.h" + +static void put_str_nb(int nb, int len_nb, char const *base, int base_len) +{ + char nb_str[len_nb]; + int figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + my_putstr(nb_str); +} + +int my_putnbr_base(int nbr, char const *base) +{ + int len_nb = 1; + int temp_nb = nbr; + int base_len = 0; + + if (nbr < 0) { + my_putchar('-'); + nbr = -nbr; + } + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + put_str_nb(nbr, len_nb, base, base_len); + return 0; +} diff --git a/lib/my/my_putstr.c b/lib/my/my_putstr.c new file mode 100644 index 0000000..4c9a736 --- /dev/null +++ b/lib/my/my_putstr.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2023 +** my_putstr +** File description: +** Prints a string (str) in stdout +*/ + +#include "my.h" + +int my_putstr(char const *str) +{ + int i = 0; + + while (str[i] != '\0') { + my_putchar(str[i]); + i = i + 1; + } + return 0; +} diff --git a/lib/my/my_putstr_error.c b/lib/my/my_putstr_error.c new file mode 100644 index 0000000..e7ce4af --- /dev/null +++ b/lib/my/my_putstr_error.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2023 +** my_putstr_error +** File description: +** Prints a string (str) in stderr +*/ + +#include + +int my_putstr_error(char const *str) +{ + int i = 0; + + while (str[i] != '\0') { + write(2, &str[i], 1); + i = i + 1; + } + return 0; +} diff --git a/lib/my/my_putstr_sized.c b/lib/my/my_putstr_sized.c new file mode 100644 index 0000000..4e926fe --- /dev/null +++ b/lib/my/my_putstr_sized.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2023 +** my_putstr_limited +** File description: +** Prints a string (str) in stdout +** with a defined size (size) +*/ + +#include "my.h" + +int my_putstr_sized(char const *str, int size) +{ + for (int i = 0; i < size; i++) { + my_putchar(str[i]); + } + return 0; +} diff --git a/lib/my/my_rev_params.c b/lib/my/my_rev_params.c new file mode 100644 index 0000000..0a1f0f1 --- /dev/null +++ b/lib/my/my_rev_params.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2023 +** day07 +** File description: +** Task05 +*/ + +#include "my.h" + +void my_rev_params(int argc, char **argv) +{ + for (int i = argc; i > 0; i--) { + my_putstr(argv[i - 1]); + my_putstr("\n"); + } +} diff --git a/lib/my/my_revstr.c b/lib/my/my_revstr.c new file mode 100644 index 0000000..4d97ce6 --- /dev/null +++ b/lib/my/my_revstr.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_revstr +** File description: +** Reverses the characters in a string (str) and returns the string (str) +*/ + +char *my_revstr(char *str) +{ + int len = 0; + char temp; + + while (str[len] != '\0') { + len = len + 1; + } + for (int i = len / 2; i > 0; i--) { + temp = str[i - 1]; + str[i - 1] = str[len - i]; + str[len - i] = temp; + } + return str; +} diff --git a/lib/my/my_round_float_str.c b/lib/my/my_round_float_str.c new file mode 100644 index 0000000..5cce67e --- /dev/null +++ b/lib/my/my_round_float_str.c @@ -0,0 +1,46 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** Modify a string (float) and round it +*/ + +#include +#include "my.h" + +static void add_number(char *float_nb) +{ + char *temp_float_str = malloc(sizeof(char) * my_strlen(float_nb + 1)); + + temp_float_str[0] = '1'; + my_strcat(temp_float_str, float_nb); + my_strcpy(float_nb, temp_float_str); + if (float_nb[1] == '-') { + float_nb[1] = float_nb[0]; + float_nb[0] = '-'; + } + free(temp_float_str); +} + +void my_round_float_str(char *float_nb, char last_char, int i, int enable) +{ + if (float_nb[i] == '\0' || float_nb[i] == '-') + add_number(float_nb); + if (float_nb[i] == '.') { + if (float_nb[i - 1] == '9') + float_nb[i - 1] = '0'; + else + float_nb[i - 1] = float_nb[i - 1] + 1; + if (float_nb[i - 1] == '0') + my_round_float_str(float_nb, float_nb[i - 1], i - 2, 0); + return; + } + if (float_nb[i] != '.' && (last_char > '4' || enable == 1)) { + if (float_nb[i] == '9') + float_nb[i] = '0'; + else + float_nb[i] = float_nb[i] + 1; + if (float_nb[i] == '0') + my_round_float_str(float_nb, float_nb[i], i - 1, 1); + } +} diff --git a/lib/my/my_show_param_array.c b/lib/my/my_show_param_array.c new file mode 100644 index 0000000..ded8210 --- /dev/null +++ b/lib/my/my_show_param_array.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2023 +** my_show_param_array +** File description: +** Prints the name, the length and word array of +** each element of an array (par) of the info_param struct +*/ + +#include "my.h" + +int my_show_param_array(struct info_param const *par) +{ + int i = 0; + + while (par[i].length != 0) { + my_putstr(par[i].str); + my_putchar('\n'); + my_put_nbr(par[i].length); + my_putchar('\n'); + my_show_word_array(par[i].word_array); + i++; + } + return 0; +} diff --git a/lib/my/my_show_word_array.c b/lib/my/my_show_word_array.c new file mode 100644 index 0000000..cd45bbc --- /dev/null +++ b/lib/my/my_show_word_array.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_show_word_array +** File description: +** Prints all word in an array (tab) +*/ + +#include +#include "my.h" + +int my_show_word_array(char *const *tab) +{ + int index_tab = 0; + + while (tab[index_tab] != NULL) { + my_putstr(tab[index_tab]); + my_putchar('\n'); + index_tab = index_tab + 1; + } + return 0; +} diff --git a/lib/my/my_showmem.c b/lib/my/my_showmem.c new file mode 100644 index 0000000..a9cb1e9 --- /dev/null +++ b/lib/my/my_showmem.c @@ -0,0 +1,90 @@ +/* +** EPITECH PROJECT, 2023 +** my_showmem +** File description: +** Prints a memory dump of a string (str) with a defined size +*/ + +#include "my.h" + +static void print_address(int line) +{ + int len = 1 + line / 16; + + for (int i = 0; i < 8 - len - 1; i++) + my_putchar('0'); + my_putnbr_base(line, "0123456789abcedf"); + my_putstr("0: "); +} + +static void check_zero_in_hexadecimal(char c) +{ + if (c < 16) + my_putchar('0'); +} + +static void check_space(int space) +{ + if (space != 2) + my_putstr(" "); +} + +static void check_backslash(char c, int space) +{ + if (c == '\b' && space == 0) + my_putstr("08"); + else if (c == '\b' && space == 2) + my_putchar('.'); + else + check_space(space); +} + +static void print_hexadecimal_content(char const *str, int size, int line) +{ + int end_of_line = 0; + + for (int i = 16 * line; i < 16 * (line + 1); i++) { + if (i != 16 * line && i % 2 == 0) { + my_putchar(' '); + } + if (i == size || str[i] == '\b' || end_of_line == 1) { + check_backslash(str[i], end_of_line); + end_of_line = 1; + } else { + check_zero_in_hexadecimal(str[i]); + my_putnbr_base(str[i], "0123456789abcdef"); + } + } + my_putchar(' '); +} + +static void print_string_content(char const *str, int size, + int line, int *error) +{ + for (int i = 16 * line; i < 16 * (line + 1); i++) { + if (i == size || str[i] == '\b') { + check_backslash(str[i], 2); + *error = 1; + break; + } + if (str[i] >= 32 && str[i] <= 126) { + my_putchar(str[i]); + } else { + my_putchar('.'); + } + } +} + +int my_showmem(char const *str, int size) +{ + int number_of_lines = size / 16; + int error = 0; + + for (int line = 0; line <= number_of_lines && error == 0; line++) { + print_address(line); + print_hexadecimal_content(str, size, line); + print_string_content(str, size, line, &error); + my_putchar('\n'); + } + return 0; +} diff --git a/lib/my/my_showstr.c b/lib/my/my_showstr.c new file mode 100644 index 0000000..397987e --- /dev/null +++ b/lib/my/my_showstr.c @@ -0,0 +1,40 @@ +/* +** EPITECH PROJECT, 2023 +** my_showstr +** File description: +** Prints a string (str) but replaces +** non-printable characters with their hexadecimal number +*/ + +#include "my.h" + +static int is_printable(char c) +{ + if (c >= 32 && c <= 126) { + return 1; + } else { + return 0; + } +} + +static void check_zero_in_hexadecimal(char c) +{ + if (c < 16) + my_putchar('0'); +} + +int my_showstr(char const *str) +{ + int len = my_strlen(str); + + for (int i = 0; i < len; i++) { + if (is_printable(str[i]) == 1) { + my_putchar(str[i]); + } else { + my_putchar('\\'); + check_zero_in_hexadecimal(str[i]); + my_putnbr_base(str[i], "0123456789abcedf"); + } + } + return 0; +} diff --git a/lib/my/my_sort_int_array.c b/lib/my/my_sort_int_array.c new file mode 100644 index 0000000..ec93dbd --- /dev/null +++ b/lib/my/my_sort_int_array.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2023 +** my_sort_int_array +** File description: +** sorting array +*/ + +#include "my.h" + +static void sort_tab(int *array, int temp, int i, int j) +{ + if (array[j] < array[i]) { + temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} + +void my_sort_int_array(int *array, int size) +{ + int temp = 0; + + for (int i = 0; i <= size; i++) { + for (int j = i + 1; j < size; j++) { + sort_tab(array, temp, i, j); + } + } +} diff --git a/lib/my/my_sort_params.c b/lib/my/my_sort_params.c new file mode 100644 index 0000000..67b2e87 --- /dev/null +++ b/lib/my/my_sort_params.c @@ -0,0 +1,67 @@ +/* +** EPITECH PROJECT, 2023 +** day07 +** File description: +** Task06 +*/ + +#include "my.h" + +static int check_equal(int len_s1, int len_s2, int j, int littlest_char_index) +{ + if (len_s1 < len_s2) { + return j; + } else if (len_s1 > len_s2) { + return littlest_char_index; + } else { + return littlest_char_index; + } +} + +static int check_littlest(char **argv, int j, int littlest_char_index) +{ + int i = 0; + + while (i < my_strlen(argv[littlest_char_index]) + && i < my_strlen(argv[j])) { + if (argv[littlest_char_index][i] < argv[j][i]) { + return j; + } + if (argv[littlest_char_index][i] > argv[j][i]) { + return littlest_char_index; + } + i++; + } + return check_equal(my_strlen(argv[littlest_char_index]), + my_strlen(argv[j]), j , littlest_char_index); +} + +static void check_swap(char **argv, int i, int littlest_char_index) +{ + char *temp; + + if (i != littlest_char_index) { + temp = argv[i]; + argv[i] = argv[littlest_char_index]; + argv[littlest_char_index] = temp; + } +} + +void my_sort_params(int argc, char **argv) +{ + int i = 0; + int j = 0; + int littlest_char_index = 0; + + for (i = 0; i < argc; i++) { + littlest_char_index = i; + for (j = i; j < argc; j++) { + littlest_char_index = check_littlest(argv, j, littlest_char_index); + } + check_swap(argv, i, littlest_char_index); + } + for (int i = argc; i > 0; i--) { + my_putstr(argv[i - 1]); + my_putstr("\n"); + } +} diff --git a/lib/my/my_str_isalpha.c b/lib/my/my_str_isalpha.c new file mode 100644 index 0000000..382f318 --- /dev/null +++ b/lib/my/my_str_isalpha.c @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_isalpha +** File description: +** return 1 if str is alphabet character only and 0 if not +*/ + +#include "my.h" + +int my_str_isalpha(char const *str) +{ + if (my_strlen(str) == 0) + return 1; + for (int i = 0; i < my_strlen(str); i++) { + if (str[i] < 'A' || (str[i] > 'Z' && str[i] < 'a') + || str[i] > 'z') + return 0; + } + return 1; +} diff --git a/lib/my/my_str_islower.c b/lib/my/my_str_islower.c new file mode 100644 index 0000000..1298283 --- /dev/null +++ b/lib/my/my_str_islower.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_islower +** File description: +** return 1 if str is only lower cases +*/ + +#include "my.h" + +int my_str_islower(char const *str) +{ + int isalpha = 0; + + if (my_strlen(str) == 0) + return 1; + isalpha = my_str_isalpha(str); + for (int i = 0; i < my_strlen(str); i++) { + if (isalpha == 0) + return 0; + if (str[i] < 'a' || str[i] > 'z') + return 0; + } + return 1; +} diff --git a/lib/my/my_str_isnum.c b/lib/my/my_str_isnum.c new file mode 100644 index 0000000..179d8d9 --- /dev/null +++ b/lib/my/my_str_isnum.c @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_isnum +** File description: +** return 1 if str is only digit or zero if not +*/ + +#include "my.h" + +int my_str_isnum(char const *str) +{ + if (my_strlen(str) == 0) + return 1; + for (int i = 0; i < my_strlen(str); i++) { + if (str[i] < '0' || str[i] > '9') { + return 0; + } + } + return 1; +} diff --git a/lib/my/my_str_isprintable.c b/lib/my/my_str_isprintable.c new file mode 100644 index 0000000..a919f5d --- /dev/null +++ b/lib/my/my_str_isprintable.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_isprintable +** File description: +** check if the char in argument is printable +*/ + +#include "my.h" + +int my_str_isprintable(char const *str) +{ + if (my_strlen(str) == 0) + return 1; + for (int i = 0; i < my_strlen(str); i++) { + if (str[i] < 32 || str[i] == 127) + return 0; + } + return 1; +} diff --git a/lib/my/my_str_isupper.c b/lib/my/my_str_isupper.c new file mode 100644 index 0000000..c62f19b --- /dev/null +++ b/lib/my/my_str_isupper.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_islower +** File description: +** return 1 if str is only upper cases +*/ + +#include "my.h" + +int my_str_isupper(char const *str) +{ + int isalpha = 0; + + if (my_strlen(str) == 0) + return 1; + isalpha = my_str_isalpha(str); + for (int i = 0; i < my_strlen(str); i++) { + if (isalpha == 0) + return 0; + if (str[i] < 'A' || str[i] > 'Z') + return 0; + } + return 1; +} diff --git a/lib/my/my_str_nbr.c b/lib/my/my_str_nbr.c new file mode 100644 index 0000000..bc0873e --- /dev/null +++ b/lib/my/my_str_nbr.c @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr +** File description: +** Returns the strings convertion of a number (nb) +*/ + +#include "my.h" + +static char *set_str_nb(int nb, int int_nb, int signe) +{ + char nb_str[int_nb + signe]; + int j = 0; + int figure_temp = nb; + + if (signe == 1) + nb_str[int_nb] = '-'; + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j + signe] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr(int nb) +{ + int int_nb = 1; + int temp_nb = nb; + int signe = 0; + + if (nb == -2147483648) + return "-2147483648"; + if (nb < 0) { + signe = 1; + nb = -nb; + } + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb(nb, int_nb, signe); +} diff --git a/lib/my/my_str_nbr_base_long_long_int.c b/lib/my/my_str_nbr_base_long_long_int.c new file mode 100644 index 0000000..a8dc4fd --- /dev/null +++ b/lib/my/my_str_nbr_base_long_long_int.c @@ -0,0 +1,39 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_base_unsigned +** File description: +** Returns the strings convertion of +** an unsigned number (nbr) in specific base (base) +*/ + +#include "my.h" + +static char *put_str_nb(long long int nb, int len_nb, + char const *base, int base_len) +{ + char nb_str[len_nb]; + long long int figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +char *my_str_nbr_base_long_long_int(long long int nbr, char const *base) +{ + int len_nb = 1; + long long int temp_nb = nbr; + int base_len = 0; + + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + return put_str_nb(nbr, len_nb, base, base_len); +} diff --git a/lib/my/my_str_nbr_base_unsigned.c b/lib/my/my_str_nbr_base_unsigned.c new file mode 100644 index 0000000..6bc06c8 --- /dev/null +++ b/lib/my/my_str_nbr_base_unsigned.c @@ -0,0 +1,39 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_base_unsigned +** File description: +** Returns the strings convertion of +** an unsigned number (nbr) in specific base (base) +*/ + +#include "my.h" + +static char *put_str_nb(unsigned int nb, int len_nb, + char const *base, int base_len) +{ + char nb_str[len_nb]; + unsigned int figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +char *my_str_nbr_base_unsigned(unsigned int nbr, char const *base) +{ + int len_nb = 1; + unsigned int temp_nb = nbr; + int base_len = 0; + + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + return put_str_nb(nbr, len_nb, base, base_len); +} diff --git a/lib/my/my_str_nbr_base_unsigned_long.c b/lib/my/my_str_nbr_base_unsigned_long.c new file mode 100644 index 0000000..772ef45 --- /dev/null +++ b/lib/my/my_str_nbr_base_unsigned_long.c @@ -0,0 +1,39 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_base_unsigned +** File description: +** Returns the strings convertion of +** an unsigned number (nbr) in specific base (base) +*/ + +#include "my.h" + +static char *put_str_nb(unsigned long nb, int len_nb, + char const *base, int base_len) +{ + char nb_str[len_nb]; + unsigned long figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +char *my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base) +{ + int len_nb = 1; + unsigned long temp_nb = nbr; + int base_len = 0; + + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + return put_str_nb(nbr, len_nb, base, base_len); +} diff --git a/lib/my/my_str_nbr_base_unsigned_short.c b/lib/my/my_str_nbr_base_unsigned_short.c new file mode 100644 index 0000000..a71142b --- /dev/null +++ b/lib/my/my_str_nbr_base_unsigned_short.c @@ -0,0 +1,39 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_base_unsigned +** File description: +** Returns the strings convertion of +** an unsigned number (nbr) in specific base (base) +*/ + +#include "my.h" + +static char *put_str_nb(unsigned short nb, int len_nb, + char const *base, int base_len) +{ + char nb_str[len_nb]; + unsigned short figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +char *my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base) +{ + int len_nb = 1; + unsigned short temp_nb = nbr; + int base_len = 0; + + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + return put_str_nb(nbr, len_nb, base, base_len); +} diff --git a/lib/my/my_str_nbr_base_unsigned_short_short.c b/lib/my/my_str_nbr_base_unsigned_short_short.c new file mode 100644 index 0000000..7adbe6a --- /dev/null +++ b/lib/my/my_str_nbr_base_unsigned_short_short.c @@ -0,0 +1,39 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_base_unsigned +** File description: +** Returns the strings convertion of +** an unsigned number (nbr) in specific base (base) +*/ + +#include "my.h" + +static char *put_str_nb(unsigned char nb, int len_nb, + char const *base, int base_len) +{ + char nb_str[len_nb]; + unsigned char figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +char *my_str_nbr_base_unsigned_short_short(unsigned char nbr, char const *base) +{ + int len_nb = 1; + unsigned char temp_nb = nbr; + int base_len = 0; + + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + return put_str_nb(nbr, len_nb, base, base_len); +} diff --git a/lib/my/my_str_nbr_base_unsigned_size_t.c b/lib/my/my_str_nbr_base_unsigned_size_t.c new file mode 100644 index 0000000..b98b1b5 --- /dev/null +++ b/lib/my/my_str_nbr_base_unsigned_size_t.c @@ -0,0 +1,40 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_base_unsigned +** File description: +** Returns the strings convertion of +** an unsigned number (nbr) in specific base (base) +*/ + +#include "my.h" +#include + +static char *put_str_nb(size_t nb, int len_nb, + char const *base, int base_len) +{ + char nb_str[len_nb]; + size_t figure_temp = nb; + + for (int i = 0; i < len_nb; i++) { + nb_str[len_nb - i - 1] = base[figure_temp % base_len]; + figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; + } + nb_str[len_nb] = '\0'; + return my_strdup(nb_str); +} + +char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base) +{ + int len_nb = 1; + size_t temp_nb = nbr; + int base_len = 0; + + while (base[base_len] != '\0') { + base_len = base_len + 1; + } + while ((temp_nb / base_len) != 0) { + len_nb = len_nb + 1; + temp_nb = temp_nb / base_len; + } + return put_str_nb(nbr, len_nb, base, base_len); +} diff --git a/lib/my/my_str_nbr_long_long.c b/lib/my/my_str_nbr_long_long.c new file mode 100644 index 0000000..7ad0d10 --- /dev/null +++ b/lib/my/my_str_nbr_long_long.c @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_long_long +** File description: +** Returns the strings convertion of a long long number (nb) +*/ + +#include "my.h" + +static char *set_str_nb(long long int nb, int int_nb, int signe) +{ + char nb_str[int_nb + signe]; + int j = 0; + long long int figure_temp = nb; + + if (signe == 1) + nb_str[int_nb] = '-'; + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j + signe] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr_long_long(long long int nb) +{ + int int_nb = 1; + long long int temp_nb = nb; + int signe = 0; + + if (nb == -9223372036854775807) + return "-9223372036854775807"; + if (nb < 0) { + signe = 1; + nb = -nb; + } + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb(nb, int_nb, signe); +} diff --git a/lib/my/my_str_nbr_short.c b/lib/my/my_str_nbr_short.c new file mode 100644 index 0000000..cbf9afb --- /dev/null +++ b/lib/my/my_str_nbr_short.c @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_long_long +** File description: +** Returns the strings convertion of a long long number (nb) +*/ + +#include "my.h" + +static char *set_str_nb(short int nb, int int_nb, int signe) +{ + char nb_str[int_nb + signe]; + int j = 0; + short int figure_temp = nb; + + if (signe == 1) + nb_str[int_nb] = '-'; + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j + signe] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr_short(short int nb) +{ + int int_nb = 1; + short temp_nb = nb; + int signe = 0; + + if (nb == -32768) + return "-32768"; + if (nb < 0) { + signe = 1; + nb = -nb; + } + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb(nb, int_nb, signe); +} diff --git a/lib/my/my_str_nbr_short_short.c b/lib/my/my_str_nbr_short_short.c new file mode 100644 index 0000000..6ed415f --- /dev/null +++ b/lib/my/my_str_nbr_short_short.c @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr +** File description: +** Returns the strings convertion of a number (nb) +*/ + +#include +#include "my.h" + +static char *set_str_nb(signed char nb, int int_nb, int signe) +{ + char nb_str[int_nb + signe]; + int j = 0; + signed char figure_temp = nb; + + if (signe == 1) + nb_str[int_nb] = '-'; + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j + signe] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr_short_short(signed char nb) +{ + int int_nb = 1; + signed char temp_nb = nb; + int signe = 0; + + if (nb == -128) + return "-128"; + if (nb < 0) { + signe = 1; + nb = -nb; + } + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb(nb, int_nb, signe); +} diff --git a/lib/my/my_str_nbr_size_t.c b/lib/my/my_str_nbr_size_t.c new file mode 100644 index 0000000..6062ccc --- /dev/null +++ b/lib/my/my_str_nbr_size_t.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_size_t +** File description: +** Returns the strings convertion of a big unsigned number (nb) +*/ + +#include +#include "my.h" + +static char *set_str_nb(size_t nb, int int_nb) +{ + char nb_str[int_nb]; + int j = 0; + size_t figure_temp = nb; + + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr_size_t(size_t nb) +{ + int int_nb = 1; + size_t temp_nb = nb; + + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb(nb, int_nb); +} diff --git a/lib/my/my_str_nbr_unsigned.c b/lib/my/my_str_nbr_unsigned.c new file mode 100644 index 0000000..90186b4 --- /dev/null +++ b/lib/my/my_str_nbr_unsigned.c @@ -0,0 +1,34 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_unsigned +** File description: +** Returns the strings convertion of an unsigned number (nb) +*/ + +#include "my.h" + +static char *set_str_nb(int nb, int int_nb) +{ + char nb_str[int_nb]; + int j = 0; + unsigned int figure_temp = nb; + + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr_unsigned(unsigned int nb) +{ + int int_nb = 1; + unsigned int temp_nb = nb; + + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb(nb, int_nb); +} diff --git a/lib/my/my_str_nbr_unsigned_long.c b/lib/my/my_str_nbr_unsigned_long.c new file mode 100644 index 0000000..e8f80ab --- /dev/null +++ b/lib/my/my_str_nbr_unsigned_long.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2023 +** my_str_nbr_unsigned_long +** File description: +** Returns the strings convertion of a long unsigned number (nb) +*/ + +#include "my.h" + +static char *set_str_nb_unsigned_long(unsigned long int nb, + int int_nb) +{ + char nb_str[int_nb]; + int j = 0; + unsigned long int figure_temp = nb; + + for (j = 0; j < int_nb; j++) { + nb_str[j] = 48 + (figure_temp % 10); + figure_temp = (figure_temp - (figure_temp % 10)) / 10; + } + nb_str[j] = '\0'; + return my_strdup(my_revstr(nb_str)); +} + +char *my_str_nbr_unsigned_long(unsigned long int nb) +{ + int int_nb = 1; + unsigned long int temp_nb = nb; + + while ((temp_nb / 10) != 0) { + int_nb = int_nb + 1; + temp_nb = temp_nb / 10; + } + return set_str_nb_unsigned_long(nb, int_nb); +} diff --git a/lib/my/my_str_to_word_array.c b/lib/my/my_str_to_word_array.c new file mode 100644 index 0000000..bea4c3d --- /dev/null +++ b/lib/my/my_str_to_word_array.c @@ -0,0 +1,100 @@ +/* +** EPITECH PROJECT, 2023 +** str to word array +** File description: +** splits str into words in a array +*/ + +#include +#include "my.h" + +int my_char_isalpha(char c) +{ + if ((c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z')) + return 1; + return 0; +} + +int my_char_isnum(char c) +{ + if (c >= '0' && c <= '9') + return 1; + return 0; +} + +static void check_words_and_num(char const *str, int *sep, int *word) +{ + for (int i = 0; str[i] != '\0'; i++) { + if ((my_char_isalpha(str[i]) == 1 || my_char_isnum(str[i]) == 1) + && *sep == 1) { + *word = *word + 1; + *sep = 0; + } + if ((my_char_isalpha(str[i]) == 0 && my_char_isnum(str[i]) == 0)) + *sep = 1; + } + return; +} + +static void count_letter(char const *str, int which_lettre, int *nbr_lettre) +{ + for (int i = which_lettre; str[i] != '\0' + && (my_char_isalpha(str[i]) != 0 + || my_char_isnum(str[i]) != 0); i++) + *nbr_lettre = *nbr_lettre + 1; +} + +static void reset_nbr_letter(char const *str, + int *which_lettre, int *nbr_lettre) +{ + for (; str[*which_lettre] != '\0' + && my_char_isalpha(str[*which_lettre]) != 1 + && my_char_isnum(str[*which_lettre]) != 1; + *which_lettre = *which_lettre + 1); + *nbr_lettre = 0; +} + +static char *nbr_sep(char *str) +{ + int nbr_sep = 0; + + for (int i = 0; my_char_isalpha(str[i]) != 1 + && my_char_isnum(str[i]) != 1; i++) + nbr_sep++; + return &str[nbr_sep]; +} + +static char **initialise_value(char **string, char const *str, + int *sep, int *word) +{ + char **array_of_word; + + *string = nbr_sep(*string); + check_words_and_num(str, sep, word); + array_of_word = malloc(sizeof(char *) * ((*word) + 1)); + return array_of_word; +} + +char **my_str_to_word_array(char const *str) +{ + int word = 0; + int sep = 1; + int nbr_lettre = 0; + int which_lettre = 0; + char *string = my_strdup(str); + char **array_of_word = initialise_value(&string, str, &sep, &word); + + for (int elem = 0; elem < word; elem++) { + count_letter(string, which_lettre, &nbr_lettre); + array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); + for (int chara = 0; chara < nbr_lettre; chara++) { + array_of_word[elem][chara] = string[which_lettre]; + which_lettre++; + } + array_of_word[elem][nbr_lettre] = '\0'; + reset_nbr_letter(string, &which_lettre, &nbr_lettre); + } + array_of_word[word] = NULL; + return array_of_word; +} diff --git a/lib/my/my_strcapitalize.c b/lib/my/my_strcapitalize.c new file mode 100644 index 0000000..cfd625f --- /dev/null +++ b/lib/my/my_strcapitalize.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_strcapitalize +** File description: +** capitalize first letter of new word (preceding by a ' ' or '-') +*/ + +#include "my.h" + +char *my_strcapitalize(char *str) +{ + str = my_strlowcase(str); + for (int i = 1; i < my_strlen(str); i++) { + if (((str[i - 1] == '+' || str[i - 1] == '-' || str[i - 1] == ' ')) + && (str[i] >= 'a' && str[i] <= 'z')) { + str[i] = str[i] - 32; + } + } + if (str[0] >= 'a' && str[0] <= 'z') + str[0] = str[0] - 32; + return str; +} diff --git a/lib/my/my_strcat.c b/lib/my/my_strcat.c new file mode 100644 index 0000000..aca796a --- /dev/null +++ b/lib/my/my_strcat.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** my_strcat +** File description: +** Concatenates a string (dest) with another string (src) +** and returns the destination string (dest) +*/ + +char *my_strcat(char *dest, char const *src) +{ + int len_dest = 0; + int len_src = 0; + int i = 0; + + while (dest[len_dest] != '\0') { + len_dest = len_dest + 1; + } + while (src[len_src] != '\0') { + len_src = len_src + 1; + } + for (i = 0; i <= len_src; i++) { + dest[len_dest + i] = src[i]; + } + dest[len_dest + i + 1] = '\0'; + return dest; +} diff --git a/lib/my/my_strcmp.c b/lib/my/my_strcmp.c new file mode 100644 index 0000000..72b9bc0 --- /dev/null +++ b/lib/my/my_strcmp.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_strcmp.c +** File description: +** return 1, 0, -1 if s1 is bigger, equal, smaller +*/ + +#include "my.h" + +int my_strcmp(char const *s1, char const *s2) +{ + int i = 0; + + while ( s1[i] != '\0' && s2[i] != '\0') { + if (s1[i] < s2[i]) + return -1; + if (s1[i] > s2[i]) + return s1[i]-s2[i]; + i++; + } + return s1[i] - s2[i]; +} diff --git a/lib/my/my_strcpy.c b/lib/my/my_strcpy.c new file mode 100644 index 0000000..be259cc --- /dev/null +++ b/lib/my/my_strcpy.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2023 +** my_strcpy +** File description: +** Copy a string (src) and paste it on another string (dest) +*/ + +char *my_strcpy(char *dest, char const *src) +{ + int i = 0; + + while (src[i] != '\0') { + dest[i] = src[i]; + i = i + 1; + } + dest[i] = '\0'; + return dest; +} diff --git a/lib/my/my_strdup.c b/lib/my/my_strdup.c new file mode 100644 index 0000000..b3bea08 --- /dev/null +++ b/lib/my/my_strdup.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2023 +** my_strdup +** File description: +** Returns a duplication of a string (src) +*/ + +#include +#include "my.h" + +char *my_strdup(char const *src) +{ + char *result; + int len_src = my_strlen(src); + int i = 0; + + result = malloc(sizeof(char) * len_src); + for (i = 0; i < len_src; i++) { + result[i] = src[i]; + } + result[i] = '\0'; + return result; +} diff --git a/lib/my/my_strlen.c b/lib/my/my_strlen.c new file mode 100644 index 0000000..7536152 --- /dev/null +++ b/lib/my/my_strlen.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2023 +** my_strlen +** File description: +** Returns the lenght of a string (str) +*/ + +int my_strlen(char const *str) +{ + int len = 0; + + while (str[len] != '\0') { + len = len + 1; + } + return (len); +} diff --git a/lib/my/my_strlowcase.c b/lib/my/my_strlowcase.c new file mode 100644 index 0000000..fe3f963 --- /dev/null +++ b/lib/my/my_strlowcase.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_strlowcase.c +** File description: +** Replaces upcase by lowcase of a string (str) and returns them +*/ + +char *my_strlowcase(char *str) +{ + int len = 0; + + while (str[len] != '\0') { + len = len + 1; + } + for (int i = 0; i < len; i++) { + if (str[i] >= 65 && str[i] <= 90) { + str[i] = str[i] + 32; + } + } + return str; +} diff --git a/lib/my/my_strncat.c b/lib/my/my_strncat.c new file mode 100644 index 0000000..64910c8 --- /dev/null +++ b/lib/my/my_strncat.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_strncat +** File description: +** Concatenates a string (dest) with another string (src) +** with a defined size (n) and returns the destination string (dest) +*/ + +#include "my.h" + +char *my_strncat(char *dest, char const *src, int n) +{ + int len_dest = my_strlen(dest); + int len_src = n; + int i = 0; + + for (i = 0; i < len_src; i++) { + dest[len_dest + i] = src[i]; + } + dest[len_dest + i] = '\0'; + return dest; +} diff --git a/lib/my/my_strncmp.c b/lib/my/my_strncmp.c new file mode 100644 index 0000000..5006bee --- /dev/null +++ b/lib/my/my_strncmp.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_strncpm +** File description: +** return int but comparing only to n elements +*/ + +#include "my.h" + +int my_strncmp(char const *s1, char const *s2, int n) +{ + int i = 0; + + while (s1[i] != '\0' && s2[i] != '\0' && i < n) { + if (s1[i] < s2[i]) + return s1[i] - s2[i]; + if (s1[i] > s2[i]) + return s1[i] - s2[i]; + i++; + } + return s1[i]-s2[i]; +} diff --git a/lib/my/my_strncpy.c b/lib/my/my_strncpy.c new file mode 100644 index 0000000..a971c29 --- /dev/null +++ b/lib/my/my_strncpy.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2023 +** my_strncpy +** File description: +** Copy a string (src) and paste it on another string (dest) +** with a defined size (n) +*/ + +char *my_strncpy(char *dest, char const *src, int n) +{ + int len = 0; + + while (src[len] != '\0') { + len = len + 1; + } + for (int i = 0; i < n; i++) { + dest[i] = src[i]; + } + if (len < n) { + dest[len] = '\0'; + } + return dest; +} diff --git a/lib/my/my_strstr.c b/lib/my/my_strstr.c new file mode 100644 index 0000000..f819e92 --- /dev/null +++ b/lib/my/my_strstr.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** my_strstr +** File description: +** Search a string (to_find) on another (str) +** and returns the address of the first occurence +*/ + +#include +#include "my.h" + +char *my_strstr(char *str, char const *to_find) +{ + if (my_strlen(to_find) == 0) + return str; + for (int i = 0; str[i] != '\0'; i++) { + if (my_strncmp(&str[i], to_find, my_strlen(to_find)) == 0) { + return &str[i]; + } + } + return NULL; +} diff --git a/lib/my/my_strupcase.c b/lib/my/my_strupcase.c new file mode 100644 index 0000000..3e12609 --- /dev/null +++ b/lib/my/my_strupcase.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** my_strupcase +** File description: +** Replaces upcase by lowcase of a string (str) and returns them +*/ + +char *my_strupcase(char *str) +{ + int len = 0; + + while (str[len] != '\0') { + len = len + 1; + } + for (int i = 0; i < len; i++) { + if (str[i] >= 97 && str[i] <= 122) { + str[i] = str[i] - 32; + } + } + return str; +} diff --git a/lib/my/my_swap.c b/lib/my/my_swap.c new file mode 100644 index 0000000..f725096 --- /dev/null +++ b/lib/my/my_swap.c @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2023 +** my_swap +** File description: +** Swap the value of two integer (a and b) +*/ + +void my_swap(int *a, int *b) +{ + int c; + + c = *a; + *a = *b; + *b = c; +} diff --git a/src/create_map.c b/src/create_map.c new file mode 100644 index 0000000..a51870f --- /dev/null +++ b/src/create_map.c @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The create_map.c +*/ + +#include "../include/setting_up.h" + +static void restart_pattern(int *pattern_index, int pattern_len) +{ + if (*pattern_index == pattern_len) + *pattern_index = 0; +} + +int create_map(int map_size, char *pattern) +{ + char *map = malloc(sizeof(char *) * map_size * map_size + map_size); + int pattern_index = 0; + int row = 0; + int col = 0; + int line = 0; + + for (; row < map_size; row++) { + for (; col < map_size; col++) { + map[(row * map_size) + col + line] = pattern[pattern_index]; + pattern_index = pattern_index + 1; + restart_pattern(&pattern_index, my_strlen(pattern)); + } + map[(row * map_size) + col + line] = '\n'; + line = line + 1; + col = 0; + } + map[map_size * map_size + map_size] = '\0'; + place_square(map, map_size, map_size); + my_putstr(map); + return 0; +} diff --git a/src/edit_map.c b/src/edit_map.c new file mode 100644 index 0000000..781cbbc --- /dev/null +++ b/src/edit_map.c @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The edit_map.c +*/ + +#include "../include/setting_up.h" + +char **get_only_map(char const *map_str, int nb_rows, int nb_cols) +{ + char **map = malloc(sizeof(char *) * nb_rows); + int start = 0; + + for ( ; map_str[start] != '\n'; start++); + for (int row = 0; row < nb_rows; row++) { + map[row] = malloc((sizeof(char) * (nb_cols + 1))); + } + for (int row = 0; row < nb_rows; row++) { + for (int col = 0; col < nb_cols; col++) { + map[row][col] = map_str[start + 1 + (row * nb_cols) + row + col]; + } + map[row][nb_cols] = '\0'; + } + return map; +} + +int edit_map(char *file_path) +{ + int file = open(file_path, O_RDONLY); + struct stat file_infos; + int infos_status = stat(file_path, &file_infos); + char *map; + int rows = 0; + int columns = 0; + + if (search_file_error(file, infos_status, &file_infos) == 1) + return 84; + map = malloc(sizeof(char) * (file_infos.st_size + 1)); + map[read(file, map, file_infos.st_size)] = '\0'; + get_size(map, &file_infos, &rows, &columns); + if (search_map_str_error(map, rows, columns) == 1) + return 84; + //search_map_error(map, rows, columns); + //place_square(map, rows, columns); + my_putstr(map); + return 0; +} + +/* +int file = open(filepath, O_RDONLY); +struct stat infos; +int status = stat(filepath, &infos); +char *map = malloc(sizeof(char) * infos.st_size); + +if (file >= 0 && status >= 0 && infos.st_size) { +read(file, map, infos.st_size); +return map; +} else { +return NULL; +}*/ diff --git a/src/error_handling.c b/src/error_handling.c new file mode 100644 index 0000000..96a497c --- /dev/null +++ b/src/error_handling.c @@ -0,0 +1,112 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The error_handling.c +*/ + +#include "../include/setting_up.h" + +static int check_valid_pattern(char *pattern) +{ + if (my_strlen(pattern) == 0) + return 0; + for (int c = 0; c < my_strlen(pattern); c++) { + if (!(pattern[c] == '.' || pattern[c] == 'o')) { + return 0; + } + } + return 1; +} + +int search_error(int argc, char **argv) +{ + if (argc == 1) { + my_putstr_error("No file specified\n"); + return 1; + } + if (argc > 3) { + my_putstr_error("Too many open files\n"); + return 1; + } + if (argc == 2 && open(argv[1], O_RDONLY) < 1) { + my_putstr_error("Can't open file\n"); + return 1; + } + if (argc == 3 && + (my_getnbr(argv[1]) < 1 || check_valid_pattern(argv[2]) == 0)) { + my_putstr_error("Invalid argument(s)\n"); + return 1; + } + return 0; +} + +int search_file_error(int file, int infos_status, struct stat *file_infos) +{ + char *map_str; + + if (file < 0 || infos_status < 0) { + my_putstr_error("Cannot open or get file information\n"); + return 1; + } + if (S_ISREG(file_infos->st_mode) != 1) { + my_putstr_error("The file is not a regular file\n"); + return 1; + } + if (file_infos->st_size == 0) { + my_putstr_error("The file is empty\n"); + return 1; + } + return 0; +} + +static int count_lines(char const *map_str) +{ + int counter = 0; + + for (int c = 0; map_str[c] != '\0'; c++) { + if (map_str[c] == '\n') + counter = counter + 1; + } + return counter - 1; +} + +int search_map_str_error(char *map_str, int rows, int columns) +{ + if (rows == -1) { + my_putstr_error("Invalid rows number\n"); + return 1; + } + if (columns == 0) { + my_putstr_error("Rows are empty\n"); + return 1; + } + if (rows != count_lines(map_str)) { + my_putstr_error( + "Number of rows is different from the indicated number\n"); + return 1; + } + return 0; +} + +static void check_map_error(char c) +{ + if (c == '\0') { + my_putstr_error("Lines are not full\n"); + exit(84); + } + if (c != 'o' && c != '.') { + my_putstr_error("Invalid characters detected in map\n"); + exit(84); + } +} + +int search_map_error(char **map, int rows, int columns) +{ + for (int row = 0; row < rows; row++) { + for (int col = 0; col < columns; col++) { + check_map_error(map[row][col]); + } + } + return 0; +} diff --git a/src/get_size.c b/src/get_size.c new file mode 100644 index 0000000..672b8f8 --- /dev/null +++ b/src/get_size.c @@ -0,0 +1,72 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The get_colums.c +*/ + +#include "../include/setting_up.h" + +static int is_number(char c) +{ + if (c > 47 && c < 58) + return 1; + return 0; +} + +static void detect_number(int *enable, char const *buffer, int *index, int *c) +{ + if (*enable == 0 && is_number(buffer[*c]) == 1) { + *enable = 1; + *index = *c; + } + while (*enable == 1 && *c != 0 && + (buffer[*c - 1] == '+' || buffer[*c - 1] == '-')) { + *index = *index - 1; + *c = *c - 1; + } +} + +int get_rows(char const *map_str, struct stat *infos) +{ + char *temp = malloc(sizeof(char) * infos->st_size); + int enable = 0; + int index = 0; + int c = 0; + + for ( ; !(map_str[c] == '\n' || map_str[c] == '\0'); c++) { + detect_number(&enable, map_str, &index, &c); + if (enable == 1 || enable == 2) { + enable = 2; + temp[c - index] = map_str[c]; + } + } + temp[c] = '\0'; + if (my_getnbr(temp) < 1) + return -1; + else + return my_getnbr(temp); +} + +int get_columns(char const *map_str) +{ + int lines = 0; + int columns = 0; + + for (int c = 0; map_str[c] != '\0'; c++) { + if (map_str[c] == '\n' || map_str[c] == '\0') { + lines = lines + 1; + } + if (lines == 1) + columns = columns + 1; + if (lines == 2) + return columns - 1; + } + return 0; +} + +void get_size(char *map_str, struct stat *infos, int *rows, int *columns) +{ + *rows = get_rows(map_str, infos); + *columns = get_columns(map_str); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9391dfb --- /dev/null +++ b/src/main.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The main.c +*/ + +#include "../include/setting_up.h" + +int main(int argc, char **argv) +{ + if (search_error(argc, argv) == 1) + return 84; + if (argc == 2) + return edit_map(argv[1]); + if (argc == 3) + return create_map(my_getnbr(argv[1]), argv[2]); +} diff --git a/src/place_square.c b/src/place_square.c new file mode 100644 index 0000000..d0bfe7e --- /dev/null +++ b/src/place_square.c @@ -0,0 +1,90 @@ +/* +** EPITECH PROJECT, 2023 +** settingup +** File description: +** The place_square.c +*/ + +#include "../include/setting_up.h" + +static void check_barrier(int *result, char c) +{ + if (c == 'o') + *result = 0; +} + +int is_square_of_size(char *map, int row, int col, int square_size) +{ + int result = 1; + + for (int y = 0; y < square_size; y++) { + for (int x = 0; x < square_size; x++) { + check_barrier(&result, map[((y + row) * row) + x + y + col + + y]); + } + if (result == 0) { + break; + } + } + return result; +} + +int find_biggest_square(char *map, int const *map_size, int + best_square_size, int *position) +{ + int square_size = best_square_size; + + if (position[0] > map_size[0] || position[1] > map_size[1]) + return 0; + while (position[0] + square_size <= map_size[0] && + position[1] + square_size <= map_size[1] && + is_square_of_size(map, position[0], position[1], + square_size) == 1) { + square_size++; + } + return square_size - 1; +} + +void check_better_square_found(int *square_size, int const square_size_temp, + int **position, int const *temp_position) +{ + if (*square_size < square_size_temp) { + *square_size = square_size_temp; + *position[0] = temp_position[0]; + *position[1] = temp_position[1]; + } +} + +void find_biggest_place(char *map, int **position, + int *map_size, int *square_size) +{ + int square_size_temp = 0; + int temp_position[2] = {0, 0}; + + for ( ; temp_position[0] < map_size[0]; temp_position[0]++) { + for ( ; temp_position[1] < map_size[1]; temp_position[1]++) { + square_size_temp = find_biggest_square(map, + map_size, *square_size, temp_position); + check_better_square_found(square_size, square_size_temp, + position, temp_position); + } + temp_position[1] = 0; + } +} + +int place_square(char *map, int nb_rows, int nb_cols) +{ + int x = 0; + int y = 0; + int *position[2] = {&x, &y}; + int map_size[2] = {nb_rows, nb_cols}; + int square_size = 0; + + find_biggest_place(map, position, map_size, &square_size); + for (int row = 0; row < square_size; row++) { + for (int col = 0; col < square_size; col++) { + map[((row + y) * nb_cols) + col + row + x + y] = 'x'; + } + } + return 0; +} diff --git a/tests/test_my_printf.c b/tests/test_my_printf.c new file mode 100644 index 0000000..c4dd786 --- /dev/null +++ b/tests/test_my_printf.c @@ -0,0 +1,1169 @@ +/* +** EPITECH PROJECT, 2023 +** my_printf +** File description: +** The test file for the unit tetes +*/ + +#include "criterion/criterion.h" +#include "criterion/redirect.h" +#include "../lib/my/my.h" +#include "../lib/my/flags/myformats.h" + +void redirect_all_std(void) +{ + cr_redirect_stdout(); + cr_redirect_stderr(); +} + +Test(my_printf, my_show_formating) +{ + formating_t formating; + + my_show_formating(&formating); +} + +Test(my_printf, empty_format, .init = redirect_all_std) +{ + int len = my_printf(""); + cr_assert_stdout_eq_str(""); + cr_assert_eq(len, 0); +} + +Test(my_printf, simple_format, .init = redirect_all_std) +{ + int len = my_printf("hello world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, wrong_flag_1, .init = redirect_all_std) +{ + int len = my_printf("hello %y"); + cr_assert_stdout_eq_str("hello %y"); + cr_assert_eq(len, 8); +} + +Test(my_printf, wrong_flag_2, .init = redirect_all_std) +{ + int len = my_printf("hello %+"); + cr_assert_stdout_eq_str("hello %+"); + cr_assert_eq(len, 8); +} + +Test(my_printf, wrong_flag_3, .init = redirect_all_std) +{ + int len = my_printf("hello %+y"); + cr_assert_stdout_eq_str("hello %+y"); + cr_assert_eq(len, 9); +} + +Test(my_printf, one_flag_percent, .init = redirect_all_std) +{ + int len = my_printf("hello %%"); + cr_assert_stdout_eq_str("hello %"); + cr_assert_eq(len, 7); +} + +Test(my_printf, multiple_flag_percent_1, .init = redirect_all_std) +{ + int len = my_printf("hello %%%%%%"); + cr_assert_stdout_eq_str("hello %%%"); + cr_assert_eq(len, 9); +} + +Test(my_printf, multiple_flag_percent_2, .init = redirect_all_std) +{ + int len = my_printf("hello %%%%%"); + cr_assert_stdout_eq_str("hello %%%"); + cr_assert_eq(len, 9); +} + +Test(my_printf, one_flag_c, .init = redirect_all_std) +{ + int len = my_printf("hello %corld", 'w'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, multiple_flag_c, .init = redirect_all_std) +{ + int len = my_printf("hello %c%c%c%c%c", 'w', 'o', 'r', 'l', 'd'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_c_1, .init = redirect_all_std) +{ + int len = my_printf("hello %2corld", 'w'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_c_2, .init = redirect_all_std) +{ + int len = my_printf("hello %#2corld", 'w'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_c_3, .init = redirect_all_std) +{ + int len = my_printf("hello %02corld", 'w'); + cr_assert_stdout_eq_str("hello 0world"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_c_4, .init = redirect_all_std) +{ + int len = my_printf("hello %+10corld", 'w'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 20); +} + +Test(my_printf, format_flag_c_5, .init = redirect_all_std) +{ + int len = my_printf("hello %-10corld", 'w'); + cr_assert_stdout_eq_str("hello w orld"); + cr_assert_eq(len, 20); +} + +Test(my_printf, format_flag_c_6, .init = redirect_all_std) +{ + int len = my_printf("hello %#+2corld", 'w'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_c_7, .init = redirect_all_std) +{ + int len = my_printf("hello %#02corld", 'w'); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_c_8, .init = redirect_all_std) +{ + int len = my_printf("hello %-#20corld", 'w'); + cr_assert_stdout_eq_str("hello %-#20corld"); + cr_assert_eq(len, 16); +} + +Test(my_printf, format_flag_c_9, .init = redirect_all_std) +{ + int len = my_printf("hello %-+10corld", 'w'); + cr_assert_stdout_eq_str("hello w orld"); + cr_assert_eq(len, 20); +} + +Test(my_printf, one_flag_s, .init = redirect_all_std) +{ + int len = my_printf("hello %s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, multiple_flag_s, .init = redirect_all_std) +{ + int len = my_printf("%s%s%s", "hello", " ", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_s_1, .init = redirect_all_std) +{ + int len = my_printf("hello %2s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_s_2, .init = redirect_all_std) +{ + int len = my_printf("hello %#2s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_s_3, .init = redirect_all_std) +{ + int len = my_printf("hello %02s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_s_4, .init = redirect_all_std) +{ + int len = my_printf("hello %+10s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 16); +} + +Test(my_printf, format_flag_s_5, .init = redirect_all_std) +{ + int len = my_printf("hello %-10s", "world"); + cr_assert_stdout_eq_str("hello world "); + cr_assert_eq(len, 16); +} + +Test(my_printf, format_flag_s_6, .init = redirect_all_std) +{ + int len = my_printf("hello %#+2s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_s_7, .init = redirect_all_std) +{ + int len = my_printf("hello %#02s", "world"); + cr_assert_stdout_eq_str("hello world"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_s_8, .init = redirect_all_std) +{ + int len = my_printf("hello %-#20s", "world"); + cr_assert_stdout_eq_str("hello %-#20s"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_s_9, .init = redirect_all_std) +{ + int len = my_printf("hello %-+10s", "world"); + cr_assert_stdout_eq_str("hello world "); + cr_assert_eq(len, 16); +} + +Test(my_printf, one_flag_bigs, .init = redirect_all_std) +{ + char *test[] = {"hello", "world", NULL}; + int len = my_printf("%S", test); + cr_assert_stdout_eq_str("hello\nworld\n"); + cr_assert_eq(len, 12); +} + +Test(my_printf, one_flag_i, .init = redirect_all_std) +{ + int len = my_printf("We are in %i", 2023); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, one_flag_i_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %i", -2023); + cr_assert_stdout_eq_str("We are in -2023"); + cr_assert_eq(len, 15); +} + +Test(my_printf, multiple_flag_i, .init = redirect_all_std) +{ + int len = my_printf("We are in %i%i%i%i", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, multiple_flag_i_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %i%i%i%i", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in -2023"); + cr_assert_eq(len, 15); +} + +Test(my_printf, one_flag_d, .init = redirect_all_std) +{ + int len = my_printf("We are in %d", 2023); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, one_flag_d_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %d", -2023); + cr_assert_stdout_eq_str("We are in -2023"); + cr_assert_eq(len, 15); +} + +Test(my_printf, multiple_flag_d, .init = redirect_all_std) +{ + int len = my_printf("We are in %d%d%d%d", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, multiple_flag_d_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %d%d%d%d", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in -2023"); + cr_assert_eq(len, 15); +} + +Test(my_printf, format_flag_d_1, .init = redirect_all_std) +{ + int len = my_printf("We are in %4.5d", 2023); + cr_assert_stdout_eq_str("We are in 02023"); + cr_assert_eq(len, 15); +} + +Test(my_printf, format_flag_d_plus, .init = redirect_all_std) +{ + int len = my_printf("%-+10d", 2023); + cr_assert_stdout_eq_str("+2023 "); + cr_assert_eq(len, 10); +} + +Test(my_printf, format_flag_d_2, .init = redirect_all_std) +{ + int len = my_printf("We are in %.0d", 0); + cr_assert_stdout_eq_str("We are in "); + cr_assert_eq(len, 10); +} + +Test(my_printf, format_flag_d_3, .init = redirect_all_std) +{ + int len = my_printf("We are in %+7.4d", 2023); + cr_assert_stdout_eq_str("We are in +2023"); + cr_assert_eq(len, 17); +} + +Test(my_printf, format_flag_d_4, .init = redirect_all_std) +{ + int len = my_printf("We are in %-7.4d", 2023); + cr_assert_stdout_eq_str("We are in 2023 "); + cr_assert_eq(len, 17); +} + +Test(my_printf, format_flag_d_5, .init = redirect_all_std) +{ + int len = my_printf("We are in %#7.4d", 2023); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 17); +} + +Test(my_printf, format_flag_d_6, .init = redirect_all_std) +{ + int len = my_printf("We are in %07d", 2023); + cr_assert_stdout_eq_str("We are in 0002023"); + cr_assert_eq(len, 17); +} + +Test(my_printf, format_flag_d_7, .init = redirect_all_std) +{ + int len = my_printf("We are in %lld", 2023); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, format_flag_d_8, .init = redirect_all_std) +{ + int len = my_printf("We are in %+-10d", 2023); + cr_assert_stdout_eq_str("We are in +2023 "); + cr_assert_eq(len, 20); +} + +Test(my_printf, format_flag_d_9, .init = redirect_all_std) +{ + int len = my_printf("We are in %0+10d", 2023); + cr_assert_stdout_eq_str("We are in +000002023"); + cr_assert_eq(len, 20); +} + +Test(my_printf, specifier_flag_d_1, .init = redirect_all_std) +{ + int len = my_printf("We are in %ld", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_d_2, .init = redirect_all_std) +{ + int len = my_printf("We are in %lld", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_d_3, .init = redirect_all_std) +{ + int len = my_printf("We are in %hd", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_d_4, .init = redirect_all_std) +{ + int len = my_printf("We are in %hhd", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_d_5, .init = redirect_all_std) +{ + int len = my_printf("We are in %zd", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, format_flag_double_1, .init = redirect_all_std) +{ + int len = my_printf("%.5f", 10.123); + cr_assert_stdout_eq_str("10.12300"); + cr_assert_eq(len, 8); +} + +Test(my_printf, format_flag_double_plus, .init = redirect_all_std) +{ + int len = my_printf("%+12f", 10.123); + cr_assert_stdout_eq_str(" +10.123000"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_double_width, .init = redirect_all_std) +{ + int len = my_printf("%12f", 10.123); + cr_assert_stdout_eq_str(" 10.123000"); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_double_moins, .init = redirect_all_std) +{ + int len = my_printf("%-12f", 10.123); + cr_assert_stdout_eq_str("10.123000 "); + cr_assert_eq(len, 12); +} + +Test(my_printf, format_flag_double_esp, .init = redirect_all_std) +{ + int len = my_printf("% f", 10.123); + cr_assert_stdout_eq_str(" 10.123000"); + cr_assert_eq(len, 10); +} + +Test(my_printf, format_flag_double_esp_a, .init = redirect_all_std) +{ + int len = my_printf("% 5a", 10.123); + cr_assert_stdout_eq_str(" 0x1.43ef9db22d0e5p+3"); + cr_assert_eq(len, 21); +} + +Test(my_printf, format_flag_double_zero, .init = redirect_all_std) +{ + int len = my_printf("%010f", 10.123); + cr_assert_stdout_eq_str("010.123000"); + cr_assert_eq(len, 10); +} + +Test(my_printf, format_flag_double_hash, .init = redirect_all_std) +{ + int len = my_printf("%#10f", 10.123); + cr_assert_stdout_eq_str(" 10.123000"); + cr_assert_eq(len, 10); +} + +Test(my_printf, format_flag_double_do_next, .init = redirect_all_std) +{ + int len = my_printf("%#+10f", 10.123); + cr_assert_stdout_eq_str("+10.123000"); + cr_assert_eq(len, 10); +} + +Test(my_printf, one_flag_u, .init = redirect_all_std) +{ + int len = my_printf("We are in %u", 2023); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, one_flag_u_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %u", -2023); + cr_assert_stdout_eq_str("We are in 4294965273"); + cr_assert_eq(len, 20); +} + +Test(my_printf, multiple_flag_u, .init = redirect_all_std) +{ + int len = my_printf("We are in %u%u%u%u", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, multiple_flag_u_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %u%u%u%u", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 4294967294023"); + cr_assert_eq(len, 23); +} + +Test(my_printf, specifier_flag_u_1, .init = redirect_all_std) +{ + int len = my_printf("We are in %lu", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_u_2, .init = redirect_all_std) +{ + int len = my_printf("We are in %llu", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_u_3, .init = redirect_all_std) +{ + int len = my_printf("We are in %hu", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_u_4, .init = redirect_all_std) +{ + int len = my_printf("We are in %hhu", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, specifier_flag_u_5, .init = redirect_all_std) +{ + int len = my_printf("We are in %zu", 1); + cr_assert_stdout_eq_str("We are in 1"); + cr_assert_eq(len, 11); +} + +Test(my_printf, one_flag_b, .init = redirect_all_std) +{ + int len = my_printf("We are in %b", 2023); + cr_assert_stdout_eq_str("We are in 11111100111"); + cr_assert_eq(len, 21); +} + +Test(my_printf, one_flag_b_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %b", -2023); + cr_assert_stdout_eq_str("We are in 11111111111111111111100000011001"); + cr_assert_eq(len, 42); +} + +Test(my_printf, multiple_flag_b, .init = redirect_all_std) +{ + int len = my_printf("We are in %b%b%b%b", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 1001011"); + cr_assert_eq(len, 17); +} + +Test(my_printf, multiple_flag_b_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %b%b%b%b", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 1111111111111111111111111111111001011"); + cr_assert_eq(len, 47); +} + +Test(my_printf, format_flag_b_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %#b", 2023); + cr_assert_stdout_eq_str("We are in 0b11111100111"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_o, .init = redirect_all_std) +{ + int len = my_printf("We are in %o", 2023); + cr_assert_stdout_eq_str("We are in 3747"); + cr_assert_eq(len, 14); +} + +Test(my_printf, one_flag_o_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %o", -2023); + cr_assert_stdout_eq_str("We are in 37777774031"); + cr_assert_eq(len, 21); +} + +Test(my_printf, multiple_flag_o, .init = redirect_all_std) +{ + int len = my_printf("We are in %o%o%o%o", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, multiple_flag_o_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %o%o%o%o", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 37777777776023"); + cr_assert_eq(len, 24); +} + +Test(my_printf, format_flag_o, .init = redirect_all_std) +{ + int len = my_printf("We are in %#o", 2023); + cr_assert_stdout_eq_str("We are in 03747"); + cr_assert_eq(len, 15); +} + +Test(my_printf, one_flag_x, .init = redirect_all_std) +{ + int len = my_printf("We are in %x", 2023); + cr_assert_stdout_eq_str("We are in 7e7"); + cr_assert_eq(len, 13); +} + +Test(my_printf, one_flag_x_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %x", -2023); + cr_assert_stdout_eq_str("We are in fffff819"); + cr_assert_eq(len, 18); +} + +Test(my_printf, multiple_flag_x, .init = redirect_all_std) +{ + int len = my_printf("We are in %x%x%x%x", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, multiple_flag_x_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %x%x%x%x", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in fffffffe023"); + cr_assert_eq(len, 21); +} + +Test(my_printf, format_flag_x, .init = redirect_all_std) +{ + int len = my_printf("We are in %#x", 2023); + cr_assert_stdout_eq_str("We are in 0x7e7"); + cr_assert_eq(len, 15); +} + +Test(my_printf, one_flag_bigx, .init = redirect_all_std) +{ + int len = my_printf("We are in %X", 2023); + cr_assert_stdout_eq_str("We are in 7E7"); + cr_assert_eq(len, 13); +} + +Test(my_printf, one_flag_bigx_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %X", -2023); + cr_assert_stdout_eq_str("We are in FFFFF819"); + cr_assert_eq(len, 18); +} + +Test(my_printf, multiple_flag_bigx, .init = redirect_all_std) +{ + int len = my_printf("We are in %X%X%X%X", 2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, multiple_flag_bigx_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %X%X%X%X", -2, 0, 2, 3); + cr_assert_stdout_eq_str("We are in FFFFFFFE023"); + cr_assert_eq(len, 21); +} + +Test(my_printf, format_flag_bigx, .init = redirect_all_std) +{ + int len = my_printf("We are in %#X", 2023); + cr_assert_stdout_eq_str("We are in 0X7E7"); + cr_assert_eq(len, 15); +} + +Test(my_printf, one_flag_p, .init = redirect_all_std) +{ + int len; + cr_assert(my_printf("We are in %p", &len)); +} + +Test(my_printf, one_flag_e_zero, .init = redirect_all_std) +{ + int len = my_printf("We are in %e", 0.0); + cr_assert_stdout_eq_str("We are in 0.000000e+00"); + cr_assert_eq(len, 22); +} + +Test(my_printf, one_flag_e, .init = redirect_all_std) +{ + int len = my_printf("We are in %e", 20.23); + cr_assert_stdout_eq_str("We are in 2.023000e+01"); + cr_assert_eq(len, 22); +} + +Test(my_printf, one_flag_e_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %e", -20.23); + cr_assert_stdout_eq_str("We are in -2.023000e+01"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_e_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %e", 0.02023); + cr_assert_stdout_eq_str("We are in 2.023000e-02"); + cr_assert_eq(len, 22); +} + +Test(my_printf, one_flag_e_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %e", -0.02023); + cr_assert_stdout_eq_str("We are in -2.023000e-02"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_e_max, .init = redirect_all_std) +{ + int len = my_printf("We are in %e", 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023); + cr_assert_stdout_eq_str("We are in 2.023000e-100"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_bige_zero, .init = redirect_all_std) +{ + int len = my_printf("We are in %E", 0.0); + cr_assert_stdout_eq_str("We are in 0.000000E+00"); + cr_assert_eq(len, 22); +} + +Test(my_printf, one_flag_bige, .init = redirect_all_std) +{ + int len = my_printf("We are in %E", 20.23); + cr_assert_stdout_eq_str("We are in 2.023000E+01"); + cr_assert_eq(len, 22); +} + +Test(my_printf, one_flag_bige_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %E", -20.23); + cr_assert_stdout_eq_str("We are in -2.023000E+01"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_bige_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %E", 0.02023); + cr_assert_stdout_eq_str("We are in 2.023000E-02"); + cr_assert_eq(len, 22); +} + +Test(my_printf, one_flag_bige_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %E", -0.02023); + cr_assert_stdout_eq_str("We are in -2.023000E-02"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_bige_max, .init = redirect_all_std) +{ + int len = my_printf("We are in %E", 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023); + cr_assert_stdout_eq_str("We are in 2.023000E-100"); + cr_assert_eq(len, 23); +} + +Test(my_printf, one_flag_f, .init = redirect_all_std) +{ + int len = my_printf("We are in %f", 20.23); + cr_assert_stdout_eq_str("We are in 20.230000"); + cr_assert_eq(len, 19); +} + +Test(my_printf, one_flag_f_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %f", -20.23); + cr_assert_stdout_eq_str("We are in -20.230000"); + cr_assert_eq(len, 20); +} + +Test(my_printf, one_flag_f_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %f", 0.02023); + cr_assert_stdout_eq_str("We are in 0.020230"); + cr_assert_eq(len, 18); +} + +Test(my_printf, one_flag_f_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %F", -0.02023); + cr_assert_stdout_eq_str("We are in -0.020230"); + cr_assert_eq(len, 19); +} + +Test(my_printf, one_flag_bigf, .init = redirect_all_std) +{ + int len = my_printf("We are in %f", 20.23); + cr_assert_stdout_eq_str("We are in 20.230000"); + cr_assert_eq(len, 19); +} + +Test(my_printf, one_flag_bigf_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %F", -20.23); + cr_assert_stdout_eq_str("We are in -20.230000"); + cr_assert_eq(len, 20); +} + +Test(my_printf, one_flag_bigf_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %F", 0.02023); + cr_assert_stdout_eq_str("We are in 0.020230"); + cr_assert_eq(len, 18); +} + +Test(my_printf, one_flag_bigf_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %F", -0.02023); + cr_assert_stdout_eq_str("We are in -0.020230"); + cr_assert_eq(len, 19); +} + +Test(my_printf, one_flag_g_zero, .init = redirect_all_std) +{ + int len = my_printf("We are in %g", 0.0); + cr_assert_stdout_eq_str("We are in 0"); + cr_assert_eq(len, 11); +} + +Test(my_printf, one_flag_g, .init = redirect_all_std) +{ + int len = my_printf("We are in %g", 20.23); + cr_assert_stdout_eq_str("We are in 20.23"); + cr_assert_eq(len, 15); +} + +Test(my_printf, one_flag_g_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %g", -20.23); + cr_assert_stdout_eq_str("We are in -20.23"); + cr_assert_eq(len, 16); +} + +Test(my_printf, one_flag_g_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %g", 0.02023); + cr_assert_stdout_eq_str("We are in 0.02023"); + cr_assert_eq(len, 17); +} + +Test(my_printf, one_flag_g_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %g", -0.02023); + cr_assert_stdout_eq_str("We are in -0.02023"); + cr_assert_eq(len, 18); +} + +Test(my_printf, one_flag_g_max, .init = redirect_all_std) +{ + int len = my_printf("We are in %g", -0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023); + cr_assert_stdout_eq_str("We are in -2.023e-100"); + cr_assert_eq(len, 21); +} + +Test(my_printf, one_flag_bigg_zero, .init = redirect_all_std) +{ + int len = my_printf("We are in %G", 0.0); + cr_assert_stdout_eq_str("We are in 0"); + cr_assert_eq(len, 11); +} + +Test(my_printf, one_flag_bigg, .init = redirect_all_std) +{ + int len = my_printf("We are in %G", 20.23); + cr_assert_stdout_eq_str("We are in 20.23"); + cr_assert_eq(len, 15); +} + +Test(my_printf, one_flag_bigg_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %G", -20.23); + cr_assert_stdout_eq_str("We are in -20.23"); + cr_assert_eq(len, 16); +} + +Test(my_printf, one_flag_bigg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %G", 0.02023); + cr_assert_stdout_eq_str("We are in 0.02023"); + cr_assert_eq(len, 17); +} + +Test(my_printf, one_flag_bigg_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %G", -0.02023); + cr_assert_stdout_eq_str("We are in -0.02023"); + cr_assert_eq(len, 18); +} + +Test(my_printf, one_flag_bigg_max, .init = redirect_all_std) +{ + int len = my_printf("We are in %G", -0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023); + cr_assert_stdout_eq_str("We are in -2.023E-100"); + cr_assert_eq(len, 21); +} + +Test(my_printf, one_flag_a_zero, .init = redirect_all_std) +{ + int len = my_printf("We are in %a", 0.0); + cr_assert_stdout_eq_str("We are in 0x0p+0"); + cr_assert_eq(len, 16); +} + +Test(my_printf, one_flag_a, .init = redirect_all_std) +{ + int len = my_printf("We are in %a", 100.020); + cr_assert_stdout_eq_str("We are in 0x1.90147ae147ae1p+6"); + cr_assert_eq(len, 30); +} + +Test(my_printf, one_flag_a_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %a", -100.020); + cr_assert_stdout_eq_str("We are in -0x1.90147ae147ae1p+6"); + cr_assert_eq(len, 31); +} + +Test(my_printf, one_flag_a_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %a", 0.10020); + cr_assert_stdout_eq_str("We are in 0x1.9a6b50b0f27bbp-4"); + cr_assert_eq(len, 30); +} + +Test(my_printf, one_flag_a_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %a", -0.10020); + cr_assert_stdout_eq_str("We are in -0x1.9a6b50b0f27bbp-4"); + cr_assert_eq(len, 31); +} + +Test(my_printf, one_flag_a_max, .init = redirect_all_std) +{ + int len = my_printf("We are in %a", 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023); + cr_assert_stdout_eq_str("We are in 0x1.c519b147ec1ebp-332"); + cr_assert_eq(len, 32); +} + +Test(my_printf, one_flag_biga_zero, .init = redirect_all_std) +{ + int len = my_printf("We are in %A", 0.0); + cr_assert_stdout_eq_str("We are in 0X0P+0"); + cr_assert_eq(len, 16); +} + +Test(my_printf, one_flag_biga, .init = redirect_all_std) +{ + int len = my_printf("We are in %A", 100.020); + cr_assert_stdout_eq_str("We are in 0X1.90147AE147AE1P+6"); + cr_assert_eq(len, 30); +} + +Test(my_printf, one_flag_biga_neg, .init = redirect_all_std) +{ + int len = my_printf("We are in %A", -100.020); + cr_assert_stdout_eq_str("We are in -0X1.90147AE147AE1P+6"); + cr_assert_eq(len, 31); +} + +Test(my_printf, one_flag_biga_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %A", 0.10020); + cr_assert_stdout_eq_str("We are in 0X1.9A6B50B0F27BBP-4"); + cr_assert_eq(len, 30); +} + +Test(my_printf, one_flag_biga_neg_decimal, .init = redirect_all_std) +{ + int len = my_printf("We are in %A", -0.10020); + cr_assert_stdout_eq_str("We are in -0X1.9A6B50B0F27BBP-4"); + cr_assert_eq(len, 31); +} + +Test(my_printf, one_flag_biga_max, .init = redirect_all_std) +{ + int len = my_printf("We are in %A", 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023); + cr_assert_stdout_eq_str("We are in 0X1.C519B147EC1EBP-332"); + cr_assert_eq(len, 32); +} + +Test(my_printf, one_flag_n, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%n in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_l, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%ln in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_ll, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%lln in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_h, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%hn in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_hh, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%hhn in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_z, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%zn in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_j, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%jn in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_t, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%tn in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, specifier_flag_n_L, .init = redirect_all_std) +{ + int test = 0; + cr_assert_eq(my_printf("We are%Ln in 2023", &test), 14); + cr_assert_eq(test, 6); +} + +Test(my_printf, one_flag_m, .init = redirect_all_std) +{ + cr_assert(my_printf("We are in %m")); +} + +Test(my_printf, one_flag_bigd, .init = redirect_all_std) +{ + cr_assert(my_printf("We are in %D")); +} + +Test(my_printf, all_formats_1, .init = redirect_all_std) +{ + int len = my_printf("We are in %#1.2lld", 2023); + cr_assert_stdout_eq_str("We are in 2023"); + cr_assert_eq(len, 14); +} + +Test(my_printf, all_formats_2, .init = redirect_all_std) +{ + int len = my_printf("We are in %#+1.2ld", 2023); + cr_assert_stdout_eq_str("We are in +2023"); + cr_assert_eq(len, 15); +} + +Test(my_printf, all_formats_star_prc, .init = redirect_all_std) +{ + int len = my_printf("%.*d", 6, 10); + cr_assert_stdout_eq_str("000010"); + cr_assert_eq(len, 6); +} + +Test(my_printf, all_formats_star_width, .init = redirect_all_std) +{ + int len = my_printf("%*d", 6, 10); + cr_assert_stdout_eq_str(" 10"); + cr_assert_eq(len, 6); +} + +Test(my_printf, flag_a_prc_zero, .init = redirect_all_std) +{ + int len = my_printf("%.4a", 0.0); + cr_assert_stdout_eq_str("0x0.0000p+0"); + cr_assert_eq(len, 11); + +} + +Test(my_printf, flag_a_prc_x, .init = redirect_all_std) +{ + int len = my_printf("%.14a", 10.123); + cr_assert_stdout_eq_str("0x1.43ef9db22d0e50p+3"); + cr_assert_eq(len, 21); +} + +Test(my_printf, flag_biga_prc_zero, .init = redirect_all_std) +{ + int len = my_printf("%.4A", 0.0); + cr_assert_stdout_eq_str("0X0.0000P+0"); + cr_assert_eq(len, 11); +} + +Test(my_printf, flag_biga_prc_x, .init = redirect_all_std) +{ + int len = my_printf("%.14A", 10.123); + cr_assert_stdout_eq_str("0X1.43EF9DB22D0E50P+3"); + cr_assert_eq(len, 21); +} + +Test(my_printf, flag_a_round_1, .init = redirect_all_std) +{ + int len = my_printf("%.1a", 1.9999); + cr_assert_stdout_eq_str("0x2.0p+0"); + cr_assert_eq(len, 8); +} + +Test(my_printf, flag_biga_round_1, .init = redirect_all_std) +{ + int len = my_printf("%.1A", 1.9999); + cr_assert_stdout_eq_str("0X2.0P+0"); + cr_assert_eq(len, 8); +} + +Test(my_printf, flag_a_round_2, .init = redirect_all_std) +{ + int len = my_printf("%.1a", 1.49); + cr_assert_stdout_eq_str("0x1.8p+0"); + cr_assert_eq(len, 8); +} + +Test(my_printf, flag_biga_round_2, .init = redirect_all_std) +{ + int len = my_printf("%.1A", 1.49); + cr_assert_stdout_eq_str("0X1.8P+0"); + cr_assert_eq(len, 8); +} + +Test(my_printf, flag_a_round_3, .init = redirect_all_std) +{ + int len = my_printf("%.3a", 1.4999); + cr_assert_stdout_eq_str("0x1.800p+0"); + cr_assert_eq(len, 10); +} + +Test(my_printf, flag_biga_round_3, .init = redirect_all_std) +{ + int len = my_printf("%.3A", 1.4999); + cr_assert_stdout_eq_str("0X1.800P+0"); + cr_assert_eq(len, 10); +} + +Test(my_printf, flag_g_prc_zero, .init = redirect_all_std) +{ + int len = my_printf("%.4g", 1000000.123); + cr_assert_stdout_eq_str("1e+06"); + cr_assert_eq(len, 5); +} + +Test(my_printf, flag_bigg_prc_zero, .init = redirect_all_std) +{ + int len = my_printf("%.4G", 1000000.123); + cr_assert_stdout_eq_str("1E+06"); + cr_assert_eq(len, 5); +} diff --git a/tests/test_mylib.c b/tests/test_mylib.c new file mode 100644 index 0000000..2da9a1d --- /dev/null +++ b/tests/test_mylib.c @@ -0,0 +1,411 @@ +/* +** EPITECH PROJECT, 2023 +** test lib +** File description: +** test each function of lib +*/ + +#include "criterion/criterion.h" +#include "criterion/redirect.h" +#include "../include/my.h" + +Test(my_compute_factorial_rec, full_covr) +{ + cr_assert_eq(my_compute_factorial_rec(3), 6); + cr_assert_eq(my_compute_factorial_rec(15), 0); + +} + +Test(my_compute_power_rec, full_covr) +{ + cr_assert_eq(my_compute_power_rec(3, 2), 9); + cr_assert_eq(my_compute_power_rec(21, 0), 1); + cr_assert_eq(my_compute_power_rec(-4, -4), 0); +} + +Test(my_compute_square_root, full_covr) +{ + cr_assert_eq(my_compute_square_root(25), 5); + cr_assert_eq(my_compute_square_root(0), 0); + cr_assert_eq(my_compute_square_root(-4), 0); +} + +Test(my_concat_params, full_covr) +{ + int argc = 4; + char *argv[] = { "unit_tests","coucou", "รงa", "va"}; + + cr_assert(my_concat_params(argc, argv)); +} + +Test(my_convert_base, full_covr) +{ + cr_assert(my_convert_base("-10", "0123456789", "01")); +} // NE PASSE PAS CAR NE PRINT PAS LE DERNIER 0 + +Test(my_convert_base_unsigned, full_covr) +{ + cr_assert_str_eq(my_convert_base_unsigned("10", "0123456789", "01"), "1010"); + cr_assert_str_eq(my_convert_base_unsigned("10a10", "0123456789", "01"), "1010"); +} + +Test(my_find_prime_sup, full_covr) +{ + cr_assert_eq(my_find_prime_sup(9), 11); + cr_assert(my_find_prime_sup(0)); + cr_assert(my_find_prime_sup(4)); +} + +Test(my_getnbr_base, full_covr) +{ + cr_assert_eq(my_getnbr_base("10", "0123456789"), 10); + cr_assert(my_getnbr_base("a10", "0123456789")); +} + +Test(my_getnbr, full_covr) +{ + cr_assert_eq(my_getnbr("-10"), -10); + cr_assert_eq(my_getnbr("-2147483648"), -2147483648); + cr_assert_eq(my_getnbr("a54"), 0); +// cr_assert_eq(my_getnbr("00012345667"), 0); +} + +Test(my_isneg, full_covr) +{ + cr_assert_eq(my_isneg(-10), 0); + cr_assert_eq(my_isneg(10), 0); +} + +Test(my_is_prime, full_covr) +{ + cr_assert_eq(my_is_prime(0), 0); + cr_assert_eq(my_is_prime(7), 1); + cr_assert_eq(my_is_prime(9), 0); +} + +Test(my_params_to_array, full_covr) +{ + char *test[] = {"unittests", "coucou"}; + + cr_assert(my_params_to_array(0, test)); + //cr_assert(my_params_to_array(1, test)); + //cr_assert(my_params_to_array(2, argv)); +} + +Test(my_print_combn, full_covr) +{ + cr_assert_eq(my_print_combn(3), 0); +} + +Test(my_print_params, full_covr, .init = cr_redirect_stdout) +{ + char *argv[] = {"unit_tests", "coucou"}; + + my_print_params(2, argv); + cr_assert_stdout_eq_str("unit_tests\ncoucou\n"); +} + +Test(my_put_nbr, fullcovr, .init = cr_redirect_stdout) +{ + my_put_nbr(-2400934); + cr_assert_stdout_eq_str("-2400934"); +} + +Test(my_put_nbr, fullcovr_2, .init = cr_redirect_stdout) +{ + my_put_nbr(-2147483648); + cr_assert_stdout_eq_str("-2147483648"); +} + +Test(my_put_nbr_base, fullcovr, .init = cr_redirect_stdout) +{ + my_putnbr_base(-10, "0123456789"); + cr_assert_stdout_eq_str("-10"); +} + +Test(my_putstr_error, full_covr) +{ + cr_assert_eq(my_putstr_error("ok"), 0); +} + +Test(my_putstr_sized, full_covr) +{ + cr_assert_eq(my_putstr_sized("ok", 2), 0); +} + +Test(my_rev_params, full_covr, .init = cr_redirect_stdout) +{ + char *argv[] = {"unit_tests", "coucou"}; + + my_rev_params(2, argv); + cr_assert_stdout_eq_str("coucou\nunit_tests\n"); +} + +Test(my_rev_str, full_covr) +{ + char str[6] = "kayak\0"; + + cr_assert(my_revstr(str)); +} + +Test(my_show_param, full_covr, .init = cr_redirect_stdout) +{ + char str[6] = "kayak\0"; + struct info_param struct1; + struct info_param struct2; + struct info_param par[2]; + + struct1.length = my_strlen(str); + struct1.str = str; + struct1.copy = my_strdup(str); + struct1.word_array = my_str_to_word_array(str); + struct2.length = 0; + struct2.str = ""; + struct2.copy = ""; + struct2.word_array = my_str_to_word_array(""); + par[0] = struct1; + par[1] = struct2; + my_show_param_array(par); + cr_assert_stdout_eq_str("kayak\n5\nkayak\n"); +} + +Test(my_show_word_array, full_covr, .init = cr_redirect_stdout) +{ + char *test[] = {"unit_tests", "coucou"}; + + my_show_word_array(test); + cr_assert_stdout_eq_str("unit_tests\ncoucou\n"); +} + +Test(my_show_mem, full_covr) +{ + char str[8] = "hel\n\blo\0"; + + cr_assert_eq(my_showmem(str, my_strlen(str)), 0); +} + +Test(my_show_str, full_covr) +{ + char str[8] = "hel\nl\bo\0"; + + cr_assert_eq(my_showstr(str), 0); +} + +Test(my_sort_int_array, full_covr) +{ + int tab_int[5] = {0, 5, 12, 42}; + + my_sort_int_array(tab_int, 5); + cr_assert_eq(tab_int[0], 0); +} + +Test(my_sort_params, full_covr) +{ + char *argv1[] = {"unit_tests", "coucou", "wesh"}; + char *argv2[] = {"coucoulL", "coucou", "coucoul"}; + char *argv3[] = {"unit_tests", "unit_tests", "wesh"}; + + my_sort_params(3, argv1); + my_sort_params(3, argv2); + my_sort_params(3, argv3); + cr_assert_str_eq(argv1[2], "coucou"); + cr_assert_str_eq(argv2[0], "coucoulL"); + cr_assert_str_eq(argv3[2], "unit_tests"); +} //Sa trie mais ร  l'envers -> ร  fix + +Test(my_str_is_alpha, full_covr) +{ + cr_assert_eq(my_str_isalpha("Coucou"), 1); + cr_assert_eq(my_str_isalpha("124"), 0); + cr_assert_eq(my_str_isalpha(""), 1); +} + +Test(my_str_is_lower, full_covr) +{ + cr_assert_eq(my_str_islower("Coucou"), 0); + cr_assert_eq(my_str_islower("124"), 0); + cr_assert_eq(my_str_islower(""), 1); + cr_assert_eq(my_str_islower("oui"), 1); +} + +Test(my_str_is_num, full_covr) +{ + cr_assert_eq(my_str_isnum("Coucou"), 0); + cr_assert_eq(my_str_isnum("124"), 1); + cr_assert_eq(my_str_isnum(""), 1); +} + +Test(my_str_is_printable, full_covr) +{ + char str[2]; + + str[0] = 31; + str[1] = '\0'; + cr_assert_eq(my_str_isprintable("Coucou"), 1); + cr_assert_eq(my_str_isprintable(str), 0); + cr_assert_eq(my_str_isprintable(""), 1); +} + +Test(my_str_is_upper, full_covr) +{ + cr_assert_eq(my_str_isupper("Coucou"), 0); + cr_assert_eq(my_str_isupper("9"), 0); + cr_assert_eq(my_str_isupper("abc"), 0); + cr_assert_eq(my_str_isupper(""), 1); + cr_assert_eq(my_str_isupper("ABC"), 1); +} + +Test(my_str_nbr, full_covr) +{ + + cr_assert_str_eq(my_str_nbr(-12), "-12"); +} + +Test(my_str_nbr, full_covr_2, .init = cr_redirect_stdout) +{ + cr_assert_str_eq(my_str_nbr(-2147483648), "-2147483648"); +} + +Test(my_str_nbr_base_long_long_int, full_covr_2) +{ + cr_assert(my_str_nbr_base_long_long_int(-12, "0123456789")); +} + +Test(my_str_nbr_base_unsigned, full_covr) +{ + cr_assert(my_str_nbr_base_unsigned(12, "0123456789")); +} + +Test(my_str_nbr_long_long, full_covr) +{ + cr_assert(my_str_nbr_long_long(-12)); +} + +Test(my_str_nbr_long_long, full_covr_2) +{ + cr_assert_str_eq(my_str_nbr_long_long(-9223372036854775807), "-9223372036854775807"); +} + +Test(my_str_nbr_size_t, full_covr) +{ + cr_assert(my_str_nbr_size_t(12)); +} + +Test(my_str_nbr_unsigned, full_covr) +{ + cr_assert(my_str_nbr_unsigned(12)); +} + +Test(my_str_nbr_unsigned_long, full_covr) +{ + cr_assert(my_str_nbr_unsigned_long(12)); +} + +Test(my_str_to_word_array, full_covr, .init = cr_redirect_stdout) +{ + char *str = "----hello 9coucou.รงava/oui"; + char **test = my_str_to_word_array(str); + + my_show_word_array(test); + cr_assert_stdout_eq_str("hello\n9coucou\nava\noui\n"); +} + +Test(my_strcapitalize, full_covr) +{ + char str[6] = "c cac\0"; + + cr_assert_str_eq(my_strcapitalize(str), "C Cac"); +} + +Test(my_strcat, full_covr) +{ + char str[30] = "coucou"; + char src[5] = "caca\0"; + + cr_assert(my_strcat(str, src)); +} + +Test(my_strcmp, full_covr) +{ + char str[7] = "cbbcbb\0"; + char src[7] = "caacaa\0"; + + cr_assert_eq(my_strcmp(str, str), 0); + cr_assert_eq(my_strcmp(str, src), 1); + cr_assert_eq(my_strcmp(src, str), -1); +} + +Test(my_strcpy, full_covr) +{ + char str[7] = "coucou\0"; + char src[7]; + + my_strcpy(src, str); + cr_assert_eq(str[0], src[0]); +} + +Test(my_strncmp, full_covr) +{ + char str[7] = "cbbcbb\0"; + char src[7] = "caacaa\0"; + + cr_assert_eq(my_strncmp(str, str, 6), 0); + cr_assert_eq(my_strncmp(src, str, 6), -1); + cr_assert_eq(my_strncmp(str, src, 6), 1); + +} + +Test(my_strncat, full_covr) +{ + char str[30] = "coucou"; + char src[5] = "caca\0"; + + cr_assert(my_strncat(str, src, 2)); +} + +Test(my_strlowcase, full_covr) +{ + char str[30] = "couCou"; + + cr_assert(my_strlowcase(str)); +} + +Test(my_strncpy, full_covr) +{ + char str[7] = "coucou\0"; + char src[7]; + + cr_assert(my_strncpy(src, str, 10)); +} + +Test(my_strstr, full_covr) +{ + char str[7] = "cbbcbb\0"; + char src[4] = "cbb\0"; + + cr_assert(my_strstr(str, src)); + cr_assert(my_strstr(str, "")); + cr_assert_eq(my_strstr(str, "la"), 0); +} + +Test(my_strupcase, full_covr) +{ + char str[30] = "couCou"; + cr_assert(my_strupcase(str)); +} + +Test(my_swap, full_covr) +{ + int a = 5; + int b = 10; + + my_swap(&a, &b); + cr_assert_eq(b, 5); +} + +Test(my_array_of_array_len, full_covr) +{ + char *tab[5] = {"a", "b", "c", "d", NULL}; + + cr_assert_eq(my_array_of_array_len(tab), 4); +}