-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
102 lines (92 loc) · 2.11 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hanmpark <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 15:46:43 by hanmpark #+# #+# */
/* Updated: 2022/12/05 07:14:41 by hanmpark ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(char *str)
{
size_t len;
len = 0;
while (str[len])
len++;
return (len);
}
void ft_freeall(char *buf, char *line, char *stash)
{
free(buf);
free(line);
free(stash);
}
void *ft_calloc(size_t count, size_t size)
{
char *tb;
size_t len;
int i;
if (count > SIZE_MAX)
return (NULL);
len = count * size;
tb = malloc(len);
if (!tb)
return (NULL);
i = 0;
while (len)
{
tb[i++] = 0;
len--;
}
return (tb);
}
char *ft_bufferjoin(char *dest, char *src)
{
char *joined;
size_t len;
int i;
if (!dest)
{
dest = ft_calloc(1, sizeof(char));
if (!dest)
return (NULL);
}
len = ft_strlen(dest) + ft_strlen(src);
joined = malloc((len + 1) * sizeof(char));
if (!joined)
return (NULL);
i = 0;
while (dest[i])
*joined++ = dest[i++];
free(dest);
i = 0;
while (src[i])
*joined++ = src[i++];
*joined = 0;
return (joined - len);
}
char *ft_linetrim(char *str)
{
char *tstr;
size_t i;
size_t len;
i = 0;
len = 0;
while (str[i] && str[i] != '\n')
i++;
if (str[i] == '\n')
i++;
while (str[i + len])
len++;
tstr = malloc((len + 1) * sizeof(char));
if (!tstr)
return (NULL);
while (str[i])
*tstr++ = str[i++];
*tstr = 0;
str[i - len] = 0;
return (tstr - len);
}