-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
101 lines (91 loc) · 2.29 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
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aachhoub <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/22 18:22:51 by aachhoub #+# #+# */
/* Updated: 2022/10/24 15:55:57 by aachhoub ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t i;
i = 0;
if (!s)
return (NULL);
if (start >= ft_strlen(s))
return ((char *) ft_calloc(1, sizeof(char)));
if (ft_strlen(s) <= start + len)
substr = malloc(sizeof(char) * (ft_strlen(s) - start + 1));
else
substr = malloc(sizeof(char) * (len + 1));
if (!substr)
return (NULL);
while (s[start] && i < len)
substr[i++] = s[start++];
substr[i] = '\0';
return (substr);
}
void ft_bzero(void *s, size_t n)
{
while (n--)
*((unsigned char *)(s + n)) = 0;
}
void *ft_calloc(size_t count, size_t size)
{
void *res;
res = malloc(size * count);
if (!res)
return (0);
ft_bzero(res, size * count);
return (res);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int s1_len;
int s2_len;
int i;
i = 0;
if (!s1 || !s2)
return (0);
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
str = malloc(sizeof(char) * (s1_len + s2_len + 1));
if (!str)
return (NULL);
while (s1[i])
{
str[i] = s1[i];
i++;
}
i = 0;
while (s2[i])
str[s1_len++] = s2[i++];
str[s1_len] = '\0';
free((void *)s1);
s1 = NULL;
return (str);
}
char *ft_strdup(const char *s1)
{
char *copy;
size_t s1_len;
int i;
i = 0;
s1_len = ft_strlen(s1);
copy = malloc(sizeof(char) * (s1_len + 1));
if (!copy)
return (NULL);
while (s1[i])
{
copy[i] = s1[i];
i++;
}
copy[i] = '\0';
return (copy);
}