diff options
Diffstat (limited to 'src/parse')
| -rw-r--r-- | src/parse/parse.c | 48 | ||||
| -rw-r--r-- | src/parse/parse_check.c | 14 | ||||
| -rw-r--r-- | src/parse/parse_color.c | 8 | ||||
| -rw-r--r-- | src/parse/parse_file.c | 38 | ||||
| -rw-r--r-- | src/parse/parse_resolution.c | 5 | ||||
| -rw-r--r-- | src/parse/parse_textures.c | 17 |
6 files changed, 90 insertions, 40 deletions
diff --git a/src/parse/parse.c b/src/parse/parse.c index 4c51945..c1f1a5f 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2020/02/04 04:41:26 by cacharle ### ########.fr */ +/* Updated: 2020/02/05 01:27:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,19 +22,15 @@ t_state *parse(char *filename) return (error_put_return("create empty state")); if ((lines = get_file_lines(filename)) == NULL) return (error_put_return_state_destroy("read .cub file", state)); - i = -1; - while (lines[++i] != NULL) + if ((i = parse_premap(state, lines)) == -1) { - if (*lines[i] == '1') - break ; - if (!parse_line(state, lines[i])) - return (error_put_return_lines_state_destroy( - "parse configuration", state, lines)); + return (error_put_return_lines_state_destroy( + "parse option", state, lines)); } - if ((state = parse_map(state, lines + i)) == NULL) + if (state->pflags != 0xFF || (state = parse_map(state, lines + i)) == NULL) { - return (error_put_return_lines_state_destroy("parse map", - state, lines)); + return (error_put_return_lines_state_destroy(state == NULL ? + "parse map" : "parse options", state, lines)); } helper_free_splited(lines); return (state); @@ -54,15 +50,14 @@ static t_option_parser g_option_parsers[] = t_bool parse_line(t_state *state, char *line) { - int i; - char *tmp; + int i; + char *tmp; if (!*line) return (TRUE); i = -1; while (++i < (int)(sizeof(g_option_parsers) / sizeof(t_option_parser))) { - if (ft_strncmp(g_option_parsers[i].id, line, ft_strlen(g_option_parsers[i].id)) == 0) { @@ -84,37 +79,27 @@ t_state *parse_map(t_state *state, char **lines) if (*lines[i] != '1') return (NULL); state->map_height = i; - if ((state->map = (t_map)malloc(sizeof(t_cell*) * i)) == NULL) + if ((state->map = (t_map)ft_calloc(i, sizeof(t_cell*))) == NULL) return (NULL); state->map_width = ft_strcount(*lines, '1'); i = -1; while (lines[++i] != NULL) - if ((state->map[i] = create_map_row(lines[i])) == NULL) + if ((state->map[i] = create_map_row(state, lines[i])) == NULL) return (NULL); return (state); } -t_cell *create_map_row(char *line) +t_cell *create_map_row(t_state *state, char *line) { int i; t_cell *row; - if ((row = (t_cell*)malloc(sizeof(t_cell) * ft_strlen(line))) == NULL) + if ((row = (t_cell*)malloc(sizeof(t_cell) * state->map_width)) == NULL) return (NULL); i = 0; while (*line) { - if (*line == '0' || *line == '1' || *line == '2') - row[i++] = *line - '0'; - else if (*line == 'N') - row[i++] = CELL_LOOK_NORTH; - else if (*line == 'S') - row[i++] = CELL_LOOK_SOUTH; - else if (*line == 'W') - row[i++] = CELL_LOOK_WEST; - else if (*line == 'E') - row[i++] = CELL_LOOK_EAST; - else + if (!create_map_row_char(*line, row, i++)) { free(row); return (NULL); @@ -123,5 +108,10 @@ t_cell *create_map_row(char *line) while (*line == ' ') line++; } + if (i != state->map_width) + { + free(row); + return (NULL); + } return (row); } diff --git a/src/parse/parse_check.c b/src/parse/parse_check.c index 976020b..09a2429 100644 --- a/src/parse/parse_check.c +++ b/src/parse/parse_check.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */ -/* Updated: 2020/02/04 05:07:22 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 18:49:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,12 @@ static t_bool check_player_count(t_state *state) if (helper_is_player_cell(state->map[i][j])) player_count++; } - return (player_count == 1); + if (player_count != 1) + { + error_put("validate map with other than one player"); + return (FALSE); + } + return (TRUE); } t_state *parse_check(t_state *state) @@ -53,9 +58,6 @@ t_state *parse_check(t_state *state) "validate map without borders", state)); } if (!check_player_count(state)) - { - return (error_put_return_state_destroy( - "validate map with other than one player", state)); - } + return (state_destroy(state)); return (state); } diff --git a/src/parse/parse_color.c b/src/parse/parse_color.c index d3cee05..b939076 100644 --- a/src/parse/parse_color.c +++ b/src/parse/parse_color.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 09:52:34 by cacharle #+# #+# */ -/* Updated: 2020/02/04 05:12:42 by cacharle ### ########.fr */ +/* Updated: 2020/02/05 00:04:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ t_bool parse_ceilling_color(t_state *state, char *line) { long tmp; + if (state->pflags & PFLAGS_C) + return (FALSE); errno = 0; state->ceilling_color.hexcode = 0x0; if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) @@ -33,6 +35,7 @@ t_bool parse_ceilling_color(t_state *state, char *line) state->ceilling_color.rgb.b = (t_byte)tmp; if (errno != 0) return (FALSE); + state->pflags |= PFLAGS_C; return (TRUE); } @@ -40,6 +43,8 @@ t_bool parse_floor_color(t_state *state, char *line) { long tmp; + if (state->pflags & PFLAGS_F) + return (FALSE); errno = 0; state->floor_color.hexcode = 0x0; if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) @@ -57,5 +62,6 @@ t_bool parse_floor_color(t_state *state, char *line) state->floor_color.rgb.b = (t_byte)tmp; if (errno != 0) return (FALSE); + state->pflags |= PFLAGS_F; return (TRUE); } diff --git a/src/parse/parse_file.c b/src/parse/parse_file.c index 443a1b5..8c6ef5e 100644 --- a/src/parse/parse_file.c +++ b/src/parse/parse_file.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/04 03:50:53 by cacharle #+# #+# */ -/* Updated: 2020/02/04 04:28:41 by cacharle ### ########.fr */ +/* Updated: 2020/02/05 01:27:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,5 +49,39 @@ char **get_file_lines(char *filename) if (ret == -1) return (NULL); close(fd); - return (trim_file_lines(ft_split(file, '\n'))); + return (trim_file_lines(ft_split_strict(file, '\n'))); +} + +int parse_premap(t_state *state, char **lines) +{ + int i; + + i = -1; + while (lines[++i] != NULL) + { + if (*lines[i] == '\n') + continue ; + if (*lines[i] == '1') + break ; + if (!parse_line(state, lines[i])) + return (-1); + } + return (i); +} + +t_bool create_map_row_char(char c, t_cell *row, int i) +{ + if (c == '0' || c == '1' || c == '2') + row[i] = c - '0'; + else if (c == 'N') + row[i] = CELL_LOOK_NORTH; + else if (c == 'S') + row[i] = CELL_LOOK_SOUTH; + else if (c == 'W') + row[i] = CELL_LOOK_WEST; + else if (c == 'E') + row[i] = CELL_LOOK_EAST; + else + return (FALSE); + return (TRUE); } diff --git a/src/parse/parse_resolution.c b/src/parse/parse_resolution.c index 300b461..b863c94 100644 --- a/src/parse/parse_resolution.c +++ b/src/parse/parse_resolution.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:27 by cacharle #+# #+# */ -/* Updated: 2020/02/04 04:40:35 by cacharle ### ########.fr */ +/* Updated: 2020/02/05 00:04:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,8 @@ t_bool parse_resolution(t_state *state, char *line) { long tmp; + if (state->pflags & PFLAGS_R) + return (FALSE); errno = 0; if ((tmp = ft_strtol(line, &line, 10)) < MIN_RES) return (FALSE); @@ -37,5 +39,6 @@ t_bool parse_resolution(t_state *state, char *line) state->window.height = tmp; if (*line != '\0' || errno != 0 || state->window.height > 2160) return (FALSE); + state->pflags |= PFLAGS_R; return (TRUE); } diff --git a/src/parse/parse_textures.c b/src/parse/parse_textures.c index f6ba1a0..e048bd5 100644 --- a/src/parse/parse_textures.c +++ b/src/parse/parse_textures.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 09:47:53 by cacharle #+# #+# */ -/* Updated: 2020/02/02 19:21:39 by cacharle ### ########.fr */ +/* Updated: 2020/02/05 00:06:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,35 +14,50 @@ t_bool parse_north_texture(t_state *state, char *line) { + if (state->pflags & PFLAGS_NO) + return (FALSE); if ((state->textures_path[TEX_NORTH] = ft_strdup(line)) == NULL) return (FALSE); + state->pflags |= PFLAGS_NO; return (TRUE); } t_bool parse_south_texture(t_state *state, char *line) { + if (state->pflags & PFLAGS_SO) + return (FALSE); if ((state->textures_path[TEX_SOUTH] = ft_strdup(line)) == NULL) return (FALSE); + state->pflags |= PFLAGS_SO; return (TRUE); } t_bool parse_west_texture(t_state *state, char *line) { + if (state->pflags & PFLAGS_WE) + return (FALSE); if ((state->textures_path[TEX_WEST] = ft_strdup(line)) == NULL) return (FALSE); + state->pflags |= PFLAGS_WE; return (TRUE); } t_bool parse_east_texture(t_state *state, char *line) { + if (state->pflags & PFLAGS_EA) + return (FALSE); if ((state->textures_path[TEX_EAST] = ft_strdup(line)) == NULL) return (FALSE); + state->pflags |= PFLAGS_EA; return (TRUE); } t_bool parse_sprite_texture(t_state *state, char *line) { + if (state->pflags & PFLAGS_S) + return (FALSE); if ((state->textures_path[TEX_SPRITE] = ft_strdup(line)) == NULL) return (FALSE); + state->pflags |= PFLAGS_S; return (TRUE); } |
