-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
129 lines (118 loc) · 2.94 KB
/
Copy pathget_next_line.c
File metadata and controls
129 lines (118 loc) · 2.94 KB
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
120
121
122
123
124
125
126
127
128
129
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aleon-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 17:53:32 by aleon-ca #+# #+# */
/* Updated: 2020/10/21 18:09:22 by aleon-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static char *ft_strchr(char *str, int c)
{
int i;
i = -1;
while (str[++i])
{
if (str[i] == c)
return (str + i);
}
return (0);
}
static char *mem_update_by_buff_join(int fd, char **mem, char *buff)
{
int i;
int j;
char *temp;
i = -1;
while (mem[fd][++i])
;
j = -1;
while (buff[++j])
;
temp = malloc(sizeof(char) * (i + j + 1));
temp[i + j] = '\0';
i = -1;
while (mem[fd][++i])
temp[i] = mem[fd][i];
j = -1;
while (buff[++j])
temp[i + j] = buff[j];
free(mem[fd]);
mem[fd] = temp;
free(buff);
return (temp + i);
}
static int lineread__mem_trunc(int fd, char **mem, char *nl, char **line)
{
int i;
int j;
char *temp;
*nl = '\0';
i = -1;
while (mem[fd][++i])
;
*line = malloc(sizeof(char) * (i + 1));
*(*line + i) = '\0';
j = -1;
while (mem[fd][++j])
*(*line + j) = mem[fd][j];
while (mem[fd][++i])
;
temp = malloc(sizeof(char) * (i - j));
temp[i - j - 1] = '\0';
i = j;
while (mem[fd][++i])
temp[i - j - 1] = mem[fd][i];
free(mem[fd]);
mem[fd] = temp;
return (1);
}
static int remaining_mem_update(int fd, char **mem, char **line)
{
int i;
char *nlpos;
if ((nlpos = ft_strchr(mem[fd], '\n')))
return (lineread__mem_trunc(fd, mem, nlpos, line));
else
{
i = -1;
while (mem[fd][++i])
;
*line = malloc(sizeof(char) * (i + 1));
i = -1;
while (mem[fd][++i])
*(*line + i) = mem[fd][i];
free(mem[fd]);
mem[fd] = 0;
return (0);
}
}
int get_next_line(int fd, char **line)
{
int bytes_read;
char *buff;
char *nl;
static char *mem[FOPEN_MAX];
if ((fd < 0) || !line || !(buff = malloc(sizeof(char) * (BUFFER_SIZE + 1))))
return (-1);
if (!mem[fd])
{
mem[fd] = malloc(sizeof(char) * 1);
mem[fd][0] = '\0';
}
while ((bytes_read = read(fd, buff, BUFFER_SIZE)) > 0)
{
buff[bytes_read] = '\0';
if ((nl = ft_strchr(mem_update_by_buff_join(fd, mem, buff), '\n')))
return (lineread__mem_trunc(fd, mem, nl, line));
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
}
if (buff)
free(buff);
if (bytes_read == 0)
return (remaining_mem_update(fd, mem, line));
return (-1);
}