Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_str_to_word_array_select | ||
4 | ** File description: | ||
5 | ** Returns an array of words delimited by a list of separator (separator) | ||
6 | ** from a string (str) | ||
7 | */ | ||
8 | /** | ||
9 | * @file my_str_to_word_array_select.c | ||
10 | * @brief The file containing the my_str_to_word_array_select function | ||
11 | * @author Nicolas TORO | ||
12 | */ | ||
13 | |||
14 | #include "my.h" | ||
15 | |||
16 | 120 | static int my_char_is_not_a_separator(char const c, char const *sep) | |
17 | { | ||
18 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 100 times.
|
300 | for (int i = 0; sep[i] != '\0'; i++) { |
19 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 180 times.
|
200 | if (sep[i] == c) |
20 | 20 | return 0; | |
21 | } | ||
22 | 100 | return 1; | |
23 | } | ||
24 | |||
25 | 2 | static void check_words_and_num(char const *str, int *sep, int *word, | |
26 | char const *separator) | ||
27 | { | ||
28 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
|
40 | for (int i = 0; str[i] != '\0'; i++) { |
29 |
4/4✓ Branch 1 taken 32 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 28 times.
|
38 | if (my_char_is_not_a_separator(str[i], separator) && *sep == 1) { |
30 | 4 | *word = *word + 1; | |
31 | 4 | *sep = 0; | |
32 | } | ||
33 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 32 times.
|
38 | if (my_char_is_not_a_separator(str[i], separator) == 0) |
34 | 6 | *sep = 1; | |
35 | } | ||
36 | 2 | return; | |
37 | } | ||
38 | |||
39 | 4 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre, | |
40 | char const *separator) | ||
41 | { | ||
42 | 4 | for (int i = which_lettre; str[i] != '\0' | |
43 |
4/4✓ Branch 0 taken 34 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 2 times.
|
36 | && my_char_is_not_a_separator(str[i], separator); i++) |
44 | 32 | *nbr_lettre = *nbr_lettre + 1; | |
45 | 4 | } | |
46 | |||
47 | 4 | static void reset_nbr_letter(char const *str, | |
48 | int *which_lettre, int *nbr_lettre, char const *separator) | ||
49 | { | ||
50 | 4 | for (; str[*which_lettre] != '\0' | |
51 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
6 | && my_char_is_not_a_separator(str[*which_lettre], separator) == 0; |
52 | 2 | *which_lettre = *which_lettre + 1); | |
53 | 4 | *nbr_lettre = 0; | |
54 | 4 | } | |
55 | |||
56 | 2 | static char *nbr_sep(char *str, char const *separator) | |
57 | { | ||
58 | 2 | int nbr_sep = 0; | |
59 | |||
60 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | for (int i = 0; my_char_is_not_a_separator(str[i], separator) == 0; i++) |
61 | 4 | nbr_sep++; | |
62 | 2 | return &str[nbr_sep]; | |
63 | } | ||
64 | |||
65 | 2 | static char **initialise_value(char **string, char const *str, | |
66 | int **info, char const *separator) | ||
67 | { | ||
68 | char **array_of_word; | ||
69 | |||
70 | 2 | *string = nbr_sep(*string, separator); | |
71 | 2 | check_words_and_num(str, info[1], info[0], separator); | |
72 | 2 | array_of_word = malloc(sizeof(char *) * ((*(info[0])) + 1)); | |
73 | 2 | return array_of_word; | |
74 | } | ||
75 | |||
76 | 2 | char **my_str_to_word_array_select(char const *str, char const *separator) | |
77 | { | ||
78 | 2 | int word = 0; | |
79 | 2 | int sep = 1; | |
80 | 2 | int *info[2] = {&word, &sep}; | |
81 | 2 | int nbr_lettre = 0; | |
82 | 2 | int which_lettre = 0; | |
83 | 2 | char *string = (char *)str; | |
84 | 2 | char **array_of_word = initialise_value(&string, str, info, separator); | |
85 | |||
86 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int elem = 0; elem < word; elem++) { |
87 | 4 | count_letter(string, which_lettre, &nbr_lettre, separator); | |
88 | 4 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
89 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
|
36 | for (int chara = 0; chara < nbr_lettre; chara++) { |
90 | 32 | array_of_word[elem][chara] = string[which_lettre]; | |
91 | 32 | which_lettre++; | |
92 | } | ||
93 | 4 | array_of_word[elem][nbr_lettre] = '\0'; | |
94 | 4 | reset_nbr_letter(string, &which_lettre, &nbr_lettre, separator); | |
95 | } | ||
96 | 2 | array_of_word[word] = NULL; | |
97 | 2 | return array_of_word; | |
98 | } | ||
99 |