-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
115 lines (104 loc) · 1.96 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amait-ou <amait-ou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/25 11:27:15 by amait-ou #+# #+# */
/* Updated: 2022/11/08 19:59:20 by amait-ou ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(char *s)
{
size_t i;
if (!s)
return (0);
i = 0;
while (s[i])
++i;
return (i);
}
char *ft_strchr(char *s, int c)
{
char *p;
if (!s)
return (0);
p = (char *)s;
while (*p && *p != (char)c)
++p;
if (*p == (char)c)
return (p);
return ((void *)0);
}
char *ft_strjoin(char *s1, char *s2)
{
int i;
int j;
char *p;
p = (char *)malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (!p)
return ((void *)0);
i = 0;
j = 0;
while (s1 && s1[i])
{
p[i] = s1[i];
++i;
}
while (s2[j])
p[i++] = s2[j++];
p[i] = '\0';
free(s1);
return (p);
}
char *get_one_line(char *stash)
{
char *line;
int i;
if (!stash)
return (NULL);
i = 0;
while (stash[i] && stash[i] != '\n')
++i;
line = malloc(i + 2);
if (!line)
return (NULL);
i = 0;
while (stash[i] && stash[i] != '\n')
{
line[i] = stash[i];
i++;
}
if (stash[i] == '\n')
line[i++] = '\n';
line[i] = '\0';
return (line);
}
char *free_stash(char *reserve)
{
char *stash;
int i;
int j;
if (!reserve)
return (NULL);
i = 0;
j = 0;
while (reserve[i] != '\n' && reserve[i])
++i;
if (!reserve[i] || (reserve[i] == '\n' && reserve[i + 1] == '\0'))
{
free(reserve);
return (NULL);
}
stash = malloc((ft_strlen(reserve) - i));
if (stash)
{
while (reserve[++i])
stash[j++] = reserve[i];
stash[j] = '\0';
}
free(reserve);
return (stash);
}