-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
executable file
·110 lines (101 loc) · 2.52 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/03 11:09:56 by ebabaogl #+# #+# */
/* Updated: 2024/11/04 18:59:10 by ebabaogl ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
#include <unistd.h>
static char *update_buffer(char *buf, int index)
{
size_t i;
i = 0;
while (buf[index] && buf[index + 1] != '\0')
{
buf[i] = buf[index + 1];
index++;
i++;
}
while (buf[i])
buf[i++] = '\0';
return (buf);
}
static char *get_line(char *buf)
{
char *line;
size_t i;
i = 0;
if (!*buf)
return (NULL);
while (buf[i] && buf[i] != '\n')
i++;
if (buf[i] == '\n')
i++;
line = ft_calloc(i + 1, sizeof(char));
if (!line)
return (NULL);
i = 0;
while (buf[i] && buf[i] != '\n')
{
line[i] = buf[i];
i++;
}
if (!buf[i])
line[i] = '\0';
else if (buf[i] == '\n')
line[i] = '\n';
buf = update_buffer(buf, i);
return (line);
}
static char *read_file(int fd, char *buf)
{
char *tmp_str;
char *tmp_buf;
ssize_t r_bytes;
tmp_str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (!tmp_str)
return (free(buf), NULL);
while (1)
{
r_bytes = read(fd, tmp_str, BUFFER_SIZE);
if (r_bytes == -1)
return (free(tmp_str), NULL);
if (r_bytes == 0)
break ;
tmp_buf = ft_strjoin(buf, tmp_str);
if (!tmp_buf)
return (free(tmp_str), free(buf), NULL);
free(buf);
buf = tmp_buf;
ft_memset(tmp_str, 0, BUFFER_SIZE + 1);
if (ft_strchr(buf, '\n'))
break ;
}
return (free(tmp_str), buf);
}
char *get_next_line(int fd)
{
static char *buf;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) == -1)
return (free(buf), buf = NULL, NULL);
if (!buf)
{
buf = ft_calloc(1, 1);
if (!buf)
return (NULL);
}
buf = read_file(fd, buf);
if (!buf)
return (NULL);
line = get_line(buf);
if (!line)
return (free(buf), buf = NULL, NULL);
return (line);
}