Skip to content

Commit 64dab68

Browse files
authored
OK
0 parents  commit 64dab68

6 files changed

+384
-0
lines changed

get_next_line.c

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

get_next_line.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: javrodri <javrodri@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/11/25 11:52:57 by javrodri #+# #+# */
9+
/* Updated: 2019/11/25 13:37:17 by javrodri ### ########.fr */
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+
19+
int get_next_line(int fd, char **line);
20+
ssize_t ft_strlen(const char *s);
21+
char *ft_strjoin(char *s1, char *s2, ssize_t r_size);
22+
char *ft_substr(char const *s, unsigned int start, ssize_t len);
23+
24+
#endif

get_next_line_bonus.c

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_bonus.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: javrodri <javrodri@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/11/25 10:14:38 by javrodri #+# #+# */
9+
/* Updated: 2019/11/25 14:33:37 by javrodri ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line_bonus.h"
14+
15+
static char *ft_strnew(void)
16+
{
17+
char *ret;
18+
19+
if (!(ret = (char *)malloc(sizeof(char))))
20+
return (0);
21+
ret[0] = '\0';
22+
return (ret);
23+
}
24+
25+
static ssize_t pos_nl(char *s)
26+
{
27+
ssize_t i;
28+
29+
i = 0;
30+
while (s[i] != '\0')
31+
{
32+
if (s[i] == '\n')
33+
return (i);
34+
i++;
35+
}
36+
return (-1);
37+
}
38+
39+
static int free_cache(char **cache, int ret)
40+
{
41+
if (*cache)
42+
{
43+
free(*cache);
44+
*cache = 0;
45+
}
46+
return (ret);
47+
}
48+
49+
static int extract(char **line, char **cache, int idx)
50+
{
51+
char *tmp;
52+
int ret;
53+
54+
if (idx >= 0)
55+
{
56+
if (!(*line = ft_substr(*cache, 0, idx)))
57+
return (free_cache(cache, -1));
58+
if (!(tmp = ft_substr(*cache, idx + 1, ft_strlen(*cache) - idx - 1)))
59+
return (free_cache(cache, -1));
60+
ret = 1;
61+
}
62+
else
63+
{
64+
if (!(*line = ft_substr(*cache, 0, ft_strlen(*cache))))
65+
return (free_cache(cache, -1));
66+
tmp = 0;
67+
ret = 0;
68+
}
69+
free_cache(cache, 0);
70+
*cache = tmp;
71+
return (ret);
72+
}
73+
74+
int get_next_line(int fd, char **line)
75+
{
76+
ssize_t r_size;
77+
char buff[BUFFER_SIZE + 1];
78+
static char *cache[4096];
79+
char *tmp;
80+
81+
if (BUFFER_SIZE == 0)
82+
return (-1);
83+
if (fd < 0 || !line)
84+
return (free_cache(&cache[fd], -1));
85+
while ((r_size = read(fd, buff, BUFFER_SIZE)) > 0)
86+
{
87+
buff[r_size] = '\0';
88+
if (!(tmp = ft_strjoin(cache[fd], buff, r_size)))
89+
return (free_cache(&cache[fd], -1));
90+
free_cache(&cache[fd], 0);
91+
cache[fd] = tmp;
92+
if (pos_nl(cache[fd]) != -1)
93+
break ;
94+
}
95+
if (r_size < 0)
96+
return (free_cache(&cache[fd], -1));
97+
if (r_size == 0 && (!cache[fd] || *cache[fd] == '\0')
98+
&& (*line = ft_strnew()))
99+
return (free_cache(&cache[fd], 0));
100+
return (extract(line, &cache[fd], pos_nl(cache[fd])));
101+
}

get_next_line_bonus.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_bonus.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: javrodri <javrodri@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/11/25 11:52:57 by javrodri #+# #+# */
9+
/* Updated: 2019/11/25 13:56:51 by javrodri ### ########.fr */
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+
19+
int get_next_line(int fd, char **line);
20+
ssize_t ft_strlen(const char *s);
21+
char *ft_strjoin(char *s1, char *s2, ssize_t r_size);
22+
char *ft_substr(char const *s, unsigned int start, ssize_t len);
23+
24+
#endif

