GCC Code Coverage Report


Directory: ./
File: src/inhibitors/inhibitors.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 101 110 91.8%
Functions: 8 8 100.0%
Branches: 79 88 89.8%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the inhibitors functions
6 */
7 /**
8 * @file inhibitors.c
9 * @brief The file containing the inhibitors functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Update the inhibitor
16 * @param command The command
17 * @param index The index
18 * @param inhibitor The inhibitor
19 * @param end_inhibitors The end inhibitors
20 * @return <b>void</b>
21 */
22 523509 static int update_inhibitor(char *command, int index,
23 char *inhibitor, int *parentheses)
24 {
25
4/4
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 523317 times.
✓ Branch 2 taken 162 times.
✓ Branch 3 taken 30 times.
523509 if ((*inhibitor == '(' && command[index] == ')') ||
26
6/6
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 522360 times.
✓ Branch 2 taken 162 times.
✓ Branch 3 taken 957 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 522492 times.
523479 ((*inhibitor == 0 || *inhibitor == '(') && command[index] == '(')) {
27
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
60 *parentheses += (command[index] == '(') ? 1 : - 1;
28
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 *inhibitor = (*parentheses) ? '(' : 0;
29 60 return 0;
30 }
31
4/4
✓ Branch 0 taken 1101 times.
✓ Branch 1 taken 522348 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 987 times.
523449 if (*inhibitor != 0 && *inhibitor == command[index]
32
1/2
✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
114 && *inhibitor != '\\') {
33 114 *inhibitor = 0;
34 114 return 1;
35 }
36
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 523300 times.
523335 if (*inhibitor == '\\') {
37 35 *inhibitor = 1;
38 35 return 0;
39 }
40
4/4
✓ Branch 0 taken 522348 times.
✓ Branch 1 taken 952 times.
✓ Branch 3 taken 149 times.
✓ Branch 4 taken 522199 times.
523300 if (*inhibitor == 0 && my_char_is(command[index], "\'\"\\")) {
41 149 *inhibitor = command[index];
42 149 return 1;
43 }
44 523151 return 0;
45 }
46
47 /**
48 * @brief Count the number of words in a string
49 * @param str The string
50 * @return <b>int</b> The number of words
51 */
52 1125 static int count_words(char *str)
53 {
54 1125 int n = 0;
55 1125 char inhibitor = 0;
56 1125 int add_it = 1;
57 1125 int parentheses = 0;
58
59
2/2
✓ Branch 0 taken 176931 times.
✓ Branch 1 taken 1125 times.
178056 for (int index = 0; str[index] != '\0'; index++) {
60
2/2
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 176846 times.
176931 if (update_inhibitor(str, index, &inhibitor, &parentheses))
61 85 continue;
62
4/4
✓ Branch 1 taken 7280 times.
✓ Branch 2 taken 169566 times.
✓ Branch 3 taken 57 times.
✓ Branch 4 taken 7223 times.
176846 if (my_char_is(str[index], " \t\n\"\'\\(") == 0 || inhibitor != 0) {
63
2/2
✓ Branch 0 taken 7268 times.
✓ Branch 1 taken 162355 times.
169623 n = (add_it) ? n + 1 : n;
64 169623 add_it = 0;
65 169623 continue;
66 }
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7223 times.
7223 if (inhibitor == 1)
68 inhibitor = 0;
69 7223 add_it = 1;
70 }
71 1125 return n;
72 }
73
74 /**
75 * @brief Count the number of letters in a word
76 * @param str The string
77 * @param save The save
78 * @return <b>int</b> The number of letters
79 */
80 7268 static int count_letters(char *str, int *save)
81 {
82 7268 int n = 0;
83 7268 char inhibitor = 0;
84 7268 int parentheses = 0;
85
86
2/2
✓ Branch 0 taken 176879 times.
✓ Branch 1 taken 88 times.
176967 for (int i = *save; str[i] != '\0'; i++) {
87 176879 update_inhibitor(str, i, &inhibitor, &parentheses);
88
4/4
✓ Branch 1 taken 7208 times.
✓ Branch 2 taken 169671 times.
✓ Branch 3 taken 7180 times.
✓ Branch 4 taken 28 times.
176879 if (my_char_is(str[i], " \t\n") && inhibitor == 0)
89 7180 return n;
90
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 169686 times.
169699 if (inhibitor == 1)
91 13 inhibitor = 0;
92 169699 (*save)++;
93 169699 n++;
94 }
95 88 return n;
96 }
97
98 /**
99 * @brief Move the save to the next word
100 * @param save The save
101 * @param str The string
102 * @return <b>void</b>
103 */
104 7268 static void move_save(int *save, char *str, int index)
105 {
106
1/2
✓ Branch 0 taken 13468 times.
✗ Branch 1 not taken.
13468 for (int i = *save; str[i] != '\0'; i++) {
107
2/2
✓ Branch 1 taken 7268 times.
✓ Branch 2 taken 6200 times.
13468 if (my_char_is(str[i], " \t\n") == 0)
108 7268 return;
109 6200 (*save)++;
110 }
111 }
112
113 /**
114 * @brief Fill the string with the letters
115 * @param to_fill The string to fill
116 * @param nb_letters The number of letters to fill
117 * @param save The save
118 * @param source The source
119 * @return <b>void</b>
120 */
121 7268 static void fill_str(char *to_fill, int nb_letters, int save, char *source)
122 {
123 7268 char inhibitor = 0;
124 7268 int parentheses = 0;
125
126
2/2
✓ Branch 0 taken 169699 times.
✓ Branch 1 taken 7268 times.
176967 for (int index = save - nb_letters; index < save; index++) {
127
2/2
✓ Branch 1 taken 89 times.
✓ Branch 2 taken 169610 times.
169699 if (update_inhibitor(source, index, &inhibitor, &parentheses))
128 89 continue;
129
3/4
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 169582 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
169610 if (my_char_is(source[index], " \t\n") && inhibitor == 0)
130 return;
131 169610 my_add_chr(to_fill, source[index]);
132
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 169597 times.
169610 if (inhibitor == 1)
133 13 inhibitor = 0;
134 }
135 }
136
137 /**
138 * @brief Add an element to the new array
139 * @param new_array The new array
140 * @param array The array
141 * @param index The index
142 * @param array_index The array index
143 * @return <b>void</b>
144 */
145 7268 static void add_element(char **new_array, char **array,
146 int *index, int array_index)
147 {
148
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7264 times.
7268 if (my_str_contains(array[array_index], "()") &&
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 array[array_index][0] != '(') {
150 new_array[*index] = my_strndup(array[array_index],
151 my_get_char_index(array[array_index], '(', 1));
152 (*index)++;
153 }
154 7268 new_array[*index] = (my_strstr(array[array_index], "(") != NULL) ?
155 4 my_strndup(my_strstr(array[array_index], "("), my_get_char_index(
156 4 my_strstr(array[array_index], "("), ')', my_count_letter(
157
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7264 times.
7268 array[array_index], ')')) + 1) : my_strdup(array[array_index]);
158
3/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7264 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
7272 if (my_str_contains(array[array_index], "()") &&
159 4 my_get_last_char(array[array_index]) != ')') {
160 (*index)++;
161 new_array[*index] = my_strdup(my_strstr(array[array_index], ")"));
162 }
163 7268 }
164
165 /**
166 * @brief Separate the words in the array which are stuck with parentheses
167 * @param array The array
168 * @param nb_words The number of words
169 * @return <b>char **</b> The new array
170 */
171 1125 static char **separate_parentheses(char **array, int nb_words)
172 {
173 1125 char **new_array = NULL;
174 1125 int array_index = 0;
175
176
3/4
✓ Branch 0 taken 7268 times.
✓ Branch 1 taken 1125 times.
✓ Branch 2 taken 7268 times.
✗ Branch 3 not taken.
8393 for (int index = 0; index < nb_words && array[index] != NULL; index++) {
177
3/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7264 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
7268 if (my_str_contains(array[index], "()") && array[index][0] != '(')
178 nb_words++;
179
3/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7264 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
7272 if (my_str_contains(array[index], "()") &&
180 4 my_get_last_char(array[index]) != ')')
181 nb_words++;
182 }
183 1125 new_array = calloc(nb_words + 1, sizeof(char *));
184
2/2
✓ Branch 0 taken 7268 times.
✓ Branch 1 taken 1125 times.
8393 for (int index = 0; array[array_index] != NULL; index++) {
185 7268 add_element(new_array, array, &index, array_index);
186 7268 array_index++;
187 }
188 1125 new_array[nb_words] = NULL;
189 1125 FREE_WORD_ARRAY(array);
190 1125 return new_array;
191 }
192
193 /**
194 * @brief Transform a string into an array of words with inhibitors
195 * @param str The string to transform
196 * @return <b>char **</b> The array of words
197 */
198 1131 char **str_to_array_inhibitors(char *str)
199 {
200 1131 int nb_words = 0;
201 1131 int save = 0;
202 1131 int nb_letters = 0;
203 1131 char **array = NULL;
204
205
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1125 times.
1131 if (str == NULL)
206 6 return NULL;
207 1125 nb_words = count_words(str);
208 1125 array = malloc(sizeof(char *) * (nb_words + 1));
209
2/2
✓ Branch 0 taken 7268 times.
✓ Branch 1 taken 1125 times.
8393 for (int index = 0; index < nb_words; index++) {
210 7268 move_save(&save, str, index);
211 7268 nb_letters = count_letters(str, &save);
212 7268 array[index] = calloc(nb_letters + 1, sizeof(char));
213 7268 fill_str(array[index], nb_letters, save, str);
214 }
215 1125 array[nb_words] = NULL;
216 1125 array = separate_parentheses(array, nb_words);
217 1125 return array;
218 }
219