-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
109 lines (99 loc) · 2.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elel-yak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/24 16:17:17 by elel-yak #+# #+# */
/* Updated: 2022/12/12 15:39:32 by elel-yak ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void shift_left(char *buffer, int nb)
{
int i;
i = 0;
if (nb == 0)
return ;
while ((i + nb) < BUFFER_SIZE)
{
buffer[i] = buffer[i + nb];
i++;
}
while (i < BUFFER_SIZE)
buffer[i++] = 0;
}
void change_buffer(char *buffer)
{
int count;
count = 0;
while (count < BUFFER_SIZE && buffer[count] && buffer[count] != '\n')
count++;
if (count == BUFFER_SIZE || buffer[count] == 0)
{
while (count--)
buffer[count] = 0;
}
else if (buffer[count] == '\n')
shift_left(buffer, count);
}
char *line_join(char *old_line, char *buffer)
{
int count;
int i;
char *new_line;
count = 0;
while (count < BUFFER_SIZE && buffer[count] && buffer[count] != '\n')
count++;
if (count < BUFFER_SIZE && buffer[count] == '\n')
count++;
new_line = malloc((ft_strlen(old_line) + count + 1) * sizeof(char));
if (!new_line)
return (ft_free(old_line));
i = -1;
while (old_line && old_line[++i])
new_line[i] = old_line[i];
if (!old_line)
i++;
new_line[i + count] = 0;
while (count--)
new_line[i + count] = buffer[count];
change_buffer(buffer);
ft_free (old_line);
return (new_line);
}
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE];
char *line;
int nb_read;
line = NULL;
if (fd >= 0 && buffer[0])
line = line_join(line, buffer);
while (fd >= 0)
{
if (buffer[0] == '\n')
{
shift_left(buffer, 1);
break ;
}
nb_read = read(fd, buffer, BUFFER_SIZE);
if (nb_read == -1)
return (ft_free(line));
if (nb_read == 0)
return (line);
line = line_join(line, buffer);
if (!line)
return (NULL);
}
return (line);
}
int main()
{
char *line;
int fd = open("./file", O_RDONLY);
while ((line = get_next_line(fd)))
printf("%s", line);
return (0);
}