This repository was 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
/
Copy pathft_get_next_line.c
executable file
·119 lines (109 loc) · 3.04 KB
/
ft_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
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_get_next_line.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/11/11 15:30:52 by fbes #+# #+# */
/* Updated: 2022/03/09 14:03:28 by jgalloni ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
#include "libft.h"
static size_t ft_strnlen_nl(const char *s, size_t maxlen)
{
size_t i;
i = 0;
while (i < maxlen && s[i] != '\0' && s[i] != '\n')
i++;
return (i);
}
static void ft_strlcpy_nl(char *dest, const char *src, size_t size)
{
size_t i;
if (size != 0)
{
i = 0;
while (i < size && src[i] != '\n')
{
dest[i] = src[i];
i++;
}
}
}
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_strnlen_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);
}
/**
* Read from a file descriptor, line by line
* @param[in] fd The file descriptor to read from
* @param[in] **line A pointer to the string to inject the line into
* @return Returns 1 if there's more to read, 0 once the input has
* been read entirely, -1 on error
*/
int ft_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);
ft_free(buff);
if (read_bytes < 0 || handle_result < 0)
return (-1);
return (0);
}