| 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 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1 times.
|
152 | if (converted_nb[chara] == '0' && *detected == -1) |
| 83 | 13 | *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 | 7 | static void my_round(char *float_nb, int precision, | |
| 113 | char last_char, int i) | ||
| 114 | { | ||
| 115 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (last_char == '\0') { |
| 116 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (; i < precision; i++) |
| 117 | 1 | my_strcat(float_nb, "0"); | |
| 118 | 1 | return; | |
| 119 | } | ||
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (float_nb[3 + i] == '.') { |
| 121 | ✗ | float_nb[2] = float_nb[2] + 1; | |
| 122 | ✗ | return; | |
| 123 | } | ||
| 124 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
6 | if (last_char > '7' && float_nb[3 + i] == '9') { |
| 125 | ✗ | float_nb[3 + i] = 'A'; | |
| 126 | ✗ | return; | |
| 127 | } | ||
| 128 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
6 | if (last_char > '7' && float_nb[3 + i] == 'F') |
| 129 | ✗ | float_nb[3 + i] = '0'; | |
| 130 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
6 | if (last_char > '7' && float_nb[3 + i] != '0') |
| 131 | 3 | float_nb[3 + i] = float_nb[3 + i] + 1; | |
| 132 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (float_nb[3 + i] == '0') |
| 133 | 2 | my_round(float_nb, precision, last_char, i - 1); | |
| 134 | } | ||
| 135 | |||
| 136 | 12 | static void precise_it_biga(char *float_nb, formating_t *formating, | |
| 137 | char *decimal_part, int detected) | ||
| 138 | { | ||
| 139 | char temp[2]; | ||
| 140 | 12 | int i = 0; | |
| 141 | |||
| 142 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
|
12 | if (formating->id_nb == 0 |
| 143 |
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)) |
| 144 | 12 | return; | |
| 145 |
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) { |
| 146 | 6 | my_strcat(float_nb, "."); | |
| 147 | 6 | my_strcat(float_nb, decimal_part); | |
| 148 | 6 | return; | |
| 149 | } | ||
| 150 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (formating->id_nb > 0) { |
| 151 | 5 | my_strcat(float_nb, "."); | |
| 152 |
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++) { |
| 153 | 37 | temp[0] = decimal_part[i]; | |
| 154 | 37 | my_strcat(float_nb, temp); | |
| 155 | } | ||
| 156 | 5 | my_round(float_nb, formating->id_nb, decimal_part[i], i); | |
| 157 | 5 | return; | |
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | 14 | static char *my_get_str_float_hexa(double nb, formating_t *formating) | |
| 162 | { | ||
| 163 | 14 | char *converted_nb = my_convert_base_size_t( | |
| 164 | 14 | my_str_nbr_size_t(*(size_t *)&nb), | |
| 165 | "0123456789", "0123456789ABCDEF"); | ||
| 166 | 14 | char *float_nb = malloc(sizeof(char) * (my_strlen(converted_nb) + 6)); | |
| 167 | 14 | int detected = -1; | |
| 168 | 14 | char *decimal_part = malloc(sizeof(char) * (my_strlen(converted_nb) - 2)); | |
| 169 | |||
| 170 | 14 | my_strcat(float_nb, my_get_sign(converted_nb)); | |
| 171 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
|
14 | if (my_strcmp(converted_nb, "0") == 0) { |
| 172 | 2 | precise_zero(float_nb, formating->id_nb); | |
| 173 | 2 | return float_nb; | |
| 174 | } | ||
| 175 | 12 | my_strcat(float_nb, "0X1"); | |
| 176 | 12 | decimal_part = my_get_decimal_part(converted_nb, &detected); | |
| 177 | 12 | precise_it_biga(float_nb, formating, decimal_part, detected); | |
| 178 | 12 | my_strcat(float_nb, my_get_exposant(converted_nb, | |
| 179 | my_get_sign(converted_nb))); | ||
| 180 | 12 | FREE(decimal_part); | |
| 181 | 12 | return float_nb; | |
| 182 | } | ||
| 183 | |||
| 184 | 14 | int flag_biga(va_list list, formating_t *formating) | |
| 185 | { | ||
| 186 | 14 | double temp_double = va_arg(list, double); | |
| 187 | 14 | char *convert_base = my_get_str_float_hexa(temp_double, formating); | |
| 188 | |||
| 189 | 14 | format_it_double(convert_base, formating, temp_double); | |
| 190 | 14 | return my_putstr_fd_free(convert_base, formating->fd); | |
| 191 | } | ||
| 192 |