-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
135 lines (123 loc) · 2.95 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybolivar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/29 17:19:34 by ybolivar #+# #+# */
/* Updated: 2022/07/05 14:39:06 by ybolivar ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
static char *ft_join_and_free(char *buffer, char *sub_bufer)
{
char *tmp;
tmp = gnl_strjoin(buffer, sub_bufer);
free(buffer);
return (tmp);
}
static char *ft_clean_buffer(char *buffer)
{
int n;
int m;
char *sub_buffer;
n = 0;
m = 0;
if (!buffer)
return (NULL);
while (buffer[n] != '\n' && buffer[n])
n++;
if (!buffer[n])
{
free(buffer);
return (NULL);
}
sub_buffer = gnl_calloc(gnl_strlen(buffer) - n + 1, sizeof(char));
n++;
while (buffer[n])
sub_buffer[m++] = buffer[n++];
free(buffer);
return (sub_buffer);
}
static char *ft_get_line(char *buffer)
{
char *line;
int n;
n = 0;
if (!buffer[n])
return (NULL);
while (buffer[n] != '\n' && buffer[n])
n++;
line = gnl_calloc(n + 2, sizeof(char));
n = 0;
while (buffer[n] != '\n' && buffer[n])
{
line[n] = buffer[n];
n++;
}
if (buffer[n] == '\n' && buffer[n])
line[n] = '\n';
return (line);
}
static char *ft_read_file(int fd, char *buffer)
{
int read_bytes;
char *sub_buffer;
read_bytes = 1;
if (!buffer)
buffer = gnl_calloc(1, 1);
sub_buffer = gnl_calloc(sizeof(char), BUFFER_SIZE + 1);
while (read_bytes > 0)
{
read_bytes = read(fd, sub_buffer, BUFFER_SIZE);
if (read_bytes == -1)
{
free(buffer);
free(sub_buffer);
return (NULL);
}
sub_buffer[read_bytes] = '\0';
buffer = ft_join_and_free(buffer, sub_buffer);
if (gnl_strchr(sub_buffer, '\n'))
break ;
}
free(sub_buffer);
return (buffer);
}
char *get_next_line(int fd)
{
static char *buffer[FOPEN_MAX];
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
return (NULL);
buffer[fd] = ft_read_file(fd, buffer[fd]);
if (!buffer[fd])
return (NULL);
line = ft_get_line(buffer[fd]);
buffer[fd] = ft_clean_buffer(buffer[fd]);
return (line);
}
/*
int main(void)
{
int fd;
char *line;
int n;
n = 0;
fd = open("../alice29.txt", O_RDONLY);
if (fd < 0)
{
printf("Error opening source file.\n");
return (1);
}
while (n < 5)
{
line = get_next_line(fd);
printf("%s", line);
n++;
}
if (close(fd) != 0)
printf("Error closing source file.\n");
return (0);
}*/