Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** str_to_array.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
/* |
9 |
|
|
** EPITECH PROJECT, 2024 |
10 |
|
|
** myftp |
11 |
|
|
** File description: |
12 |
|
|
** str_to_array |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
#include <stdlib.h> |
16 |
|
|
#include <stdbool.h> |
17 |
|
|
|
18 |
|
60 |
static bool is_separator(char c, char const *separator) |
19 |
|
|
{ |
20 |
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 |
if (separator == NULL) |
21 |
|
|
return false; |
22 |
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 60 times.
|
240 |
for (size_t i = 0; separator[i]; i++) { |
23 |
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 |
if (separator[i] == c) |
24 |
|
|
return false; |
25 |
|
|
} |
26 |
|
|
return true; |
27 |
|
|
} |
28 |
|
|
|
29 |
|
2 |
static size_t my_cols_counter(char const *str, size_t i, char *separator) |
30 |
|
|
{ |
31 |
|
|
size_t word = 0; |
32 |
|
|
|
33 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 |
for (; str[i] != '\0' && !is_separator(str[i], separator); i++); |
34 |
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
16 |
for (; str[i] != '\0' && is_separator(str[i], separator); i++) |
35 |
|
14 |
word++; |
36 |
|
2 |
return word; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
4 |
static size_t count_lines(char const *str, char *separator) |
40 |
|
|
{ |
41 |
|
|
size_t n = 0; |
42 |
|
|
|
43 |
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
|
18 |
for (size_t i = 0; str[i] != '\0'; i++) { |
44 |
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 |
if ((is_separator(str[i], separator)) |
45 |
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 |
&& (!is_separator(str[i + 1], separator) |
46 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 |
|| str[i + 1] == '\0')) { |
47 |
|
2 |
n++; |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
4 |
return (n); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
2 |
static char *set_line(char const *str, size_t j, char *separator) |
54 |
|
|
{ |
55 |
|
2 |
size_t cols = my_cols_counter(str, j, separator); |
56 |
|
2 |
char *cpy = malloc(sizeof(char) * (cols + 1)); |
57 |
|
|
|
58 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!cpy) |
59 |
|
|
return NULL; |
60 |
|
2 |
cpy[cols] = '\0'; |
61 |
|
2 |
return cpy; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
4 |
char **str_to_array_separator(char const *str, char *separator) |
65 |
|
|
{ |
66 |
|
4 |
size_t lines = count_lines(str, separator); |
67 |
|
4 |
char **array = malloc(sizeof(char *) * (lines + 1)); |
68 |
|
|
size_t k = 0; |
69 |
|
|
size_t j = 0; |
70 |
|
|
|
71 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
if (array == NULL || str == NULL) |
72 |
|
|
return NULL; |
73 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 |
for (size_t i = 0; i < lines; i++) { |
74 |
|
2 |
array[i] = set_line(str, j, separator); |
75 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!array[i]) |
76 |
|
|
return NULL; |
77 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 |
for (; str[j] != '\0' && (!is_separator(str[j], separator)); j++); |
78 |
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
16 |
for (; str[j] != '\0' && is_separator(str[j], separator); j++) { |
79 |
|
14 |
array[i][k] = str[j]; |
80 |
|
14 |
k++; |
81 |
|
|
} |
82 |
|
|
k = 0; |
83 |
|
|
} |
84 |
|
4 |
array[lines] = NULL; |
85 |
|
4 |
return (array); |
86 |
|
|
} |
87 |
|
|
|