-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
94 lines (85 loc) · 1.94 KB
/
get_next_line.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zwalad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/30 20:37:33 by zwalad #+# #+# */
/* Updated: 2022/02/10 02:17:19 by zwalad ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_line(char *str)
{
int i;
char *l;
i = 0;
while (str[i] && str[i] != '\n')
i++;
if (str[i] == '\n')
{
l = get_substr(str, 0, i + 1);
return (l);
}
l = get_strdup(str);
return (l);
}
char *get_next(char *str)
{
int i;
char *sstr;
i = 0;
sstr = NULL;
while (str[i] && str[i] != '\n')
i++;
if (str[i] == '\n')
{
i++;
sstr = get_substr(str, i, get_strlen(str) - i);
}
free(str);
str = NULL;
return (sstr);
}
char *norm_kekw(char *str, int fd)
{
int len;
char *buf;
int bz;
len = 1;
bz = 1;
buf = malloc(bz + 1);
if (!buf)
return (NULL);
while (len > 0 && !get_strchr(str))
{
len = read(fd, buf, bz);
if (len <= 0)
break ;
buf[len] = '\0';
str = get_strjoin(str, buf);
}
free(buf);
buf = NULL;
return (str);
}
char *get_next_line(int fd)
{
static char *str;
char *line;
if (fd < 0)
return (NULL);
if (!str)
str = get_strdup("");
str = norm_kekw(str, fd);
if (!str[0])
{
free(str);
str = NULL;
return (NULL);
}
line = get_line(str);
str = get_next(str);
return (line);
}