-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
71 lines (64 loc) · 1.94 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mouaammo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 13:46:08 by mouaammo #+# #+# */
/* Updated: 2022/11/06 12:10:09 by mouaammo ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
#endif
char *get_next_line(int fd)
{
char *buffer;
static char *stash;
int bytes;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || BUFFER_SIZE >= 2147483647
|| read(fd, NULL, 0) < 0)
return (NULL);
line = NULL;
while (check_new_line(stash) == -1)
{
buffer = (char *) malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buffer)
return (NULL);
bytes = (int) read(fd, buffer, BUFFER_SIZE);
buffer[bytes] = 0;
stash = str_join(stash, buffer);
if (!bytes && !stash[0])
break ;
if (!bytes)
return (get_reminder(&stash, 0));
}
if (check_new_line(stash) != -1)
return (get_my_line(&stash, line, check_new_line(stash) + 1));
return (free(stash), stash = NULL, NULL);
}
int check_new_line(char *str)
{
int i;
if (!str)
return (-1);
i = 0;
while (str[i])
{
if (str[i] == '\n')
return (i);
i++;
}
return (-1);
}
char *get_my_line(char **temp, char *line, int ln)
{
if (!temp)
return (NULL);
line = ft_substr(*temp, 0, ln);
*temp = get_reminder(temp, ln);
return (line);
}