Ajout get_next_line
This commit is contained in:
parent
e22383189f
commit
c3e321b3ab
157
get_next_line.c
157
get_next_line.c
|
@ -12,10 +12,16 @@
|
||||||
|
|
||||||
#include "get_next_line.h"
|
#include "get_next_line.h"
|
||||||
|
|
||||||
void *read_line(char *str, char *buff, int fd, int y)
|
static char *read_line(int fd)
|
||||||
{
|
{
|
||||||
|
char *buff;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
buff = ft_calloc(BUFFER_SIZE + 1, 1);
|
||||||
|
if (buff == NULL)
|
||||||
|
return (NULL);
|
||||||
y = read(fd, buff, BUFFER_SIZE);
|
y = read(fd, buff, BUFFER_SIZE);
|
||||||
if (y == -1 || (ft_strlen(buff) == 0 && str == NULL))
|
if (y == -1)
|
||||||
{
|
{
|
||||||
free(buff);
|
free(buff);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
@ -24,102 +30,85 @@ void *read_line(char *str, char *buff, int fd, int y)
|
||||||
return (buff);
|
return (buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_next(char *str, char *buff, int y, int last_malloc)
|
static char *join_str(char *s1, char *s2)
|
||||||
{
|
{
|
||||||
if (y == -1 || (ft_strlen(buff) == 0 && str == NULL))
|
char *n_str;
|
||||||
|
int i;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = ft_strlen(s1);
|
||||||
|
n_str = ft_calloc(len + ft_strlen(s2) + 1, 1);
|
||||||
|
if (n_str == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = -1;
|
||||||
|
if (s1 != NULL)
|
||||||
|
while (s1[++i])
|
||||||
|
n_str[i] = s1[i];
|
||||||
|
i = -1;
|
||||||
|
while (s2[++i])
|
||||||
|
n_str[i + len] = s2[i];
|
||||||
|
n_str[i + len] = '\0';
|
||||||
|
free(s1);
|
||||||
|
free(s2);
|
||||||
|
return (n_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *get_text(char *save)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *new_s;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (save[i] && save[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (save[i] == '\n')
|
||||||
|
i++;
|
||||||
|
new_s = ft_calloc(i + 1, 1);
|
||||||
|
if (new_s == NULL)
|
||||||
|
return (NULL);
|
||||||
|
ft_strlcpy(new_s, save, i + 1);
|
||||||
|
ft_strlcpy(save, save + i, ft_strlen(save));
|
||||||
|
return (new_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *make_free(char *buff, char **save, int choice)
|
||||||
|
{
|
||||||
|
if (choice == 1)
|
||||||
{
|
{
|
||||||
free(buff);
|
free(buff);
|
||||||
return (-1);
|
free(*save);
|
||||||
}
|
|
||||||
buff[y] = '\0';
|
|
||||||
if (str == NULL)
|
|
||||||
{
|
|
||||||
str = ft_calloc(BUFFER_SIZE + 1, 1);
|
|
||||||
ft_strlcpy(str, buff, BUFFER_SIZE + 1);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ((size_t)last_malloc - 1 < ft_strlen(str) + ft_strlen(buff))
|
free(*save);
|
||||||
{
|
*save = NULL;
|
||||||
str = ft_realloc(str, last_malloc);
|
|
||||||
last_malloc += BUFFER_SIZE;
|
|
||||||
}
|
|
||||||
ft_strlcpy(str + ft_strlen(str), buff, BUFFER_SIZE + 1);
|
|
||||||
}
|
}
|
||||||
return (last_malloc);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_next_line(int fd)
|
char *get_next_line(int fd)
|
||||||
{
|
{
|
||||||
char *buff;
|
char *buff;
|
||||||
static char *str;
|
char *str;
|
||||||
size_t i;
|
static char *save;
|
||||||
int y;
|
|
||||||
static int last_malloc;
|
|
||||||
|
|
||||||
last_malloc = BUFFER_SIZE + 1;
|
buff = NULL;
|
||||||
buff = ft_calloc(BUFFER_SIZE + 1, 1);
|
while (buff == NULL || (ft_strchr(save, '\n') == -1))
|
||||||
y = 1;
|
|
||||||
while ((str == NULL || ft_strchr(str, '\n') == -1) && y > 0)
|
|
||||||
{
|
{
|
||||||
y = read(fd, buff, BUFFER_SIZE);
|
buff = read_line(fd);
|
||||||
last_malloc = get_next(str, buff, y, last_malloc);
|
if (buff == NULL || (ft_strlen(buff) == 0 && ft_strlen(save) == 0))
|
||||||
if (last_malloc == -1)
|
return (make_free(buff, &save, 1));
|
||||||
|
if (ft_strlen(buff) == 0)
|
||||||
|
{
|
||||||
|
free(buff);
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
save = join_str(save, buff);
|
||||||
|
if (save == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
i = 0;
|
str = get_text(save);
|
||||||
while (str[i] != '\n' && str[i])
|
if (ft_strlen(save) == 0)
|
||||||
i++;
|
make_free(NULL, &save, 2);
|
||||||
if (str[i] == '\n')
|
return (str);
|
||||||
i++;
|
|
||||||
free(buff);
|
|
||||||
buff = malloc(i + 1);
|
|
||||||
ft_strlcpy(buff, str, i + 1);
|
|
||||||
buff[i] = '\0';
|
|
||||||
ft_strlcpy(str, str + i, BUFFER_SIZE + 1);
|
|
||||||
if (ft_strlen(str) == 0)
|
|
||||||
{
|
|
||||||
free(str);
|
|
||||||
str = NULL;
|
|
||||||
}
|
|
||||||
return (buff);
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
char *r;
|
|
||||||
int fd;
|
|
||||||
(void) argc;
|
|
||||||
|
|
||||||
fd = open(argv[1], 0);
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
free(r);
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
free(r);
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
r = get_next_line(fd);
|
|
||||||
printf("%s", r);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
free(r);
|
|
||||||
close(fd);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/15 23:40:12 by erey-bet #+# #+# */
|
/* Created: 2022/10/15 23:40:12 by erey-bet #+# #+# */
|
||||||
/* Updated: 2022/10/15 23:51:30 by erey-bet ### ########.fr */
|
/* Updated: 2022/11/15 19:42:13 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -15,6 +15,9 @@
|
||||||
|
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
# define BUFFER_SIZE 41
|
||||||
|
# endif
|
||||||
|
|
||||||
char *get_next_line(int fd);
|
char *get_next_line(int fd);
|
||||||
void *ft_calloc(size_t nitems, size_t size);
|
void *ft_calloc(size_t nitems, size_t size);
|
||||||
|
|
116
get_next_line_bonus.c
Normal file
116
get_next_line_bonus.c
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/10/15 23:42:35 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2022/11/16 13:20:26 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
static char *read_line(int fd)
|
||||||
|
{
|
||||||
|
char *buff;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
buff = ft_calloc(BUFFER_SIZE + 1, 1);
|
||||||
|
if (buff == NULL)
|
||||||
|
return (NULL);
|
||||||
|
y = read(fd, buff, BUFFER_SIZE);
|
||||||
|
if (y == -1)
|
||||||
|
{
|
||||||
|
free(buff);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
buff[y] = '\0';
|
||||||
|
return (buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *join_str(char *s1, char *s2)
|
||||||
|
{
|
||||||
|
char *n_str;
|
||||||
|
int i;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = ft_strlen(s1);
|
||||||
|
n_str = ft_calloc(len + ft_strlen(s2) + 1, 1);
|
||||||
|
if (n_str == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = -1;
|
||||||
|
if (s1 != NULL)
|
||||||
|
while (s1[++i])
|
||||||
|
n_str[i] = s1[i];
|
||||||
|
i = -1;
|
||||||
|
while (s2[++i])
|
||||||
|
n_str[i + len] = s2[i];
|
||||||
|
n_str[i + len] = '\0';
|
||||||
|
free(s1);
|
||||||
|
free(s2);
|
||||||
|
return (n_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *get_text(char *save)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *new_s;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (save[i] && save[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (save[i] == '\n')
|
||||||
|
i++;
|
||||||
|
new_s = ft_calloc(i + 1, 1);
|
||||||
|
if (new_s == NULL)
|
||||||
|
return (NULL);
|
||||||
|
ft_strlcpy(new_s, save, i + 1);
|
||||||
|
ft_strlcpy(save, save + i, ft_strlen(save));
|
||||||
|
return (new_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *make_free(char *buff, char **save, int choice)
|
||||||
|
{
|
||||||
|
if (choice == 1)
|
||||||
|
{
|
||||||
|
free(buff);
|
||||||
|
free(*save);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
free(*save);
|
||||||
|
*save = NULL;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
char *buff;
|
||||||
|
char *str;
|
||||||
|
static char *save[1024];
|
||||||
|
|
||||||
|
if (fd < 0 || fd >= 1024)
|
||||||
|
return (NULL);
|
||||||
|
buff = NULL;
|
||||||
|
while (buff == NULL || ft_strchr(save[fd], '\n') == -1)
|
||||||
|
{
|
||||||
|
buff = read_line(fd);
|
||||||
|
if (buff == NULL || (ft_strlen(buff) == 0 && ft_strlen(save[fd]) == 0))
|
||||||
|
return (make_free(buff, &save[fd], 1));
|
||||||
|
if (ft_strlen(buff) == 0)
|
||||||
|
{
|
||||||
|
free(buff);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
save[fd] = join_str(save[fd], buff);
|
||||||
|
if (save[fd] == NULL)
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
str = get_text(save[fd]);
|
||||||
|
if (ft_strlen(save[fd]) == 0)
|
||||||
|
make_free(NULL, &save[fd], 2);
|
||||||
|
return (str);
|
||||||
|
}
|
29
get_next_line_bonus.h
Normal file
29
get_next_line_bonus.h
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_bonus.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/10/15 23:40:12 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2022/11/15 20:00:55 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef GET_NEXT_LINE_BONUS_H
|
||||||
|
# define GET_NEXT_LINE_BONUS_H
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
# define BUFFER_SIZE 41
|
||||||
|
# endif
|
||||||
|
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
void *ft_calloc(size_t nitems, size_t size);
|
||||||
|
size_t ft_strlen(const char *str);
|
||||||
|
int ft_strchr(const char *str, int search);
|
||||||
|
void ft_strlcpy(char *dest, const char *src, size_t size);
|
||||||
|
void *ft_realloc(void *ptr, size_t size);
|
||||||
|
|
||||||
|
#endif
|
|
@ -37,6 +37,8 @@ size_t ft_strlen(const char *str)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (str == NULL)
|
||||||
|
return (0);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
i++;
|
i++;
|
||||||
|
@ -72,7 +74,7 @@ void ft_strlcpy(char *dest, const char *src, size_t size)
|
||||||
dest[i] = '\0';
|
dest[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ft_realloc(void *ptr, size_t size)
|
void *ft_realloc(void *ptr, size_t new_size)
|
||||||
{
|
{
|
||||||
char *str;
|
char *str;
|
||||||
char *p;
|
char *p;
|
||||||
|
@ -85,7 +87,7 @@ void *ft_realloc(void *ptr, size_t size)
|
||||||
str[i] = p[i];
|
str[i] = p[i];
|
||||||
str[i] = '\0';
|
str[i] = '\0';
|
||||||
free(ptr);
|
free(ptr);
|
||||||
p = ft_calloc(size + BUFFER_SIZE + 1, 1);
|
p = ft_calloc(new_size, 1);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
104
get_next_line_utils_bonus.c
Normal file
104
get_next_line_utils_bonus.c
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/07 17:19:41 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2022/11/15 19:57:30 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
void *ft_calloc(size_t nitems, size_t size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
if (nitems == 0 || size == 0)
|
||||||
|
return (malloc(0));
|
||||||
|
if (nitems * size < nitems)
|
||||||
|
return (NULL);
|
||||||
|
tmp = malloc(nitems * size);
|
||||||
|
if (tmp == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (i < nitems * size)
|
||||||
|
{
|
||||||
|
tmp[i] = '\0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlen(const char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (str == NULL)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_strchr(const char *str, int search)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i] || str[i] == (unsigned char)search)
|
||||||
|
{
|
||||||
|
if (str[i] == (unsigned char)search)
|
||||||
|
return (i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_strlcpy(char *dest, const char *src, size_t size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!size || !dest || !src)
|
||||||
|
return ;
|
||||||
|
while (i < size - 1 && src[i] != '\0')
|
||||||
|
{
|
||||||
|
dest[i] = src[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dest[i] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_realloc(void *ptr, size_t new_size)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
char *p;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
p = ptr;
|
||||||
|
str = ft_calloc(ft_strlen(p) + 1, 1);
|
||||||
|
while (p[++i])
|
||||||
|
str[i] = p[i];
|
||||||
|
str[i] = '\0';
|
||||||
|
free(ptr);
|
||||||
|
p = ft_calloc(new_size, 1);
|
||||||
|
if (p == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
p[i] = str[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
p[i] = '\0';
|
||||||
|
ptr = p;
|
||||||
|
free(str);
|
||||||
|
str = NULL;
|
||||||
|
return (ptr);
|
||||||
|
}
|
Loading…
Reference in a new issue