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 | 1080247 | static int my_char_is_not_a_separator(char const c, char const *sep) | |
22 | { | ||
23 |
2/2✓ Branch 0 taken 1080247 times.
✓ Branch 1 taken 1079375 times.
|
2159622 | for (int i = 0; sep[i] != '\0'; i++) { |
24 |
2/2✓ Branch 0 taken 872 times.
✓ Branch 1 taken 1079375 times.
|
1080247 | if (sep[i] == c) |
25 | 872 | return 0; | |
26 | } | ||
27 | 1079375 | 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 | 5760 | static void check_words_and_num(char const *str, int *sep, int *word, | |
39 | char const *separator) | ||
40 | { | ||
41 |
2/2✓ Branch 0 taken 358041 times.
✓ Branch 1 taken 5760 times.
|
363801 | for (int i = 0; str[i] != '\0'; i++) { |
42 |
2/2✓ Branch 1 taken 230 times.
✓ Branch 2 taken 357811 times.
|
358041 | if ((my_char_is_not_a_separator(str[i], separator) |
43 |
4/4✓ Branch 1 taken 33 times.
✓ Branch 2 taken 197 times.
✓ Branch 3 taken 2201 times.
✓ Branch 4 taken 355643 times.
|
358041 | || char_is_protected((char *)str, i)) && *sep == 1) { |
44 | 2201 | *word = *word + 1; | |
45 | 2201 | *sep = 0; | |
46 | } | ||
47 |
4/4✓ Branch 1 taken 230 times.
✓ Branch 2 taken 357811 times.
✓ Branch 3 taken 197 times.
✓ Branch 4 taken 33 times.
|
358271 | if (my_char_is_not_a_separator(str[i], separator) == 0 && |
48 | 230 | char_is_protected((char *)str, i) == 0) | |
49 | 197 | *sep = 1; | |
50 | } | ||
51 | 5760 | } | |
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 | 2201 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre, | |
62 | char const *separator) | ||
63 | { | ||
64 | 2201 | for (int i = which_lettre; str[i] != '\0' | |
65 |
6/6✓ Branch 0 taken 358026 times.
✓ Branch 1 taken 2019 times.
✓ Branch 3 taken 357811 times.
✓ Branch 4 taken 215 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 182 times.
|
360260 | && (my_char_is_not_a_separator(str[i], separator) || |
66 | 215 | char_is_protected((char *)str, i)); i++) | |
67 | 357844 | *nbr_lettre = *nbr_lettre + 1; | |
68 | 2201 | } | |
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 | 2201 | static void reset_nbr_letter(char const *str, | |
79 | int *which_lettre, int *nbr_lettre, char const *separator) | ||
80 | { | ||
81 | 2201 | for (; str[*which_lettre] != '\0' | |
82 |
2/2✓ Branch 1 taken 182 times.
✓ Branch 2 taken 182 times.
|
364 | && my_char_is_not_a_separator(str[*which_lettre], separator) == 0 |
83 |
3/4✓ Branch 0 taken 364 times.
✓ Branch 1 taken 2019 times.
✓ Branch 3 taken 182 times.
✗ Branch 4 not taken.
|
2565 | && char_is_protected((char *)str, *which_lettre) == 0; |
84 | 182 | *which_lettre = *which_lettre + 1); | |
85 | 2201 | *nbr_lettre = 0; | |
86 | 2201 | } | |
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 | 5760 | static char *nbr_sep(char *str, char const *separator) | |
95 | { | ||
96 | 5760 | int nbr_sep = 0; | |
97 | |||
98 | 5775 | for (int i = 0; my_char_is_not_a_separator(str[i], separator) == 0 | |
99 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5760 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
5775 | && char_is_protected((char *)str, i) == 0; i++) |
100 | 15 | nbr_sep++; | |
101 | 5760 | 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 | 5760 | static char **initialise_value(char **string, char const *str, | |
113 | int **info, char const *separator) | ||
114 | { | ||
115 | char **array_of_word; | ||
116 | |||
117 | 5760 | *string = nbr_sep(*string, separator); | |
118 | 5760 | check_words_and_num(str, info[1], info[0], separator); | |
119 | 5760 | array_of_word = malloc(sizeof(char *) * ((*(info[0])) + 1)); | |
120 | 5760 | 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 | 5760 | char **array_separators(char const *str, char const *separator) | |
131 | { | ||
132 | 5760 | int word = 0; | |
133 | 5760 | int sep = 1; | |
134 | 5760 | int *info[2] = {&word, &sep}; | |
135 | 5760 | int nbr_lettre = 0; | |
136 | 5760 | int which_lettre = 0; | |
137 | 5760 | char *string = (char *)str; | |
138 | 5760 | char **array_of_word = initialise_value(&string, str, info, separator); | |
139 | |||
140 |
2/2✓ Branch 0 taken 2201 times.
✓ Branch 1 taken 5760 times.
|
7961 | for (int elem = 0; elem < word; elem++) { |
141 | 2201 | count_letter(string, which_lettre, &nbr_lettre, separator); | |
142 | 2201 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
143 |
2/2✓ Branch 0 taken 357844 times.
✓ Branch 1 taken 2201 times.
|
360045 | for (int chara = 0; chara < nbr_lettre; chara++) { |
144 | 357844 | array_of_word[elem][chara] = string[which_lettre]; | |
145 | 357844 | which_lettre++; | |
146 | } | ||
147 | 2201 | array_of_word[elem][nbr_lettre] = '\0'; | |
148 | 2201 | reset_nbr_letter(string, &which_lettre, &nbr_lettre, separator); | |
149 | } | ||
150 | 5760 | array_of_word[word] = NULL; | |
151 | 5760 | return array_of_word; | |
152 | } | ||
153 |