-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_next_line_utils.c
128 lines (118 loc) · 2.41 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhiguera <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 13:56:48 by mhiguera #+# #+# */
/* Updated: 2023/04/26 18:47:05 by mhiguera ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strchr(const char *s, int c)
{
int i;
char *cast_s;
if (!s)
return (NULL);
i = 0;
cast_s = (char *) s;
while (s[i])
{
if (s[i] == (char)c)
return ((char *)s + i);
i++;
}
if (c == 0)
return (cast_s + i);
return (0);
}
char *ft_strjoin(char *s1, char *s2)
{
char *str;
int i;
int j;
i = -1;
j = -1;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
if (!s1 || !s2)
return (NULL);
str = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof (char));
if (str == (0))
return (0);
while (s1[++i] != '\0')
str[i] = s1[i];
while (s2[++j] != '\0')
str[i + j] = s2[j];
str[ft_strlen(s1) + ft_strlen(s2)] = '\0';
free(s1);
return (str);
}
size_t ft_strlen(const char *s)
{
int i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
char *ft_ending(char *str)
{
int i;
char *ret;
i = 0;
if (!str[i])
return (NULL);
while (str[i] && str[i] != '\n')
i++;
ret = malloc(sizeof(char) * (i + 2));
if (!ret)
return (NULL);
i = 0;
while (str[i] && str[i] != '\n')
{
ret[i] = str[i];
i++;
}
if (str[i] == '\n')
{
ret[i] = str[i];
i++;
}
ret[i] = '\0';
return (ret);
}
char *ft_extra(char *aux)
{
int i;
int j;
char *ret;
i = 0;
while (aux[i] && aux[i] != '\n')
i++;
if (!aux[i])
{
free(aux);
return (NULL);
}
ret = malloc(sizeof(char) * (ft_strlen(aux) - i + 1));
if (!ret)
{
free(aux);
return (NULL);
}
i++;
j = 0;
while (aux[i] != '\0')
ret[j++] = aux[i++];
ret[j] = '\0';
free(aux);
return (ret);
}