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