-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
113 lines (102 loc) · 2.69 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dshatilo <dshatilo@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 16:39:03 by dshatilo #+# #+# */
/* Updated: 2023/11/15 16:58:24 by dshatilo ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_string(char *s, char *buffer, int fd, ssize_t bytes);
char *ft_get_line(char *str);
char *dealloc(char *s, char *line);
char *get_next_line(int fd)
{
static char *string;
char *buffer;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || BUFFER_SIZE >= (size_t)(-2))
return (0);
buffer = 0;
string = get_string(string, buffer, fd, 1);
if (!string)
return (0);
line = ft_get_line(string);
if (!line)
{
string = ft_free(string);
return (0);
}
string = dealloc(string, line);
return (line);
}
char *get_string(char *s, char *buffer, int fd, ssize_t bytes)
{
s = check_string(s);
if (!s)
return (0);
while ((s && !ft_strchr(s, '\n')) && bytes != 0)
{
buffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
if (!buffer)
return (ft_free(s));
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes == -1)
{
free(s);
return (ft_free(buffer));
}
if (!bytes)
{
if (*s == 0)
s = ft_free(s);
free(buffer);
return (s);
}
s = add_to_string(s, buffer, bytes);
free (buffer);
}
return (s);
}
char *ft_get_line(char *str)
{
char *line;
size_t line_len;
line_len = 0;
while (str[line_len] != 0 && str[line_len] != '\n')
line_len++;
if (str[line_len] == '\n')
line_len++;
line = (char *)malloc(sizeof(char) * (line_len + 1));
if (!line)
return (0);
line[line_len] = 0;
ft_strncpy(line, str, line_len);
return (line);
}
char *dealloc(char *s, char *line)
{
char *new_s;
size_t s_len;
size_t n_len;
size_t line_len;
s_len = 0;
line_len = 0;
while (line[line_len] != 0)
line_len++;
while (s[s_len] != 0)
s_len++;
n_len = s_len - line_len;
if (!n_len)
return (ft_free(s));
new_s = (char *)malloc(sizeof(char) * (n_len + 1));
if (!new_s)
return (ft_free(s));
ft_strncpy(new_s, s + line_len, n_len);
new_s[n_len] = 0;
free(s);
return (new_s);
}