-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
168 lines (154 loc) · 3.79 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkoupy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/26 11:41:33 by jkoupy #+# #+# */
/* Updated: 2023/10/06 17:30:06 by jkoupy ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void free_line(t_list **llist)
{
t_list *last;
char *leftover;
int i;
int j;
if (!*llist)
return ;
last = ft_lstlast(*llist);
i = line_length(last);
if (!last->content[i])
return (free_list(llist, NULL));
j = 0;
leftover = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!leftover)
return ;
while (last->content[i])
leftover[j++] = last->content[i++];
leftover[j] = '\0';
free_list(llist, leftover);
}
//takes the list and returns the full string until newline
char *read_line(t_list *llist)
{
char *line;
int i;
int j;
if (!llist || !llist->content)
return (NULL);
line = malloc((line_length(llist) + 1) * sizeof(char));
if (!line)
return (NULL);
i = 0;
while (llist)
{
j = 0;
while (llist->content[j] && llist->content[j] != '\n')
line[i++] = llist->content[j++];
if (llist->content[j] == '\n')
{
line[i++] = llist->content[j++];
break ;
}
llist = llist->next;
}
line[i] = '\0';
return (line);
}
// returns 1 if finds a newline
// else 0
int line_ends(char *line)
{
int i;
i = 0;
if (!line)
return (1);
while (line[i])
{
if (line[i] == '\n')
return (1);
i++;
}
return (0);
}
// creates a linked list and reads BUFFER_SIZE bytes each time
// until it finds a buffer smaller than B_S or a \n in it
void save_line(t_list **llist, int fd)
{
int readlen;
char *buf;
if (*llist && line_ends((*llist)->content))
return ;
buf = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return ;
readlen = read(fd, buf, BUFFER_SIZE);
if (readlen == 0)
return (free(buf));
if (readlen == -1)
return (free(buf), free_list(llist, NULL));
buf[readlen] = '\0';
ft_lstadd_new(llist, buf);
if (line_ends(buf) || readlen < BUFFER_SIZE)
return ;
return (save_line(llist, fd));
}
char *get_next_line(int fd)
{
static t_list *llist;
char *next_line;
if (fd < 0 || BUFFER_SIZE <= 0)
{
free_list(&llist, NULL);
return (NULL);
}
save_line(&llist, fd);
next_line = read_line(llist);
free_line(&llist);
return (next_line);
}
// printf("\tlist before:\n");
// print_list(llist);
// printf("\tlist mid:\n");
// print_list(llist);
// printf("\tlist after:\n");
// print_list(llist);
// #include <fcntl.h>
// #include <stdio.h>
// int main(void)
// {
// int fd;
// char *line;
// int i;
// int j;
// char *files[] = {"test.txt"};
// //lorem_ipsum.txt, "empty.txt", "one_nl.txt",
// "multiple_nl.txt", "one_char_lines.txt", "no_nl.txt"
// i = 0;
// while (i < 1)
// {
// printf("------------------------------\n");
// printf("file: %s\n", files[i]);
// printf("------------------------------\n");
// fd = open(files[i], O_RDONLY);
// line = get_next_line(fd);
// j = 1;
// while (line)
// {
// printf("Line %d: ", j);
// //print_nl(line);
// printf("\n");
// free(line);
// line = get_next_line(fd);
// j++;
// }
// printf("Line %d: ", j);
// //print_nl(line);
// free(line);
// i++;
// }
// return (0);
// }