-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
198 lines (182 loc) · 5.36 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlucio-l <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/01 17:42:23 by rlucio-l #+# #+# */
/* Updated: 2021/10/02 17:44:59 by rlucio-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/*
** NAME
** strlcat -- size-bounded string concatenation
** DESCRIPTION
** strlcat() appends string src to the end of dst. It will append at most
** dstsize - strlen(dst) - 1 characters. It will then NUL-terminate,
** unless dstsize is 0 or the original dst string was longer than dstsize
** (in practice this should not happen as it means that either dstsize is
** incorrect or that dst is not a proper string).
** If the src and dst strings overlap, the behavior is undefined.
** RETURN VALUES
** strlcat() returns the total length of the string it tried to create.
** That means the initial length of dst plus the length of src.
*/
static size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
{
char *destination;
const char *source;
size_t size;
size_t dst_len;
destination = dst;
source = src;
size = dstsize;
while (size-- && *destination != '\0')
destination++;
dst_len = destination - dst;
size = dstsize - dst_len;
if (size == 0)
return (dst_len + ft_strlen(source));
while (size-- > 1 && *source != '\0')
*destination++ = *source++;
*destination = '\0';
while (*source++)
;
return (dst_len + (source - src) - 1);
}
/*
** NAME
** ft_strjoin -- concatenate two strings
** DESCRIPTION
** Allocates (with malloc(3)) and returns a new string,
** which is the result of the concatenation of ’s1’ and ’s2’.
** RETURN VALUE
** The new string. NULL if the allocation fails.
*/
static char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t str_size;
size_t s1_len;
size_t s2_len;
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
str_size = s1_len + s2_len + 1;
str = (char *) malloc(str_size * sizeof(char));
if (str == NULL)
return (NULL);
ft_strlcpy(str, s1, s1_len + 1);
ft_strlcat(str, s2, str_size);
return (str);
}
/*
** NAME
** get_line - get a line from buffer
** DESCRIPTION
** Uses ft_strchr to locate a newline character
** on buffer
** RETURN VALUE
** On success, returns a string containing
** the first line found on buffer
** If there is no newline character or else,
** returns NULL
*/
static char *get_line(char **static_buffer)
{
char *ptr_to_newline_char;
size_t line_size;
char *line;
char *temp_buffer;
if (*static_buffer == NULL)
return (NULL);
ptr_to_newline_char = ft_strchr(*static_buffer, '\n');
if (ptr_to_newline_char)
{
line_size = (ptr_to_newline_char - *static_buffer) + 1;
line = ft_substr(*static_buffer, 0, line_size);
ptr_to_newline_char++;
temp_buffer = ft_strdup(ptr_to_newline_char);
free(*static_buffer);
*static_buffer = ft_strdup(temp_buffer);
free(temp_buffer);
return (line);
}
else
return (NULL);
}
/*
** NAME
** assign_line - assign a string to the static variable line
** DESCRIPTION
** Receive bytes read from a previous call to read on file descriptor,
** and the addresses of the static variables static_buffer, buffer
** and line, which were initialized in get_next_line.
**
** If read was successfully called, join buffer to static_buffer,
** call get_line to check if there is a line on the string created.
** If a line was found, assign it to static variable line.
**
** If read returned EOF, and there is a string stored on static_buffer,
** assign it to line and set static_buffer to NULL.
*/
static void assign_line(int bytes, char **sta_buf, char **buf, char **line)
{
char *temp;
if (bytes > 0)
{
if (*sta_buf == NULL)
*sta_buf = ft_strdup("");
temp = ft_strjoin(*sta_buf, *buf);
free(*sta_buf);
*sta_buf = ft_strdup(temp);
free(temp);
*line = get_line(sta_buf);
}
free(*buf);
if (bytes <= 0 && *sta_buf != NULL)
{
if (**sta_buf != '\0')
*line = ft_strdup(*sta_buf);
free(*sta_buf);
*sta_buf = NULL;
}
}
/*
** NAME
** get_next_line
** DESCRIPTION
** A function which returns a line read from a
** file descriptor
** RETURN VALUE
** Read line: correct behavior
** NULL: nothing else to read or an error occurred
*/
char *get_next_line(int fd)
{
char *buffer;
static char *static_buffer;
char *line;
int bytes_read;
if (fd == -1 || BUFFER_SIZE <= 0)
return (NULL);
line = get_line(&static_buffer);
while (line == NULL)
{
buffer = malloc(BUFFER_SIZE + 1);
if (buffer == NULL)
return (NULL);
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read == -1)
{
free(buffer);
return (NULL);
}
buffer[bytes_read] = '\0';
assign_line(bytes_read, &static_buffer, &buffer, &line);
if (bytes_read <= 0 && static_buffer == NULL)
break ;
}
return (line);
}