| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_convert_base_size_t | ||
| 4 | ** File description: | ||
| 5 | ** Returns the result of the conversion of a size_t (nbr) | ||
| 6 | ** in a specific base (base_from) to another base (base_to) | ||
| 7 | */ | ||
| 8 | /** | ||
| 9 | * @file my_convert_base_size_t.c | ||
| 10 | * @brief The file containing the my_convert_base_size_t function | ||
| 11 | * @author Nicolas TORO | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "my.h" | ||
| 15 | |||
| 16 | 4791 | static int is_valid(char c, char const *base) | |
| 17 | { | ||
| 18 | 2/2✓ Branch 1 taken 26630 times. ✓ Branch 2 taken 1 times. | 26631 | for (int i = 0; i < my_strlen(base); i++) { | 
| 19 | 2/2✓ Branch 0 taken 4790 times. ✓ Branch 1 taken 21840 times. | 26630 | if (c == base[i]) | 
| 20 | 4790 | return 1; | |
| 21 | } | ||
| 22 | 1 | return 0; | |
| 23 | } | ||
| 24 | |||
| 25 | 4791 | static size_t calculate_base(char const *str, char const *base, | |
| 26 | int *index, int *error) | ||
| 27 | { | ||
| 28 | 2/2✓ Branch 1 taken 4790 times. ✓ Branch 2 taken 1 times. | 4791 | if (is_valid(str[index[0]], base) == 1) { | 
| 29 | 2/2✓ Branch 0 taken 479 times. ✓ Branch 1 taken 4311 times. | 4790 | if (str[index[0]] == base[index[1]]) { | 
| 30 | 479 | return index[1] * my_compute_power_rec_size_t(my_strlen(base), | |
| 31 | 479 | my_strlen(str) - index[0] - 1); | |
| 32 | } | ||
| 33 | } else { | ||
| 34 | 1 | *error = 1; | |
| 35 | } | ||
| 36 | 4312 | return 0; | |
| 37 | } | ||
| 38 | |||
| 39 | 31 | static size_t my_getnbr_base_size_t(char const *str, char const *base) | |
| 40 | { | ||
| 41 | 31 | size_t result = 0; | |
| 42 | 31 | int error = 0; | |
| 43 | int index[2]; | ||
| 44 | |||
| 45 | 2/2✓ Branch 1 taken 482 times. ✓ Branch 2 taken 31 times. | 513 | for (int i = my_strlen(str) - 1; i >= 0; i--) { | 
| 46 | 4/4✓ Branch 1 taken 4794 times. ✓ Branch 2 taken 479 times. ✓ Branch 3 taken 4791 times. ✓ Branch 4 taken 3 times. | 5273 | for (int j = 0; j < my_strlen(base) && error == 0; j++) { | 
| 47 | 4791 | index[0] = i; | |
| 48 | 4791 | index[1] = j; | |
| 49 | 4791 | result = result + calculate_base(str, base, index, &error); | |
| 50 | } | ||
| 51 | } | ||
| 52 | 31 | return result; | |
| 53 | } | ||
| 54 | |||
| 55 | 31 | static char *put_str_nb(size_t nb, char const *base, int base_len) | |
| 56 | { | ||
| 57 | 31 | int len_nb = 1; | |
| 58 | 31 | size_t temp_nb = nb; | |
| 59 | char *nb_str; | ||
| 60 | 31 | size_t figure_temp = nb; | |
| 61 | |||
| 62 | 2/2✓ Branch 0 taken 373 times. ✓ Branch 1 taken 31 times. | 404 | while ((temp_nb / base_len) != 0) { | 
| 63 | 373 | len_nb = len_nb + 1; | |
| 64 | 373 | temp_nb = temp_nb / base_len; | |
| 65 | } | ||
| 66 | 31 | nb_str = malloc(sizeof(char) * len_nb); | |
| 67 | 2/2✓ Branch 0 taken 404 times. ✓ Branch 1 taken 31 times. | 435 | for (int i = 0; i < len_nb; i++) { | 
| 68 | 404 | nb_str[len_nb - i - 1] = base[figure_temp % base_len]; | |
| 69 | 404 | figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; | |
| 70 | } | ||
| 71 | 31 | nb_str[len_nb] = '\0'; | |
| 72 | 31 | return my_strdup(nb_str); | |
| 73 | } | ||
| 74 | |||
| 75 | 31 | static char *my_setnbr_base(size_t nbr, char const *base) | |
| 76 | { | ||
| 77 | 31 | int base_len = 0; | |
| 78 | |||
| 79 | 2/2✓ Branch 0 taken 468 times. ✓ Branch 1 taken 31 times. | 499 | while (base[base_len] != '\0') | 
| 80 | 468 | base_len = base_len + 1; | |
| 81 | 31 | return put_str_nb(nbr, base, base_len); | |
| 82 | } | ||
| 83 | |||
| 84 | 31 | char *my_convert_base_size_t(char const *nbr, | |
| 85 | char const *base_from, char const *base_to) | ||
| 86 | { | ||
| 87 | 31 | size_t number = my_getnbr_base_size_t(nbr, base_from); | |
| 88 | 31 | char *result = my_setnbr_base(number, base_to); | |
| 89 | |||
| 90 | 31 | return result; | |
| 91 | } | ||
| 92 |