-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
119 lines (108 loc) · 2.36 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils.c :+: :+: */
/* +:+ */
/* By: lbartels <lbartels@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/13 16:55:11 by lbartels #+# #+# */
/* Updated: 2023/11/15 15:55:14 by lbartels ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
int check_new_line(char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i])
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}
char *ft_strjoin(char *str, char const *buffer)
{
char *new_str;
size_t i;
size_t j;
if (!str)
{
str = (char *)malloc(1 * sizeof(char));
str[0] = '\0';
}
new_str = (char *)malloc(sizeof(char)
* (ft_strlen(str) + ft_strlen(buffer) + 1));
if (!new_str)
return (NULL);
i = 0;
j = 0;
while (str[i])
new_str[j++] = str[i++];
i = 0;
while (buffer[i])
new_str[j++] = buffer[i++];
new_str[j] = '\0';
free(str);
return (new_str);
}
char *set_next_line(char *str)
{
int i;
char *next_line;
i = 0;
if (!str[i])
return (NULL);
while (str[i] && str[i] != '\n')
i++;
if (str[i] == '\n')
i++;
next_line = (char *)malloc((i + 1) * sizeof(char));
if (!next_line)
return (NULL);
i = 0;
while (str[i] && str[i] != '\n')
{
next_line[i] = str[i];
i++;
}
if (str[i])
next_line[i++] = '\n';
next_line[i] = '\0';
return (next_line);
}
char *new_str(char *str)
{
char *new_str;
int i;
int j;
i = 0;
while (str[i] && str[i] != '\n')
i++;
if (!str[i])
{
free(str);
return (NULL);
}
new_str = (char *)malloc(sizeof(char) * (ft_strlen(str) - i + 1));
if (!new_str)
return (NULL);
i++;
j = 0;
while (str[i])
new_str[j++] = str[i++];
new_str[j] = '\0';
free(str);
return (new_str);
}