| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the array_separators function | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file array_separators.c | ||
| 9 | * @brief The file containing the array_separators function | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Check if the character is not a separator | ||
| 16 | * @param c The character | ||
| 17 | * @param sep The list of separators | ||
| 18 | * @return <b>int</b> <u>1</u> if the character is not a separator, | ||
| 19 | * <u>0</u> otherwise | ||
| 20 | */ | ||
| 21 | 267497 | static int my_char_is_not_a_separator(char const c, char const *sep) | |
| 22 | { | ||
| 23 |
2/2✓ Branch 0 taken 267497 times.
✓ Branch 1 taken 266536 times.
|
534033 | for (int i = 0; sep[i] != '\0'; i++) { |
| 24 |
2/2✓ Branch 0 taken 961 times.
✓ Branch 1 taken 266536 times.
|
267497 | if (sep[i] == c) |
| 25 | 961 | return 0; | |
| 26 | } | ||
| 27 | 266536 | return 1; | |
| 28 | } | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @brief Count the number of words and separators | ||
| 32 | * @param str The string | ||
| 33 | * @param sep The separator | ||
| 34 | * @param word The number of words | ||
| 35 | * @param separator The list of separators | ||
| 36 | * @return <b>void</b> | ||
| 37 | */ | ||
| 38 | 5973 | static void check_words_and_num(char const *str, int *sep, int *word, | |
| 39 | char const *separator) | ||
| 40 | { | ||
| 41 |
2/2✓ Branch 0 taken 87052 times.
✓ Branch 1 taken 5973 times.
|
93025 | for (int i = 0; str[i] != '\0'; i++) { |
| 42 |
2/2✓ Branch 1 taken 259 times.
✓ Branch 2 taken 86793 times.
|
87052 | if ((my_char_is_not_a_separator(str[i], separator) |
| 43 |
4/4✓ Branch 1 taken 75 times.
✓ Branch 2 taken 184 times.
✓ Branch 3 taken 2426 times.
✓ Branch 4 taken 84442 times.
|
87052 | || char_is_protected((char *)str, i)) && *sep == 1) { |
| 44 | 2426 | *word = *word + 1; | |
| 45 | 2426 | *sep = 0; | |
| 46 | } | ||
| 47 |
4/4✓ Branch 1 taken 259 times.
✓ Branch 2 taken 86793 times.
✓ Branch 3 taken 184 times.
✓ Branch 4 taken 75 times.
|
87311 | if (my_char_is_not_a_separator(str[i], separator) == 0 && |
| 48 | 259 | char_is_protected((char *)str, i) == 0) | |
| 49 | 184 | *sep = 1; | |
| 50 | } | ||
| 51 | 5973 | } | |
| 52 | |||
| 53 | /** | ||
| 54 | * @brief Count the number of letters in a word | ||
| 55 | * @param str The string | ||
| 56 | * @param which_lettre The index of the letter | ||
| 57 | * @param nbr_lettre The number of letters | ||
| 58 | * @param separator The list of separators | ||
| 59 | * @return <b>void</b> | ||
| 60 | */ | ||
| 61 | 2426 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre, | |
| 62 | char const *separator) | ||
| 63 | { | ||
| 64 | 2426 | for (int i = which_lettre; str[i] != '\0' | |
| 65 |
6/6✓ Branch 0 taken 87052 times.
✓ Branch 1 taken 2242 times.
✓ Branch 3 taken 86793 times.
✓ Branch 4 taken 259 times.
✓ Branch 5 taken 75 times.
✓ Branch 6 taken 184 times.
|
89553 | && (my_char_is_not_a_separator(str[i], separator) || |
| 66 | 259 | char_is_protected((char *)str, i)); i++) | |
| 67 | 86868 | *nbr_lettre = *nbr_lettre + 1; | |
| 68 | 2426 | } | |
| 69 | |||
| 70 | /** | ||
| 71 | * @brief Reset the number of letters in a word | ||
| 72 | * @param str The string | ||
| 73 | * @param which_lettre The index of the letter | ||
| 74 | * @param nbr_lettre The number of letters | ||
| 75 | * @param separator The list of separators | ||
| 76 | * @return <b>void</b> | ||
| 77 | */ | ||
| 78 | 2426 | static void reset_nbr_letter(char const *str, | |
| 79 | int *which_lettre, int *nbr_lettre, char const *separator) | ||
| 80 | { | ||
| 81 | 2426 | for (; str[*which_lettre] != '\0' | |
| 82 |
2/2✓ Branch 1 taken 184 times.
✓ Branch 2 taken 184 times.
|
368 | && my_char_is_not_a_separator(str[*which_lettre], separator) == 0 |
| 83 |
3/4✓ Branch 0 taken 368 times.
✓ Branch 1 taken 2242 times.
✓ Branch 3 taken 184 times.
✗ Branch 4 not taken.
|
2794 | && char_is_protected((char *)str, *which_lettre) == 0; |
| 84 | 184 | *which_lettre = *which_lettre + 1); | |
| 85 | 2426 | *nbr_lettre = 0; | |
| 86 | 2426 | } | |
| 87 | |||
| 88 | /** | ||
| 89 | * @brief Get the next word | ||
| 90 | * @param str The string | ||
| 91 | * @param separator The list of separators | ||
| 92 | * @return <b>char *</b> The next word | ||
| 93 | */ | ||
| 94 | 5973 | static char *nbr_sep(char *str, char const *separator) | |
| 95 | { | ||
| 96 | 5973 | int nbr_sep = 0; | |
| 97 | |||
| 98 | 5973 | for (int i = 0; my_char_is_not_a_separator(str[i], separator) == 0 | |
| 99 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5973 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
5973 | && char_is_protected((char *)str, i) == 0; i++) |
| 100 | ✗ | nbr_sep++; | |
| 101 | 5973 | return &str[nbr_sep]; | |
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * @brief Initialise the value of the array of words | ||
| 106 | * @param string The string | ||
| 107 | * @param str The string | ||
| 108 | * @param info The information | ||
| 109 | * @param separator The list of separators | ||
| 110 | * @return <b>char **</b> The array of words | ||
| 111 | */ | ||
| 112 | 5973 | static char **initialise_value(char **string, char const *str, | |
| 113 | int **info, char const *separator) | ||
| 114 | { | ||
| 115 | char **array_of_word; | ||
| 116 | |||
| 117 | 5973 | *string = nbr_sep(*string, separator); | |
| 118 | 5973 | check_words_and_num(str, info[1], info[0], separator); | |
| 119 | 5973 | array_of_word = malloc(sizeof(char *) * ((*(info[0])) + 1)); | |
| 120 | 5973 | return array_of_word; | |
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * @brief Transform a string into an array of words delimited by separators | ||
| 125 | * with inhibitors | ||
| 126 | * @param str The string to transform | ||
| 127 | * @param separator The separator | ||
| 128 | * @return <b>char **</b> The array of words | ||
| 129 | */ | ||
| 130 | 5973 | char **array_separators(char const *str, char const *separator) | |
| 131 | { | ||
| 132 | 5973 | int word = 0; | |
| 133 | 5973 | int sep = 1; | |
| 134 | 5973 | int *info[2] = {&word, &sep}; | |
| 135 | 5973 | int nbr_lettre = 0; | |
| 136 | 5973 | int which_lettre = 0; | |
| 137 | 5973 | char *string = (char *)str; | |
| 138 | 5973 | char **array_of_word = initialise_value(&string, str, info, separator); | |
| 139 | |||
| 140 |
2/2✓ Branch 0 taken 2426 times.
✓ Branch 1 taken 5973 times.
|
8399 | for (int elem = 0; elem < word; elem++) { |
| 141 | 2426 | count_letter(string, which_lettre, &nbr_lettre, separator); | |
| 142 | 2426 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
| 143 |
2/2✓ Branch 0 taken 86868 times.
✓ Branch 1 taken 2426 times.
|
89294 | for (int chara = 0; chara < nbr_lettre; chara++) { |
| 144 | 86868 | array_of_word[elem][chara] = string[which_lettre]; | |
| 145 | 86868 | which_lettre++; | |
| 146 | } | ||
| 147 | 2426 | array_of_word[elem][nbr_lettre] = '\0'; | |
| 148 | 2426 | reset_nbr_letter(string, &which_lettre, &nbr_lettre, separator); | |
| 149 | } | ||
| 150 | 5973 | array_of_word[word] = NULL; | |
| 151 | 5973 | return array_of_word; | |
| 152 | } | ||
| 153 |