Skip to content

Commit 3802f9d

Browse files
Kris StallenbergKris Stallenberg
Kris Stallenberg
authored and
Kris Stallenberg
committed
passed moulinette
0 parents  commit 3802f9d

7 files changed

+581
-0
lines changed

get_next_line.c

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* :::::::: */
4+
/* get_next_line.c :+: :+: */
5+
/* +:+ */
6+
/* By: kris <[email protected]> +#+ */
7+
/* +#+ */
8+
/* Created: 2020/05/20 18:44:16 by kris #+# #+# */
9+
/* Updated: 2020/07/06 15:08:41 by kris ######## odam.nl */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line.h"
14+
15+
/*
16+
** Retrieves fd to read from and buffer to read into.
17+
** Reads BUFFER_SIZE * characters from fd and concatenates to buffer.
18+
** Returns 0 if EOF of fd is reached.
19+
*/
20+
21+
size_t ft_strlen(const char *str)
22+
{
23+
size_t i;
24+
25+
i = 0;
26+
while (str[i] != '\0')
27+
i++;
28+
return (i);
29+
}
30+
31+
/*
32+
** Frees s1.
33+
*/
34+
35+
char *ft_strjoin(char const *s1, char const *s2)
36+
{
37+
char *newstr;
38+
int len_s1;
39+
int len_s2;
40+
41+
if (!s1 || !s2)
42+
return (NULL);
43+
len_s1 = ft_strlen(s1);
44+
len_s2 = ft_strlen(s2);
45+
newstr = (char *)malloc(sizeof(char) * (len_s1 + len_s2 + 1));
46+
if (newstr == NULL)
47+
return (NULL);
48+
ft_memcpy(newstr, s1, len_s1 + 1);
49+
ft_memcpy(&newstr[len_s1], s2, len_s2 + 1);
50+
free((void *)s1);
51+
return (newstr);
52+
}
53+
54+
/*
55+
** Reads into a temporary fixed length array.
56+
** String joins the existing buffer and the temporary buffer.
57+
** Returns 0 if 0 bytes were read or return value of read call < BUFFER_SIZE.
58+
** Returns -1 if malloc on new buffer failed or read returned -1.
59+
** Else returns a positive integer; the return value of read.
60+
*/
61+
62+
int read_and_append_buffer(int fd, char **buffer)
63+
{
64+
int ret;
65+
char temp[BUFFER_SIZE + 1];
66+
67+
ft_memset(temp, 0, BUFFER_SIZE + 1);
68+
ret = read(fd, temp, BUFFER_SIZE);
69+
*buffer = ft_strjoin(*buffer, temp);
70+
if (buffer == NULL)
71+
return (-1);
72+
if (ret < BUFFER_SIZE)
73+
return (0);
74+
return (ret);
75+
}
76+
77+
/*
78+
** Copies the content of the buffer before '\0' or '\n' to string.
79+
** Copies the content of the buffer after '\n' to buffer_list.
80+
** In case the EOF is copied to the string returns 0.
81+
** Else returns 1.
82+
*/
83+
84+
int buffer_to_line(int eof, char *buffer, char **buffer_list, char **line)
85+
{
86+
int i;
87+
88+
i = 0;
89+
while (buffer[i] != '\0' && buffer[i] != '\n')
90+
i++;
91+
*line = ft_memmalloc(i + 1);
92+
if (line == NULL)
93+
return (-1);
94+
ft_memcpy(*line, buffer, i);
95+
if (ft_strchr(buffer, '\n'))
96+
*buffer_list = ft_strdup(&buffer[i + 1]);
97+
else if (!eof)
98+
{
99+
*buffer_list = NULL;
100+
return (0);
101+
}
102+
return (1);
103+
}
104+
105+
/*
106+
** Calling function get_next_linein a loop will allow you to read
107+
** the text available on a file descriptor one line at a time until the EOF.
108+
** Lines are written to double pointer line, exluding newline char.
109+
** The line is freed by the user.
110+
**
111+
** Return value:
112+
** 1 : A line has been read.
113+
** 0 : EOF has been reached.
114+
** -1: An error happened.
115+
*/
116+
117+
int get_next_line(int fd, char **line)
118+
{
119+
static char *buffer_list[1024];
120+
char *buf;
121+
int ret;
122+
123+
ret = 1;
124+
buf = ft_memmalloc(BUFFER_SIZE + 1);
125+
if (fd < 0 || fd > 1024 || !line || read(fd, buf, 0) < 0 || buf == NULL ||
126+
BUFFER_SIZE > 10000000)
127+
{
128+
free(buf);
129+
return (-1);
130+
}
131+
buffer_list[fd] ? ft_memcpy(buf, buffer_list[fd],
132+
ft_strlen(buffer_list[fd])) : 0;
133+
buffer_list[fd] ? free(buffer_list[fd]) : 0;
134+
while (ret > 0 && !ft_strchr(buf, '\n'))
135+
ret = read_and_append_buffer(fd, &buf);
136+
if (ret == -1)
137+
return (-1);
138+
ret = buffer_to_line(ret, buf, &buffer_list[fd], line);
139+
if (buf)
140+
free(buf);
141+
if (ret == 0 || ret == -1)
142+
return (ret);
143+
return (1);
144+
}

