-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
108 lines (97 loc) · 2.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: deleusis <deleusis@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/13 16:32:21 by deleusis #+# #+# */
/* Updated: 2021/11/27 19:44:08 by deleusis ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *check_buf(int fd, char *b, int *i)
{
char *line;
int j;
int count;
if (b[*i] == '\n')
(*i)++;
if (b[*i] == '\0')
{
count = read(fd, b, BUFFER_SIZE);
if (count <= 0)
return (NULL);
b[count] = '\0';
*i = 0;
}
j = 0;
while (b[*i + j] && b[*i + j] != '\n')
j++;
line = ((char *)malloc(sizeof (char) * (j + 1 + (b[*i + j] == '\n'))));
return (line);
}
int ft_strcpy(char *b, char *line, int *i)
{
int j;
j = 0;
while (b[*i] && b[*i] != '\n')
line[j++] = b[(*i)++];
if (b[*i] == '\n')
line [j++] = '\n';
line[j] = '\0';
return (j);
}
char *get_line(char *b, int *i, int *j, char *line)
{
char *line2;
line2 = malloc (*j + 1 + (b[(*i) + (*j)] == '\n'));
if (!line2)
{
free (line);
return (NULL);
}
*j = 0;
while (b[*i] && b[*i] != '\n')
line2[(*j)++] = b[(*i)++];
if (b[*i] == '\n')
line2[(*j)++] = '\n';
line2[*j] = '\0';
return (ft_strjoin(line, line2));
}
char *ft_my_read(int fd, char *b, int *i, char *line)
{
int count;
int j;
count = 1;
j = ft_strlen(line);
while (b[*i] != '\n' && count)
{
count = read(fd, b, BUFFER_SIZE);
if (count < 0)
{
free (line);
return (NULL);
}
b[count] = '\0';
*i = 0;
while (b[(*i) + j] && b[(*i) + j] != '\n')
j++;
line = get_line(b, i, &j, line);
}
return (line);
}
char *get_next_line(int fd)
{
static char b[BUFFER_SIZE + 1];
static int i = 0;
char *line;
if (read (fd, (void *)0, 0) != 0 || BUFFER_SIZE <= 0)
return (NULL);
line = check_buf(fd, b, &i);
if (!line)
return (NULL);
ft_strcpy(b, line, &i);
line = ft_my_read(fd, b, &i, line);
return (line);
}