-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
79 lines (71 loc) · 2 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhuthmay <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/28 15:48:59 by mhuthmay #+# #+# */
/* Updated: 2024/09/28 20:27:17 by mhuthmay ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
unsigned char *d;
const unsigned char *s;
size_t i;
if (!dst && !src)
return (NULL);
d = (unsigned char *)dst;
s = (const unsigned char *)src;
i = 0;
while (i < n)
{
d[i] = s[i];
i++;
}
return (dst);
}
int find_newline(char *buffer, int *pos, int bytes_read)
{
while (*pos < bytes_read)
{
if (buffer[*pos] == '\n')
return (1);
(*pos)++;
}
return (0);
}
char *allocate_line(int length)
{
char *line;
line = malloc(sizeof(char) * (length + 1));
if (line)
line[length] = '\0';
return (line);
}
int fill_line(char *line, t_gnl *gnl, int line_len)
{
int i;
int copy_len;
i = 0;
while (i < line_len)
{
if (gnl->buf_pos >= gnl->bytes_read)
{
gnl->bytes_read = read(gnl->fd, gnl->buffer, BUFFER_SIZE);
if (gnl->bytes_read <= 0)
break ;
gnl->buf_pos = 0;
}
if (gnl->bytes_read - gnl->buf_pos < line_len - i)
copy_len = gnl->bytes_read - gnl->buf_pos;
else
copy_len = line_len - i;
ft_memcpy(line + i, gnl->buffer + gnl->buf_pos, copy_len);
i += copy_len;
gnl->buf_pos += copy_len;
}
return (i);
}