This commit is contained in:
kinou-p 2021-11-22 11:33:32 +01:00
parent 0c29f08378
commit 3ba1886eee
5 changed files with 172 additions and 195 deletions

View File

@ -1 +0,0 @@
# get_next_line

View File

@ -1,175 +1,100 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */ /* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: kinou <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 02:40:53 by apommier #+# #+# */ /* Created: 2021/10/23 07:55:17 by kinou #+# #+# */
/* Updated: 2020/12/18 10:05:11 by apommier ### ########.fr */ /* Updated: 2021/11/01 10:21:35 by kinou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
#include <sys/types.h>
#include <sys/stat.h> char *ft_free(char *save, int *end)
#include <fcntl.h> {
#include <stdio.h> if (!*end)
{
char *ft_strjoin(char *save, char *s2) free(save);
{ free(end);
char *dest; return (0);
int i; }
int j; free(end);
return (save);
i = 0; }
j = 0;
if (!save && !s2) char *set_line(char *line, char *save)
return(0); {
if(!(dest = (char*)malloc(ft_strlen(save) + ft_strlen(s2) + 1))) int i;
return (0);
while (save && save[i]) i = 0;
{ line = ft_strjoin(save, 0);
dest[j] = save[i]; while (line[i] && line[i] != '\n')
j++; i++;
i++; if (line[i] == '\n')
} {
i = 0; while (line[i++])
while (s2 && s2[i]) line[i] = '\0';
{ }
dest[j] = s2[i]; return (line);
j++; }
i++;
} char *set_save(char *save)
dest[j] = 0; {
return (dest); char *delete;
} int i;
char *up_save(char **save, int fd, int *end) i = 0;
{ delete = save;
char *dest; while (save[i] && save[i] != '\n')
char *delete; i++;
if (save[i] != '\n')
dest = 0; i = 0;
delete = *save; save = ft_strjoin((save + i + 1), 0);
if (!(dest = (char*)malloc(1 + BUFFER_SIZE))) free(delete);
{ return (save);
if (*save) }
free(*save);
return (0); char *next_line(char *save, int *end, int fd)
} {
*end = read(fd, dest, BUFFER_SIZE); char *delete;
if (*end == -1) char *dest;
return (0);
dest[*end] = 0; while (!ft_strchr(save, '\n') && *end > 0)
if (!(*save = ft_strjoin(*save, dest))) {
{ dest = ft_calloc(1, BUFFER_SIZE + 1);
free(dest); *end = read(fd, dest, BUFFER_SIZE);
free(delete); delete = save;
return (0); save = ft_strjoin(save, dest);
} free(delete);
free(delete); free(dest);
free(dest); }
return (*save); return (save);
} }
int is_line(char *save, int *end) char *get_next_line(int fd)
{ {
int i; static char *save = NULL;
int *end;
i = 0; char *line;
if (save == 0)
return (1); line = 0;
if (*end < BUFFER_SIZE) if (fd < 0 || BUFFER_SIZE < 1)
{ return (0);
while (save[i] && save[i] != '\n') end = malloc(sizeof(int *));
i++; *end = 1;
if (save[i] == '\n') if (save == NULL)
*end = BUFFER_SIZE; save = ft_calloc(1, 1);
} save = next_line(save, end, fd);
i = 0; line = set_line(line, save);
while (save[i]) if (ft_strlen(line) > 0)
{ {
if (save[i] == '\n') save = set_save(save);
{ save = ft_free(save, end);
save[i] = 0; return (line);
return (1); }
} free(line);
i++; save = ft_free(save, end);
} return (0);
if (*end < BUFFER_SIZE) }
return (1);
return (0);
}
char *is_next_line(char **save, int *end, int fd)
{
char *delete;
int i;
i = 0;
delete = *save;
while (*save && (*save)[i])
i++;
if (*save && !(*save = ft_strjoin(*save + i + 1, 0)))
{
free(delete);
return (0);
}
up_save(save, fd, end);
while (!is_line(*save, end))
{
up_save(save, fd, end);
if (!(*save))
{
free(delete);
return (0);
}
}
free(delete);
return (*save);
}
int get_next_line(int fd, char **line)
{
int end;
static char **save;
if (!save)
{
if(!(save = malloc(sizeof(char*))))
return (-1);
*save = 0;
}
else if (*save == 0)
{
*line = *save;
free(*save);
free(save);
return (0);
}
end = BUFFER_SIZE;
if (fd < 0 || !line || BUFFER_SIZE < 1)
{
return (-1);
free(save);
}
is_next_line(save, &end, fd);
if (!(*save))
return (-1);
free(*line);
*line = ft_strjoin(*save, 0);
if (!(*line))
{
free(*save);
return (-1);
}
if (end < BUFFER_SIZE)
{
free(*save);
free(save);
return (0);
}
return (1);
}

View File

@ -6,20 +6,20 @@
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */ /* Created: 2020/12/14 05:58:49 by apommier #+# #+# */
/* Updated: 2020/12/16 08:44:39 by apommier ### ########.fr */ /* Updated: 2021/11/02 18:16:14 by kinou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef GET_NEXT_LINE_H #ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H # define GET_NEXT_LINE_H
#include <stdlib.h> # include <stdlib.h>
#include <unistd.h> # include <unistd.h>
int get_next_line(int fd, char **line);
int is_line(char *save, int *end);
char *up_save(char **save, int fd, int *end);
char *ft_strjoin(char *savee, char *s2);
int ft_strlen(char *s1); int ft_strlen(char *s1);
char *is_nex_line(char **save, int *end, int fd); char *get_next_line(int fd);
char *ft_strjoin(char *savee, char *s2);
char *ft_strchr(const char *s, int c);
void *ft_calloc(size_t nmemb, size_t size);
#endif #endif

View File

@ -12,9 +12,10 @@
#include "get_next_line.h" #include "get_next_line.h"
int ft_strlen(char *s1) int ft_strlen(char *s1)
{ {
int i; int i;
i = 0; i = 0;
if (!s1) if (!s1)
return (0); return (0);
@ -22,3 +23,64 @@ int ft_strlen(char *s1)
i++; i++;
return (i); return (i);
} }
char *ft_strjoin(char *save, char *s2)
{
char *dest;
int i;
int j;
i = 0;
j = 0;
if (!save && !s2)
return (0);
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
while (save && save[i])
{
dest[j] = save[i];
j++;
i++;
}
i = 0;
while (s2 && s2[i])
{
dest[j] = s2[i];
j++;
i++;
}
dest[j] = 0;
return (dest);
}
char *ft_strchr(const char *s, int c)
{
char *str;
if (!s)
return ("no s");
str = (char *)s;
while ((*str != c) && (*str != 0))
str++;
if (*str == c)
return ((char *)str);
else
return (0);
}
void *ft_calloc(size_t nmemb, size_t size)
{
char *new;
int i;
i = 0;
new = malloc(size * nmemb);
if (new)
{
while (size * nmemb - i)
{
new[i] = 0;
i++;
}
}
return ((void *)new);
}

9
texte
View File

@ -1,9 +0,0 @@
ijhgfddfghjkjhgfdl
dhjkjhlkgjgfh
cgbxf
gsdfg
dfgdgfdghdfhdg
dg
dhgdhdfjhdghdghd
hg
dhgdhgdhgdgh``