-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
107 lines (98 loc) · 2.21 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aizsak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/19 17:25:34 by aizsak #+# #+# */
/* Updated: 2022/12/11 12:29:24 by aizsak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *get_file(char *buff, int fd)
{
char *reader;
int i;
reader = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!reader)
return (NULL);
i = 1;
while (i > 0)
{
i = read(fd, reader, BUFFER_SIZE);
if (i < 0)
{
free(reader);
return (NULL);
}
reader[i] = '\0';
buff = ft_strjoin_gnl(reader, buff);
if (check(buff))
break ;
}
free(reader);
return (buff);
}
char *get_line(char *buff)
{
char *str;
int i;
i = 0;
if (buff[i] == 0)
return (NULL);
str = malloc(sizeof(char) * ft_strlen2(buff) + 1);
if (!str)
return (NULL);
while (buff[i] && buff[i] != '\n')
{
str[i] = buff[i];
i++;
}
if (buff[i] == '\n')
{
str[i] = '\n';
i++;
}
str[i] = 0;
return (str);
}
char *trimmed_buff(char *buff)
{
char *str;
int i;
int j;
int size;
i = 0;
j = 0;
size = ft_strlen1(buff) - ft_strlen2(buff);
if (!size)
{
free(buff);
return (NULL);
}
str = malloc(sizeof (char) * (size + 1));
if (!str)
return (NULL);
i = ft_strlen2(buff);
while (buff[i])
str[j++] = buff[i++];
str[j] = 0;
free(buff);
return (str);
}
char *get_next_line(int fd)
{
static char *buff;
char *str;
if (fd < 0 || BUFFER_SIZE < 0)
return (NULL);
buff = get_file(buff, fd);
if (!buff)
return (NULL);
str = get_line(buff);
buff = trimmed_buff(buff);
if (!str)
free(buff);
return (str);
}