Bonus: Best Error Handler (more details about the error)

This commit is contained in:
Manzovince
2019-05-03 19:13:48 +02:00
parent 8d9b314781
commit 1cf791746c
5 changed files with 35 additions and 11 deletions

View File

@@ -17,6 +17,6 @@ Le but de ce projet est dagencer les Tetriminos entre eux pour former le plus
- [ ] Free everything (NO LEAKS) - [ ] Free everything (NO LEAKS)
## BONUS ## BONUS
- [x] `ft_check_tetri_errors` indicate which tetri is wrong - [x] Best error handler (more details on why there is an error.)
- [ ] Optimisation - [ ] Optimisation
- [ ] Add colors to tetri when printing result map - [ ] Add colors to tetri when printing result map

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/01 13:34:46 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); char *read_file(char *file);
void print_error(char *s); void print_error(char *s);
void print_error_extended(int error);
void parse_input(char *input); void parse_input(char *input);
int check_file_errors(char *file); int check_file_errors(char *file);
int check_tetri_errors(char *tetri); int check_tetri_errors(char *tetri);

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/01 13:29:05 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); 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 ** Function to see if there if an error if the file
** - less than 4 lines ** - less than 4 lines
@@ -44,11 +65,11 @@ int check_file_errors(char *file)
line_nbr++; line_nbr++;
if (file[i] == '\n' && file[i+1] != '\0' && \ if (file[i] == '\n' && file[i+1] != '\0' && \
file[i+2] != '.' && file[i+2] != '#') file[i+2] != '.' && file[i+2] != '#')
return (1); return (2);
i++; i++;
} }
if (line_nbr < 4 || line_nbr > 129) if (line_nbr < 4 || line_nbr > 129)
return (1); return (3);
return (0); return (0);
} }
@@ -76,7 +97,7 @@ int check_tetri_errors(char *tetri)
i++; i++;
} }
if (htg != 4 || dot != 12 || check_tetri_errors_proxy(tetri)) if (htg != 4 || dot != 12 || check_tetri_errors_proxy(tetri))
return (1); return (4 + check_tetri_errors_proxy(tetri));
return (0); return (0);
} }

5
main.c
View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/12 13:20:48 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]))) if (!(input = read_file(argv[1])))
print_error("error: Could not read file.\n"); print_error("error: Could not read file.\n");
if (check_file_errors(input)) 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); parse_input(input);
/* /*
Backtracking for smallest square Backtracking for smallest square

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/15 14:48:14 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) void fill_list(char line[], t_fillist *list)
{ {
unsigned int tmp; // unsigned int tmp;
unsigned int mask; unsigned int mask;
int i; int i;
@@ -142,7 +142,8 @@ void parse_input(char *input)
if (check_tetri_errors(tetri)) if (check_tetri_errors(tetri))
{ {
ft_putstr(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++); add_to_list(tetri, &list, letter++);
while (input[i] && input[i] != '.' && input[i] != '#') while (input[i] && input[i] != '.' && input[i] != '#')