-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_to_word_array.c
More file actions
87 lines (76 loc) · 1.74 KB
/
Copy pathstr_to_word_array.c
File metadata and controls
87 lines (76 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
** EPITECH PROJECT, 2023
** amogud
** File description:
** zzz
*/
#include "my.h"
int count_spacec(char *str)
{
int i = 0;
for (i = 0; str[i] != '\0' && (str[i] == ' ' || str[i] == ';'); i++);
return i;
}
int calc_nb_wordc(char *str)
{
int nb_word = 1;
int i = 0;
int quote = 0;
for (i = 0; str[i]; i++) {
if (str[i] == '"' && quote == 0) {
quote = 1;
continue;
}
if (((str[i] != ';' && str[i + 1] == ';') ||
(str[i] != ';' && str[i + 1] == '\0')) && !quote)
nb_word++;
if (str[i] == '"' && quote == 1)
quote = 0;
}
return nb_word;
}
int calc_len_wordc(char *str)
{
int quote = 0;
int i = 0;
for (i = 0; str[i]; i++) {
if (str[i] == '"' && quote == 0) {
quote = 1;
continue;
}
if (str[i] == ';' && quote == 0) {
str[i] = ' ';
break;
}
if (str[i] == '"' && quote == 1)
quote = 0;
}
return i;
}
int stwa2c(char *str, char **tab, int i, int j)
{
int pass = 0;
tab[i][j] = str[j];
return pass;
}
char **stwac(char *str)
{
int nb_word = calc_nb_wordc(str + count_spacec(str));
int len_word = calc_len_wordc(str + count_spacec(str));
int i = 0;
int j = 0;
char **tab = malloc(sizeof(char *) * (nb_word + 1));
str += count_spacec(str);
for (i = 0; i < nb_word; i++) {
tab[i] = malloc(sizeof(char) * (len_word + 1));
for (j = 0; j < len_word; j++)
str += stwa2c(str, tab, i, j);
tab[i][j] = '\0';
if (str[j] == ';')
str++;
str += len_word;
len_word = calc_len_wordc(str);
}
tab[i] = NULL;
return tab;
}