bonus 1try algo finish
This commit is contained in:
@@ -6,44 +6,134 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/11/24 15:55:25 by dgaillet #+# #+# */
|
/* Created: 2025/11/24 15:55:25 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/11/24 17:22:10 by dgaillet ### ########lyon.fr */
|
/* Updated: 2025/11/26 12:20:50 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "get_next_line_bonus.h"
|
#include "get_next_line_bonus.h"
|
||||||
|
|
||||||
static char *get_lines(int fd, char *str)
|
static int del_before_nl(char buf[BUFFER_SIZE])
|
||||||
{
|
{
|
||||||
char buf[BUFFER_SIZE];
|
int nl_i;
|
||||||
int res;
|
int i;
|
||||||
|
char *temp;
|
||||||
|
|
||||||
res = read(fd, buf, BUFFER_SIZE);
|
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||||
while (res > 0)
|
if (nl_i >= 0)
|
||||||
{
|
{
|
||||||
if (!str)
|
temp = ft_substr(buf, nl_i + 1, BUFFER_SIZE - nl_i);
|
||||||
str = ft_strdup(buf);
|
if (!temp)
|
||||||
else
|
return (-1);
|
||||||
str = ft_strjoin_new(str, buf, res);
|
|
||||||
if (!str)
|
|
||||||
return (NULL);
|
|
||||||
ft_bzero(buf, BUFFER_SIZE);
|
ft_bzero(buf, BUFFER_SIZE);
|
||||||
res = read(fd, buf, BUFFER_SIZE);
|
i = 0;
|
||||||
}
|
while (temp[i])
|
||||||
if (res < 0)
|
|
||||||
{
|
{
|
||||||
if (str)
|
buf[i] = temp[i];
|
||||||
free(str);
|
i++;
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
free(temp);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *extract_all_nl(char buf[BUFFER_SIZE], int fd, char *str, int nl_i)
|
||||||
|
{
|
||||||
|
int temp;
|
||||||
|
|
||||||
|
str = ft_substr(buf, 0, BUFFER_SIZE);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
while (nl_i < 0)
|
||||||
|
{
|
||||||
|
str = ft_strjoin_new(str, buf, temp);
|
||||||
|
ft_bzero(buf, BUFFER_SIZE);
|
||||||
|
temp = read(fd, buf, BUFFER_SIZE);
|
||||||
|
if (temp < 0)
|
||||||
|
return (free(str), NULL);
|
||||||
|
if ((!str || ft_strlen(str) == 0) && temp == 0)
|
||||||
|
return (free(str), NULL);
|
||||||
|
if (temp == 0)
|
||||||
|
return (str);
|
||||||
|
nl_i = index_of_nl(buf, temp);
|
||||||
|
}
|
||||||
|
str = ft_strjoin_new(str, buf, nl_i);
|
||||||
|
temp = del_before_nl(buf);
|
||||||
|
if (temp < 0)
|
||||||
|
return (free(str), NULL);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_gnl_extra(char buf[BUFFER_SIZE], int fd)
|
||||||
|
{
|
||||||
|
int nl_i;
|
||||||
|
int temp;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||||
|
if (nl_i >= 0)
|
||||||
|
{
|
||||||
|
str = ft_substr(buf, 0, nl_i);
|
||||||
|
temp = del_before_nl(buf);
|
||||||
|
if (temp < 0)
|
||||||
|
return (free(str), NULL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
str = extract_all_nl(buf, fd, "", nl_i);
|
||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_next_line(int fd)
|
char *get_next_line(int fd)
|
||||||
{
|
{
|
||||||
static char *strs[1024];
|
static char strs[1024][BUFFER_SIZE];
|
||||||
|
char *to_return;
|
||||||
if (!strs[fd])
|
|
||||||
strs[fd] = get_lines(fd, strs[fd]);
|
|
||||||
|
|
||||||
|
to_return = ft_gnl_extra(strs[fd], fd);
|
||||||
|
return (to_return);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
int fd1;
|
||||||
|
int fd2;
|
||||||
|
int fd3;
|
||||||
|
|
||||||
|
fd1 = open("a.txt", O_RDONLY);
|
||||||
|
fd2 = open("b.txt", O_RDONLY);
|
||||||
|
fd3 = open("c.txt", O_RDONLY);
|
||||||
|
str = get_next_line(fd1);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd2);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd3);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd1);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd2);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd3);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd1);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd2);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd3);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
str = get_next_line(fd1);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/11/24 15:56:45 by dgaillet #+# #+# */
|
/* Created: 2025/11/24 15:56:45 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/11/24 17:22:18 by dgaillet ### ########lyon.fr */
|
/* Updated: 2025/11/25 15:52:05 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -23,7 +23,8 @@ char *get_next_line(int fd);
|
|||||||
|
|
||||||
size_t ft_strlen(const char *s);
|
size_t ft_strlen(const char *s);
|
||||||
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
|
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
|
||||||
char *ft_strdup(const char *s);
|
|
||||||
void ft_bzero(void *s, size_t n);
|
void ft_bzero(void *s, size_t n);
|
||||||
|
int index_of_nl(char *str, int limit);
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
|
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/11/24 14:01:01 by dgaillet ### ########lyon.fr */
|
/* Updated: 2025/11/25 12:55:35 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,6 +17,8 @@ int index_of_nl(char *str, int limit)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (-1);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < limit && str[i])
|
while (i < limit && str[i])
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,12 +6,28 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/11/24 16:04:39 by dgaillet #+# #+# */
|
/* Created: 2025/11/24 16:04:39 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/11/24 16:12:37 by dgaillet ### ########lyon.fr */
|
/* Updated: 2025/11/25 15:51:31 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int index_of_nl(char *str, int limit)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (-1);
|
||||||
|
i = 0;
|
||||||
|
while (i < limit && str[i])
|
||||||
|
{
|
||||||
|
if (str[i] == '\n')
|
||||||
|
return (i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
size_t ft_strlen(const char *s)
|
size_t ft_strlen(const char *s)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
@@ -48,22 +64,30 @@ char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
|
|||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ft_strdup(const char *s)
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
{
|
{
|
||||||
char *dest;
|
size_t i;
|
||||||
int i;
|
char *sub_str;
|
||||||
|
size_t s_len;
|
||||||
|
|
||||||
dest = malloc(sizeof(char) *(ft_strlen(s) + 1));
|
if (!s)
|
||||||
if (!dest)
|
return (NULL);
|
||||||
|
s_len = ft_strlen(s);
|
||||||
|
if (start > s_len)
|
||||||
|
len = 0;
|
||||||
|
else if (s_len < (start + len))
|
||||||
|
len = s_len - start;
|
||||||
|
sub_str = malloc(sizeof(char) * (len + 1));
|
||||||
|
if (!sub_str)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s[i])
|
while (i < len)
|
||||||
{
|
{
|
||||||
dest[i] = s[i];
|
sub_str[i] = s[start + i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
dest[i] = '\0';
|
sub_str[i] = '\0';
|
||||||
return (dest);
|
return (sub_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_bzero(void *s, size_t n)
|
void ft_bzero(void *s, size_t n)
|
||||||
|
|||||||
Reference in New Issue
Block a user