-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
60 lines (53 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsoares- <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 17:13:54 by vsoares- #+# #+# */
/* Updated: 2024/11/28 18:52:39 by vsoares- ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int line_len(char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i] && str[i] != '\n')
i++;
return (i);
}
void bufshift(char *buf, size_t size)
{
int i;
i = 0;
while (buf[size])
buf[i++] = buf[size++];
while (i < BUFFER_SIZE)
buf[i++] = 0;
}
char *str_append(char *old_line, char *buf)
{
char *new_line;
int i;
int j;
i = 0;
j = 0;
new_line = malloc(line_len(old_line) + line_len(buf) + 2);
if (!new_line)
return (NULL);
while (old_line && old_line[i])
{
new_line[i] = old_line[i];
i++;
}
while (buf[j] && buf[j - 1] != '\n')
new_line[i++] = buf[j++];
new_line[i] = 0;
bufshift(buf, j);
free(old_line);
return (new_line);
}