From 451a07b7623ad6d5dbd3261eef45e69991dee215 Mon Sep 17 00:00:00 2001 From: Manzovince Date: Sun, 14 Apr 2019 17:47:13 +0200 Subject: [PATCH] READING FILE WORKING --- includes/fillit.h | 4 ++-- srcs/handle_errors.c | 18 ++++++++++-------- srcs/main.c | 10 +++++----- srcs/read_file.c | 11 ++++++----- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/includes/fillit.h b/includes/fillit.h index 803d391..3a3ea69 100644 --- a/includes/fillit.h +++ b/includes/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/04/14 17:29:58 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/14 17:43:41 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,6 @@ void print_test(char *test); char *read_file(char *file); void print_error(char *s); -int ft_file_errors(char *file); +int check_file_errors(char *file); #endif diff --git a/srcs/handle_errors.c b/srcs/handle_errors.c index 66a0915..5553a00 100644 --- a/srcs/handle_errors.c +++ b/srcs/handle_errors.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:29:05 by vmanzoni #+# #+# */ -/* Updated: 2019/04/14 17:30:30 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/14 17:46:32 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ ** Function that display error message *s on fd */ -void print_error(char *s, int fd) +void print_error(char *s) { write(2, s, strlen(s)); } @@ -28,18 +28,20 @@ void print_error(char *s, int fd) ** - two \n in a row */ -int ft_file_errors(char *file) +int check_file_errors(char *file) { + int i; int line_nbr; + i = 0; line_nbr = 0; - while (*file) + while (file[i]) { - if (*file == '\n') + if (file[i] != '.' && file[i] != '#' && file[i] != '\n') + return (1); + if (file[i] == '\n') line_nbr++; - file++; - if (*file == '\n') - return (1); + i++; } if (line_nbr < 4 || line_nbr > 129) return (1); diff --git a/srcs/main.c b/srcs/main.c index f88adee..0896793 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */ -/* Updated: 2019/04/14 17:30:14 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/14 17:44:38 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ int main(int argc, char **argv) if (argc == 2) { if (!(input = read_file(argv[1]))) - ft_display_error("Error: Could not read file.\n"); - if (ft_file_errors(input)) - ft_display_error("Error: Invalid file.\n"); + print_error("Error: Could not read file.\n"); + if (check_file_errors(input)) + print_error("Error: Invalid file.\n"); /* Check if every tetrimino is valid Transform input to tetriminos @@ -31,7 +31,7 @@ int main(int argc, char **argv) */ } else - ft_display_error("Error: Please submit a file.\n"); + print_error("Error: Please submit a file.\n"); return (0); print_test(input); } diff --git a/srcs/read_file.c b/srcs/read_file.c index 837ce68..954e52c 100644 --- a/srcs/read_file.c +++ b/srcs/read_file.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */ -/* Updated: 2019/04/14 14:23:24 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/14 17:44:18 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,24 @@ char *read_file(char *file) { - char buf[BUFFER_SIZE]; + char buf[BUFF_SIZE]; int fd; int rv; int i; char *result; if (((fd = open(file, O_RDONLY)) < 0) \ - || ((rv = read(fd, &buf, BUFFER_SIZE)) < 0) \ - || !(result = malloc(sizeof(char)))) + || ((rv = read(fd, &buf, BUFF_SIZE)) < 0) \ + || !(result = malloc(sizeof(char) * rv))) return (NULL); buf[rv] = '\0'; i = 0; - while (rv--) + while (buf[i]) { result[i] = buf[i]; i++; } + result[i] = '\0'; close(fd); return (result); }