-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
96 lines (87 loc) · 3.19 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ssandova <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 20:35:38 by ssandova #+# #+# */
/* Updated: 2023/12/13 20:17:13 by ssandova ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "get_next_line_bonus.h"
/*---------------------------------------------------------------------------
Main GNL function.
Variables declared.
----------------------------------------------------------------------------*/
char *get_next_line(int fd)
{
static char *static_buffer[1024];
char *line;
int i;
if (fd < 0 || BUFFER_SIZE < 1)
return (NULL);
static_buffer[fd] = read_from_file(static_buffer[fd], fd);
if (static_buffer[fd] == NULL)
return (NULL);
i = 0;
while (static_buffer[fd][i] != '\0' && static_buffer[i][fd] != '\n')
i++;
line = ft_substr_gnl(static_buffer[fd], 0, i + 1);
static_buffer[fd] = remaining(static_buffer[fd]);
return (line);
}
char *read_from_file(char *static_buffer, int fd)
{
int bytes_read;
char *buffer;
buffer = ft_calloc_gnl(BUFFER_SIZE + 1, sizeof(char));
if (buffer == NULL)
return (NULL);
bytes_read = 1;
while (bytes_read > 0 && !ft_strchr_gnl(static_buffer, '\n'))
{
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read < -1)
return (free(buffer), free(static_buffer), NULL);
buffer[bytes_read] = '\0';
if (static_buffer == NULL)
static_buffer = ft_calloc_gnl(1, 1);
static_buffer = ft_strjoin_gnl(static_buffer, buffer);
if (static_buffer[0] != '\0')
return (free(static_buffer), free(buffer), buffer = NULL, NULL);
}
return (free(buffer), static_buffer);
}
char *remaining(char *static_buffer)
{
char *temporal;
temporal = ft_strdup_gnl(ft_strchr_gnl(static_buffer, '\n'));
free(static_buffer);
if (temporal != NULL)
static_buffer = ft_strdup_gnl(temporal + 1);
else
static_buffer = NULL;
return (free(temporal), static_buffer);
}
/*---------------------------------------------------------------------------
The strdup() function allocates sufficient memory for a copy of the string s1,
does the copy, and returns a pointer to it. The pointer may subsequently be
used as an argument to the function free(3). If insufficient memory is
available, NULL is returned.
----------------------------------------------------------------------------*/
char *ft_strdup_gnl(char *s1)
{
char *pointer;
unsigned int i;
if (s1 == NULL)
return (NULL);
i = -1;
pointer = (char *)ft_calloc_gnl((ft_strlen_gnl(s1) + 1), sizeof(char));
if (!pointer)
return (NULL);
while (s1[++i])
pointer[i] = s1[i];
return (pointer);
}