-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
142 lines (127 loc) · 3.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpeulet <mpeulet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/02 15:53:36 by mpeulet #+# #+# */
/* Updated: 2023/10/03 10:05:27 by mpeulet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
static char *ft_strjoin_gnl_bonus(char *s1, char *s2)
{
int i;
int j;
char *join;
if (!s1)
{
s1 = malloc(1 * sizeof(char));
s1[0] = 0;
}
if (!s1 || !s2)
return (free(s1), NULL);
join = malloc(((ft_strlen(s1) + ft_strlen(s2)) + 1) * sizeof(char));
if (!join)
return (free(s1), NULL);
i = -1;
j = 0;
if (s1)
while (s1[++i])
join[i] = s1[i];
while (s2[j])
join[i++] = s2[j++];
join[ft_strlen(s1) + ft_strlen(s2)] = 0;
free(s1);
return (join);
}
/* Reads from fd and add to the static varible */
static char *read_and_add_to_static_str_bonus(char *text_read, int fd)
{
int tt_de_lecture;
char *buffer;
buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buffer)
return (NULL);
tt_de_lecture = 1;
while (!ft_strchr(text_read, 10) && tt_de_lecture != 0)
{
tt_de_lecture = read(fd, buffer, BUFFER_SIZE);
if (tt_de_lecture == -1)
{
free(buffer);
return (NULL);
}
buffer[tt_de_lecture] = 0;
text_read = ft_strjoin_gnl_bonus(text_read, buffer);
}
free(buffer);
return (text_read);
}
/* Copies from the static and allocate a string to return from main functions */
static char *extract_to_line_bonus(char *text_read)
{
int i;
char *line;
i = 0;
if (!text_read[i])
return (NULL);
while (text_read[i] && text_read[i] != 10)
i++;
line = malloc((i + 2) * sizeof(char));
if (!line)
return (NULL);
i = 0;
while (text_read[i] && text_read[i] != 10)
{
line[i] = text_read[i];
i++;
}
if (text_read[i] == 10)
{
line[i] = text_read[i];
i++;
}
line[i] = 0;
return (line);
}
/* Allocates a new string to be read and initilize it to the start */
static char *update_static_str_bonus(char *text_read)
{
int i;
int j;
char *new_text_to_read;
i = 0;
while (text_read[i] && text_read[i] != 10)
i++;
if (!text_read[i])
{
free(text_read);
return (NULL);
}
new_text_to_read = malloc(sizeof(char) * (ft_strlen(text_read) - i + 1));
if (!new_text_to_read)
return (NULL);
i++;
j = 0;
while (text_read[i])
new_text_to_read[j++] = text_read[i++];
new_text_to_read[j] = 0;
free(text_read);
return (new_text_to_read);
}
/* Main function */
char *get_next_line(int fd)
{
char *line;
static char *text_read[MAX_FD];
if (fd < 0 || BUFFER_SIZE <= 0 || fd > MAX_FD)
return (NULL);
text_read[fd] = read_and_add_to_static_str_bonus(text_read[fd], fd);
if (!text_read[fd])
return (NULL);
line = extract_to_line_bonus(text_read[fd]);
text_read[fd] = update_static_str_bonus(text_read[fd]);
return (line);
}