kekw
This commit is contained in:
39
gnl/Makefile
Normal file
39
gnl/Makefile
Normal file
@ -0,0 +1,39 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/12/14 15:49:18 by cchauvet #+# #+# #
|
||||
# Updated: 2023/01/05 19:22:40 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = get_next_line.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
NAME = get_next_line.a
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g
|
||||
|
||||
CC = clang
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
ar -rc $(NAME) $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(BOBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all bonus clean fclean re
|
104
gnl/get_next_line.c
Normal file
104
gnl/get_next_line.c
Normal file
@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 13:07:10 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *ft_getstash(int fd)
|
||||
{
|
||||
char *str;
|
||||
int readed;
|
||||
|
||||
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
readed = read(fd, str, BUFFER_SIZE);
|
||||
if (readed < 1)
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_getline(int fd)
|
||||
{
|
||||
char *stash;
|
||||
char *buf;
|
||||
|
||||
stash = NULL;
|
||||
buf = NULL;
|
||||
while (ft_strchri(stash, '\n') == -1)
|
||||
{
|
||||
buf = ft_getstash(fd);
|
||||
if (buf == NULL)
|
||||
return (stash);
|
||||
stash = ft_strfjoin(stash, buf);
|
||||
if (stash == NULL)
|
||||
return (NULL);
|
||||
}
|
||||
return (stash);
|
||||
}
|
||||
|
||||
char *ft_getreturn(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
i = ft_strchri(str, '\n') + 1;
|
||||
if (i == 0)
|
||||
i = ft_strlen(str);
|
||||
return (ft_strndup(str, i));
|
||||
}
|
||||
|
||||
char *ft_getextra(char *str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
i = ft_strchri(str, '\n') + 1;
|
||||
if (i == 0)
|
||||
return (NULL);
|
||||
j = ft_strlen(str + i);
|
||||
return (ft_strndup(str + i, j));
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *stash = NULL;
|
||||
char *buf1;
|
||||
char *buf2;
|
||||
|
||||
buf2 = stash;
|
||||
if (ft_strchri(stash, '\n') == -1)
|
||||
{
|
||||
buf1 = ft_getline(fd);
|
||||
buf2 = ft_strfjoin(stash, buf1);
|
||||
if (buf2 == NULL)
|
||||
{
|
||||
free(buf1);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
buf1 = ft_getreturn(buf2);
|
||||
stash = ft_getextra(buf2);
|
||||
free(buf2);
|
||||
if (buf1 == NULL)
|
||||
{
|
||||
free(stash);
|
||||
free(buf1);
|
||||
return (NULL);
|
||||
}
|
||||
return (buf1);
|
||||
}
|
25
gnl/get_next_line.h
Normal file
25
gnl/get_next_line.h
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/10 18:26:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 42
|
||||
# endif
|
||||
# include "../libft/libft.h"
|
||||
# include "../extra/extra.h"
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
||||
#endif
|
BIN
gnl/get_next_line_utils.o
Normal file
BIN
gnl/get_next_line_utils.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user