-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
163 lines (147 loc) · 4.32 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: apolo-to <apolo-to@student.42madrid> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 10:26:54 by apolo-to #+# #+# */
/* Updated: 2023/04/12 12:36:54 by apolo-to ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/**
* This ft read the txt file and obtain blocks of text with the size of
* BUFFER_SIZE. When the ft find '\n' stop and return the united text blocks.
* @param fd The file descriptor of the txt file.
* @param *static_line An static variable str to save the text obtained.
* @return A pointer to text obtained.
*/
char *ft_get_static_line(int fd, char *static_line)
{
char *buffer;
ssize_t bytes_readed;
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buffer)
return (NULL);
bytes_readed = 1;
while (!ft_strchr_gnl(static_line, '\n') && bytes_readed > 0)
{
bytes_readed = read(fd, buffer, BUFFER_SIZE);
if (bytes_readed == -1)
{
free (static_line);
free (buffer);
return (NULL);
}
buffer[bytes_readed] = '\0';
static_line = ft_strjoin(static_line, buffer);
}
free (buffer);
return (static_line);
}
/**
* This ft obtain the final line removing text after '\n' from static_line.
* Return a ptr to the final line obtained after clean the static line.
* @param *static_line Text obtained after use ft_get_static_line.
* @return A pointer to the final line.
*/
char *ft_get_final_line(char *static_line)
{
int i;
char *final_line;
i = 0;
if (!static_line[i])
return (NULL);
while (static_line[i] && static_line[i] != '\n')
i++;
final_line = malloc(sizeof(char) * (i + 2));
if (!final_line)
return (NULL);
i = 0;
while (static_line[i] && static_line[i] != '\n')
{
final_line[i] = static_line[i];
i++;
}
if (static_line[i] == '\n')
final_line[i++] = '\n';
final_line[i] = '\0';
return (final_line);
}
/**
* This ft obtain the stash removing text after the '\n' from static_line.
* Return a ptr to the stash obtained.
* @param static_line Text obtained after use ft_get_static_line.
* @return A pointer to stash.
*/
char *ft_get_static_stash(char *static_line)
{
char *stash;
int size_stash;
char *ptr_start_stash;
ptr_start_stash = ft_strchr_gnl(static_line, '\n');
if (!ptr_start_stash)
{
free(static_line);
return (NULL);
}
size_stash = (ft_strlen(ptr_start_stash) + 1);
stash = malloc(sizeof(char) * size_stash);
if (!stash)
return (NULL);
ft_strlcpy(stash, ptr_start_stash, size_stash);
free(static_line);
return (stash);
}
/**
* This ft returns a line of a document on each call.
* The ft shoud safe the reading point to avoid return always the same line.
* @param fd The file descriptor.
* @return A pointer to the final line to print.
*/
char *get_next_line(int fd)
{
static char *static_line[1024];
char *final_line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
static_line[fd] = ft_get_static_line(fd, static_line[fd]);
if (!static_line[fd])
return (NULL);
final_line = ft_get_final_line(static_line[fd]);
static_line[fd] = ft_get_static_stash(static_line[fd]);
return (final_line);
}
// int main(void)
// {
// int fd1;
// int fd2;
// int fd3;
// char *ptr1;
// char *ptr2;
// char *ptr3;
// fd1 = open("example1.txt", O_RDONLY);
// fd2 = open("example2.txt", O_RDONLY);
// fd3 = open("example3.txt", O_RDONLY);
// if (fd1 <= 0 || fd2 <= 0 || fd3 <= 0)
// return (0);
// while (1)
// {
// ptr1 = get_next_line(fd1);
// ptr2 = get_next_line(fd2);
// ptr3 = get_next_line(fd3);
// if (!ptr1 || !ptr2 || !ptr3)
// break;
// printf("\n%s", ptr1);
// printf("\n%s", ptr2);
// printf("\n%s", ptr3);
// free(ptr1);
// free(ptr2);
// free(ptr3);
// }
// close(fd1);
// close(fd2);
// close(fd3);
// return (0);
// }