-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
96 lines (85 loc) · 2.14 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
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhenin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 11:32:21 by mhenin #+# #+# */
/* Updated: 2024/10/31 11:32:23 by mhenin ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int check_if_endline(char *str)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == '\n')
return (i);
i++;
}
return (-1);
}
int ft_strlen(const char *str)
{
int length;
length = 0;
while (str[length] != '\0')
length++;
return (length);
}
char *ft_strdup(const char *src)
{
char *res;
size_t i;
res = (char *)malloc(sizeof(char) * (ft_strlen(src) + 1));
if (!res)
return (NULL);
i = 0;
while (src[i])
{
res[i] = src[i];
i++;
}
res[i] = '\0';
return (res);
}
char *get_line_from_buff(t_buffer *buffer)
{
char *res;
int i;
i = buffer->cursor;
while (buffer->buffer[i] != '\n' && buffer->buffer[i] != '\0')
i++;
if (buffer->buffer[i] == '\n')
{
res = malloc((i + 2) - buffer->cursor);
if (!res)
return (NULL);
i = 0;
while (buffer->buffer[buffer->cursor] != '\n')
res[i++] = buffer->buffer[buffer->cursor++];
res[i] = '\n';
res[++i] = '\0';
if (buffer->buffer[++buffer->cursor] == '\0')
buffer->cursor = 0;
return (res);
}
res = ft_strdup(buffer->buffer + buffer->cursor);
buffer->cursor = 0;
return (res);
}
char *ft_strcpy(char *dest, const char *src, int decale)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[decale + i] = src[i];
i++;
}
dest[decale + i] = '\0';
return (dest);
}