-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
69 lines (63 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jquintin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/10 14:15:45 by jquintin #+# #+# */
/* Updated: 2022/11/22 09:37:47 by jquintin ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *buf_leftover(char *line, char *buf, int *line_found)
{
while (*buf)
{
line = save_line(line, buf, buf, line_found);
if (line_found || *buf)
break ;
}
return (line);
}
char *save_line(char *line, char *new_buf, char *buf, int *line_found)
{
char *cache;
int i;
i = 0;
cache = (char *)malloc(sizeof(char) * (mem_len(line) + mem_len(buf) + 1));
if (!cache)
return (NULL);
while (line && line[i])
{
cache[i] = line[i];
i++;
}
while (*buf)
{
if (*line_found == 1)
*new_buf++ = *buf;
else
cache[i++] = *buf;
if (*buf == '\n')
*line_found = 1;
*buf++ = 0;
}
cache[i] = '\0';
free(line);
return (cache);
}
ssize_t mem_len(char *s)
{
ssize_t len;
if (!s || !*s)
return (0);
len = 0;
while (s[len])
{
if (s[len] == '\n')
return (len + 1);
len++;
}
return (len);
}