gnl added
This commit is contained in:
16
Makefile
16
Makefile
@@ -8,12 +8,15 @@ LIBFT_A := $(LIBFT)/libft.a
|
||||
FTPRINTF := ./lib/ft_printf
|
||||
FT_PRINTF_A := ./lib/ft_printf/libftprintf.a
|
||||
|
||||
HEADERS := -I ./include -I $(LIBMLX)/include -I $(LIBFT) -I $(FTPRINTF)
|
||||
LIBS := $(LIBMLX)/build/libmlx42.a $(LIBFT_A) $(FT_PRINTF_A) -ldl -lglfw -pthread -lm
|
||||
GNL := ./lib/gnl
|
||||
GNL_A := ./lib/gnl/get_next_line.a
|
||||
|
||||
HEADERS := -I ./include -I $(LIBMLX)/include -I $(LIBFT) -I $(FTPRINTF) -I $(GNL)
|
||||
LIBS := $(LIBMLX)/build/libmlx42.a $(LIBFT_A) $(FT_PRINTF_A) $(GNL_A) -ldl -lglfw -pthread -lm
|
||||
SRCS := $(shell find ./src -iname "*.c")
|
||||
OBJS := ${SRCS:.c=.o}
|
||||
|
||||
all: libmlx libft ft_printf $(NAME)
|
||||
all: libmlx libft ft_printf gnl $(NAME)
|
||||
|
||||
libmlx:
|
||||
@cmake $(LIBMLX) -B $(LIBMLX)/build && make -C $(LIBMLX)/build -j4
|
||||
@@ -24,6 +27,9 @@ libft:
|
||||
ft_printf:
|
||||
@$(MAKE) -C $(FTPRINTF)
|
||||
|
||||
gnl:
|
||||
@$(MAKE) -C $(GNL)
|
||||
|
||||
%.o: %.c
|
||||
@$(CC) $(CFLAGS) -o $@ -c $< $(HEADERS) && printf "Compiling: $(notdir $<)"
|
||||
|
||||
@@ -35,12 +41,14 @@ clean:
|
||||
@rm -rf $(LIBMLX)/build
|
||||
@$(MAKE) -C $(LIBFT) clean
|
||||
@$(MAKE) -C $(FTPRINTF) clean
|
||||
@$(MAKE) -C $(GNL) clean
|
||||
|
||||
fclean: clean
|
||||
@rm -rf $(NAME)
|
||||
@$(MAKE) -C $(LIBFT) fclean
|
||||
@$(MAKE) -C $(FTPRINTF) fclean
|
||||
@$(MAKE) -C $(GNL) fclean
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all, clean, fclean, re, libmlx, libft, ft_printf
|
||||
.PHONY: all, clean, fclean, re, libmlx, libft, ft_printf, gnl
|
||||
28
lib/gnl/Makefile
Normal file
28
lib/gnl/Makefile
Normal file
@@ -0,0 +1,28 @@
|
||||
CC = cc
|
||||
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
|
||||
SRCS = get_next_line.c get_next_line_utils.c
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
NAME = get_next_line.a
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
ar rcs $(NAME) $(OBJS)
|
||||
ranlib $(NAME)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
73
lib/gnl/get_next_line.c
Normal file
73
lib/gnl/get_next_line.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/15 18:37:44 by aortigos #+# #+# */
|
||||
/* Updated: 2024/03/15 19:31:56 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *ft_read_to_left_str(int fd, char *left_str)
|
||||
{
|
||||
char *buff;
|
||||
int rd_bytes;
|
||||
|
||||
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
|
||||
if (!buff)
|
||||
return (NULL);
|
||||
rd_bytes = 1;
|
||||
while (!ft_strchr(left_str, '\n') && rd_bytes != 0)
|
||||
{
|
||||
rd_bytes = read(fd, buff, BUFFER_SIZE);
|
||||
if (rd_bytes == -1)
|
||||
{
|
||||
free(left_str);
|
||||
free(buff);
|
||||
return (NULL);
|
||||
}
|
||||
buff[rd_bytes] = '\0';
|
||||
left_str = ft_strjoin(left_str, buff);
|
||||
}
|
||||
free(buff);
|
||||
return (left_str);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
char *line;
|
||||
static char *left_str;
|
||||
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
left_str = ft_read_to_left_str(fd, left_str);
|
||||
if (!left_str)
|
||||
{
|
||||
free(left_str);
|
||||
return (NULL);
|
||||
}
|
||||
line = ft_get_line(left_str);
|
||||
left_str = ft_new_left_str(left_str);
|
||||
return (line);
|
||||
}
|
||||
|
||||
/*
|
||||
int main()
|
||||
{
|
||||
int fd = open("test.txt", O_RDONLY);
|
||||
char *line;
|
||||
|
||||
while((line = get_next_line(fd)) != NULL)
|
||||
{
|
||||
printf("Linea leida: %s\n", line);
|
||||
free(line);
|
||||
}
|
||||
close(fd);
|
||||
return (0);
|
||||
}*/
|
||||
32
lib/gnl/get_next_line.h
Normal file
32
lib/gnl/get_next_line.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/15 18:52:24 by aortigos #+# #+# */
|
||||
/* Updated: 2024/03/15 18:52:26 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 10000
|
||||
# endif
|
||||
|
||||
char *get_next_line(int fd);
|
||||
char *ft_read_to_left_str(int fd, char *left_str);
|
||||
size_t ft_strlen(char *s);
|
||||
char *ft_strchr(char *s, int c);
|
||||
char *ft_strjoin(char *left_str, char *buff);
|
||||
char *ft_get_line(char *left_str);
|
||||
char *ft_new_left_str(char *left_str);
|
||||
|
||||
#endif
|
||||
132
lib/gnl/get_next_line_utils.c
Normal file
132
lib/gnl/get_next_line_utils.c
Normal file
@@ -0,0 +1,132 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/15 18:37:57 by aortigos #+# #+# */
|
||||
/* Updated: 2024/03/15 18:56:09 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
size_t ft_strlen(char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (0);
|
||||
while (s[i] != '\0')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strchr(char *s, int c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (0);
|
||||
if (c == '\0')
|
||||
return ((char *)&s[ft_strlen(s)]);
|
||||
while (s[i] != '\0')
|
||||
{
|
||||
if (s[i] == (char) c)
|
||||
{
|
||||
return ((char *)&s[i]);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *ft_strjoin(char *left_str, char *buff)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
|
||||
if (!left_str)
|
||||
{
|
||||
left_str = (char *)malloc(1 * sizeof(char));
|
||||
left_str[0] = '\0';
|
||||
}
|
||||
if (!left_str || !buff)
|
||||
return (NULL);
|
||||
str = malloc(sizeof(char) * ((ft_strlen(left_str))
|
||||
+ (ft_strlen(buff))) + 1);
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
j = 0;
|
||||
if (left_str)
|
||||
while (left_str[++i] != '\0')
|
||||
str[i] = left_str[i];
|
||||
while (buff[j] != '\0')
|
||||
str[i++] = buff[j++];
|
||||
str[ft_strlen(left_str) + ft_strlen(buff)] = '\0';
|
||||
free(left_str);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_get_line(char *left_str)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
if (!left_str[i])
|
||||
return (NULL);
|
||||
while (left_str[i] && left_str[i] != '\n')
|
||||
i++;
|
||||
str = (char *)malloc(sizeof(char) * (i + 2));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (left_str[i] && left_str[i] != '\n')
|
||||
{
|
||||
str[i] = left_str[i];
|
||||
i++;
|
||||
}
|
||||
if (left_str[i] == '\n')
|
||||
{
|
||||
str[i] = left_str[i];
|
||||
i++;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_new_left_str(char *left_str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
while (left_str[i] && left_str[i] != '\n')
|
||||
i++;
|
||||
if (!left_str[i])
|
||||
{
|
||||
free(left_str);
|
||||
return (NULL);
|
||||
}
|
||||
str = (char *)malloc(sizeof(char) * (ft_strlen(left_str) - i + 1));
|
||||
if (!str)
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
i++;
|
||||
j = 0;
|
||||
while (left_str[i])
|
||||
str[j++] = left_str[i++];
|
||||
str[j] = '\0';
|
||||
free(left_str);
|
||||
return (str);
|
||||
}
|
||||
Reference in New Issue
Block a user