| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** my_printf | ||
| 4 | ** File description: | ||
| 5 | ** Prints a string (format) with possible flags and format | ||
| 6 | ** in stdout and returns the length the printed string | ||
| 7 | */ | ||
| 8 | /** | ||
| 9 | * @file my_printf.c | ||
| 10 | * @brief The file containing the my_printf function | ||
| 11 | * @author Nicolas TORO | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "./my_printf/myprintf.h" | ||
| 15 | |||
| 16 | 333 | static void find_format(user_t *user, | |
| 17 | flags_t *flgs, formating_t *formating) | ||
| 18 | { | ||
| 19 | 333 | int copy = user->i + 1; | |
| 20 | |||
| 21 | 333 | formating->nb_format = 0; | |
| 22 | 333 | format_first(user, flgs, formating, ©); | |
| 23 | 333 | copy = user->i + 1 + formating->nb_format; | |
| 24 | 333 | format_width(user, flgs, formating, ©); | |
| 25 | 333 | copy = user->i + 1 + formating->nb_format; | |
| 26 | 333 | format_precision(user, flgs, formating, ©); | |
| 27 | 333 | copy = user->i + 1 + formating->nb_format; | |
| 28 | 333 | format_numbers(user, flgs, formating, ©); | |
| 29 | 333 | copy = user->i + 1 + formating->nb_format; | |
| 30 | 333 | format_specifier(user, flgs, formating, ©); | |
| 31 | 333 | copy = user->i + 1 + formating->nb_format; | |
| 32 | 333 | formating->flag = user->str[copy]; | |
| 33 | 333 | } | |
| 34 | |||
| 35 | 6 | static void not_a_flag(formating_t *formating, user_t *user) | |
| 36 | { | ||
| 37 | 6 | write(formating->fd, "%", 1); | |
| 38 | 6 | user->total_len = user->total_len + 1; | |
| 39 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | if (my_strcmp(formating->final_format, "\0") != 0) |
| 40 | 4 | user->total_len = user->total_len + | |
| 41 | 4 | my_putstr_fd(formating->final_format, formating->fd); | |
| 42 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | if (formating->flag != '\0') { |
| 43 | 4 | write(formating->fd, &formating->flag, 1); | |
| 44 | 4 | user->total_len = user->total_len + 1; | |
| 45 | } | ||
| 46 | 6 | } | |
| 47 | |||
| 48 | 333 | static void find_flags(user_t *user, va_list list, | |
| 49 | formating_t *formating, int *index) | ||
| 50 | { | ||
| 51 | 333 | flags_t flgs = {.str = "dicspouxXeEfFgGaAbSD%m", .index_flag = 0}; | |
| 52 | int copi; | ||
| 53 | |||
| 54 | 333 | find_format(user, &flgs, formating); | |
| 55 | 333 | copi = user->i + my_strlen(formating->final_format) + 1; | |
| 56 | 333 | user->i = user->i + (my_strlen(formating->final_format)) + 1; | |
| 57 |
2/2✓ Branch 1 taken 2606 times.
✓ Branch 2 taken 6 times.
|
2612 | for (; flgs.index_flag < my_strlen(flgs.str); flgs.index_flag++) { |
| 58 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2597 times.
|
2606 | if (user->str[copi] == 'n') { |
| 59 | 9 | flag_n(list, formating, user->total_len); | |
| 60 | 327 | return; | |
| 61 | } | ||
| 62 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 2279 times.
|
2597 | if (user->str[copi] == flgs.str[flgs.index_flag]) { |
| 63 | 318 | *index = flgs.index_flag; | |
| 64 | 318 | return; | |
| 65 | } | ||
| 66 | } | ||
| 67 | 6 | not_a_flag(formating, user); | |
| 68 | 6 | *index = -1; | |
| 69 | } | ||
| 70 | |||
| 71 | 2188 | static void select_display(int fd, user_t *user, va_list *liste) | |
| 72 | { | ||
| 73 | 2188 | int index_flag = -1; | |
| 74 | 2188 | formating_t formating = {.fd = fd, .liste = liste}; | |
| 75 | |||
| 76 |
2/2✓ Branch 0 taken 333 times.
✓ Branch 1 taken 1855 times.
|
2188 | if (user->str[user->i] == '%') |
| 77 | 333 | find_flags(user, *liste, &formating, &index_flag); | |
| 78 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 1870 times.
|
2188 | if (index_flag != -1) |
| 79 | 318 | user->total_len += FLAGS[index_flag](*liste, &formating); | |
| 80 |
4/4✓ Branch 0 taken 2182 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2180 times.
✓ Branch 3 taken 2 times.
|
2188 | if (user->str[user->i] != '%' && user->str[user->i] != '\0' |
| 81 |
2/2✓ Branch 0 taken 1855 times.
✓ Branch 1 taken 325 times.
|
2180 | && (user->str[user->i] != formating.flag)) { |
| 82 | 1855 | write(fd, &user->str[user->i], 1); | |
| 83 | 1855 | (user->total_len)++; | |
| 84 | } | ||
| 85 | 2188 | } | |
| 86 | |||
| 87 | 322 | int my_printf(char const *format, ...) | |
| 88 | { | ||
| 89 | va_list liste; | ||
| 90 | 322 | user_t user = {.str = format, .total_len = 0}; | |
| 91 | |||
| 92 | 322 | va_start(liste, format); | |
| 93 |
2/2✓ Branch 1 taken 2188 times.
✓ Branch 2 taken 322 times.
|
2510 | for (user.i = 0; user.i < my_strlen(user.str); (user.i)++) |
| 94 | 2188 | select_display(1, &user, &liste); | |
| 95 | 322 | va_end(liste); | |
| 96 | 322 | return user.total_len; | |
| 97 | } | ||
| 98 |