aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-11 13:11:27 +0100
committerCharles <sircharlesaze@gmail.com>2020-01-11 13:11:27 +0100
commit2fdb9ad1d66e28e056f31bc6321d2c198db33290 (patch)
treec3c32462b6a09f1a9f393cc0216c2ffe3bdaaa09
parentcff05e83256a67b8cb23b16b1e1e6f761ff52f4c (diff)
downloadcub3d-2fdb9ad1d66e28e056f31bc6321d2c198db33290.tar.gz
cub3d-2fdb9ad1d66e28e056f31bc6321d2c198db33290.tar.bz2
cub3d-2fdb9ad1d66e28e056f31bc6321d2c198db33290.zip
Error messages with error helper
-rw-r--r--cub3d.h6
-rw-r--r--error.c24
-rw-r--r--main.c9
-rw-r--r--parse/parse.c26
-rw-r--r--parse/parse_check.c12
-rw-r--r--state.c4
6 files changed, 50 insertions, 31 deletions
diff --git a/cub3d.h b/cub3d.h
index 71268b1..d14e512 100644
--- a/cub3d.h
+++ b/cub3d.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */
-/* Updated: 2020/01/11 10:59:58 by cacharle ### ########.fr */
+/* Updated: 2020/01/11 12:54:44 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
@@ -203,6 +203,10 @@ double vector_norm(t_vector v);
*/
void error_put_usage_exit(char *name);
+void error_put(char *message);
+void *error_put_return(char *message);
+void *error_put_return_state_destroy(char *message, t_state *state);
+void *error_put_return_lines_state_destroy(char *message, t_state *state, char **lines);
/*
** helper.c
diff --git a/error.c b/error.c
index 577597a..e16930f 100644
--- a/error.c
+++ b/error.c
@@ -9,3 +9,27 @@ void error_put_usage_exit(char *name)
ft_putendl_fd(" [.cub file] [--save]", STDERR_FILENO);
exit(EXIT_FAILURE);
}
+
+void error_put(char *message)
+{
+ ft_putstr("Error\nCouldnt ");
+ ft_putendl(message);
+}
+
+void *error_put_return(char *message)
+{
+ error_put(message);
+ return (NULL);
+}
+
+void *error_put_return_state_destroy(char *message, t_state *state)
+{
+ state_destroy(state);
+ return error_put_return(message);
+}
+
+void *error_put_return_lines_state_destroy(char *message, t_state *state, char **lines)
+{
+ helper_free_splited(lines);
+ return (error_put_return_state_destroy(message, state));
+}
diff --git a/main.c b/main.c
index 6b754f2..7d61681 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */
-/* Updated: 2020/01/11 12:33:37 by cacharle ### ########.fr */
+/* Updated: 2020/01/11 13:04:38 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,14 +36,9 @@ int main(int argc, char **argv)
int main(int argc, char **argv)
{
(void)argc;
- t_state *s = parse(argv[1]);
+ t_state *s = parse_check(parse(argv[1]));
if (s == NULL)
return (1);
- if (parse_check(s) == NULL)
- {
- printf("wrong .cub format");
- return (1);
- }
printf("R %d %d\n", s->window.width, s->window.height);
printf("NO %s\n", s->textures_path[TEX_NORTH]);
printf("SO %s\n", s->textures_path[TEX_SOUTH]);
diff --git a/parse/parse.c b/parse/parse.c
index 84a8957..3eb8846 100644
--- a/parse/parse.c
+++ b/parse/parse.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */
-/* Updated: 2020/01/11 11:29:45 by cacharle ### ########.fr */
+/* Updated: 2020/01/11 12:59:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,32 +19,26 @@ t_state *parse(char *filename)
t_state *state;
if ((state = state_new_empty()) == NULL)
- return (NULL);
+ return (error_put_return("create empty state"));
if ((lines = get_file_lines(filename)) == NULL)
- return (state_destroy(state));
- /* for (int i = 0; lines[i]; i++) */
- /* printf("%d [%s]\n", i, lines[i]); */
+ return (error_put_return_state_destroy("read .cub file", state));
i = -1;
while (lines[++i] != NULL)
{
if (*lines[i] == '1')
break ;
if (!parse_line(state, lines[i]))
- {
- helper_free_splited(lines);
- return (state_destroy(state));
- }
+ return (error_put_return_lines_state_destroy(
+ "parse configuration", state, lines));
}
if ((state = parse_map(state, lines + i)) == NULL)
- {
- helper_free_splited(lines);
- return (state_destroy(state));
- }
+ return (error_put_return_lines_state_destroy(
+ "parse map", state, lines));
helper_free_splited(lines);
return (state);
}
-char **get_file_lines(char *filename)
+char **get_file_lines(char *filename)
{
int fd;
int ret;
@@ -81,7 +75,7 @@ static t_option_parser g_option_parsers[] =
#define OPTIONS_PARSERS_SIZE (sizeof(g_option_parsers) / sizeof(t_option_parser))
-t_bool parse_line(t_state *state, char *line)
+t_bool parse_line(t_state *state, char *line)
{
int i;
@@ -115,7 +109,7 @@ t_state *parse_map(t_state *state, char **lines)
return (state);
}
-t_cell *create_map_row(char *line)
+t_cell *create_map_row(char *line)
{
int i;
t_cell *row;
diff --git a/parse/parse_check.c b/parse/parse_check.c
index 7bcfc8b..a65d74a 100644
--- a/parse/parse_check.c
+++ b/parse/parse_check.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */
-/* Updated: 2020/01/11 11:15:04 by cacharle ### ########.fr */
+/* Updated: 2020/01/11 13:03:33 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,19 +15,21 @@
t_state *parse_check(t_state *state)
{
int i;
- int j;
- int player_count;
+ /* int j; */
+ /* int player_count; */
i = -1;
while (++i < state->map_width)
if (state->map[0][i] != CELL_WALL
|| state->map[state->map_height - 1][i] != CELL_WALL)
- return (state_destroy(state));
+ return (error_put_return_state_destroy(
+ "validate map without borders", state));
i = -1;
while (++i < state->map_height)
if (state->map[i][0] != CELL_WALL
|| state->map[i][state->map_width - 1] != CELL_WALL)
- return (state_destroy(state));
+ return (error_put_return_state_destroy(
+ "validate map without borders", state));
// maybe not necessary
/* player_count = 0; */
/* i = -1; */
diff --git a/state.c b/state.c
index d718493..acc14c6 100644
--- a/state.c
+++ b/state.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */
-/* Updated: 2020/01/11 12:32:13 by cacharle ### ########.fr */
+/* Updated: 2020/01/11 13:10:45 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,7 +31,7 @@ t_state *state_new(t_state *state)
{
load_texture(state->mlx_ptr, &state->textures[i], state->textures_path[i]);
if (state->textures[i].id == NULL)
- return (state_destroy(state));
+ return (error_put_return_state_destroy("load texture", state));
}
if ((state->window.id = mlx_new_image(state->mlx_ptr,
state->window.width, state->window.height)) == NULL)