-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
128 lines (116 loc) · 2.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lucavall <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/19 14:10:17 by lucavall #+# #+# */
/* Updated: 2024/01/10 14:32:29 by lucavall ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
static char *fd_reader(int fd)
{
char *line;
int chars_read;
line = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!line)
return (NULL);
chars_read = read(fd, line, BUFFER_SIZE);
if (chars_read <= 0)
{
free(line);
return (NULL);
}
line[chars_read] = '\0';
return (line);
}
static char *process_line(char **buffer)
{
char *line;
char *left_chars;
int len;
len = 0;
while (((*buffer)[len] != '\n') && ((*buffer)[len] != '\0'))
len++;
if ((*buffer)[len] == '\n')
{
line = ft_substr(*buffer, 0, len + 1);
left_chars = ft_substr(*buffer, len +1, ft_strlen(*buffer) - (len +1));
free(*buffer);
*buffer = left_chars;
return (line);
}
return (NULL);
}
static char *get_next_line_helper(char **buffer, int fd)
{
char *tmp_buffer;
char *line_read;
char *line;
line = process_line(buffer);
if (line)
return (line);
line_read = fd_reader(fd);
if (!line_read)
{
line = ft_strdup(*buffer);
free(*buffer);
*buffer = NULL;
if (*line)
return (line);
free(line);
return (NULL);
}
tmp_buffer = ft_strjoin(*buffer, line_read);
free(*buffer);
*buffer = tmp_buffer;
free(line_read);
return (get_next_line_helper(buffer, fd));
}
char *get_next_line(int fd)
{
static char *buffer[MAX_FD];
if (fd < 0 || fd >= MAX_FD || BUFFER_SIZE <= 0 || read(fd, NULL, 0) < 0)
{
free(buffer[fd]);
buffer[fd] = NULL;
return (NULL);
}
if (!buffer[fd])
buffer[fd] = ft_strdup("");
return (get_next_line_helper(&buffer[fd], fd));
}
/* #include <fcntl.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int fd[argc - 1];
char *line;
int i;
if (argc < 2)
{
printf("Usage: %s <filename1> <filename2> ...\n", argv[0]);
return (1);
}
for (i = 1; i < argc; i++)
{
fd[i - 1] = open(argv[i], O_RDONLY);
if (fd[i - 1] == -1)
return (1);
}
for (i = 0; i < argc - 1; i++)
{
printf("Reading file %s:\n", argv[i + 1]);
line = get_next_line(fd[i]);
while (line != NULL)
{
printf("%s\n", line);
free(line);
line = get_next_line(fd[i]);
}
close(fd[i]);
}
return (0);
} */