-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
114 lines (105 loc) · 2.73 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line.c :+: :+: */
/* +:+ */
/* By: splattje <splattje@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/01 13:07:19 by splattje #+# #+# */
/* Updated: 2023/11/14 09:29:33 by splattje ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t find_line_ending(char *str, size_t index)
{
while (str[index] && str[index] != '\n')
index++;
if (str[index] == '\n')
index++;
return (index);
}
char *get_string(char *str)
{
char *new_str;
size_t index;
size_t sec_index;
index = 0;
sec_index = 0;
if (str[index] == '\0')
{
free(str);
return (NULL);
}
index = find_line_ending(str, index);
new_str = (char *)malloc((ft_strlen(str) - index + 1));
if (!new_str)
{
free(new_str);
return (NULL);
}
while (str[index])
new_str[sec_index++] = str[index++];
new_str[sec_index] = '\0';
if (!new_str[0])
return (free_and_null(new_str, &str), NULL);
free(str);
return (new_str);
}
char *read_the_line(char *str)
{
char *line;
size_t index;
index = 0;
if (!str || str[0] == '\0')
return (NULL);
index = find_line_ending(str, index);
line = (char *)malloc(sizeof(char) * index + 1);
if (!line)
return (NULL);
index = 0;
while (str[index] && str[index] != '\n')
{
line[index] = str[index];
index++;
}
if (str[index] == '\n')
{
line[index] = str[index];
index++;
}
line[index] = '\0';
return (line);
}
char *free_and_null(char *buffer1, char **buffer2)
{
free(buffer1);
free(*buffer2);
*buffer2 = NULL;
return (NULL);
}
char *get_next_line(int fd)
{
static char *buffer;
char *line;
int read_bytes;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
line = (char *)malloc(sizeof(char) * BUFFER_SIZE + 1);
if (!line)
return (NULL);
read_bytes = 1;
while (!(ft_strchr(buffer, '\n')) && read_bytes != 0)
{
read_bytes = read(fd, line, BUFFER_SIZE);
if (read_bytes == -1)
return (free_and_null(line, &buffer), NULL);
line[read_bytes] = '\0';
buffer = ft_strjoin(buffer, line);
if (!buffer)
return (free_and_null(line, &buffer), NULL);
}
free(line);
line = read_the_line(buffer);
buffer = get_string(buffer);
return (line);
}