-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
78 lines (71 loc) · 1.89 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atweek <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/25 17:44:55 by atweek #+# #+# */
/* Updated: 2021/03/06 20:45:19 by atweek ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t gnl_ft_strlen(const char *str)
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *gnl_strjoin(char *s1, char const *s2)
{
char *dst;
int i;
int j;
i = 0;
j = 0;
if (s1 == NULL || s2 == NULL)
return (NULL);
dst = NULL;
dst = malloc(sizeof(char) *
(gnl_ft_strlen((char *)s1) + gnl_ft_strlen((char *)s2) + 1));
if (dst)
{
while (s1[j] != '\0')
dst[i++] = s1[j++];
j = 0;
while (s2[j] != '\0')
dst[i++] = s2[j++];
dst[i] = '\0';
free(s1);
return (dst);
}
else
return (NULL);
}
char *gnl_substr(char *s, unsigned int start, size_t len, int flag)
{
size_t j;
char *dst;
size_t strlen;
if (s == NULL)
return (NULL);
strlen = gnl_ft_strlen(s);
if (start > strlen)
start = strlen;
if (len > strlen - start)
len = strlen - start;
j = 0;
if (!(dst = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
while ((j < len) && (s[start]))
dst[j++] = s[start++];
dst[j] = '\0';
if (flag == 1)
{
free(s);
s = NULL;
}
return (dst);
}