-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
82 lines (75 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: natamazy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/31 16:38:23 by natamazy #+# #+# */
/* Updated: 2024/02/01 16:37:43 by natamazy ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strchr(const char *s, int c)
{
int i;
int len;
i = 0;
len = ft_strlen(s);
while (i <= len)
{
if (s[i] == (char) c)
return ((char *) &s[i]);
i++;
}
return (NULL);
}
char *ft_strjoin(char const *s1, char const *s2, size_t k)
{
char *r_s;
size_t i;
size_t j;
if (s1 == NULL || s2 == NULL)
return (NULL);
r_s = (char *) malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!r_s)
return (NULL);
i = 0;
j = 0;
while (s1[i])
{
r_s[j++] = s1[i];
i++;
}
i = 0;
while (s2[i] && k--)
{
r_s[j++] = s2[i];
i++;
}
r_s[j] = '\0';
return (r_s);
}
char *new_line_finder(char *buff, char **line)
{
char *new_line_address;
char *tmp;
int buff_len;
new_line_address = ft_strchr(buff, '\n');
if (new_line_address)
{
tmp = *line;
*line = ft_strjoin(*line, buff, new_line_address - buff + 1);
free(tmp);
ft_strlcpy(buff, new_line_address + 1, BUFFER_SIZE);
return (*line);
}
else
{
buff_len = ft_strlen(buff);
tmp = *line;
*line = ft_strjoin(*line, buff, buff_len);
free(tmp);
}
return (NULL);
}