-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
142 lines (132 loc) · 3.08 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/01 09:39:52 by jvan-hal #+# #+# */
/* Updated: 2023/10/10 13:39:05 by jvan-hal ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static int getstart(char **newline, char **leftstr)
{
int nlindex;
nlindex = copytomem(newline, *leftstr, 0, gnl_strlen(*leftstr));
if (nlindex == -2)
{
free(*leftstr);
free(*newline);
return (0);
}
if (nlindex > -1)
{
shiftmem(*leftstr, nlindex + 1);
if (*leftstr[0] == '\0')
{
free(*leftstr);
*leftstr = NULL;
}
return (2);
}
else
{
free(*leftstr);
*leftstr = NULL;
}
return (1);
}
static int checkreaderror(int bytesread, char **newline, char **leftstr)
{
if (bytesread == -1)
{
if (*leftstr)
{
free(*leftstr);
*leftstr = NULL;
}
if (*newline)
{
free(*newline);
*newline = NULL;
}
return (1);
}
return (0);
}
static char *copyleftstr(char *buffer, int bytesread, int nlindex)
{
int i;
char *leftstr;
leftstr = ft_calloc(bytesread - nlindex, sizeof(char));
if (!leftstr)
return (NULL);
i = 0;
while (i < bytesread - nlindex - 1)
{
leftstr[i] = buffer[nlindex + i + 1];
++i;
}
leftstr[bytesread - nlindex - 1] = '\0';
return (leftstr);
}
static int processbuffer(char **newline, char *buffer, char **leftstr,
int bytesread)
{
int nlindex;
if (checkreaderror(bytesread, newline, leftstr) == 1)
return (1);
if (bytesread == 0)
return (1);
buffer[bytesread] = '\0';
nlindex = copytomem(newline, buffer, 0, bytesread);
if (nlindex == -2)
{
free(*leftstr);
free(*newline);
return (1);
}
else if (nlindex > -1)
{
if (nlindex < bytesread - 1)
{
*leftstr = copyleftstr(buffer, bytesread, nlindex);
if (!*leftstr)
free(*newline);
}
return (1);
}
return (0);
}
char *get_next_line(int fd)
{
static char *leftstr[FOPEN_MAX];
char buffer[BUFFER_SIZE + 1];
char *newline;
int bytesread;
int nlindex;
if (fd < 0 || fd >= FOPEN_MAX || BUFFER_SIZE == 0)
return (NULL);
newline = NULL;
bytesread = 1;
if (leftstr[fd])
{
nlindex = getstart(&newline, &leftstr[fd]);
if (nlindex == 0)
return (NULL);
else if (nlindex == 2)
bytesread = 0;
}
while (bytesread > 0)
{
bytesread = read(fd, buffer, BUFFER_SIZE);
if (processbuffer(&newline, buffer, &leftstr[fd], bytesread) == 1)
break ;
}
return (newline);
}