-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
92 lines (81 loc) · 1.96 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
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aldokezer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 22:24:23 by aldokezer #+# #+# */
/* Updated: 2023/11/08 20:57:24 by aldokezer ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ft_strlen(char *str)
{
int i;
i = 0;
if (str == NULL)
return (0);
while (*str++)
i++;
return (i);
}
int ft_find_newline_position(char *str)
{
int i;
i = 0;
while (*str)
{
if (*str == '\n')
return (i);
i++;
str++;
}
return (-1);
}
void *ft_memmove(void *to, const void *from, size_t size)
{
unsigned char *des;
const unsigned char *src;
des = to;
src = from;
if (des < src)
{
while (size--)
*(des++) = *(src++);
}
else if (des > src)
{
des += size;
src += size;
while (size--)
*--des = *--src;
}
return (to);
}
char *ft_strncpy(char *dest, char *src, int n)
{
int i;
i = 0;
while (i < n)
{
dest[i] = src[i];
i++;
}
return (dest);
}
char *ft_extract_line_and_movebytes(char *buf)
{
char *newline;
int nl_pos;
nl_pos = ft_find_newline_position(buf);
if (nl_pos >= 0)
{
newline = malloc(sizeof(char) * (nl_pos + 2));
newline = ft_strncpy(newline, buf, nl_pos + 1);
newline[nl_pos + 1] = '\0';
buf = ft_memmove(buf, buf + nl_pos + 1, ft_strlen(buf) - nl_pos);
return (newline);
}
return (NULL);
}