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 | #include <stdarg.h> | ||
9 | #include <stdlib.h> | ||
10 | #include "my.h" | ||
11 | #include "myformats.h" | ||
12 | |||
13 | 4 | static char *my_get_exposant_neg(int *nbr, char *converted_nb, | |
14 | char *exposant_temp) | ||
15 | { | ||
16 | 4 | char *signe = malloc(sizeof(char) * 2); | |
17 | |||
18 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (converted_nb[0] < '4' |
19 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | && my_strcmp(converted_nb, "3FF0000000000000") <= 0) { |
20 | 2 | signe[0] = '\0'; | |
21 | 2 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", | |
22 | 2 | "0123456789")) - 0x400; | |
23 | } else { | ||
24 | 2 | signe[0] = '+'; | |
25 | 2 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", | |
26 | 2 | "0123456789")) - 0x400; | |
27 | } | ||
28 | 4 | signe[1] = '\0'; | |
29 | 4 | return my_strdup(signe); | |
30 | } | ||
31 | |||
32 | 2 | static char *my_get_exposant_pos(int *nbr, char *converted_nb, | |
33 | char *exposant_temp) | ||
34 | { | ||
35 | 2 | char *signe = malloc(sizeof(char) * 2); | |
36 | |||
37 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (converted_nb[0] < 'C' |
38 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | && my_strcmp(converted_nb, "BFF0000000000000") <= 0) { |
39 | 1 | signe[0] = '\0'; | |
40 | 1 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", | |
41 | 1 | "0123456789")) - 0xC00; | |
42 | } else { | ||
43 | 1 | signe[0] = '+'; | |
44 | 1 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", | |
45 | 1 | "0123456789")) - 0xC00; | |
46 | } | ||
47 | 2 | signe[1] = '\0'; | |
48 | 2 | return my_strdup(signe); | |
49 | } | ||
50 | |||
51 | 6 | static char *my_get_exposant(char *converted_nb, char *signe) | |
52 | { | ||
53 | 6 | char *exposant = malloc(sizeof(char) * 6); | |
54 | char exposant_temp[4]; | ||
55 | 6 | int nbr = 0; | |
56 | |||
57 | 6 | exposant[0] = 'P'; | |
58 | 6 | exposant_temp[0] = converted_nb[0]; | |
59 | 6 | exposant_temp[1] = converted_nb[1]; | |
60 | 6 | exposant_temp[2] = converted_nb[2]; | |
61 | 6 | exposant_temp[3] = '\0'; | |
62 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | if (my_strcmp(signe, "-") != 0) { |
63 | 4 | my_strcat(exposant, my_get_exposant_neg(&nbr, converted_nb, | |
64 | exposant_temp)); | ||
65 | } else { | ||
66 | 2 | my_strcat(exposant, my_get_exposant_pos(&nbr, converted_nb, | |
67 | exposant_temp)); | ||
68 | } | ||
69 | 6 | my_strcat(exposant, my_str_nbr(nbr + 1)); | |
70 | 6 | my_strcat(exposant, "\0"); | |
71 | 6 | return exposant; | |
72 | } | ||
73 | |||
74 | 6 | static char *my_get_decimal_part(char *converted_nb, int *detected) | |
75 | { | ||
76 | 6 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
77 | 6 | int chara = 3; | |
78 | |||
79 |
2/2✓ Branch 1 taken 78 times.
✓ Branch 2 taken 6 times.
|
84 | for (; chara < my_strlen(converted_nb); chara++) { |
80 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
78 | if (converted_nb[chara] == '0' && *detected == -1) |
81 | 7 | *detected = chara - 3; | |
82 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 7 times.
|
78 | if (converted_nb[chara] != '0') |
83 | 71 | *detected = -1; | |
84 | 78 | decimal_part[chara - 3] = converted_nb[chara]; | |
85 | } | ||
86 | 6 | decimal_part[*detected] = '\0'; | |
87 | 6 | return decimal_part; | |
88 | } | ||
89 | |||
90 | 14 | static char *my_get_sign(char *converted_nb) | |
91 | { | ||
92 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
|
14 | if (converted_nb[0] > '7') |
93 | 4 | return "-"; | |
94 | else | ||
95 | 10 | return ""; | |
96 | } | ||
97 | |||
98 | 2 | static void precise_zero(char *float_nb, int precision) | |
99 | { | ||
100 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (precision < 1) { |
101 | 1 | my_strcat(float_nb, "0X0P+0"); | |
102 | } else { | ||
103 | 1 | my_strcat(float_nb, "0X0."); | |
104 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (int i = 0; i < precision; i++) |
105 | 4 | my_strcat(float_nb, "0"); | |
106 | 1 | my_strcat(float_nb, "P+0"); | |
107 | } | ||
108 | 2 | } | |
109 | |||
110 | 1 | static void my_round(char *float_nb, int precision, | |
111 | char last_char, int i) | ||
112 | { | ||
113 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (last_char == '\0') { |
114 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (; i < precision; i++) |
115 | 1 | my_strcat(float_nb, "0"); | |
116 | 1 | return; | |
117 | } | ||
118 | ✗ | if (float_nb[3 + i] == '.') { | |
119 | ✗ | float_nb[2] = float_nb[2] + 1; | |
120 | ✗ | return; | |
121 | } | ||
122 | ✗ | if (last_char > '7' && float_nb[3 + i] == '9') { | |
123 | ✗ | float_nb[3 + i] = 'A'; | |
124 | ✗ | return; | |
125 | } | ||
126 | ✗ | if (last_char > '7' && float_nb[3 + i] == 'F') | |
127 | ✗ | float_nb[3 + i] = '0'; | |
128 | ✗ | if (last_char > '7' && float_nb[3 + i] != '0') | |
129 | ✗ | float_nb[3 + i] = float_nb[3 + i] + 1; | |
130 | ✗ | if (float_nb[3 + i] == '0') | |
131 | ✗ | my_round(float_nb, precision, last_char, i - 1); | |
132 | } | ||
133 | |||
134 | 6 | static void precise_it_biga(char *float_nb, formating_t *formating, | |
135 | char *decimal_part, int detected) | ||
136 | { | ||
137 | char temp[2]; | ||
138 | 6 | int i = 0; | |
139 | |||
140 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (formating->id_nb == 0 |
141 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
6 | || (formating->id_prc == 0 && formating->id_nb == -1)) |
142 | 6 | return; | |
143 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
6 | if (formating->id_nb == -1 && detected != 0) { |
144 | 5 | my_strcat(float_nb, "."); | |
145 | 5 | my_strcat(float_nb, decimal_part); | |
146 | 5 | return; | |
147 | } | ||
148 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (formating->id_nb > 0) { |
149 | 1 | my_strcat(float_nb, "."); | |
150 |
3/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1 times.
|
14 | for (; i < formating->id_nb && decimal_part[i] != '\0'; i++) { |
151 | 13 | temp[0] = decimal_part[i]; | |
152 | 13 | my_strcat(float_nb, temp); | |
153 | } | ||
154 | 1 | my_round(float_nb, formating->id_nb, decimal_part[i], i); | |
155 | 1 | return; | |
156 | } | ||
157 | } | ||
158 | |||
159 | 8 | static char *my_get_str_float_hexa(double nb, formating_t *formating) | |
160 | { | ||
161 | 8 | char *converted_nb = my_convert_base_size_t( | |
162 | 8 | my_str_nbr_size_t(*(size_t *)&nb), | |
163 | "0123456789", "0123456789ABCDEF"); | ||
164 | 8 | char *float_nb = malloc(sizeof(char) * (my_strlen(converted_nb) + 6)); | |
165 | 8 | int detected = -1; | |
166 | 8 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
167 | |||
168 | 8 | my_strcat(float_nb, my_get_sign(converted_nb)); | |
169 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
|
8 | if (my_strcmp(converted_nb, "0") == 0) { |
170 | 2 | precise_zero(float_nb, formating->id_nb); | |
171 | 2 | return float_nb; | |
172 | } | ||
173 | 6 | my_strcat(float_nb, "0X1"); | |
174 | 6 | decimal_part = my_get_decimal_part(converted_nb, &detected); | |
175 | 6 | precise_it_biga(float_nb, formating, decimal_part, detected); | |
176 | 6 | my_strcat(float_nb, my_get_exposant(converted_nb, | |
177 | my_get_sign(converted_nb))); | ||
178 | 6 | free(decimal_part); | |
179 | 6 | return float_nb; | |
180 | } | ||
181 | |||
182 | 8 | int flag_biga(va_list list, formating_t *formating) | |
183 | { | ||
184 | 8 | double temp_double = va_arg(list, double); | |
185 | 8 | char *convert_base = my_get_str_float_hexa(temp_double, formating); | |
186 | |||
187 | 8 | format_it_double(convert_base, formating, temp_double); | |
188 | 8 | my_putstrf(formating->fd, convert_base); | |
189 | 8 | return my_strlen(convert_base); | |
190 | } | ||
191 |