-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
111 lines (100 loc) · 2.51 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
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hel-mefe <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 15:09:46 by hel-mefe #+# #+# */
/* Updated: 2021/12/02 12:48:24 by hel-mefe ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int in_buffer(char *buffer)
{
size_t i;
i = 0;
while (buffer[i])
{
if (buffer[i] == '\n')
return (1);
i++;
}
return (0);
}
void ft_switch_everything_left(char *buffer)
{
size_t i;
size_t len;
i = 0;
len = ft_strlen(buffer);
if (in_buffer(buffer) && buffer[len] != 0)
{
while (buffer[len] != 0)
{
buffer[i] = buffer[len];
i++;
len++;
}
ft_bzero(buffer, i, len);
}
else
ft_bzero(buffer, 0, len);
}
char *ft_read_file(int fd, char *buffer)
{
char *line;
char *keep_line;
size_t bytes;
line = NULL;
if (buffer[0] != 0)
line = ft_strjoin(line, buffer);
while (!in_buffer(buffer))
{
ft_bzero(buffer, 0, BUFFER_SIZE);
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes < 0 || (buffer[0] == 0 && bytes == 0 && line == NULL))
return (NULL);
if (bytes == 0 && line != NULL)
return (line);
keep_line = line;
line = ft_strjoin(line, buffer);
if (!line)
return (NULL);
if (keep_line)
free(keep_line);
}
return (line);
}
char *ft_treat_all_cases(int fd, char *buffer)
{
char *line;
line = NULL;
if (!in_buffer(buffer) || buffer[0] == 0)
{
line = ft_read_file(fd, buffer);
if (!line)
return (NULL);
ft_switch_everything_left(buffer);
}
else if (in_buffer(buffer))
{
line = ft_substr(buffer, 0, ft_strlen(buffer));
if (!line)
return (NULL);
ft_switch_everything_left(buffer);
return (line);
}
return (line);
}
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1];
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, buffer, 0) < 0)
return (NULL);
line = ft_treat_all_cases(fd, buffer);
if (!line)
return (NULL);
return (line);
}