-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
131 lines (123 loc) · 2.91 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 12:02:35 by fvieira #+# #+# */
/* Updated: 2022/11/14 12:02:38 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdio.h>
char *backtoleft(char *s1, int offset)
{
int count;
char *array;
count = 0;
if (s1[offset + 1] == '\0' || offset == -1)
{
free(s1);
return (NULL);
}
array = malloc((ft_strlen(s1) - offset + 1) * sizeof(char));
while (s1[count + offset + 1] != '\0')
{
array[count] = s1[count + offset + 1];
count++;
}
array[count] = '\0';
free(s1);
return (array);
}
int ft_alt_strchr(const char *s)
{
int count;
count = 0;
while (s[count] != '\0')
{
if (s[count] == '\n')
return (count);
count++;
}
return (-1);
}
char *get_next_line(int fd)
{
ssize_t byte;
char *lido;
static char *notready;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
lido = ft_calloc((BUFFER_SIZE + 1), sizeof(char));
byte = 1;
while (ft_alt_strchr(lido) == -1 && byte > 0)
{
byte = read(fd, lido, BUFFER_SIZE);
if (!lido || (byte <= 0 && !notready))
{
free(lido);
return (NULL);
}
lido[byte] = '\0';
notready = ft_alt_strjoin(notready, lido);
}
if (ft_alt_strchr(notready) > -1)
lido = ft_alt_strdup(lido, notready, ft_alt_strchr(notready) + 2);
else
lido = ft_alt_strjoin(lido, notready);
notready = backtoleft(notready, ft_alt_strchr(notready));
return (lido);
}
/*#include <fcntl.h>
int main(void)
{
int fd;
char *prt;
fd = open("files/test.txt", O_RDONLY);
prt = get_next_line(fd);
printf("\nFunc Return: %s\n", prt);
free(prt);
prt = get_next_line(fd);
printf("\nFunc Return: %s\n", prt);
free(prt);
prt = get_next_line(fd);
printf("\nFunc Return: %s\n", prt);
free(prt);
close(fd);
}*/
/*#include <fcntl.h>
int main(void)
{
int fd;
char *prt;
int i = 0;
fd = open("files/1-brouette.txt", O_RDONLY);
printf("fd = %d\n", fd);
while (i < 80)
{
prt = get_next_line(fd);
printf("%s", prt);
free(prt);
i++;
}
close(fd);
}*/
/*#include <fcntl.h>
int main(void)
{
int fd;
char *prt;
int i = 0;
fd = open("files/42_no_nl", O_RDONLY);
printf("fd = %d\n", fd);
while (i < 1)
{
prt = get_next_line(fd);
printf("%s", prt);
free(prt);
i++;
}
close(fd);
}*/