-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
109 lines (98 loc) · 2.1 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
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils.c :+: :+: */
/* +:+ */
/* By: nvan-der <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/01/17 18:44:59 by nvan-der #+# #+# */
/* Updated: 2021/05/17 11:31:33 by nvan-der ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
if (s == NULL)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
char *ft_substr(char *s)
{
char *ret;
int i;
int j;
i = 0;
j = 0;
if (s == NULL)
return (NULL);
while (s[i] != '\n' && s[i] != '\0')
i++;
ret = (char *)malloc(sizeof(char) * (i + 1));
if (ret == NULL)
return (NULL);
while (j < i)
{
ret[j] = s[j];
j++;
}
ret[j] = '\0';
return (ret);
}
char *ft_strjoin(char *s1, char *s2)
{
char *result;
size_t str_size;
str_size = ft_strlen(s1) + ft_strlen(s2) + 1;
result = malloc(sizeof(char) * str_size);
if (result == NULL)
{
free(s1);
free(s2);
return (NULL);
}
result = ft_cpy_cat(s1, s2, result);
free(s1);
free(s2);
return (result);
}
char *ft_strdup(char *s)
{
int i;
char *ret;
i = ft_strlen(s);
ret = (char *)malloc(sizeof(char) * (i + 1));
if (ret == NULL)
return (NULL);
i = 0;
while (s[i] != '\0')
{
ret[i] = s[i];
i++;
}
ret[i] = '\0';
return (ret);
}
char *ft_cpy_cat(char *ori, char *app, char *ret)
{
int i;
int j;
i = 0;
j = 0;
while (ori[i] != '\0')
{
ret[i] = ori[i];
i++;
}
while (app[j] != '\0')
{
ret[i] = app[j];
i++;
j++;
}
ret[i] = '\0';
return (ret);
}