This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
85 lines (79 loc) · 2.39 KB
/
get_next_line.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line.c :+: :+: */
/* +:+ */
/* By: fbes <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/11 15:30:52 by fbes #+# #+# */
/* Updated: 2020/11/18 17:20:36 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int init_gnl(int fd, char **line, void **buff, ssize_t *read_bytes)
{
if (fd < 0 || BUFFER_SIZE < 1 || line == NULL)
return (-1);
if (!*buff)
{
*buff = ft_calloc(sizeof(char), BUFFER_SIZE);
if (!*buff)
return (-1);
*read_bytes = read(fd, *buff, BUFFER_SIZE);
}
*line = NULL;
return (0);
}
static int handle_buff(char **line, size_t *size,
char *buff, size_t read_bytes)
{
static size_t start;
char *temp;
size_t bytes_to_copy;
bytes_to_copy = ft_strlen_nl(buff + start, read_bytes - start);
temp = (char *)ft_calloc(sizeof(char), (*size + bytes_to_copy + 1));
if (!temp)
return (-1);
if (*line)
{
ft_strlcpy_nl(temp, *line, *size);
free(*line);
}
if (bytes_to_copy > 0)
ft_strlcpy_nl(temp + *size, buff + start, bytes_to_copy);
*line = temp;
*size += bytes_to_copy;
if (bytes_to_copy < read_bytes - start)
{
start += bytes_to_copy + 1;
return (1);
}
ft_bzero(buff, BUFFER_SIZE);
start = 0;
return (0);
}
int get_next_line(int fd, char **line)
{
static void *buff;
static ssize_t read_bytes;
size_t size;
int handle_result;
if (init_gnl(fd, line, &buff, &read_bytes) == -1)
return (-1);
size = 0;
while (read_bytes > 0)
{
handle_result = handle_buff(line, &size, buff, read_bytes);
if (handle_result > 0)
return (1);
else if (handle_result < 0)
break ;
read_bytes = read(fd, buff, BUFFER_SIZE);
}
if (!*line)
*line = ft_calloc(sizeof(char), 1);
free(buff);
if (read_bytes < 0 || handle_result < 0)
return (-1);
return (0);
}