-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
91 lines (80 loc) · 2.22 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fhideous <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 17:22:58 by fhideous #+# #+# */
/* Updated: 2020/12/02 17:23:01 by fhideous ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *ft_calloc(size_t count, size_t size)
{
void *ptr;
char *s;
ptr = malloc(count * size);
if (!ptr)
return (NULL);
s = (char *)ptr;
while (count--)
*s++ = '\0';
return (ptr);
}
size_t ft_strlen(const char *s)
{
size_t cnt;
if (!s)
return (0);
cnt = 0;
while (*(s + cnt) != '\0')
cnt++;
return (cnt);
}
void *ft_memcpy(void *dst, const void *src, size_t len)
{
unsigned char *ptr;
const unsigned char *src_ptr;
if (dst == NULL && src == NULL)
return (0);
ptr = (unsigned char *)dst;
src_ptr = (unsigned char *)src;
while (len--)
*ptr++ = *src_ptr++;
return (dst);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *conc_str;
size_t s1_len;
size_t s2_len;
if (s1 == NULL && s2 == NULL)
return (NULL);
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
conc_str = (char *)malloc(s1_len + s2_len + 1);
if (!conc_str)
return (NULL);
ft_memcpy(conc_str, s1, s1_len);
ft_memcpy(conc_str + s1_len, s2, s2_len);
*(conc_str + s1_len + s2_len) = '\0';
return (conc_str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub_str;
if (s == NULL)
return (NULL);
if (start >= ft_strlen(s))
{
start = 0;
len = 0;
}
sub_str = (char *)malloc(len + 1);
if (!sub_str)
return (NULL);
ft_memcpy(sub_str, s + start, len);
*(sub_str + len) = '\0';
return (sub_str);
}