-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
103 lines (92 loc) · 2.22 KB
/
get_next_line_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aeloyan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 15:33:24 by aeloyan #+# #+# */
/* Updated: 2022/06/20 16:00:39 by aeloyan ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
#include <stdio.h>
char *my_strcut(char **str)
{
char *ptr;
char *transmit;
char *temp;
if (!*str || !**str)
return (0);
ptr = my_strchr(*str, '\n');
if (!ptr)
{
temp = my_strjoin(*str, "\0");
*str = NULL;
return (temp);
}
transmit = ft_substr(*str, 0, ptr - (*str) + 1);
temp = ft_substr(ptr, 0 + sizeof(char), my_strlen(ptr) - 1);
free(*str);
*str = temp;
return (transmit);
}
char *my_strchr(const char *s, int c)
{
int i;
i = 0;
if (!s)
return (0);
while (s[i])
{
if (s[i] == (char)c)
return ((char *)(s + i));
i++;
}
if (s[i] == (char)c)
return ((char *)(s + i));
return (0);
}
size_t my_strlen(const char *str)
{
size_t k;
k = 0;
if (!str)
return (0);
while (str[k++] != '\0')
;
return (--k);
}
size_t my_strlcpy(char *dst, const char *src, size_t dstsize)
{
size_t i;
i = 0;
if (dstsize == 0)
return (my_strlen(src));
while (i < dstsize - 1 && src[i] != '\0')
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
if (src)
while (src[i] != '\0')
i++;
return (i);
}
char *my_strjoin(char const *s1, char const *s2)
{
char *ptr;
size_t i;
size_t j;
i = my_strlen(s1);
j = my_strlen(s2);
ptr = (char *)malloc(sizeof(char) * (i + j + 1));
if (!ptr)
return (0);
my_strlcpy(ptr, s1, i + 1);
if (s1)
free((void *)s1);
my_strlcpy(ptr + i, s2, j + 1);
return (ptr);
}