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_biga.c | ||
9 | * @brief The file containing the flag_biga function | ||
10 | * @author Nicolas TORO | ||
11 | */ | ||
12 | |||
13 | #include "myprintf.h" | ||
14 | |||
15 | 10 | static char *my_get_exposant_neg(int *nbr, char *converted_nb, | |
16 | char *exposant_temp) | ||
17 | { | ||
18 | 10 | char *signe = malloc(sizeof(char) * 2); | |
19 | |||
20 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | 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 | 7 | signe[0] = '+'; | |
27 | 7 | *nbr = my_getnbr(my_convert_base(exposant_temp, "0123456789ABCDEF", | |
28 | 7 | "0123456789")) - 0x400; | |
29 | } | ||
30 | 10 | signe[1] = '\0'; | |
31 | 10 | 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 | 12 | static char *my_get_exposant(char *converted_nb, char *signe) | |
54 | { | ||
55 | 12 | char *exposant = malloc(sizeof(char) * 6); | |
56 | char exposant_temp[4]; | ||
57 | 12 | int nbr = 0; | |
58 | |||
59 | 12 | exposant[0] = 'P'; | |
60 | 12 | exposant_temp[0] = converted_nb[0]; | |
61 | 12 | exposant_temp[1] = converted_nb[1]; | |
62 | 12 | exposant_temp[2] = converted_nb[2]; | |
63 | 12 | exposant_temp[3] = '\0'; | |
64 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
|
12 | if (my_strcmp(signe, "-") != 0) { |
65 | 10 | 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 | 12 | my_strcat(exposant, my_str_nbr(nbr + 1)); | |
72 | 12 | my_strcat(exposant, "\0"); | |
73 | 12 | return exposant; | |
74 | } | ||
75 | |||
76 | 12 | static char *my_get_decimal_part(char *converted_nb, int *detected) | |
77 | { | ||
78 | 12 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
79 | 12 | int chara = 3; | |
80 | |||
81 |
2/2✓ Branch 1 taken 152 times.
✓ Branch 2 taken 12 times.
|
164 | for (; chara < my_strlen(converted_nb); chara++) { |
82 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
152 | if (converted_nb[chara] == '0' && *detected == -1) |
83 | 14 | *detected = chara - 3; | |
84 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 14 times.
|
152 | if (converted_nb[chara] != '0') |
85 | 138 | *detected = -1; | |
86 | 152 | decimal_part[chara - 3] = converted_nb[chara]; | |
87 | } | ||
88 | 12 | decimal_part[*detected] = '\0'; | |
89 | 12 | return decimal_part; | |
90 | } | ||
91 | |||
92 | 26 | static char *my_get_sign(char *converted_nb) | |
93 | { | ||
94 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 22 times.
|
26 | if (converted_nb[0] > '7') |
95 | 4 | return "-"; | |
96 | else | ||
97 | 22 | 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 | 12 | static void precise_it_biga(char *float_nb, formating_t *formating, | |
113 | char *decimal_part, int detected) | ||
114 | { | ||
115 | char temp[2]; | ||
116 | 12 | int i = 0; | |
117 | |||
118 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
|
12 | if (formating->id_nb == 0 |
119 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
11 | || (formating->id_prc == 0 && formating->id_nb == -1)) |
120 | 12 | return; | |
121 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
11 | if (formating->id_nb == -1 && detected != 0) { |
122 | 6 | my_strcat(float_nb, "."); | |
123 | 6 | my_strcat(float_nb, decimal_part); | |
124 | 6 | 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_biga(float_nb, formating->id_nb, decimal_part[i], i); | |
133 | 5 | return; | |
134 | } | ||
135 | } | ||
136 | |||
137 | 14 | static char *my_get_str_float_hexa(double nb, formating_t *formating) | |
138 | { | ||
139 | 14 | char *converted_nb = my_convert_base_size_t( | |
140 | 14 | my_str_nbr_size_t(*(size_t *)&nb), | |
141 | "0123456789", "0123456789ABCDEF"); | ||
142 | 14 | char *float_nb = malloc(sizeof(char) * (my_strlen(converted_nb) + 6)); | |
143 | 14 | int detected = -1; | |
144 | 14 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
145 | |||
146 | 14 | my_strcat(float_nb, my_get_sign(converted_nb)); | |
147 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
|
14 | if (my_strcmp(converted_nb, "0") == 0) { |
148 | 2 | precise_zero(float_nb, formating->id_nb); | |
149 | 2 | return float_nb; | |
150 | } | ||
151 | 12 | my_strcat(float_nb, "0X1"); | |
152 | 12 | decimal_part = my_get_decimal_part(converted_nb, &detected); | |
153 | 12 | precise_it_biga(float_nb, formating, decimal_part, detected); | |
154 | 12 | my_strcat(float_nb, my_get_exposant(converted_nb, | |
155 | my_get_sign(converted_nb))); | ||
156 | 12 | FREE(decimal_part); | |
157 | 12 | return float_nb; | |
158 | } | ||
159 | |||
160 | 14 | int flag_biga(va_list list, formating_t *formating) | |
161 | { | ||
162 | 14 | double temp_double = va_arg(list, double); | |
163 | 14 | char *convert_base = my_get_str_float_hexa(temp_double, formating); | |
164 | |||
165 | 14 | format_it_double(convert_base, formating, temp_double); | |
166 | 14 | return my_putstr_fd_free(convert_base, formating->fd); | |
167 | } | ||
168 |