Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_printf | ||
4 | ** File description: | ||
5 | ** Flag to print a %g (a double) | ||
6 | */ | ||
7 | /** | ||
8 | * @file flag_g.c | ||
9 | * @brief The file containing the flag_g function | ||
10 | * @author Nicolas TORO | ||
11 | */ | ||
12 | |||
13 | #include "myprintf.h" | ||
14 | |||
15 | 25 | static int check_precision(int precision, int nb) | |
16 | { | ||
17 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 24 times.
|
25 | if (precision - my_strlen(my_str_nbr(ABS(nb))) <= 0) |
18 | 1 | return 0; | |
19 | else | ||
20 | 24 | return precision - my_strlen(my_str_nbr(ABS(nb))); | |
21 | } | ||
22 | |||
23 | 33 | static int get_nb_zero(double nb) | |
24 | { | ||
25 | 33 | int index = 0; | |
26 | 33 | int temp_nb = 0; | |
27 | 33 | double temp_nb_double = nb; | |
28 | |||
29 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 12 times.
|
33 | if (ABS(nb) < 1) { |
30 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 21 times.
|
96 | while (temp_nb % 10 == 0) { |
31 | 75 | index = index + 1; | |
32 | 75 | temp_nb_double = temp_nb_double * 10; | |
33 | 75 | temp_nb = temp_nb_double; | |
34 | } | ||
35 | 21 | return index - 1; | |
36 | } | ||
37 | 12 | return 0; | |
38 | } | ||
39 | |||
40 | 6 | static void my_get_decimal_part(char *float_nb, double nb, | |
41 | formating_t *formating) | ||
42 | { | ||
43 | 12 | size_t precision_size = my_compute_power_rec_size_t(10, check_precision | |
44 | 6 | (formating->id_nb, nb) + get_nb_zero(nb)); | |
45 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | size_t nb_convert = ABS(nb * precision_size); |
46 | 6 | int i = 0; | |
47 | 6 | char temp[2] = ""; | |
48 | |||
49 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | if (nb_convert % precision_size != 0) |
50 | 5 | my_strcat(float_nb, "."); | |
51 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 6 times.
|
15 | for (i = 0; i < get_nb_zero(nb - (int)nb); i++) |
52 | 9 | my_strcat(float_nb, "0"); | |
53 |
2/2✓ Branch 2 taken 32 times.
✓ Branch 3 taken 6 times.
|
38 | for (int j = 0; j < my_strlen(my_str_nbr_long_long(nb_convert)); j++) { |
54 | 32 | temp[0] = my_str_nbr_long_long(nb_convert % precision_size)[j]; | |
55 | 32 | my_strcat(float_nb, temp); | |
56 | } | ||
57 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
|
6 | nb_convert = ABS(nb * my_compute_power_rec_size_t(10, check_precision |
58 | (formating->id_nb, nb) + get_nb_zero(nb) + 1)); | ||
59 | 6 | my_round_float_str(float_nb, 48 + (nb_convert % 10), | |
60 | 6 | my_strlen(float_nb) - 1, 0); | |
61 | 6 | } | |
62 | |||
63 | 7 | static char *my_get_str_float(double nb, formating_t *formating) | |
64 | { | ||
65 | 7 | char *float_nb = malloc(sizeof(char) * | |
66 | 7 | my_strlen(my_str_nbr(nb)) + 9 + formating->id_nb); | |
67 | 7 | int i = 0; | |
68 | |||
69 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | formating->id_nb = (formating->id_prc != -1) ? (formating->id_nb) : 6; |
70 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | if (nb < 0) |
71 | 3 | my_strcat(float_nb, "-"); | |
72 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | my_strcat(float_nb, my_str_nbr_long_long(ABS(nb))); |
73 |
3/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
|
7 | if (formating->id_nb > 0 && check_precision(formating->id_nb, nb) != 0) { |
74 | 6 | my_get_decimal_part(float_nb, nb, formating); | |
75 | 6 | i = my_strlen(float_nb) - 1; | |
76 |
4/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
|
21 | for (i; (float_nb[i] == '0' || float_nb[i] == '.') && i != 0; i--) |
77 | 15 | float_nb[i] = '\0'; | |
78 | } | ||
79 | 7 | my_strcat(float_nb, "\0"); | |
80 | 7 | return float_nb; | |
81 | } | ||
82 | |||
83 | 3 | static char *my_get_power(int index_nb) | |
84 | { | ||
85 | char *power; | ||
86 | |||
87 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (ABS(index_nb) >= 100) { |
88 | 1 | power = malloc(sizeof(char) * 5); | |
89 | } else { | ||
90 | 2 | power = malloc(sizeof(char) * 4); | |
91 | } | ||
92 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (index_nb >= 0) { |
93 | 2 | my_strcat(power, "+"); | |
94 | } else { | ||
95 | 1 | my_strcat(power, "-"); | |
96 | } | ||
97 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
3 | if (ABS(index_nb) < 10) |
98 | 2 | my_strcat(power, "0"); | |
99 | 3 | my_strcat(power, my_str_nbr(ABS(index_nb))); | |
100 | 3 | my_strcat(power, "\0"); | |
101 | 3 | return power; | |
102 | } | ||
103 | |||
104 | 3 | static void condition(double nb, int *index_nb, | |
105 | double *temp_nb_double, int *temp_nb) | ||
106 | { | ||
107 |
4/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
|
3 | if (ABS(nb) > 1) { |
108 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 2 times.
|
10 | for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { |
109 | 8 | *index_nb = *index_nb + 1; | |
110 | 8 | *temp_nb_double = *temp_nb_double / 10; | |
111 | } | ||
112 | } else { | ||
113 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
|
101 | while (*temp_nb % 10 == 0) { |
114 | 100 | *index_nb = *index_nb - 1; | |
115 | 100 | *temp_nb_double = *temp_nb_double * 10; | |
116 | 100 | *temp_nb = *temp_nb_double; | |
117 | } | ||
118 | } | ||
119 | 3 | } | |
120 | |||
121 | 3 | static char *my_get_str_float_scientific(double nb, formating_t *formating) | |
122 | { | ||
123 | char *float_nb; | ||
124 | 3 | int signe = 0; | |
125 | 3 | int index_nb = 0; | |
126 | 3 | int temp_nb = nb; | |
127 | 3 | double temp_nb_double = nb; | |
128 | |||
129 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (nb < 0) |
130 | 1 | signe = 1; | |
131 | 3 | float_nb = malloc(sizeof(char) * (13 + signe)); | |
132 | 3 | condition(nb, &index_nb, &temp_nb_double, &temp_nb); | |
133 | 3 | my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); | |
134 | 3 | my_strcat(float_nb, "e"); | |
135 | 3 | my_strcat(float_nb, my_get_power(index_nb)); | |
136 | 3 | return float_nb; | |
137 | } | ||
138 | |||
139 | 16 | static int my_get_index(double nb) | |
140 | { | ||
141 | 16 | int index_nb = 0; | |
142 | 16 | int temp_nb = nb; | |
143 | 16 | double temp_nb_double = nb; | |
144 | |||
145 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
|
16 | if (nb == 0.0) |
146 | 2 | return 0; | |
147 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 6 times.
|
14 | if (ABS(nb) > 1) { |
148 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | return my_strlen(my_str_nbr(ABS(nb))); |
149 | } else { | ||
150 |
2/2✓ Branch 0 taken 208 times.
✓ Branch 1 taken 6 times.
|
214 | while (temp_nb % 10 == 0) { |
151 | 208 | index_nb = index_nb - 1; | |
152 | 208 | temp_nb_double = temp_nb_double * 10; | |
153 | 208 | temp_nb = temp_nb_double; | |
154 | } | ||
155 | } | ||
156 | 6 | return index_nb; | |
157 | } | ||
158 | |||
159 | 8 | int flag_g(va_list list, formating_t *formating) | |
160 | { | ||
161 | 8 | double temp_double = va_arg(list, double); | |
162 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
|
8 | int index = ABS(my_get_index(temp_double)); |
163 | char *temp; | ||
164 | 8 | int free = 1; | |
165 | |||
166 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (temp_double == 0.0) { |
167 | 1 | temp = "0"; | |
168 | 1 | free = 0; | |
169 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
7 | } else if (index - 3 < 4 && (index <= formating->id_nb || |
170 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | formating->id_nb == -1)) { |
171 | 4 | temp = my_get_str_float(temp_double, formating); | |
172 | } else { | ||
173 | 3 | temp = my_get_str_float_scientific(temp_double, formating); | |
174 | } | ||
175 | 8 | format_it_double(temp, formating, temp_double); | |
176 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | if (free) |
177 | 7 | return my_putstr_fd_free(temp, formating->fd); | |
178 | 1 | return my_putstr_fd(temp, formating->fd); | |
179 | } | ||
180 |