-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils.c
More file actions
135 lines (125 loc) · 2.89 KB
/
Copy pathget_next_line_utils.c
File metadata and controls
135 lines (125 loc) · 2.89 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adapassa <adapassa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 18:19:19 by adapassa #+# #+# */
/* Updated: 2024/02/04 18:39:48 by adapassa ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *ft_free_mat(char **mat, char *str)
{
if (!mat && !str)
return (NULL);
else if ((mat != NULL) && (str != NULL))
{
free(mat[1]);
free(mat[0]);
free(mat);
free(str);
}
else if (str == NULL)
{
free(mat[1]);
free(mat[0]);
free(mat);
}
else if (mat == NULL)
free(str);
return (NULL);
}
void *ft_custom_function(size_t nmemb, size_t size, char *str, bool flag)
{
char *ptr;
size_t i;
int length;
i = 0;
length = 0;
if (flag == TRUE)
{
ptr = (char *)malloc(nmemb * size);
if (!ptr)
return (NULL);
while (i < (nmemb * size))
ptr[i++] = 0;
return ((void *)ptr);
}
else
{
while (str && str[length] != '\0')
length++;
return ((void *)(long int)length);
}
}
char *ft_strdup(const char *src)
{
char *dest;
long int l;
size_t i;
i = -1;
if (!src)
return (NULL);
l = (long int)ft_custom_function(0, 0, (char *)src, 0);
dest = malloc(sizeof(char) * (l + 1));
if (!src)
return (0);
if (!dest)
return (0);
while (++i <= (size_t)l)
((char *)dest)[i] = ((char *)src)[i];
dest[l] = '\0';
return (dest);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *s3;
int i;
int j;
i = 0;
j = -1;
s3 = (char *)ft_custom_function(sizeof(char),
((long)ft_custom_function(0, 0, (char *)s1, 0)
+ (long)ft_custom_function(0, 0, (char *)s2, 0) + 1), NULL, 1);
if (!s3)
return (0);
if (s1)
{
while (s1[i] != '\0')
{
s3[i] = s1[i];
i++;
}
}
while (s2[++j] != '\0')
s3[i + j] = s2[j];
s3[i + j] = '\0';
return (s3);
}
char **ft_minisplit(char *str)
{
char **tmp;
int i;
int j;
i = 0;
tmp = (char **)ft_custom_function(sizeof(char *), 2, NULL, 1);
while (str[i] != '\n')
i++;
tmp[0] = (char *)ft_custom_function(sizeof(char), (i + 1), NULL, 1);
i = 0;
while (str[i] != '\0')
i++;
tmp[1] = ft_custom_function(sizeof(char), (i + 1), NULL, 1);
i = -1;
while (str[++i] != '\n')
tmp[0][i] = str[i];
tmp[0][i] = '\0';
j = i + 1;
i = 0;
while (str[j] != '\0')
tmp[1][i++] = str[j++];
tmp[1][i] = '\0';
return (tmp);
}