Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_printf | ||
4 | ** File description: | ||
5 | ** Prints a string (format) with possible flags and format | ||
6 | ** in stdout and returns the lenght the printed string | ||
7 | */ | ||
8 | |||
9 | #include <stdarg.h> | ||
10 | #include "my.h" | ||
11 | #include "./flags/myflags.h" | ||
12 | #include "./flags/myformats.h" | ||
13 | |||
14 | 213 | static void find_format(user_t *user, | |
15 | flags_t *flgs, formating_t *formating) | ||
16 | { | ||
17 | 213 | int copy = user->i + 1; | |
18 | |||
19 | 213 | formating->nb_format = 0; | |
20 | 213 | format_first(user, flgs, formating, ©); | |
21 | 213 | copy = user->i + 1 + formating->nb_format; | |
22 | 213 | format_width(user, flgs, formating, ©); | |
23 | 213 | copy = user->i + 1 + formating->nb_format; | |
24 | 213 | format_precision(user, flgs, formating, ©); | |
25 | 213 | copy = user->i + 1 + formating->nb_format; | |
26 | 213 | format_numbers(user, flgs, formating, ©); | |
27 | 213 | copy = user->i + 1 + formating->nb_format; | |
28 | 213 | format_specifier(user, flgs, formating, ©); | |
29 | 213 | copy = user->i + 1 + formating->nb_format; | |
30 | 213 | formating->flag = user->str[copy]; | |
31 | 213 | } | |
32 | |||
33 | 6 | static void not_a_flag(formating_t *formating, user_t *user) | |
34 | { | ||
35 | 6 | my_putcharf(formating->fd, '%'); | |
36 | 6 | (user->total_len) += 1; | |
37 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | if (my_strcmp(formating->final_format, "\0") != 0) { |
38 | 4 | my_putstrf(formating->fd, formating->final_format); | |
39 | 4 | (user->total_len) += my_strlen(formating->final_format); | |
40 | } | ||
41 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | if (formating->flag != '\0') { |
42 | 4 | my_putcharf(formating->fd, formating->flag); | |
43 | 4 | (user->total_len) += 1; | |
44 | } | ||
45 | 6 | } | |
46 | |||
47 | 213 | static void find_flags(user_t *user, va_list list, | |
48 | formating_t *formating, int *index) | ||
49 | { | ||
50 | 213 | flags_t flgs = {.str = "dicspouxXeEfFgGaAmbSD%", | |
51 | .index_flag = 0}; | ||
52 | int copi; | ||
53 | |||
54 | 213 | find_format(user, &flgs, formating); | |
55 | 213 | copi = user->i + my_strlen(formating->final_format) + 1; | |
56 | 213 | user->i = user->i + (my_strlen(formating->final_format)) + 1; | |
57 |
2/2✓ Branch 1 taken 1928 times.
✓ Branch 2 taken 6 times.
|
1934 | for (; flgs.index_flag < my_strlen(flgs.str); flgs.index_flag++) { |
58 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1919 times.
|
1928 | if (user->str[copi] == 'n') { |
59 | 9 | flag_n(list, formating, user->total_len); | |
60 | 207 | return; | |
61 | } | ||
62 |
2/2✓ Branch 0 taken 198 times.
✓ Branch 1 taken 1721 times.
|
1919 | if (user->str[copi] == flgs.str[flgs.index_flag]) { |
63 | 198 | *index = flgs.index_flag; | |
64 | 198 | return; | |
65 | } | ||
66 | } | ||
67 | 6 | not_a_flag(formating, user); | |
68 | 6 | *index = -1; | |
69 | } | ||
70 | |||
71 | 1558 | static void select_display(user_t *user, va_list *liste, int fd) | |
72 | { | ||
73 | 1558 | int index_flag = -1; | |
74 | 1558 | formating_t formating = {.liste = liste, .fd = fd}; | |
75 | |||
76 |
2/2✓ Branch 0 taken 213 times.
✓ Branch 1 taken 1345 times.
|
1558 | if (user->str[user->i] == '%') |
77 | 213 | find_flags(user, *liste, &formating, &index_flag); | |
78 |
2/2✓ Branch 0 taken 198 times.
✓ Branch 1 taken 1360 times.
|
1558 | if (index_flag != -1) |
79 | 198 | user->total_len += FLAGS[index_flag](*liste, &formating); | |
80 |
4/4✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1550 times.
✓ Branch 3 taken 2 times.
|
1558 | if (user->str[user->i] != '%' && user->str[user->i] != '\0' |
81 |
2/2✓ Branch 0 taken 1345 times.
✓ Branch 1 taken 205 times.
|
1550 | && (user->str[user->i] != formating.flag)) { |
82 | 1345 | my_putcharf(formating.fd, user->str[user->i]); | |
83 | 1345 | (user->total_len)++; | |
84 | } | ||
85 | 1558 | } | |
86 | |||
87 | 163 | int my_printf(int fd, char const *format, ...) | |
88 | { | ||
89 | va_list liste; | ||
90 | 163 | user_t user = {.str = format, .total_len = 0}; | |
91 | |||
92 | 163 | va_start(liste, format); | |
93 |
2/2✓ Branch 1 taken 1558 times.
✓ Branch 2 taken 163 times.
|
1721 | for (user.i = 0; user.i < my_strlen(user.str); (user.i)++) { |
94 | 1558 | select_display(&user, &liste, fd); | |
95 | } | ||
96 | 163 | va_end(liste); | |
97 | 163 | return user.total_len; | |
98 | } | ||
99 |