Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_printf | ||
4 | ** File description: | ||
5 | ** Flag to print a %a (a double in hexadecimal) | ||
6 | */ | ||
7 | /** | ||
8 | * @file flag_a.c | ||
9 | * @brief The file containing the flag_a function | ||
10 | * @author Nicolas TORO | ||
11 | */ | ||
12 | |||
13 | #include "myprintf.h" | ||
14 | |||
15 | 11 | static char *my_get_exposant_neg(int *nbr, char *converted_nb, | |
16 | char *exposant_temp) | ||
17 | { | ||
18 | 11 | char *signe = malloc(sizeof(char) * 2); | |
19 | |||
20 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
|
11 | if (converted_nb[0] < '4' |
21 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
|
4 | && my_strcmp(converted_nb, "3ff0000000000000") <= 0) { |
22 | 3 | signe[0] = '\0'; | |
23 | 3 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", | |
24 | 3 | "0123456789")) - 0x400; | |
25 | } else { | ||
26 | 8 | signe[0] = '+'; | |
27 | 8 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", | |
28 | 8 | "0123456789")) - 0x400; | |
29 | } | ||
30 | 11 | signe[1] = '\0'; | |
31 | 11 | return signe; | |
32 | } | ||
33 | |||
34 | 2 | static char *my_get_exposant_pos(int *nbr, char *converted_nb, | |
35 | char *exposant_temp) | ||
36 | { | ||
37 | 2 | char *signe = malloc(sizeof(char) * 2); | |
38 | |||
39 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (converted_nb[0] < 'c' |
40 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | && my_strcmp(converted_nb, "bff0000000000000") <= 0) { |
41 | 1 | signe[0] = '\0'; | |
42 | 1 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", | |
43 | 1 | "0123456789")) - 0xC00; | |
44 | } else { | ||
45 | 1 | signe[0] = '+'; | |
46 | 1 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789abcdef", | |
47 | 1 | "0123456789")) - 0xC00; | |
48 | } | ||
49 | 2 | signe[1] = '\0'; | |
50 | 2 | return signe; | |
51 | } | ||
52 | |||
53 | 13 | static char *my_get_exposant(char *converted_nb, char *signe) | |
54 | { | ||
55 | 13 | char *exposant = malloc(sizeof(char) * 6); | |
56 | char exposant_temp[4]; | ||
57 | 13 | int nbr = 0; | |
58 | |||
59 | 13 | exposant[0] = 'p'; | |
60 | 13 | exposant_temp[0] = converted_nb[0]; | |
61 | 13 | exposant_temp[1] = converted_nb[1]; | |
62 | 13 | exposant_temp[2] = converted_nb[2]; | |
63 | 13 | exposant_temp[3] = '\0'; | |
64 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
|
13 | if (my_strcmp(signe, "-") != 0) { |
65 | 11 | my_strcat(exposant, my_get_exposant_neg(&nbr, converted_nb, | |
66 | exposant_temp)); | ||
67 | } else { | ||
68 | 2 | my_strcat(exposant, my_get_exposant_pos(&nbr, converted_nb, | |
69 | exposant_temp)); | ||
70 | } | ||
71 | 13 | my_strcat(exposant, my_str_nbr(nbr + 1)); | |
72 | 13 | my_strcat(exposant, "\0"); | |
73 | 13 | return exposant; | |
74 | } | ||
75 | |||
76 | 13 | static char *my_get_decimal_part(char *converted_nb, int *detected) | |
77 | { | ||
78 | 13 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
79 | 13 | int chara = 3; | |
80 | |||
81 |
2/2✓ Branch 1 taken 165 times.
✓ Branch 2 taken 13 times.
|
178 | for (; chara < my_strlen(converted_nb); chara++) { |
82 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
165 | if (converted_nb[chara] == '0' && *detected == -1) |
83 | 15 | *detected = chara - 3; | |
84 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 15 times.
|
165 | if (converted_nb[chara] != '0') |
85 | 150 | *detected = -1; | |
86 | 165 | decimal_part[chara - 3] = converted_nb[chara]; | |
87 | } | ||
88 | 13 | decimal_part[*detected] = '\0'; | |
89 | 13 | return decimal_part; | |
90 | } | ||
91 | |||
92 | 28 | static char *my_get_sign(char *converted_nb) | |
93 | { | ||
94 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
|
28 | if (converted_nb[0] > '7') |
95 | 4 | return "-"; | |
96 | else | ||
97 | 24 | return ""; | |
98 | } | ||
99 | |||
100 | 2 | static void precise_zero(char *float_nb, int precision) | |
101 | { | ||
102 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (precision < 1) { |
103 | 1 | my_strcat(float_nb, "0x0p+0"); | |
104 | } else { | ||
105 | 1 | my_strcat(float_nb, "0x0."); | |
106 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (int i = 0; i < precision; i++) |
107 | 4 | my_strcat(float_nb, "0"); | |
108 | 1 | my_strcat(float_nb, "p+0"); | |
109 | } | ||
110 | 2 | } | |
111 | |||
112 | 13 | static void precise_it_a(char *float_nb, formating_t *formating, | |
113 | char *decimal_part, int detected) | ||
114 | { | ||
115 | char temp[2]; | ||
116 | 13 | int i = 0; | |
117 | |||
118 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
|
13 | if (formating->id_nb == 0 |
119 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
12 | || (formating->id_prc == 0 && formating->id_nb == -1)) |
120 | 13 | return; | |
121 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
12 | if (formating->id_nb == -1 && detected != 0) { |
122 | 7 | my_strcat(float_nb, "."); | |
123 | 7 | my_strcat(float_nb, decimal_part); | |
124 | 7 | return; | |
125 | } | ||
126 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (formating->id_nb > 0) { |
127 | 5 | my_strcat(float_nb, "."); | |
128 |
4/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 1 times.
|
42 | for (; i < formating->id_nb && decimal_part[i] != '\0'; i++) { |
129 | 37 | temp[0] = decimal_part[i]; | |
130 | 37 | my_strcat(float_nb, temp); | |
131 | } | ||
132 | 5 | round_a(float_nb, formating->id_nb, decimal_part[i], i); | |
133 | 5 | return; | |
134 | } | ||
135 | } | ||
136 | |||
137 | 15 | static char *my_get_str_float_hexa(double nb, formating_t *formating) | |
138 | { | ||
139 | 15 | char *converted_nb = my_convert_base_size_t( | |
140 | 15 | my_str_nbr_size_t(*(size_t *)&nb), | |
141 | "0123456789", "0123456789abcdef"); | ||
142 | 15 | char *float_nb = malloc(sizeof(char) * (my_strlen(converted_nb) + 6)); | |
143 | 15 | int detected = -1; | |
144 | 15 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
145 | |||
146 | 15 | my_strcat(float_nb, my_get_sign(converted_nb)); | |
147 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 13 times.
|
15 | if (my_strcmp(converted_nb, "0") == 0) { |
148 | 2 | precise_zero(float_nb, formating->id_nb); | |
149 | 2 | return float_nb; | |
150 | } | ||
151 | 13 | my_strcat(float_nb, "0x1"); | |
152 | 13 | decimal_part = my_get_decimal_part(converted_nb, &detected); | |
153 | 13 | precise_it_a(float_nb, formating, decimal_part, detected); | |
154 | 13 | my_strcat(float_nb, my_get_exposant(converted_nb, | |
155 | my_get_sign(converted_nb))); | ||
156 | 13 | FREE(decimal_part); | |
157 | 13 | return float_nb; | |
158 | } | ||
159 | |||
160 | 15 | int flag_a(va_list list, formating_t *formating) | |
161 | { | ||
162 | 15 | double temp_double = va_arg(list, double); | |
163 | 15 | char *convert_base = my_get_str_float_hexa(temp_double, formating); | |
164 | |||
165 | 15 | format_it_double(convert_base, formating, temp_double); | |
166 | 15 | return my_putstr_fd_free(convert_base, formating->fd); | |
167 | } | ||
168 |