-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
163 lines (149 loc) · 3.74 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnishsha <dnishsha@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/17 14:22:16 by dnishsha #+# #+# */
/* Updated: 2023/05/26 15:06:55 by dnishsha ### ########.fr */
/* */
/* ************************************************************************** */
/*
fd: The file descriptor to read from
Write a function that returns a line read from a
file descriptor
Return: Read line: correct behavior
NULL: there is nothing else to read, or an error occurred
*/
#include "get_next_line_bonus.h"
// Read from the file to the buffer and join them to a string until a
// new line or end of file occur in the buffer.
static char *fd_read(int fd, char *left_str)
{
int bytes_rd;
char *buf;
bytes_rd = 1;
buf = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (0);
while (bytes_rd != 0 && find_index(left_str, '\n') == -1)
{
bytes_rd = read(fd, buf, BUFFER_SIZE);
if (bytes_rd == -1)
{
free(left_str);
free (buf);
buf = NULL;
return (0);
}
buf[bytes_rd] = '\0';
left_str = str_join(left_str, buf);
}
free(buf);
buf = NULL;
return (left_str);
}
static char *get_line(char *left_str)
{
char *line;
int pos;
int i;
pos = find_index(left_str, '\n');
i = 0;
if (pos != -1)
line = (char *)malloc(sizeof(char) * (pos + 2));
else
line = (char *)malloc(sizeof(char) * (str_length(left_str) + 1));
if (!line)
return (0);
while (left_str[i] && left_str[i] != '\n')
{
line[i] = left_str[i];
i ++;
}
if (left_str[i] == '\n')
{
line[i] = left_str[i];
i ++;
}
line[i] = '\0';
return (line);
}
static char *get_residual_str(char *left_str)
{
char *residual_str;
int pos;
int i;
pos = find_index(left_str, '\n');
i = 0;
if (pos != -1)
residual_str = (char *)malloc(sizeof(char)
*(str_length(left_str) - pos));
else
residual_str = (char *)malloc(sizeof(char) * 1);
if (!residual_str)
return (0);
while (pos != -1 && left_str[pos + 1 + i])
{
residual_str[i] = left_str[pos + 1 + i];
i ++;
}
residual_str[i] = '\0';
free(left_str);
return (residual_str);
}
char *get_next_line(int fd)
{
static char *left_str[1000] = {NULL};
char *line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
left_str[fd] = fd_read(fd, left_str[fd]);
if (!left_str[fd])
return (0);
line = get_line(left_str[fd]);
if (!line || str_length(line) == 0)
{
free(left_str[fd]);
free(line);
left_str[fd] = NULL;
return (NULL);
}
left_str[fd] = get_residual_str(left_str[fd]);
return (line);
}
// Main file for testing
/*
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd1;
int fd2;
char *line[2];
int i;
fd1 = open("test.txt", O_RDONLY);
fd2 = open("test2.txt", O_RDONLY);
line[0] = get_next_line(fd1);
printf("Line : %s", line[0]);
free(line[0]);
line[1] = get_next_line(fd2);
printf("Line : %s", line[1]);
free(line[1]);
line[0] = get_next_line(fd1);
printf("Line : %s", line[0]);
free(line[0]);
line[1] = get_next_line(fd2);
printf("Line : %s", line[1]);
free(line[1]);
line[0] = get_next_line(fd1);
printf("Line : %s", line[0]);
free(line[0]);
line[1] = get_next_line(fd2);
printf("Line : %s", line[1]);
free(line[1]);
close(fd1);
close(fd2);
return (0);
}*/