-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
92 lines (82 loc) · 1.63 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
86
87
88
89
90
91
92
#include "get_next_line.h"
char *ft_strdup(const char *s1)
{
size_t i;
void *p;
i = ft_strlen(s1) + 1;
p = malloc(i * sizeof(char));
if (p == NULL)
return (NULL);
ft_strlcpy(p, s1, i);
return (p);
}
static int newline_handler(char **string, char **line)
{
int i;
char *temp_str;
i = 0;
while ((*string)[i] != '\n')
if ((*string)[i++] == '\0')
return (0);
(*string)[i] = 0;
*line = ft_strdup(*string);
temp_str = *string;
*string = ft_strdup(&(*string)[i + 1]);
free(temp_str);
return (1);
}
static int eof_handler(char **string, char **line)
{
char *temp_str;
if (*string == NULL)
{
*line = ft_strdup("");
return (0);
}
if (newline_handler(string, line))
return (1);
*line = ft_strdup(*string);
temp_str = *string;
free(temp_str);
*string = NULL;
return (0);
}
static int get_line(int fd, char **string, char *slice, char **line)
{
char *temp;
int read_val;
read_val = read(fd, slice, BUFFER_SIZE);
if (read_val == -1)
return (-1);
while (read_val > 0)
{
if (*string)
{
temp = *string;
*string = ft_strjoin(*string, slice);
free(temp);
}
else
*string = ft_strdup(slice);
if (newline_handler(string, line))
return (1);
ft_bzero(slice, BUFFER_SIZE + 1);
read_val = read(fd, slice, BUFFER_SIZE);
}
return (eof_handler(string, line));
}
int get_next_line(int fd, char **line)
{
char *slice;
static char *string;
int res;
if (!line || BUFFER_SIZE <= 0)
return (-1);
slice = (char *) malloc(sizeof(char) * BUFFER_SIZE + 1);
ft_bzero(slice, BUFFER_SIZE + 1);
res = get_line(fd, &string, slice, line);
free(slice);
if (*line == NULL)
return (-1);
return (res);
}