-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
82 lines (74 loc) · 1.45 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
#include "hdrs/get_next_line.h"
#include "lib/libft.h"
static char *ft_begin(const char *s)
{
char *s1;
int last;
last = 0;
if (s[last] == '\n' || s[last] == '\0')
return (ft_strdup(""));
else
while (*s && s[last] != '\0' && s[last] != '\n')
last++;
s1 = ft_substr(s, 0, last);
return (s1);
}
static char *ft_end(char *s)
{
char *s1;
int last;
last = 0;
while (s[last] != '\n' && s[last] != '\0')
last++;
if (s[last] == '\0')
{
free(s);
return (0);
}
s1 = ft_substr(s, last + 1, ft_strlen_gnl((char *)s) - last);
free(s);
return (s1);
}
static int save(char **ost, char **line, int byte)
{
*line = ft_begin(*ost);
*ost = ft_end(*ost);
while (ft_strchrr(*ost) && (byte == 0))
return (1);
if (byte == 0)
return (0);
else
return (1);
}
static int readfile(int fd, char *buf, char **ost, char **line)
{
int reading_byte;
if (!(*ost))
*ost = ft_strdup("");
reading_byte = 1;
while (!(ft_strchrr(*ost)) && ((reading_byte != 0)))
{
reading_byte = read(fd, buf, BUFFER_SIZE);
if (reading_byte == -1)
{
free(*ost);
*ost = NULL;
return (-1);
}
buf[reading_byte] = '\0';
*ost = ft_strjoin(*ost, buf);
}
free(buf);
return (save(&*ost, line, reading_byte));
}
int get_next_line(int fd, char **line)
{
char *buf;
static char *ost;
if ((fd < 0) || (!(line)) || (BUFFER_SIZE <= 0))
return (-1);
buf = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (!(buf))
return (-1);
return (readfile(fd, buf, &ost, line));
}