Skip to content

Commit bc96941

Browse files
author
Regulus Biodiesel
committed
17.01.2022
0 parents  commit bc96941

7 files changed

+481
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
The aim of the main part of this project is to code a function that returns a line,
2+
read from a file descriptor.
3+
4+
The aim of the bonus part of this project is to code a function get_next_line that be able to manage multiple file descriptors. For
5+
example, if the file descriptors 3, 4 and 5 are accessible for reading, then you can
6+
call get_next_line once on 3, once on 4, once again on 3 then once on 5 etc.
7+
without losing the reading thread on each of the descriptors.

get_next_line.c

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: rbiodies <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/10/18 11:07:14 by rbiodies #+# #+# */
9+
/* Updated: 2021/10/20 14:54:42 by rbiodies ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line.h"
14+
15+
static char *make_newbackup(char *backup)
16+
{
17+
int i;
18+
char *newbackup;
19+
20+
i = 0;
21+
while (backup[i] && backup[i] != '\n')
22+
i++;
23+
if (backup[i] == '\0')
24+
{
25+
free(backup);
26+
return (NULL);
27+
}
28+
newbackup = (char *)malloc(sizeof(char) * (ft_strlen(backup) - i + 1));
29+
if (!newbackup)
30+
return (NULL);
31+
ft_strlcpy(newbackup, backup + i + 1, ft_strlen(backup) - i + 1);
32+
free(backup);
33+
return (newbackup);
34+
}
35+
36+
static char *make_line(char *backup)
37+
{
38+
int i;
39+
char *line;
40+
41+
i = 0;
42+
while (backup[i] && backup[i] != '\n')
43+
i++;
44+
line = malloc(sizeof(char *) * (i + 2));
45+
if (!line)
46+
return (NULL);
47+
ft_strlcpy(line, backup, i + 2);
48+
if (line[0] == '\0')
49+
{
50+
free(line);
51+
return (NULL);
52+
}
53+
return (line);
54+
}
55+
56+
static char *make_backup(int fd, char *backup, char *buffer)
57+
{
58+
char *tmp;
59+
int bytes_was_read;
60+
int flag;
61+
62+
bytes_was_read = 1;
63+
flag = 0;
64+
while (flag == 0 && bytes_was_read != 0)
65+
{
66+
bytes_was_read = read(fd, buffer, BUFFER_SIZE);
67+
if (bytes_was_read == -1)
68+
{
69+
free(buffer);
70+
return (NULL);
71+
}
72+
buffer[bytes_was_read] = '\0';
73+
if (!backup)
74+
backup = ft_strdup("");
75+
tmp = backup;
76+
backup = ft_strjoin(backup, buffer);
77+
free(tmp);
78+
if (ft_strchr(backup, '\n'))
79+
flag = 1;
80+
}
81+
free(buffer);
82+
return (backup);
83+
}
84+
85+
char *get_next_line(int fd)
86+
{
87+
char *buffer;
88+
static char *backup;
89+
char *line;
90+
91+
line = NULL;
92+
if (fd < 0 || BUFFER_SIZE <= 0)
93+
return (NULL);
94+
buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
95+
if (!buffer)
96+
{
97+
free(buffer);
98+
return (NULL);
99+
}
100+
backup = make_backup(fd, backup, buffer);
101+
if (!backup)
102+
return (NULL);
103+
line = make_line(backup);
104+
backup = make_newbackup(backup);
105+
return (line);
106+
}

get_next_line.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: rbiodies <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/10/19 11:21:33 by rbiodies #+# #+# */
9+
/* Updated: 2021/10/20 14:54:47 by rbiodies ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef GET_NEXT_LINE_H
14+
# define GET_NEXT_LINE_H
15+
16+
# include <stdlib.h>
17+
# include <unistd.h>
18+
19+
char *get_next_line(int fd);
20+
char *ft_strjoin(char const *s1, char const *s2);
21+
char *ft_strchr(const char *s, int c);
22+
char *ft_strdup(const char *s1);
23+
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
24+
size_t ft_strlen(const char *s);
25+
26+
#endif

get_next_line_bonus.c

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_bonus.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: rbiodies <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/10/20 11:14:46 by rbiodies #+# #+# */
9+
/* Updated: 2021/10/21 16:21:41 by rbiodies ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line_bonus.h"
14+
15+
static char *make_newbackup(char *backup)
16+
{
17+
int i;
18+
char *newbackup;
19+
20+
i = 0;
21+
while (backup[i] && backup[i] != '\n')
22+
i++;
23+
if (backup[i] == '\0')
24+
{
25+
free(backup);
26+
return (NULL);
27+
}
28+
newbackup = (char *)malloc(sizeof(char) * (ft_strlen(backup) - i + 1));
29+
if (!newbackup)
30+
return (NULL);
31+
ft_strlcpy(newbackup, backup + i + 1, ft_strlen(backup) - i + 1);
32+
free(backup);
33+
return (newbackup);
34+
}
35+
36+
static char *make_line(char *backup)
37+
{
38+
int i;
39+
char *line;
40+
41+
i = 0;
42+
while (backup[i] && backup[i] != '\n')
43+
i++;
44+
line = malloc(sizeof(char *) * (i + 2));
45+
if (!line)
46+
return (NULL);
47+
ft_strlcpy(line, backup, i + 2);
48+
if (line[0] == '\0')
49+
{
50+
free(line);
51+
return (NULL);
52+
}
53+
return (line);
54+
}
55+
56+
static char *make_backup(int fd, char *backup, char *buffer)
57+
{
58+
char *tmp;
59+
int bytes_was_read;
60+
int flag;
61+
62+
bytes_was_read = 1;
63+
flag = 0;
64+
while (flag == 0 && bytes_was_read != 0)
65+
{
66+
bytes_was_read = read(fd, buffer, BUFFER_SIZE);
67+
if (bytes_was_read == -1)
68+
{
69+
free(buffer);
70+
return (NULL);
71+
}
72+
buffer[bytes_was_read] = '\0';
73+
if (!backup)
74+
backup = ft_strdup("");
75+
tmp = backup;
76+
backup = ft_strjoin(backup, buffer);
77+
free(tmp);
78+
if (ft_strchr(backup, '\n'))
79+
flag = 1;
80+
}
81+
free(buffer);
82+
return (backup);
83+
}
84+
85+
char *get_next_line(int fd)
86+
{
87+
char *buffer;
88+
static char *backup[FD_MAX];
89+
char *line;
90+
91+
line = NULL;
92+
if (fd < 0 || BUFFER_SIZE <= 0 || fd > FD_MAX)
93+
return (NULL);
94+
buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
95+
if (!buffer)
96+
{
97+
free(buffer);
98+
return (NULL);
99+
}
100+
backup[fd] = make_backup(fd, backup[fd], buffer);
101+
if (!backup[fd])
102+
return (NULL);
103+
line = make_line(backup[fd]);
104+
backup[fd] = make_newbackup(backup[fd]);
105+
return (line);
106+
}

get_next_line_bonus.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_bonus.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: rbiodies <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/10/20 11:17:35 by rbiodies #+# #+# */
9+
/* Updated: 2021/10/20 14:55:06 by rbiodies ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef GET_NEXT_LINE_BONUS_H
14+
# define GET_NEXT_LINE_BONUS_H
15+
16+
# include <stdlib.h>
17+
# include <unistd.h>
18+
19+
# ifndef FD_MAX
20+
# define FD_MAX 256
21+
# endif
22+
23+
char *get_next_line(int fd);
24+
char *ft_strjoin(char const *s1, char const *s2);
25+
char *ft_strchr(const char *s, int c);
26+
char *ft_strdup(const char *s1);
27+
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
28+
size_t ft_strlen(const char *s);
29+
30+
#endif

get_next_line_utils.c

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_utils.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: rbiodies <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/10/18 12:33:30 by rbiodies #+# #+# */
9+
/* Updated: 2021/10/20 14:54:53 by rbiodies ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line.h"
14+
15+
size_t ft_strlen(char const *s)
16+
{
17+
unsigned long int len;
18+
19+
len = 0;
20+
while (s[len] != '\0')
21+
len++;
22+
return (len);
23+
}
24+
25+
char *ft_strjoin(char const *s1, char const *s2)
26+
{
27+
char *buffer;
28+
unsigned int i;
29+
unsigned int j;
30+
31+
i = 0;
32+
j = 0;
33+
if (s1 == NULL || s2 == NULL)
34+
return (NULL);
35+
buffer = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
36+
if (buffer == NULL)
37+
return (NULL);
38+
while (s1[i] != '\0')
39+
{
40+
buffer[i] = s1[i];
41+
i++;
42+
}
43+
while (s2[j] != '\0')
44+
{
45+
buffer[i] = s2[j];
46+
i++;
47+
j++;
48+
}
49+
buffer[i] = '\0';
50+
return (buffer);
51+
}
52+
53+
char *ft_strchr(const char *s, int c)
54+
{
55+
int i;
56+
char ch;
57+
58+
i = 0;
59+
ch = (char)c;
60+
while (s[i] != '\0' && s[i] != ch)
61+
i++;
62+
if (s[i] == ch)
63+
return ((char *)s);
64+
return ((void *)0);
65+
}
66+
67+
char *ft_strdup(const char *s1)
68+
{
69+
int i;
70+
char *buffer;
71+
72+
i = 0;
73+
buffer = (char *)malloc(ft_strlen(s1) + 1);
74+
if (buffer == NULL)
75+
return (0);
76+
while (s1[i] != '\0')
77+
{
78+
buffer[i] = s1[i];
79+
i++;
80+
}
81+
buffer[i] = '\0';
82+
return (buffer);
83+
}
84+
85+
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
86+
{
87+
size_t i;
88+
89+
i = 0;
90+
if (dstsize > 0)
91+
{
92+
while (dstsize != 1 && src[i] != '\0')
93+
{
94+
dst[i] = src[i];
95+
i++;
96+
dstsize--;
97+
}
98+
dst[i] = '\0';
99+
}
100+
while (src[i] != '\0')
101+
i++;
102+
return (i);
103+
}

0 commit comments

Comments
 (0)