Archived
1
0

started print map to window

This commit is contained in:
Adam Joly
2024-01-09 16:12:58 +01:00
parent f33d52e643
commit 0dc9dcf954
22 changed files with 349 additions and 27 deletions

42
get_next_line/Makefile Normal file
View File

@ -0,0 +1,42 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
# Updated: 2024/01/08 14:31:46 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = get_next_line.a
CC = clang
SRCS = get_next_line.c \
get_next_line_utils.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

Binary file not shown.

View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2024/01/08 21:49:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <unistd.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_gnl(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc_gnl(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);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2024/01/08 21:47:06 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 5
# endif
char *get_next_line(int fd);
char *ft_strjoin_gnl(char *s1, char *s2);
size_t ft_strlen_gnl(char *s);
void *ft_calloc_gnl(size_t nmemb, size_t size);
#endif

Binary file not shown.

View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2024/01/08 21:48:30 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen_gnl(char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
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_gnl((ft_strlen_gnl(s1) + ft_strlen_gnl(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);
}
void *ft_calloc_gnl(size_t nmemb, size_t size)
{
char *result;
size_t i;
i = 0;
if (nmemb == 0 || size == 0)
return (malloc(1));
if (((unsigned long long)(size * nmemb) > 4294967295))
return (NULL);
if ((int)size < 0 && (int)nmemb < 0)
return (NULL);
result = malloc(size * nmemb);
if (!result)
return (NULL);
while (i < (size * nmemb))
{
*(unsigned char *)(result + i) = '\0';
i++;
}
return (result);
}

Binary file not shown.