-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
93 lines (84 loc) · 2.44 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebouabba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/05 19:54:36 by ebouabba #+# #+# */
/* Updated: 2021/12/05 20:45:38 by ebouabba ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_readfile_bs(char *var_static, int fd)
{
char *var;
int r;
var = malloc(BUFFER_SIZE + 1);
if (var == NULL)
return (NULL);
r = 1;
while (ft_backslash_bs(var_static) == 0 && r != 0)
{
r = read(fd, var, BUFFER_SIZE);
if (r == -1)
return (free(var), NULL);
if (r == 0)
break ;
var[r] = '\0';
var_static = ft_strjoin_bs(var_static, var);
}
free(var);
return (var_static);
}
char *ft_get_line_bs(char *var_static)
{
int count;
char *nev_var;
count = 0;
if (var_static[0] == '\0')
return (NULL);
while (var_static[count] != '\n' && var_static[count])
count++;
nev_var = malloc(count + 2);
if (nev_var == NULL)
return (NULL);
count = -1;
while (var_static[++count] && var_static[count] != '\n')
nev_var[count] = var_static[count];
if (var_static[count] == '\n')
nev_var[count++] = '\n';
nev_var[count] = '\0';
return (nev_var);
}
char *ft_flash_var_bs(char *var_static)
{
char *flash;
int count;
int j;
count = 0;
while (var_static[count] != '\n' && var_static[count])
count++;
if (var_static[count] == '\0')
{
free(var_static);
return (NULL);
}
j = ft_strlen_bs(var_static) - count;
flash = ft_substr_bs(var_static, count + 1, j);
free(var_static);
return (flash);
}
char *get_next_line(int fd)
{
char static *var_static[10240];
char *line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
var_static[fd] = ft_readfile_bs(var_static[fd], fd);
if (!var_static[fd])
return (NULL);
line = ft_get_line_bs(var_static[fd]);
var_static[fd] = ft_flash_var_bs(var_static[fd]);
return (line);
}