-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
115 lines (106 loc) · 2.55 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
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eschirni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/06 21:16:35 by eschirni #+# #+# */
/* Updated: 2021/09/07 14:34:18 by eschirni ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static char *ft_delall(char **a)
{
free(*a);
*a = NULL;
return (NULL);
}
static char *ft_append(char *start, char *end)
{
size_t i;
size_t j;
char *tmp;
tmp = malloc(ft_strclen(start, '\0') + ft_strclen(end, '\0') + 1);
if (tmp == NULL)
return (NULL);
i = 0;
while (start[i] != '\0')
{
tmp[i] = start[i];
i++;
}
j = 0;
while (end[j] != '\0')
{
tmp[i] = end[j];
i++;
j++;
}
tmp[i] = '\0';
free(start);
return (tmp);
}
static void ft_reading(char **rest, int fd)
{
char *buffer;
int val;
buffer = ft_calloc(BUFFER_SIZE + 1);
val = read(fd, buffer, BUFFER_SIZE);
while (val > 0)
{
buffer[val] = '\0';
if (*rest == NULL)
*rest = ft_strcdup(buffer, '\0', 0);
else
*rest = ft_append(*rest, buffer);
if (ft_strchr(*rest, '\n') != NULL)
break ;
val = read(fd, buffer, BUFFER_SIZE);
}
if (val < 0)
ft_delall(rest);
ft_delall(&buffer);
}
char *get_next_line(int fd)
{
static char *rest;
char *temp;
char *ret;
if (fd < 0)
return (NULL);
ft_reading(&rest, fd);
if (rest == NULL || rest[0] == '\0')
return (NULL);
else if (ft_strchr(rest, '\n') != NULL)
{
ret = ft_strcdup(rest, '\n', 0);
temp = ft_strcdup(rest, '\0', ft_strclen(rest, '\n') + 1);
free(rest);
rest = temp;
if (rest[0] == '\0')
ft_delall(&rest);
}
else
{
ret = ft_strcdup(rest, '\0', 0);
ft_delall(&rest);
}
return (ret);
}
// #include <stdio.h>
// #include <fcntl.h>
// int main()
// {
// int fd;
// char *str;
// fd = open("abc.txt",O_RDONLY);
// while ((str = get_next_line(fd)))
// {
// printf("%s",str);
// free(str);
// }
// free(str);
// printf("\n");
// system("leaks a.out");
// }