-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
99 lines (88 loc) · 2.17 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
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vscabell <vscabell@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/13 10:58:26 by vscabell #+# #+# */
/* Updated: 2020/07/29 18:39:09 by vscabell ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
void *ft_memcpy(void *dst, const void *src, int n)
{
int i;
unsigned char *d;
const unsigned char *s;
if (!src && !dst)
return (NULL);
i = 0;
s = (const unsigned char *)src;
d = (unsigned char *)dst;
while (i < n)
{
d[i] = s[i];
i++;
}
return (d);
}
char *ft_strdup(const char *s1)
{
int len;
char *dup;
len = ft_strlen(s1);
if (!(dup = malloc(sizeof(char) * (len + 1))))
return (NULL);
ft_memcpy(dup, s1, (len + 1));
return (dup);
}
char *ft_strjoin(const char *s1, const char *s2)
{
size_t lens1;
size_t lens2;
size_t i;
char *join;
lens1 = ft_strlen(s1);
lens2 = ft_strlen(s2);
if (!s1 || !s2)
return (NULL);
if (!(join = malloc(sizeof(char) * (lens1 + lens2 + 1))))
return (NULL);
ft_memcpy(join, s1, lens1);
i = 0;
while (s2[i])
{
join[lens1 + i] = s2[i];
i++;
}
join[lens1 + i] = '\0';
return (join);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t i;
if (!s)
return (NULL);
if (ft_strlen(s) < start)
return (ft_strdup(""));
if (!(sub = malloc(sizeof(char) * (len + 1))))
return (NULL);
i = 0;
while (i < len && s[start + i])
{
sub[i] = s[start + i];
i++;
}
sub[i] = '\0';
return (sub);
}