get_next_line.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* :::::::: */
4+
/* get_next_line.h :+: :+: */
5+
/* +:+ */
6+
/* By: kris <[email protected]> +#+ */
7+
/* +#+ */
8+
/* Created: 2020/05/20 18:44:20 by kris #+# #+# */
9+
/* Updated: 2020/07/05 13:59:30 by kstallen ######## odam.nl */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef GET_NEXT_LINE_H
14+
# define GET_NEXT_LINE_H
15+
16+
# include <unistd.h>
17+
# include <stdlib.h>
18+
# include <string.h>
19+
20+
# ifndef BUFFER_SIZE
21+
# define BUFFER_SIZE 32
22+
# endif
23+
24+
/*
25+
** get_next_line functions
26+
*/
27+
28+
int get_next_line(int fd, char **line);
29+
int buffer_to_line(int eof, char *buffer, char **buffer_list, char **line);
30+
int read_and_append_buffer(int fd, char **buffer);
31+
32+
/*
33+
** libft functions
34+
*/
35+
36+
size_t ft_strlen(const char *str);
37+
void *ft_memcpy(void *dst, const void *src, size_t n);
38+
char *ft_strjoin(char const *s1, char const *s2);
39+
char *ft_strchr(const char *str, int c);
40+
void *ft_memset(void *b, int c, size_t len);
41+
void *ft_memmalloc(size_t size);
42+
char *ft_strdup(const char *s1);
43+
44+
#endif

