-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
123 lines (112 loc) · 2.38 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yvann <yvann@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/21 10:09:56 by ybarbot #+# #+# */
/* Updated: 2023/11/29 09:31:25 by yvann ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *handle_line(char **backup)
{
char *line;
char *line_end;
char *new_backup;
line = create_new_line(backup);
line_end = ft_strchr(*backup, '\n');
if (line_end != NULL && *(line_end + 1) != '\0')
{
new_backup = ft_strdup(line_end + 1);
free(*backup);
*backup = new_backup;
}
else
{
free(*backup);
*backup = NULL;
}
return (line);
}
char *ft_strjoin(char *s1, char *s2)
{
char *full_s;
size_t i;
size_t j;
if (s1 == NULL || s2 == NULL)
return (NULL);
i = 0;
while (s1[i])
i++;
j = 0;
while (s2[j])
j++;
full_s = malloc ((i + j + 1) * sizeof(char));
if (full_s == NULL)
return (NULL);
i = -1;
while (s1[++i])
full_s[i] = s1[i];
j = -1;
while (s2[++j])
full_s[i++] = s2[j];
full_s[i] = '\0';
return (full_s);
}
char *ft_strchr(const char *str, int c)
{
int i;
if (str == NULL)
return (NULL);
i = 0;
while (str[i] != '\0')
{
if ((unsigned char)str[i] == (unsigned char)c)
return ((char *)&str[i]);
i++;
}
if ((unsigned char)c == '\0')
return ((char *)&str[i]);
return (NULL);
}
char *ft_strndup(char *s, size_t len)
{
size_t i;
size_t j;
char *s2;
i = 0;
while (s[i] && i < len)
i++;
s2 = malloc(i + 1);
if (s2 == NULL)
return (NULL);
j = 0;
while (j < i)
{
s2[j] = s[j];
j++;
}
s2[i] = '\0';
return (s2);
}
char *ft_strdup(char *s)
{
size_t i;
char *s2;
i = 0;
while (s[i])
i++;
s2 = malloc(i + 1);
if (s2 == NULL)
return (NULL);
i = 0;
while (s[i])
{
s2[i] = s[i];
i++;
}
s2[i] = '\0';
return (s2);
}