-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
104 lines (95 loc) · 2.08 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybachar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 16:53:37 by ybachar #+# #+# */
/* Updated: 2022/12/05 16:07:58 by ybachar ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ft_strlen(char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strjoin(char *s1, char *s2)
{
char *str;
int i;
int j;
if (!s2 && !s1)
return (NULL);
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2)) + 1);
if (!str)
return (NULL);
i = 0;
while (s1 && s1[i] != '\0')
{
str[i] = s1[i];
i++;
}
j = 0;
while (s2 && s2[j] != '\0')
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
free(s1);
return (str);
}
char *get_ligne(char *str)
{
char *res;
int i;
i = 0;
while (str[i] && str[i] != '\n')
i++;
res = malloc(sizeof(char) * i + 2);
i = 0;
while (str[i] != '\n' && str[i] != '\0')
{
res[i] = str[i];
i++;
}
if (str[i] == '\n')
res[i++] = '\n';
res[i] = '\0';
return (res);
}
char *get_extra(char *str)
{
int k;
char *extra;
int j;
k = 0;
while (str[k] && str[k] != '\n')
k++;
if (str[k] == '\0' || (str[k] == '\n' && str[k + 1] == '\0'))
{
free(str);
return (NULL);
}
extra = malloc(sizeof(char) * (ft_strlen(str) - k + 1));
j = 0;
k = k + 1;
while (str[k] != '\0')
{
extra[j] = str[k];
j++;
k++;
}
extra[j] = '\0';
free(str);
return (extra);
}