get_next_line_bonus.c

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* :::::::: */
4+
/* get_next_line_bonus.c :+: :+: */
5+
/* +:+ */
6+
/* By: kstallen <[email protected]> +#+ */
7+
/* +#+ */
8+
/* Created: 2020/07/05 15:35:12 by kstallen #+# #+# */
9+
/* Updated: 2020/07/05 15:35:18 by kstallen ######## odam.nl */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line_bonus.h"
14+
15+
/*
16+
** Retrieves fd to read from and buffer to read into.
17+
** Reads BUFFER_SIZE * characters from fd and concatenates to buffer.
18+
** Returns 0 if EOF of fd is reached.
19+
*/
20+
21+
size_t ft_strlen(const char *str)
22+
{
23+
size_t i;
24+
25+
i = 0;
26+
while (str[i] != '\0')
27+
i++;
28+
return (i);
29+
}
30+
31+
/*
32+
** Frees s1.
33+
*/
34+
35+
char *ft_strjoin(char const *s1, char const *s2)
36+
{
37+
char *newstr;
38+
int len_s1;
39+
int len_s2;
40+
41+
if (!s1 || !s2)
42+
return (NULL);
43+
len_s1 = ft_strlen(s1);
44+
len_s2 = ft_strlen(s2);
45+
newstr = (char *)malloc(sizeof(char) * (len_s1 + len_s2 + 1));
46+
if (newstr == NULL)
47+
return (NULL);
48+
ft_memcpy(newstr, s1, len_s1 + 1);
49+
ft_memcpy(&newstr[len_s1], s2, len_s2 + 1);
50+
free((void *)s1);
51+
return (newstr);
52+
}
53+
54+
/*
55+
** Reads into a temporary fixed length array.
56+
** String joins the existing buffer and the temporary buffer.
57+
** Returns 0 if 0 bytes were read or return value of read call < BUFFER_SIZE.
58+
** Returns -1 if malloc on new buffer failed or read returned -1.
59+
** Else returns a positive integer; the return value of read.
60+
*/
61+
62+
int read_and_append_buffer(int fd, char **buffer)
63+
{
64+
int ret;
65+
char temp[BUFFER_SIZE + 1];
66+
67+
ft_memset(temp, 0, BUFFER_SIZE + 1);
68+
ret = read(fd, temp, BUFFER_SIZE);
69+
*buffer = ft_strjoin(*buffer, temp);
70+
if (buffer == NULL)
71+
return (-1);
72+
if (ret < BUFFER_SIZE)
73+
return (0);
74+
return (ret);
75+
}
76+
77+
/*
78+
** Copies the content of the buffer before '\0' or '\n' to string.
79+
** Copies the content of the buffer after '\n' to buffer_list.
80+
** In case the EOF is copied to the string returns 0.
81+
** Else returns 1.
82+
*/
83+
84+
int buffer_to_line(int eof, char *buffer, char **buffer_list, char **line)
85+
{
86+
int i;
87+
88+
i = 0;
89+
while (buffer[i] != '\0' && buffer[i] != '\n')
90+
i++;
91+
*line = ft_memmalloc(i + 1);
92+
if (line == NULL)
93+
return (-1);
94+
ft_memcpy(*line, buffer, i);
95+
if (ft_strchr(buffer, '\n'))
96+
*buffer_list = ft_strdup(&buffer[i + 1]);
97+
else if (!eof)
98+
{
99+
*buffer_list = NULL;
100+
return (0);
101+
}
102+
return (1);
103+
}
104+
105+
/*
106+
** Calling function get_next_linein a loop will allow you to read
107+
** the text available on a file descriptor one line at a time until the EOF.
108+
** Lines are written to double pointer line, exluding newline char.
109+
** The line is freed by the user.
110+
**
111+
** Return value:
112+
** 1 : A line has been read.
113+
** 0 : EOF has been reached.
114+
** -1: An error happened.
115+
*/
116+
117+
int get_next_line(int fd, char **line)
118+
{
119+
static char *buffer_list[1024];
120+
char *buf;
121+
int ret;
122+
123+
ret = 1;
124+
buf = ft_memmalloc(BUFFER_SIZE + 1);
125+
if (fd < 0 || fd > 1024 || !line || read(fd, buf, 0) < 0 || buf == NULL ||
126+
BUFFER_SIZE > 10000000)
127+
{
128+
free(buf);
129+
return (-1);
130+
}
131+
buffer_list[fd] ? ft_memcpy(buf, buffer_list[fd],
132+
ft_strlen(buffer_list[fd])) : 0;
133+
buffer_list[fd] ? free(buffer_list[fd]) : 0;
134+
while (ret > 0 && !ft_strchr(buf, '\n'))
135+
ret = read_and_append_buffer(fd, &buf);
136+
if (ret == -1)
137+
return (-1);
138+
ret = buffer_to_line(ret, buf, &buffer_list[fd], line);
139+
if (buf)
140+
free(buf);
141+
if (ret == 0 || ret == -1)
142+
return (ret);
143+
return (1);
144+
}

get_next_line_bonus.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* :::::::: */
4+
/* get_next_line_bonus.h :+: :+: */
5+
/* +:+ */
6+
/* By: kris <[email protected]> +#+ */
7+
/* +#+ */
8+
/* Created: 2020/05/20 18:44:20 by kris #+# #+# */
9+
/* Updated: 2020/07/05 14:31:18 by kstallen ######## odam.nl */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef GET_NEXT_LINE_BONUS_H
14+
# define GET_NEXT_LINE_BONUS_H
15+
16+
# include <unistd.h>
17+
# include <stdlib.h>
18+
# include <string.h>
19+
20+
# ifndef BUFFER_SIZE
21+
# define BUFFER_SIZE 32
22+
# endif
23+
24+
/*
25+
** get_next_line functions
26+
*/
27+
28+
int get_next_line(int fd, char **line);
29+
int buffer_to_line(int eof, char *buffer, char **buffer_list, char **line);
30+
int read_and_append_buffer(int fd, char **buffer);
31+
32+
/*
33+
** libft functions
34+
*/
35+
36+
size_t ft_strlen(const char *str);
37+
void *ft_memcpy(void *dst, const void *src, size_t n);
38+
char *ft_strjoin(char const *s1, char const *s2);
39+
char *ft_strchr(const char *str, int c);
40+
void *ft_memset(void *b, int c, size_t len);
41+
void *ft_memmalloc(size_t size);
42+
char *ft_strdup(const char *s1);
43+
44+
#endif

0 commit comments

Comments
 (0)