From 1cf791746c56a24a3a23c9031794fe6a8a98b344 Mon Sep 17 00:00:00 2001 From: Manzovince Date: Fri, 3 May 2019 19:13:48 +0200 Subject: [PATCH] Bonus: Best Error Handler (more details about the error) --- README.md | 2 +- fillit.h | 3 ++- handle_errors.c | 29 +++++++++++++++++++++++++---- main.c | 5 +++-- parse_input.c | 7 ++++--- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 869d728..2c30b69 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,6 @@ Le but de ce projet est d’agencer les Tetriminos entre eux pour former le plus - [ ] Free everything (NO LEAKS) ## BONUS -- [x] `ft_check_tetri_errors` indicate which tetri is wrong +- [x] Best error handler (more details on why there is an error.) - [ ] Optimisation - [ ] Add colors to tetri when printing result map diff --git a/fillit.h b/fillit.h index 429050e..1964374 100644 --- a/fillit.h +++ b/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/05/03 16:10:34 by hulamy ### ########.fr */ +/* Updated: 2019/05/03 19:01:49 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,7 @@ typedef struct s_fillist char *read_file(char *file); void print_error(char *s); +void print_error_extended(int error); void parse_input(char *input); int check_file_errors(char *file); int check_tetri_errors(char *tetri); diff --git a/handle_errors.c b/handle_errors.c index 9d514ef..41c3ee6 100644 --- a/handle_errors.c +++ b/handle_errors.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:29:05 by vmanzoni #+# #+# */ -/* Updated: 2019/04/27 15:06:26 by vmanzoni ### ########.fr */ +/* Updated: 2019/05/03 19:11:02 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,27 @@ void print_error(char *s) exit(1); } +/* +** Function that display error message *s on fd +** with more informations +** and exit program +*/ + +void print_error_extended(int error) +{ + if (error == 1) + ft_putstr("error: File contains char other than . # \\n found.\n"); + if (error == 2) + ft_putstr("error: File contains more than 2 \\n in a row.\n"); + if (error == 3) + ft_putstr("error: File contains less than 1 tetrimino or more than 26.\n"); + if (error == 4) + ft_putstr("\n\nerror: This tetrimino has more or less than 4 #.\n"); + if (error == 5) + ft_putstr("\n\nerror: This tetrimino # are not well connected.\n"); + exit(1); +} + /* ** Function to see if there if an error if the file ** - less than 4 lines @@ -44,11 +65,11 @@ int check_file_errors(char *file) line_nbr++; if (file[i] == '\n' && file[i+1] != '\0' && \ file[i+2] != '.' && file[i+2] != '#') - return (1); + return (2); i++; } if (line_nbr < 4 || line_nbr > 129) - return (1); + return (3); return (0); } @@ -76,7 +97,7 @@ int check_tetri_errors(char *tetri) i++; } if (htg != 4 || dot != 12 || check_tetri_errors_proxy(tetri)) - return (1); + return (4 + check_tetri_errors_proxy(tetri)); return (0); } diff --git a/main.c b/main.c index 798e64c..bfa33ec 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */ -/* Updated: 2019/04/27 16:36:09 by vmanzoni ### ########.fr */ +/* Updated: 2019/05/03 19:12:47 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,8 @@ int main(int argc, char **argv) if (!(input = read_file(argv[1]))) print_error("error: Could not read file.\n"); if (check_file_errors(input)) - print_error("error: Invalid file.\n"); + print_error_extended(check_file_errors(input)); +// print_error("error: Invalid file.\n"); parse_input(input); /* Backtracking for smallest square diff --git a/parse_input.c b/parse_input.c index 0e42162..9ba1740 100644 --- a/parse_input.c +++ b/parse_input.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/05/03 16:10:12 by hulamy ### ########.fr */ +/* Updated: 2019/05/03 19:08:42 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ unsigned short reduce_tetri(unsigned short tetri, int width) void fill_list(char line[], t_fillist *list) { - unsigned int tmp; +// unsigned int tmp; unsigned int mask; int i; @@ -142,7 +142,8 @@ void parse_input(char *input) if (check_tetri_errors(tetri)) { ft_putstr(tetri); - print_error("\n\nerror: This tetrimino is not valid.\n"); + print_error_extended(check_tetri_errors(tetri)); + //print_error("\n\nerror: This tetrimino is not valid.\n"); } add_to_list(tetri, &list, letter++); while (input[i] && input[i] != '.' && input[i] != '#')