-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
100 lines (91 loc) · 2.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: taretiuk <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/23 10:49:23 by psulzyck #+# #+# */
/* Updated: 2024/02/12 15:39:11 by taretiuk ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
//read_line function read file till the \n character, store it in buf
//and concatenate with the string stored in the static variable
//written in previous call. Why we dont free buf: read
//system call does not append data to the buffer;
//instead, it overwrites the existing content of the buffer
//with the newly read data.
static char *read_line(int fd, char *buf, char *backup)
{
ssize_t i;
char *char_temp;
i = 1;
while (i > 0)
{
i = read(fd, buf, BUFFER_SIZE);
if (i == -1)
return (0);
else if (i == 0)
break ;
buf[i] = '\0';
if (backup == NULL)
{
backup = ft_strdup("");
}
char_temp = backup;
backup = ft_strjoin(char_temp, buf);
free(char_temp);
char_temp = NULL;
if (ft_strchr (buf, '\n'))
break ;
}
return (backup);
}
//extract function extract the substring from the string
//returned by read_line function. it statrs extraction from
//the next to the \n character (count + 1) and goes
//till the end of thistring
static char *extract(char *line)
{
size_t count;
char *rest;
count = 0;
while (line[count] != '\n' && line[count] != '\0')
count++;
if (line[count] == '\0' || line[1] == '\0')
return (0);
rest = ft_substr(line, count + 1, ft_strlen(line) - count);
if (rest == NULL || *rest == '\0')
{
free(rest);
rest = NULL;
}
line[count + 1] = '\0';
return (rest);
}
char *get_next_line(int fd)
{
char *line;
char *buf;
static char *backup;
if (fd == -1 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
{
free(backup);
backup = NULL;
return (0);
}
buf = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buf)
return (0);
line = read_line(fd, buf, backup);
free(buf);
buf = NULL;
if (!line)
{
backup = NULL;
return (NULL);
}
backup = extract(line);
return (line);
}