-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_next_line.c
115 lines (108 loc) · 3.22 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
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajordan- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 10:12:14 by ajordan- #+# #+# */
/* Updated: 2021/10/20 10:04:09 by ajordan- ### ########.fr */
/* */
/* ************************************************************************** */
/*
* GET_NEXT_LINE
* -------------
* DESCRIPTION
* This function takes an opened file descriptor and returns its next line.
* This function has undefined behavior when reading from a binary file.
* PARAMETERS
* #1. A file descriptor
* RETURN VALUES
* If successful, get_next_line returns a string with the full line ending in
* a line break (`\n`) when there is one.
* If an error occurs, or there's nothing more to read, it returns NULL.
* ----------------------------------------------------------------------------
* AUXILIARY FUNCTIONS
* -------------------
* READ_TO_LEFT_STR
* -----------------
* DESCRIPTION
* Takes the opened file descriptor and saves on a "buff" variable what readed
* from it. Then joins it to the cumulative static variable for the persistence
* of the information.
* PARAMETERS
* #1. A file descriptor.
* #2. The pointer to the cumulative static variable from previous runs of
* get_next_line.
* RETURN VALUES
* The new static variable value with buffer joined for the persistence of the info,
* or NULL if error.
*/
#include "get_next_line.h"
#include <unistd.h>
//#include <stdio.h>
//#include <fcntl.h>
char *ft_read_to_left_str(int fd, char *left_str)
{
char *buff;
int rd_bytes;
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buff)
return (NULL);
rd_bytes = 1;
while (!ft_strchr(left_str, '\n') && rd_bytes != 0)
{
rd_bytes = read(fd, buff, BUFFER_SIZE);
if (rd_bytes == -1)
{
free(buff);
return (NULL);
}
buff[rd_bytes] = '\0';
left_str = ft_strjoin(left_str, buff);
}
free(buff);
return (left_str);
}
char *get_next_line(int fd)
{
char *line;
static char *left_str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
left_str = ft_read_to_left_str(fd, left_str);
if (!left_str)
return (NULL);
line = ft_get_line(left_str);
left_str = ft_new_left_str(left_str);
return (line);
}
/*int main(void)
{
char *line;
int i;
int fd1;
int fd2;
int fd3;
fd1 = open("tests/test.txt", O_RDONLY);
fd2 = open("tests/test2.txt", O_RDONLY);
fd3 = open("tests/test3.txt", O_RDONLY);
i = 1;
while (i < 7)
{
line = get_next_line(fd1);
printf("line [%02d]: %s", i, line);
free(line);
line = get_next_line(fd2);
printf("line [%02d]: %s", i, line);
free(line);
line = get_next_line(fd3);
printf("line [%02d]: %s", i, line);
free(line);
i++;
}
close(fd1);
close(fd2);
close(fd3);
return (0);
}*/