-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_tofill.c
109 lines (94 loc) · 2.55 KB
/
get_next_line_tofill.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_tofill.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vangirov <vangirov@student.42wolfsburg. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/25 22:14:42 by vangirov #+# #+# */
/* Updated: 2022/09/14 21:17:58 by vangirov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <unistd.h>
#include "libft.h"
#ifndef BUFFER_SIZE
# define BUFFER_SIZE 1
#endif
static int ft_read(int fd, char *buff, int size)
{
int rd;
rd = read(fd, buff, size);
buff[rd] = '\0';
return (rd);
}
static char *ft_concat(char *line, char *nonl_buff)
{
char *newline;
newline = ft_strjoin(line, nonl_buff);
free(line);
return (newline);
}
static char *ft_get_tail(char *line, char *buff)
{
size_t len;
char *tail;
char *rest;
rest = ft_strchr(buff, '\n');
if (!rest)
return (ft_concat(line, buff));
rest++;
len = rest - buff;
tail = (char *)malloc(sizeof(char) * len + 1);
ft_strlcpy(tail, buff, len + 1);
len = ft_strlen(rest);
ft_memmove(buff, rest, len + 1);
line = ft_concat(line, tail);
free(tail);
return (line);
}
char *get_next_line_tofill(int fd, char **to_fill)
{
static char buff[BUFFER_SIZE + 1];
char *line;
ssize_t rd;
if (fd < 0)
return (NULL);
line = (char *)malloc(1);
line[0] = '\0';
while (!ft_strchr(buff, '\n'))
{
line = ft_concat(line, buff);
rd = ft_read(fd, buff, BUFFER_SIZE);
if (!rd && line[0])
break ;
if (rd < 1)
{
free(line);
return (NULL);
}
}
line = ft_get_tail(line, buff);
*to_fill = line;
return (line);
}
// #define BUFFER_SIZE 1
// #include <fcntl.h>
// #include <stdio.h>
// int main()
// {
// int fd;
// char *ret;
// printf("===============================================================\n");
// fd = open("text", O_RDONLY);
// // for (int i = 0; (ret = get_next_line(fd)) && i <= 22; i++)
// while ((ret = get_next_line(fd)))
// {
// printf(">>>>>>>>>>>>>>>> %s", ret);
// free(ret);
// }
// free(ret);
// close(fd);
// return (0);
// }