-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
117 lines (107 loc) · 2.43 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
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moseddik <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/23 18:19:53 by moseddik #+# #+# */
/* Updated: 2021/12/01 05:02:08 by moseddik ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *save(char *str)
{
char *ptr;
int i;
int j;
i = 0;
while (str[i] && str[i] != '\n')
i++;
if (str[i] == '\0')
{
free (str);
return (NULL);
}
ptr = malloc(sizeof(char) * ft_strlen(str) - i);
i++;
j = 0;
while (str[i])
ptr[j++] = str[i++];
ptr[j] = '\0';
free (str);
return (ptr);
}
int ft_error(ssize_t byt_to_read, char *buff, char *ptr)
{
if (byt_to_read < 0)
{
free(buff);
if (ptr != NULL)
free(ptr);
return (1);
}
else if (byt_to_read == 0 && !ptr)
{
free(buff);
return (1);
}
else if (ptr[0] == '\0')
{
free(ptr);
free(buff);
return (1);
}
free (buff);
return (0);
}
char *read_caracters(int fd, char *str)
{
char *buff;
char *temp;
ssize_t byt_to_read;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
byt_to_read = read(fd, buff, BUFFER_SIZE);
while (byt_to_read > 0)
{
buff[byt_to_read] = '\0';
if (!str)
str = ft_strdup(buff);
else
{
temp = ft_strjoin(str, buff);
free (str);
str = temp;
}
if (ft_strchr(str, '\n'))
break ;
byt_to_read = read(fd, buff, BUFFER_SIZE);
}
if (ft_error(byt_to_read, buff, str))
return (0);
return (str);
}
char *remplis_line(char *str)
{
int len;
char *line;
len = 0;
while (str[len] != '\n' && str[len])
len++;
if (str[len] == '\n')
line = ft_substr(str, 0, len + 1);
else
line = ft_substr(str, 0, len);
return (line);
}
char *get_next_line(int fd)
{
static char *ptr;
char *line;
ptr = read_caracters(fd, ptr);
if (!ptr)
return (NULL);
line = remplis_line(ptr);
ptr = save(ptr);
return (line);
}