-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils3.c
113 lines (102 loc) · 2.43 KB
/
utils3.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amontign <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/24 08:08:01 by amontign #+# #+# */
/* Updated: 2023/07/25 09:31:05 by amontign ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
int ft_count_words(char const *s, int *lw_size)
{
int lg;
int nb_words;
lg = ft_strlen(s);
*lw_size = (lg % 100);
nb_words = (lg / 100) + (*lw_size > 0);
return (nb_words);
}
int alloc_tabs(char **res, int lw_size, int nb_words)
{
int i;
i = 0;
while (i < nb_words - 1)
{
res[i] = malloc(sizeof(char) * 101);
if (!res[i])
return (0);
i++;
}
if (lw_size > 0)
res[i] = malloc(sizeof(char) * (lw_size + 1));
else
res[i] = malloc(sizeof(char) * 101);
if (!res[i])
return (0);
return (1);
}
void complete_tab(int nb_words, int lw_size, char **res, const char *s)
{
int i;
int j;
i = 0;
while (i < nb_words)
{
if (lw_size > 0 && i == (nb_words - 1))
{
j = -1;
while (++j < lw_size)
res[i][j] = s[i * 100 + j];
res[i][j] = '\0';
}
else
{
j = -1;
while (++j < 100)
res[i][j] = s[i * 100 + j];
res[i][j] = '\0';
}
i++;
}
res[i] = NULL;
}
char **ft_split(char const *s)
{
char **res;
int nb_words;
int lw_size;
nb_words = ft_count_words(s, &lw_size);
res = malloc((nb_words + 1) * sizeof(char *));
if (!res)
return (NULL);
if (!alloc_tabs(res, lw_size, nb_words))
return (NULL);
complete_tab(nb_words, lw_size, res, s);
return (res);
}
int ft_atoi(const char *nptr)
{
int neg;
int i;
int res;
neg = 1;
i = 0;
res = 0;
while ((nptr[i] > 8 && nptr[i] < 14) || nptr[i] == ' ')
i++;
if (nptr[i] == '-' || nptr[i] == '+')
{
if (nptr[i] == '-')
neg = -1;
i++;
}
while (nptr[i] > 47 && nptr[i] < 58)
{
res = res * 10 + nptr[i] - 48;
i++;
}
return (res * neg);
}