From fa0cfaf22ded4c9cba53283682d6cd11582d683d Mon Sep 17 00:00:00 2001 From: Nathalia Vitoria Buchholz Date: Fri, 3 Jan 2025 13:53:03 -0300 Subject: [PATCH] initial structure close #1 --- Makefile | 36 ++++++++ includes/so_long.h | 32 +++++++ src/alloc_mem.c | 0 src/enemy.c | 0 src/error_handling.c | 0 src/free_map.c | 0 src/game_utils.c | 0 src/get_map.c | 0 src/get_next_line/get_next_line.c | 112 ++++++++++++++++++++++++ src/get_next_line/get_next_line_utils.c | 102 +++++++++++++++++++++ src/player.c | 0 src/so_long.c | 22 +++++ src/utils.c | 0 src/validate_map.c | 16 ++++ 14 files changed, 320 insertions(+) create mode 100644 Makefile create mode 100644 includes/so_long.h create mode 100644 src/alloc_mem.c create mode 100644 src/enemy.c create mode 100644 src/error_handling.c create mode 100644 src/free_map.c create mode 100644 src/game_utils.c create mode 100644 src/get_map.c create mode 100644 src/get_next_line/get_next_line.c create mode 100644 src/get_next_line/get_next_line_utils.c create mode 100644 src/player.c create mode 100644 src/so_long.c create mode 100644 src/utils.c create mode 100644 src/validate_map.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf54050 --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: nbuchhol +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/01/03 12:09:52 by nbuchhol #+# #+# # +# Updated: 2025/01/03 12:12:09 by nbuchhol ### ########.fr # +# # +# **************************************************************************** # + +NAME = solong +CC = cc +CFLAGS = -Wall -Wextra -Werror +SRC = +OBJ = ${SRC:.c=.o} +RM = rm -f + +all: ${NAME} + +${NAME}: ${OBJ} + ar rcs ${NAME} ${OBJ} + +%.o: %.c + @${CC} ${CFLAGS} -c $^ -o $@ + +clean: + ${RM} ${OBJ} + +fclean: clean + ${RM} ${NAME} + +re: fclean all + +.PHONY = all clean fclean re diff --git a/includes/so_long.h b/includes/so_long.h new file mode 100644 index 0000000..859c3a9 --- /dev/null +++ b/includes/so_long.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* so_long.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nbuchhol +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/03 12:15:22 by nbuchhol #+# #+# */ +/* Updated: 2025/01/03 13:51:07 by nbuchhol ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SO_LONG_H +# define SO_LONG_H + +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif // END BUFFER_SIZE + +int validate_map(char *map); +size_t ft_strlen(const char *s); +char *ft_strchr(const char *s, int c); +char *ft_strjoin(const char *s1, const char *s2); +void *ft_calloc(size_t nmemb, size_t size); +char *ft_substr(const char *s, unsigned int start, size_t len); +char *get_next_line(int fd); + +#endif // SO_LONG + diff --git a/src/alloc_mem.c b/src/alloc_mem.c new file mode 100644 index 0000000..e69de29 diff --git a/src/enemy.c b/src/enemy.c new file mode 100644 index 0000000..e69de29 diff --git a/src/error_handling.c b/src/error_handling.c new file mode 100644 index 0000000..e69de29 diff --git a/src/free_map.c b/src/free_map.c new file mode 100644 index 0000000..e69de29 diff --git a/src/game_utils.c b/src/game_utils.c new file mode 100644 index 0000000..e69de29 diff --git a/src/get_map.c b/src/get_map.c new file mode 100644 index 0000000..e69de29 diff --git a/src/get_next_line/get_next_line.c b/src/get_next_line/get_next_line.c new file mode 100644 index 0000000..1fb6a05 --- /dev/null +++ b/src/get_next_line/get_next_line.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nbuchhol +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/29 13:06:53 by nbuchhol #+# #+# */ +/* Updated: 2025/01/03 13:50:05 by nbuchhol ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/so_long.h" + +static char *ft_strdup(const char *s1) +{ + char *buffer; + size_t len; + size_t i; + + len = ft_strlen(s1); + buffer = malloc(len + 1); + if (!buffer) + return (NULL); + i = 0; + while (s1[i] != '\0') + { + buffer[i] = s1[i]; + i++; + } + buffer[i] = '\0'; + return (buffer); +} + +static char *ft_free(char **buffer, char **stash) +{ + if (buffer && *buffer) + { + free(*buffer); + *buffer = NULL; + } + if (stash && *stash) + { + free(*stash); + *stash = NULL; + } + return (NULL); +} + +static char *read_and_stash(int fd, char *stash) +{ + char *temp; + char *read_buffer; + ssize_t bytes_read; + + if (!stash) + stash = ft_calloc(1, sizeof(char)); + read_buffer = ft_calloc(BUFFER_SIZE + 1, sizeof(char)); + bytes_read = 1; + while (!ft_strchr(stash, '\n') && bytes_read != 0) + { + bytes_read = read(fd, read_buffer, BUFFER_SIZE); + if (bytes_read == -1) + return (ft_free(&read_buffer, &stash)); + read_buffer[bytes_read] = '\0'; + temp = ft_strjoin(stash, read_buffer); + ft_free(NULL, &stash); + stash = temp; + } + ft_free(&read_buffer, NULL); + if ((stash[0] == '\0' || !stash) && bytes_read == 0) + ft_free(NULL, &stash); + return (stash); +} + +static char *get_line(char **stash) +{ + char *line; + char *new_buffer; + size_t len; + + if (ft_strchr(*stash, '\n')) + { + len = ft_strchr(*stash, '\n') - *stash + 1; + line = ft_substr(*stash, 0, len); + new_buffer = ft_strdup(*stash + len); + ft_free(NULL, stash); + *stash = new_buffer; + } + else + { + line = ft_strdup(*stash); + ft_free(NULL, stash); + } + return (line); +} + +char *get_next_line(int fd) +{ + static char *stash; + char *line; + + if (fd < 0 || BUFFER_SIZE <= 0) + return (NULL); + stash = read_and_stash(fd, stash); + if (!stash) + return (NULL); + line = get_line(&stash); + if (!line) + return (ft_free(NULL, &stash)); + return (line); +} diff --git a/src/get_next_line/get_next_line_utils.c b/src/get_next_line/get_next_line_utils.c new file mode 100644 index 0000000..9752dd0 --- /dev/null +++ b/src/get_next_line/get_next_line_utils.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nbuchhol +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/29 13:06:56 by nbuchhol #+# #+# */ +/* Updated: 2025/01/03 13:50:23 by nbuchhol ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/so_long.h" + +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s[i]) + i++; + return (i); +} + +char *ft_strchr(const char *s1, int c) +{ + if (!s1) + return (NULL); + while (*s1) + { + if (*s1 == (char)c) + return ((char *)s1); + s1++; + } + if (*s1 == (char)c) + return ((char *)s1); + return (NULL); +} + +char *ft_strjoin(const char *prefix, const char *suffix) +{ + char *str; + size_t i; + size_t j; + + if (!prefix && !suffix) + return (NULL); + str = malloc(sizeof(char) * (ft_strlen(prefix) + ft_strlen(suffix) + 1)); + if (!str) + return (NULL); + i = 0; + while (*prefix) + str[i++] = *prefix++; + j = 0; + while (suffix[j]) + str[i++] = suffix[j++]; + str[i] = '\0'; + return (str); +} + +void *ft_calloc(size_t nmemb, size_t size) +{ + size_t length; + void *buffer; + unsigned char *ptr; + size_t i; + + if ((nmemb != 0 && size) > ((size_t)-1) / nmemb) + return (NULL); + length = size * nmemb; + buffer = malloc(length); + if (!buffer) + return (NULL); + ptr = (unsigned char *)buffer; + i = 0; + while (i < length) + ptr[i++] = 0; + return (buffer); +} + +char *ft_substr(const char *s, unsigned int start, size_t len) +{ + size_t size; + size_t i; + char *buffer; + + if (!s) + return (NULL); + size = ft_strlen(s); + if (start >= size) + return (ft_calloc(1, sizeof(char))); + if (start + len > size) + len = size - start; + buffer = malloc(len + 1); + if (!buffer) + return (NULL); + i = 0; + while (i < len) + buffer[i++] = s[start++]; + buffer[i] = '\0'; + return (buffer); +} diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..e69de29 diff --git a/src/so_long.c b/src/so_long.c new file mode 100644 index 0000000..4bc0a22 --- /dev/null +++ b/src/so_long.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* so_long.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nbuchhol +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/03 12:12:47 by nbuchhol #+# #+# */ +/* Updated: 2025/01/03 12:24:48 by nbuchhol ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/so_long.h" + +int main(int argc, char **argv) +{ + if (argc != 2 || argv == NULL) + return (1); + if (!validate_map) + return (1); + return (0); +} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..e69de29 diff --git a/src/validate_map.c b/src/validate_map.c new file mode 100644 index 0000000..370e14d --- /dev/null +++ b/src/validate_map.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* validate_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nbuchhol +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/03 12:22:54 by nbuchhol #+# #+# */ +/* Updated: 2025/01/03 12:28:07 by nbuchhol ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int validate_map(char *map) +{ + return (0); +}