libft reformat
This commit is contained in:
44
io/get_next_line/Makefile
Normal file
44
io/get_next_line/Makefile
Normal file
@ -0,0 +1,44 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
|
||||
# Updated: 2024/02/04 14:54:32 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = get_next_line.a
|
||||
|
||||
CC = clang
|
||||
|
||||
SRCS = get_next_line.c \
|
||||
get_next_line_utils.c \
|
||||
../../mem/ft_calloc.c \
|
||||
../../str/ft_strlen.c \
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
FLAGS = -Werror -Wall -Wextra
|
||||
|
||||
HEADER = get_next_line.h
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
ar -rcs $(NAME) $(OBJS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: clean all re fclean
|
74
io/get_next_line/get_next_line.c
Normal file
74
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/02/04 14:23:07 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.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_gnl(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);
|
||||
}
|
28
io/get_next_line/get_next_line.h
Normal file
28
io/get_next_line/get_next_line.h
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/04 14:26:01 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 1
|
||||
# endif
|
||||
|
||||
char *get_next_line(int fd);
|
||||
char *ft_strjoin_gnl(char *s1, char *s2);
|
||||
void *ft_calloc(size_t nmemb, size_t size);
|
||||
size_t ft_strlen(char *s);
|
||||
|
||||
#endif
|
44
io/get_next_line/get_next_line_utils.c
Normal file
44
io/get_next_line/get_next_line_utils.c
Normal file
@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/04 14:24:58 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *ft_strjoin_gnl(char *s1, char *s2)
|
||||
{
|
||||
char *result;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (!s2)
|
||||
return (NULL);
|
||||
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
|
||||
if (result == NULL)
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
{
|
||||
result[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (s2[j])
|
||||
{
|
||||
result[i] = s2[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
free(s1);
|
||||
result[i] = '\0';
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
BIN
io/get_next_line/objget_next_line.o
Normal file
BIN
io/get_next_line/objget_next_line.o
Normal file
Binary file not shown.
BIN
io/get_next_line/objget_next_line_utils.o
Normal file
BIN
io/get_next_line/objget_next_line_utils.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user