get_next_line_utils.c

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_utils.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: javrodri <javrodri@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/11/25 11:46:19 by javrodri #+# #+# */
9+
/* Updated: 2019/11/25 13:36:47 by javrodri ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line.h"
14+
15+
ssize_t ft_strlen(const char *s)
16+
{
17+
ssize_t len;
18+
19+
if (!s)
20+
return (0);
21+
len = 0;
22+
while (s[len] != '\0')
23+
len++;
24+
return (len);
25+
}
26+
27+
char *ft_substr(const char *s, unsigned int start, ssize_t len)
28+
{
29+
char *ptr;
30+
char *ret;
31+
char *cpy_ret;
32+
33+
if (!s || !(ret = (char*)malloc(sizeof(char) * (len + 1))))
34+
return (0);
35+
if (start >= ft_strlen(s))
36+
ret[0] = '\0';
37+
else
38+
{
39+
ptr = (char*)s + start;
40+
cpy_ret = ret;
41+
while (*ptr != '\0' && len-- > 0)
42+
*cpy_ret++ = *ptr++;
43+
*cpy_ret = '\0';
44+
}
45+
return (ret);
46+
}
47+
48+
char *ft_strjoin(char *s1, char *s2, ssize_t r_size)
49+
{
50+
int len;
51+
char *res;
52+
ssize_t i;
53+
54+
len = ft_strlen(s1) + r_size;
55+
if (!(res = (char*)malloc(sizeof(char) * (len + 1))))
56+
return (0);
57+
i = 0;
58+
while (s1 && s1[i] != '\0')
59+
{
60+
res[i] = s1[i];
61+
i++;
62+
}
63+
while (s2 && *s2 != '\0')
64+
res[i++] = *s2++;
65+
res[i] = '\0';
66+
return (res);
67+
}

get_next_line_utils_bonus.c

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_utils_bonus.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: javrodri <javrodri@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/11/25 11:46:19 by javrodri #+# #+# */
9+
/* Updated: 2019/11/25 13:57:14 by javrodri ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "get_next_line_bonus.h"
14+
15+
ssize_t ft_strlen(const char *s)
16+
{
17+
ssize_t len;
18+
19+
if (!s)
20+
return (0);
21+
len = 0;
22+
while (s[len] != '\0')
23+
len++;
24+
return (len);
25+
}
26+
27+
char *ft_substr(const char *s, unsigned int start, ssize_t len)
28+
{
29+
char *ptr;
30+
char *ret;
31+
char *cpy_ret;
32+
33+
if (!s || !(ret = (char*)malloc(sizeof(char) * (len + 1))))
34+
return (0);
35+
if (start >= ft_strlen(s))
36+
ret[0] = '\0';
37+
else
38+
{
39+
ptr = (char*)s + start;
40+
cpy_ret = ret;
41+
while (*ptr != '\0' && len-- > 0)
42+
*cpy_ret++ = *ptr++;
43+
*cpy_ret = '\0';
44+
}
45+
return (ret);
46+
}
47+
48+
char *ft_strjoin(char *s1, char *s2, ssize_t r_size)
49+
{
50+
int len;
51+
char *res;
52+
ssize_t i;
53+
54+
len = ft_strlen(s1) + r_size;
55+
if (!(res = (char*)malloc(sizeof(char) * (len + 1))))
56+
return (0);
57+
i = 0;
58+
while (s1 && s1[i] != '\0')
59+
{
60+
res[i] = s1[i];
61+
i++;
62+
}
63+
while (s2 && *s2 != '\0')
64+
res[i++] = *s2++;
65+
res[i] = '\0';
66+
return (res);
67+
}

0 commit comments

Comments
 (0)