-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
109 lines (101 loc) · 2.49 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
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmasuda <mmasuda@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/27 23:21:10 by mmasuda #+# #+# */
/* Updated: 2021/04/29 23:20:59 by mmasuda ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
int ft_strchr_idx(const char *s, int c)
{
unsigned char char_c;
int i;
char_c = (char)c;
i = 0;
while (s[i] != '\0')
{
if (s[i] == char_c)
return (i);
i++;
}
if (s[i] == char_c)
return (i);
return (-1);
}
int ft_read(int fd, char **save)
{
char *buf;
char *tmp;
int ret;
ret = 0;
buf = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buf)
return (-1);
while (ret >= 0)
{
ret = read(fd, buf, BUFFER_SIZE);
if (ret >= 0)
buf[ret] = '\0';
if (ret == 0 || ret == -1)
break ;
tmp = ft_strjoin(*save, buf);
free(*save);
*save = tmp;
if ((ft_strchr_idx(*save, '\n')) >= 0)
break ;
}
free(buf);
if (ret > 0)
return (1);
return (ret);
}
char *ft_result_if_else(char **line, char *save, int result, int *tosave)
{
if (result < 0)
{
*line = NULL;
return (NULL);
}
else if (result == 0)
{
if (!save)
{
*line = ft_substr("", 0, 0);
return (NULL);
}
*tosave = ft_strlen(save);
return (NULL);
}
else
{
*tosave = ft_strchr_idx(save, '\n');
return (ft_strdup(save + (*tosave) + 1));
}
}
int get_next_line(int fd, char **line)
{
static char *save[256];
char *tmp;
int tosave;
int result;
result = 1;
tosave = 0;
if (fd < 0 || 255 < fd || !line || BUFFER_SIZE <= 0)
{
*line = NULL;
return (-1);
}
if (!save[fd] || ft_strchr_idx(save[fd], '\n') < 0)
result = ft_read(fd, &save[fd]);
tmp = ft_result_if_else(line, save[fd], result, &tosave);
if ((tosave == 0 && !tmp && !save[fd]) || result == -1)
return (result);
*line = ft_substr(save[fd], 0, tosave);
free(save[fd]);
save[fd] = tmp;
return (result);
}