-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
124 lines (115 loc) · 2.84 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
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajaidi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/14 09:40:22 by ajaidi #+# #+# */
/* Updated: 2021/11/22 01:50:41 by ajaidi ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void send_line(char *buffer, char **buffer_reminder, int l, char **rtn)
{
int i;
char *temp;
i = ft_strchr(buffer, '\n');
if (i < l)
{
if (*buffer_reminder)
{
temp = ft_substr(buffer, i + 1, (l - i - 1));
free(*buffer_reminder);
*buffer_reminder = temp;
}
else if (buffer[i + 1] != 0)
*buffer_reminder = ft_substr(buffer, i + 1, (l - i - 1));
}
buffer[i + 1] = 0;
if (!(*rtn))
*rtn = ft_strdup(buffer);
else
{
temp = ft_strjoin(*rtn, buffer);
free(*rtn);
*rtn = temp;
}
}
int get_line(char *buffer, char **buffer_reminder, int l, char **rtn)
{
int i;
char *temp;
buffer[l] = 0;
i = ft_strchr(buffer, '\n');
if (i > -1)
{
send_line(buffer, buffer_reminder, l, rtn);
return (1);
}
else
{
if (!*rtn)
*rtn = ft_strdup(buffer);
else
{
temp = ft_strjoin(*rtn, buffer);
free(*rtn);
*rtn = temp;
}
return (0);
}
return (0);
}
int check_reminder(char **buffer_reminder, char **rtn)
{
int i;
char *temp;
int size;
size = ft_strlen(*buffer_reminder);
i = ft_strchr(*buffer_reminder, '\n');
if (i > -1)
{
if (i < size - 1)
{
*rtn = ft_substr(*buffer_reminder, 0, i + 1);
temp = ft_substr(*buffer_reminder, i + 1, (size - i - 1));
free(*buffer_reminder);
*buffer_reminder = temp;
return (1);
}
*rtn = *buffer_reminder;
*buffer_reminder = NULL;
return (1);
}
temp = ft_strjoin("", *buffer_reminder);
free(*buffer_reminder);
*buffer_reminder = NULL;
*rtn = temp;
return (0);
}
char *get_next_line(int fd)
{
char *buffer;
static char *buffer_reminder;
int l;
char *rtn;
rtn = NULL;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (buffer_reminder && check_reminder(&buffer_reminder, &rtn))
return (rtn);
buffer = malloc(BUFFER_SIZE + 1);
l = read(fd, buffer, BUFFER_SIZE);
while (l > 0)
{
if (get_line(buffer, &buffer_reminder, l, &rtn))
{
free(buffer);
return (rtn);
}
l = read(fd, buffer, BUFFER_SIZE);
}
free(buffer);
return (rtn);
}