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