-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
114 lines (103 loc) · 2.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mudanaci <mudanaci@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/08 16:11:42 by mudanaci #+# #+# */
/* Updated: 2023/05/08 16:11:45 by mudanaci ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(char *buf)
{
size_t i;
i = 0;
if (!buf)
return (0);
while (buf[i])
i++;
return (i);
}
char *strjoin(char *s1, char *s2)
{
char *return_str;
size_t i;
size_t j;
if (!s1)
{
s1 = malloc (sizeof(char));
s1[0] = '\0';
}
if (!s1 || !s2)
return (NULL);
return_str = malloc (sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
i = -1;
j = 0;
while (s1[++i])
return_str[i] = s1[i];
while (s2[j])
return_str[i++] = s2[j++];
return_str[i] = '\0';
free (s1);
return (return_str);
}
int find_nl_char(char *buffer)
{
int i;
i = -1;
if (!buffer)
return (0);
while (buffer[++i])
if (buffer[i] == '\n')
return (1);
return (0);
}
char *get_new_line(char *buffer)
{
char *line;
int i;
int nl;
nl = find_nl_char(buffer);
i = 0;
if (!buffer[i])
return (NULL);
while (buffer[i] && buffer[i] != '\n')
i++;
line = malloc(sizeof(char) * (i + 1 + nl));
i = 0;
while (buffer[i] && buffer[i] != '\n')
{
line[i] = buffer[i];
i++;
}
if (buffer[i] == '\n')
line[i++] = '\n';
line[i] = 0;
return (line);
}
char *get_new_buffer(char *buffer)
{
char *new_buffer;
int i;
int j;
j = 0;
i = 0;
while (buffer[i] && buffer[i] != '\n')
i++;
if (!buffer[i])
{
free (buffer);
return (NULL);
}
new_buffer = malloc(sizeof(char) * (ft_strlen(buffer) - i + 1));
if (!new_buffer)
return (NULL);
i++;
while (buffer[i])
new_buffer[j++] = buffer[i++];
new_buffer[j] = 0;
free (buffer);
return (new_buffer);
}