-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
100 lines (91 loc) · 2.5 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jchene <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/23 14:30:43 by jchene #+# #+# */
/* Updated: 2020/02/06 16:43:27 by jchene ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdio.h>
char *eol_pos(char *buff)
{
int i;
i = 0;
while (buff[i] && buff[i] != '\n')
i++;
if (buff[i])
return (&buff[i]);
return (0);
}
int line_realloc(char *str, char **line)
{
char *buff;
int len;
if (!*line)
{
if (!(*line = (char *)malloc(sizeof(char) * (ft_strlen(str) + 1))))
return (-1);
ft_strcpyn(*line, str);
return (0);
}
len = ft_strlen(str) + ft_strlen(*line);
if (!(buff = (char *)malloc(sizeof(char) * (len + 1))))
return (-1);
ft_strcpyn(buff, *line);
ft_strcatn(buff, str);
free(*line);
*line = buff;
return (0);
}
int checkbuff(char *buff, char **line)
{
char *eol;
if (ft_strlen(buff))
{
eol = eol_pos(buff);
if (eol)
{
*eol = '\0';
if (line_realloc(buff, line))
return (-1);
ft_strcpyn(buff, (eol + 1));
return (1);
}
if (line_realloc(buff, line))
return (-1);
}
ft_memset(buff, 0, BUFFER_SIZE + 1);
return (0);
}
int get_next_line(int fd, char **line)
{
static char *statbuff[4096];
int check;
int count;
if (fd < 0 || line == NULL || BUFFER_SIZE == 0)
return (-1);
*line = NULL;
if (!statbuff[fd])
if (!(statbuff[fd] = (char *)ft_calloc(BUFFER_SIZE + 1, sizeof(char))))
return (-1);
if ((check = checkbuff(statbuff[fd], line)))
return ((check = 1) ? 1 : -1);
while ((count = read(fd, statbuff[fd], BUFFER_SIZE)))
{
if (count == -1)
return (-1);
statbuff[fd][count] = '\0';
if ((check = checkbuff(statbuff[fd], line)))
return ((check = 1) ? 1 : -1);
}
if (!*line)
if (!(*line = (char *)ft_calloc(1, sizeof(char))))
return (-1);
free(statbuff[fd]);
statbuff[fd] = NULL;
return (0);
}