-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
124 lines (113 loc) · 2.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ikgonzal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/31 20:13:29 by ikgonzal #+# #+# */
/* Updated: 2021/12/13 17:57:57 by ikgonzal ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
size_t ft_strlen2(char *str, int c)
{
size_t i;
if (!str)
return (0);
i = 0;
while (str[i])
{
if (str[i] == c)
break ;
i++;
}
return (i);
}
int check_line_break(char *buff)
{
int i;
if (!buff)
return (0);
i = 0;
while (buff[i])
{
if (buff[i] == '\n')
return (1);
i++;
}
return (0);
}
char *ft_strjoin(char *s1, char *s2)
{
int i;
int k;
char *join;
int join_len;
if (!s1)
{
s1 = malloc(1);
s1[0] = '\0';
}
join_len = ft_strlen2(s1, '\0') + ft_strlen2(s2, '\0') + 1;
join = malloc(sizeof(char) * join_len);
if (!join)
return (NULL);
i = 0;
k = 0;
while (s1[i])
join[i++] = s1[k++];
k = 0;
while (i < join_len - 1)
join[i++] = s2[k++];
join[i] = '\0';
free(s1);
return (join);
}
char *ft_substr(char const *buff, unsigned int start, size_t end)
{
char *line;
size_t i;
size_t k;
line = malloc(sizeof(char) * (end + 1));
if (!line || !buff)
return (NULL);
i = 0;
k = 0;
while (buff[i])
{
if (i >= start && k < end)
{
line[k] = buff[i];
k++;
}
i++;
}
line[k] = '\0';
return (line);
}
char *ft_substr2(char *buff, unsigned int start, size_t end)
{
char *line;
size_t i;
size_t k;
if (!check_line_break(buff))
{
free(buff);
return (NULL);
}
line = malloc(sizeof(char) * (end + 1));
if (!line || !buff)
return (NULL);
i = 0;
k = 0;
while (buff[i])
{
if (i >= start && k < end)
line[k++] = buff[i];
i++;
}
line[k] = '\0';
free(buff);
return (line);
}