mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 11:26:51 +01:00
「✨」 feat: Added get_next_line to libft
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
|
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
|
||||||
# Updated: 2024/05/13 17:33:16 by adjoly ### ########.fr #
|
# Updated: 2024/05/25 14:22:48 by adjoly ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -28,6 +28,7 @@ SRCS = src/io/ft_putnbr_fd.c \
|
|||||||
src/io/ft_printf.c \
|
src/io/ft_printf.c \
|
||||||
src/io/ft_printf_fd.c \
|
src/io/ft_printf_fd.c \
|
||||||
src/io/ft_putchar_fd.c \
|
src/io/ft_putchar_fd.c \
|
||||||
|
src/io/get_next_line/get_next_line.c \
|
||||||
\
|
\
|
||||||
src/int/ft_min.c \
|
src/int/ft_min.c \
|
||||||
src/int/ft_max.c \
|
src/int/ft_max.c \
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/10/30 23:06:24 by mmoussou #+# #+# */
|
/* Created: 2023/10/30 23:06:24 by mmoussou #+# #+# */
|
||||||
/* Updated: 2024/04/30 11:03:55 by mmoussou ### ########.fr */
|
/* Updated: 2024/05/25 14:24:29 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -126,4 +126,11 @@ t_stack *ft_stacklast(t_stack *stack);
|
|||||||
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
||||||
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
||||||
|
|
||||||
|
/* ----- GET_NEXT_LINE ------ */
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
# define BUFFER_SIZE 5
|
||||||
|
# endif
|
||||||
|
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
74
libft/src/io/get_next_line/get_next_line.c
Normal file
74
libft/src/io/get_next_line/get_next_line.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/05/25 14:23:08 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char check_line(char *res, char *buf)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
while (res[i] && res[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (res[i] == '\n')
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
while (res[i])
|
||||||
|
{
|
||||||
|
buf[j] = res[i];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
buf[j] = 0;
|
||||||
|
res[i - j] = 0;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_read_error(char **buf, char *res)
|
||||||
|
{
|
||||||
|
free(*buf);
|
||||||
|
*buf = NULL;
|
||||||
|
if (res[0] != 0)
|
||||||
|
return (res);
|
||||||
|
free(res);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
static char *buf;
|
||||||
|
char *res;
|
||||||
|
ssize_t bytes_read;
|
||||||
|
|
||||||
|
if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023)
|
||||||
|
return (NULL);
|
||||||
|
if (!buf)
|
||||||
|
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
|
||||||
|
res = ft_calloc(1, 1);
|
||||||
|
while (buf)
|
||||||
|
{
|
||||||
|
res = ft_strjoin_free_s1(res, buf);
|
||||||
|
if (!res)
|
||||||
|
return (NULL);
|
||||||
|
if (check_line(res, buf))
|
||||||
|
return (res);
|
||||||
|
bytes_read = read(fd, buf, BUFFER_SIZE);
|
||||||
|
if (bytes_read < 1)
|
||||||
|
return (ft_read_error(&buf, res));
|
||||||
|
buf[bytes_read] = 0;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
Reference in New Issue
Block a user