GCC Code Coverage Report


Directory: ./
File: src/inhibitors/inhibitors.c
Date: 2024-05-07 14:54:03
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 119014 static int update_inhibitor(char *command, int index,
23 char *inhibitor, int *parentheses)
24 {
25
4/4
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 118582 times.
✓ Branch 2 taken 414 times.
✓ Branch 3 taken 18 times.
119014 if ((*inhibitor == '(' && command[index] == ')') ||
26
6/6
✓ Branch 0 taken 3436 times.
✓ Branch 1 taken 115560 times.
✓ Branch 2 taken 414 times.
✓ Branch 3 taken 3022 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 115956 times.
118996 ((*inhibitor == 0 || *inhibitor == '(') && command[index] == '(')) {
27
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 *parentheses += (command[index] == '(') ? 1 : - 1;
28
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 *inhibitor = (*parentheses) ? '(' : 0;
29 36 return 0;
30 }
31
4/4
✓ Branch 0 taken 3436 times.
✓ Branch 1 taken 115542 times.
✓ Branch 2 taken 265 times.
✓ Branch 3 taken 3171 times.
118978 if (*inhibitor != 0 && *inhibitor == command[index]
32
1/2
✓ Branch 0 taken 265 times.
✗ Branch 1 not taken.
265 && *inhibitor != '\\') {
33 265 *inhibitor = 0;
34 265 return 1;
35 }
36
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 118639 times.
118713 if (*inhibitor == '\\') {
37 74 *inhibitor = 1;
38 74 return 0;
39 }
40
4/4
✓ Branch 0 taken 115542 times.
✓ Branch 1 taken 3097 times.
✓ Branch 3 taken 339 times.
✓ Branch 4 taken 115203 times.
118639 if (*inhibitor == 0 && my_char_is(command[index], "\'\"\\")) {
41 339 *inhibitor = command[index];
42 339 return 1;
43 }
44 118300 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 1217 static int count_words(char *str)
53 {
54 1217 int n = 0;
55 1217 char inhibitor = 0;
56 1217 int add_it = 1;
57 1217 int parentheses = 0;
58
59
2/2
✓ Branch 0 taken 40612 times.
✓ Branch 1 taken 1217 times.
41829 for (int index = 0; str[index] != '\0'; index++) {
60
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 40420 times.
40612 if (update_inhibitor(str, index, &inhibitor, &parentheses))
61 192 continue;
62
4/4
✓ Branch 1 taken 2974 times.
✓ Branch 2 taken 37446 times.
✓ Branch 3 taken 208 times.
✓ Branch 4 taken 2766 times.
40420 if (my_char_is(str[index], " \t\n\"\'\\(") == 0 || inhibitor != 0) {
63
2/2
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 34829 times.
37654 n = (add_it) ? n + 1 : n;
64 37654 add_it = 0;
65 37654 continue;
66 }
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2766 times.
2766 if (inhibitor == 1)
68 inhibitor = 0;
69 2766 add_it = 1;
70 }
71 1217 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 2825 static int count_letters(char *str, int *save)
81 {
82 2825 int n = 0;
83 2825 char inhibitor = 0;
84 2825 int parentheses = 0;
85
86
2/2
✓ Branch 0 taken 40580 times.
✓ Branch 1 taken 67 times.
40647 for (int i = *save; str[i] != '\0'; i++) {
87 40580 update_inhibitor(str, i, &inhibitor, &parentheses);
88
4/4
✓ Branch 1 taken 2910 times.
✓ Branch 2 taken 37670 times.
✓ Branch 3 taken 2758 times.
✓ Branch 4 taken 152 times.
40580 if (my_char_is(str[i], " \t\n") && inhibitor == 0)
89 2758 return n;
90
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 37794 times.
37822 if (inhibitor == 1)
91 28 inhibitor = 0;
92 37822 (*save)++;
93 37822 n++;
94 }
95 67 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 2825 static void move_save(int *save, char *str, int index)
105 {
106
1/2
✓ Branch 0 taken 4460 times.
✗ Branch 1 not taken.
4460 for (int i = *save; str[i] != '\0'; i++) {
107
2/2
✓ Branch 1 taken 2825 times.
✓ Branch 2 taken 1635 times.
4460 if (my_char_is(str[i], " \t\n") == 0)
108 2825 return;
109 1635 (*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 2825 static void fill_str(char *to_fill, int nb_letters, int save, char *source)
122 {
123 2825 char inhibitor = 0;
124 2825 int parentheses = 0;
125
126
2/2
✓ Branch 0 taken 37822 times.
✓ Branch 1 taken 2825 times.
40647 for (int index = save - nb_letters; index < save; index++) {
127
2/2
✓ Branch 1 taken 206 times.
✓ Branch 2 taken 37616 times.
37822 if (update_inhibitor(source, index, &inhibitor, &parentheses))
128 206 continue;
129
3/4
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 37464 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 152 times.
37616 if (my_char_is(source[index], " \t\n") && inhibitor == 0)
130 return;
131 37616 my_add_chr(to_fill, source[index]);
132
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 37588 times.
37616 if (inhibitor == 1)
133 28 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 2825 static void add_element(char **new_array, char **array,
146 int *index, int array_index)
147 {
148
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2819 times.
2825 if (my_str_contains(array[array_index], "()") &&
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 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 2825 new_array[*index] = (my_strstr(array[array_index], "(") != NULL) ?
155 6 my_strndup(my_strstr(array[array_index], "("), my_get_char_index(
156 6 my_strstr(array[array_index], "("), ')', my_count_letter(
157
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2819 times.
2825 array[array_index], ')')) + 1) : my_strdup(array[array_index]);
158
3/4
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2819 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
2831 if (my_str_contains(array[array_index], "()") &&
159 6 my_get_last_char(array[array_index]) != ')') {
160 (*index)++;
161 new_array[*index] = my_strdup(my_strstr(array[array_index], ")"));
162 }
163 2825 }
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 1217 static char **separate_parentheses(char **array, int nb_words)
172 {
173 1217 char **new_array = NULL;
174 1217 int array_index = 0;
175
176
3/4
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 1217 times.
✓ Branch 2 taken 2825 times.
✗ Branch 3 not taken.
4042 for (int index = 0; index < nb_words && array[index] != NULL; index++) {
177
3/4
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2819 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
2825 if (my_str_contains(array[index], "()") && array[index][0] != '(')
178 nb_words++;
179
3/4
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2819 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
2831 if (my_str_contains(array[index], "()") &&
180 6 my_get_last_char(array[index]) != ')')
181 nb_words++;
182 }
183 1217 new_array = calloc(nb_words + 1, sizeof(char *));
184
2/2
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 1217 times.
4042 for (int index = 0; array[array_index] != NULL; index++) {
185 2825 add_element(new_array, array, &index, array_index);
186 2825 array_index++;
187 }
188 1217 new_array[nb_words] = NULL;
189 1217 FREE_WORD_ARRAY(array);
190 1217 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 1220 char **str_to_array_inhibitors(char *str)
199 {
200 1220 int nb_words = 0;
201 1220 int save = 0;
202 1220 int nb_letters = 0;
203 1220 char **array = NULL;
204
205
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1217 times.
1220 if (str == NULL)
206 3 return NULL;
207 1217 nb_words = count_words(str);
208 1217 array = malloc(sizeof(char *) * (nb_words + 1));
209
2/2
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 1217 times.
4042 for (int index = 0; index < nb_words; index++) {
210 2825 move_save(&save, str, index);
211 2825 nb_letters = count_letters(str, &save);
212 2825 array[index] = calloc(nb_letters + 1, sizeof(char));
213 2825 fill_str(array[index], nb_letters, save, str);
214 }
215 1217 array[nb_words] = NULL;
216 1217 array = separate_parentheses(array, nb_words);
217 1217 return array;
218 }
219