libft
Loading...
Searching...
No Matches
get_next_line.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* get_next_line.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tspoof <tspoof@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2022/11/10 00:04:35 by tspoof #+# #+# */
9/* Updated: 2022/12/17 01:28:15 by tspoof ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "get_next_line.h"
14
15static void gnl_zerobuffer(char *buff)
16{
17 size_t i;
18
19 i = 0;
20 while (i < BUFFER_SIZE + 1)
21 {
22 buff[i] = '\0';
23 i++;
24 }
25}
26
27static int gnl_read(int fd, char **fd_data)
28{
29 char buffer[BUFFER_SIZE + 1];
30 char *new_line;
31 char *tmp;
32 size_t size;
33
34 new_line = NULL;
35 while (!new_line)
36 {
37 new_line = ft_strchr(*fd_data, '\n');
38 if (new_line)
39 return (2);
40 gnl_zerobuffer(buffer);
41 size = read(fd, buffer, BUFFER_SIZE);
42 if (!size)
43 break ;
44 tmp = ft_strjoin(*fd_data, buffer);
45 if (!tmp)
46 return (0);
47 free(*fd_data);
48 *fd_data = tmp;
49 }
50 return (1);
51}
52
53static char *gnl_get_line(char **fd_data)
54{
55 char *line;
56 char *tmp;
57 size_t i;
58
59 tmp = ft_strchr(*fd_data, '\n');
60 i = (size_t)(tmp - *fd_data) + 1;
61 line = ft_substr(*fd_data, 0, i);
62 tmp = ft_substr(*fd_data, i, ft_strlen(*fd_data));
63 if (!tmp || !line)
64 return (NULL);
65 free(*fd_data);
66 *fd_data = tmp;
67 return (line);
68}
69
70static char *gnl_line(int read_return, char **fd_data)
71{
72 char *line;
73
74 if (read_return == 1)
75 {
76 if (!fd_data || !*fd_data)
77 return (NULL);
78 if (*fd_data[0])
79 {
80 line = ft_substr(*fd_data, 0, ft_strlen(*fd_data));
81 free(*fd_data);
82 *fd_data = NULL;
83 if (!line)
84 return (NULL);
85 return (line);
86 }
87 free(*fd_data);
88 *fd_data = NULL;
89 return (NULL);
90 }
91 line = gnl_get_line(fd_data);
92 return (line);
93}
94
95char *get_next_line(int fd)
96{
97 static char *fd_list[1024] = {0};
98 char *line;
99 int read_return;
100
101 if (fd < 0 || read(fd, 0, 0) < 0 || BUFFER_SIZE <= 0 || fd > 1024)
102 return (NULL);
103 if (!fd_list[fd])
104 {
105 fd_list[fd] = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
106 if (!fd_list[fd])
107 return (NULL);
108 fd_list[fd][0] = '\0';
109 }
110 if (!fd_list[fd])
111 return (NULL);
112 read_return = gnl_read(fd, &fd_list[fd]);
113 if (!read_return)
114 {
115 free(fd_list[fd]);
116 return (NULL);
117 }
118 line = gnl_line(read_return, &fd_list[fd]);
119 return (line);
120}
char * get_next_line(int fd)
Gets next line from a file.
#define BUFFER_SIZE
char * ft_substr(char const *s, unsigned int start, size_t len)
Gets a piece of string out of a string.
Definition ft_substr.c:22
char * ft_strjoin(char const *s1, char const *s2)
Joins two strings together.
Definition ft_strjoin.c:15
char * ft_strchr(const char *s, int c)
Search a character.
Definition ft_strchr.c:15
size_t ft_strlen(const char *s)
Length of the string.
Definition ft_strlen.c:15