-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
124 lines (114 loc) · 3.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: duha <duha@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/14 08:04:31 by duha #+# #+# */
/* Updated: 2025/01/09 18:46:47 by duha ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <unistd.h>
/**
* get_next_line - Returns a line read from a file descriptor.
*
* @fd: The file descriptor to read from.
*
* Helper function:
* initialize: Checks the validity of fd and initializes storage.
* read_fail_eol: Handles end-of-file and read errors, frees buffers.
* do_line: Extracts line from storage and updates storage.
* free_and_null: Frees storage and read buffer, sets them to NULL.
*
* Return: Read line: correct behavior
* or NULL if there is nothing else to read, or an error occurred.
*/
char *get_next_line(int fd);
static int initialize(int fd, char **storage);
static char *read_fail_eol(ssize_t bytes, char **read_buffer, char **storage);
static char *do_line(char **storage, char **read_buffer);
static void free_and_null(char **storage, char **read_buffer);
char *get_next_line(int fd)
{
char *read_buffer;
static char *storage;
char *tmp;
ssize_t bytes;
if (initialize(fd, &storage) == 1)
return (NULL);
read_buffer = malloc((unsigned int)BUFFER_SIZE + 1);
if (!read_buffer)
return (NULL);
while (!ft_strchr(storage, '\n'))
{
bytes = read(fd, read_buffer, BUFFER_SIZE);
if (bytes <= 0)
return (read_fail_eol(bytes, &read_buffer, &storage));
read_buffer[bytes] = '\0';
tmp = storage;
storage = ft_strjoin_free(storage, read_buffer);
if (!storage)
return (free(tmp), free(read_buffer), NULL);
}
return (do_line(&storage, &read_buffer));
}
static int initialize(int fd, char **storage)
{
if (fd < 0 || BUFFER_SIZE <= 0)
return (1);
if (!*storage)
*storage = ft_strdup("");
if (!*storage)
return (1);
return (0);
}
static char *read_fail_eol(ssize_t bytes, char **read_buffer, char **storage)
{
char *line;
free(*read_buffer);
if (bytes == 0 && **storage)
{
line = ft_strdup(*storage);
if (!line)
return (NULL);
free (*storage);
*storage = NULL;
return (line);
}
free (*storage);
*storage = NULL;
return (NULL);
}
static char *do_line(char **storage, char **read_buffer)
{
char *newline_ptr;
char *remainer;
char *line;
newline_ptr = ft_strchr(*storage, '\n');
if (newline_ptr)
{
remainer = ft_strdup(newline_ptr + 1);
if (!remainer)
return (free_and_null(storage, read_buffer), NULL);
*(newline_ptr + 1) = '\0';
line = ft_strdup(*storage);
if (!line)
return (free_and_null(storage, read_buffer), NULL);
free(*storage);
*storage = remainer;
free(*read_buffer);
return (line);
}
line = ft_strdup(*storage);
if (!line)
return (free_and_null(storage, read_buffer), NULL);
return (free_and_null(storage, read_buffer), line);
}
static void free_and_null(char **storage, char **read_buffer)
{
free(*storage);
*storage = NULL;
free(*read_buffer);
}