diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-11 10:38:41 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-11 10:39:37 +0100 |
| commit | 0dcba6ff7e68ed13f8e6caadd80b77506b917050 (patch) | |
| tree | 16e1393115941b87ca50f04f1e37511f3f7bbdb5 /parse | |
| parent | 988e058680280e25d345b17d840c3c6d40e30a76 (diff) | |
| download | cub3d-0dcba6ff7e68ed13f8e6caadd80b77506b917050.tar.gz cub3d-0dcba6ff7e68ed13f8e6caadd80b77506b917050.tar.bz2 cub3d-0dcba6ff7e68ed13f8e6caadd80b77506b917050.zip | |
Hardcore refactoring
Diffstat (limited to 'parse')
| -rw-r--r-- | parse/parse.c | 71 | ||||
| -rw-r--r-- | parse/parse_ceilling_color.c | 27 | ||||
| -rw-r--r-- | parse/parse_color.c | 55 | ||||
| -rw-r--r-- | parse/parse_east_texture.c | 19 | ||||
| -rw-r--r-- | parse/parse_floor_color.c | 27 | ||||
| -rw-r--r-- | parse/parse_north_texture.c | 19 | ||||
| -rw-r--r-- | parse/parse_resolution.c | 13 | ||||
| -rw-r--r-- | parse/parse_south_texture.c | 19 | ||||
| -rw-r--r-- | parse/parse_sprite_texture.c | 19 | ||||
| -rw-r--r-- | parse/parse_textures.c | 48 | ||||
| -rw-r--r-- | parse/parse_west_texture.c | 19 |
11 files changed, 150 insertions, 186 deletions
diff --git a/parse/parse.c b/parse/parse.c index 8f3124d..145d5ad 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -6,60 +6,65 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:21:38 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 10:12:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -t_parsing *parse(char *filename) +t_state *parse(char *filename) { - int i; - char **lines; - t_parsing *parsing; + int i; + char **lines; + t_state *state; - if ((lines = get_file_lines(filename)) == NULL) - return (NULL); - if ((parsing = (t_parsing*)malloc(sizeof(t_parsing))) == NULL) + if ((state = state_new_empty()) == NULL) return (NULL); - parsing->map = NULL; - parsing->ceilling_color.hexcode = 0; - parsing->floor_color.hexcode = 0; + if ((lines = get_file_lines(filename)) == NULL) + return (state_destroy(state)); i = -1; while (lines[++i] != NULL) { if (*lines[i] == '1') break ; - if (!parse_line(parsing, lines[i])) - return (NULL); + if (!parse_line(state, lines[i])) + { + helper_free_splited(lines); + return (state_destroy(state)); + } } - if ((parsing = parse_map(parsing, lines + i)) == NULL) - return (NULL); - free(lines); - return (parsing); + if ((state = parse_map(state, lines + i)) == NULL) + { + helper_free_splited(lines); + return (state_destroy(state)); + } + helper_free_splited(lines); + return (state); } char **get_file_lines(char *filename) { int fd; int ret; - char *line; + char buf[BUFFER_SIZE]; char *file; fd = open(filename, O_RDONLY); if ((file = ft_strdup("")) == NULL) return (NULL); - while ((ret = get_next_line(fd, &line)) == 1) - if ((file = ft_strjoin_free(file, ft_strjoin_free(line, ft_strdup("\n"), 2), 2)) == NULL) + while ((ret = read(fd, buf, BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((file = ft_strjoin_free(file, buf, 1)) == NULL) return (NULL); + } if (ret == -1) return (NULL); - free(line); close(fd); return (ft_split(file, '\n')); } -static t_option_parser option_parsers[] = +static t_option_parser g_option_parsers[] = { {"R", parse_resolution}, {"NO", parse_north_texture}, @@ -71,9 +76,9 @@ static t_option_parser option_parsers[] = {"C", parse_ceilling_color} }; -#define OPTIONS_PARSERS_SIZE (sizeof(option_parsers) / sizeof(t_option_parser)) +#define OPTIONS_PARSERS_SIZE (sizeof(g_option_parsers) / sizeof(t_option_parser)) -t_bool parse_line(t_parsing *parsing, char *line) +t_bool parse_line(t_state *state, char *line) { int i; @@ -81,12 +86,14 @@ t_bool parse_line(t_parsing *parsing, char *line) return (TRUE); i = -1; while (++i < (int)OPTIONS_PARSERS_SIZE) - if (ft_strncmp(option_parsers[i].id, line, ft_strlen(option_parsers[i].id)) == 0) - return (option_parsers[i].func(parsing, line + ft_strlen(option_parsers[i].id))); + if (ft_strncmp(g_option_parsers[i].id, line, + ft_strlen(g_option_parsers[i].id)) == 0) + return (g_option_parsers[i].func( + state, line + ft_strlen(g_option_parsers[i].id) + 1)); return (FALSE); } -t_parsing *parse_map(t_parsing *parsing, char **lines) +t_state *parse_map(t_state *state, char **lines) { int i; @@ -94,15 +101,15 @@ t_parsing *parse_map(t_parsing *parsing, char **lines) while (lines[++i] != NULL) if (*lines[i] != '1') return (NULL); - parsing->map_height = i; - if ((parsing->map = (t_map)malloc(sizeof(t_cell*) * i)) == NULL) + state->map_height = i; + if ((state->map = (t_map)malloc(sizeof(t_cell*) * i)) == NULL) return (NULL); - parsing->map_width = ft_strcount(*lines, '1'); + state->map_width = ft_strcount(*lines, '1'); i = -1; while (lines[++i] != NULL) - if ((parsing->map[i] = create_map_row(lines[i])) == NULL) + if ((state->map[i] = create_map_row(lines[i])) == NULL) return (NULL); - return (parsing); + return (state); } t_cell *create_map_row(char *line) diff --git a/parse/parse_ceilling_color.c b/parse/parse_ceilling_color.c deleted file mode 100644 index 3b6c334..0000000 --- a/parse/parse_ceilling_color.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_ceilling_color.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:31:32 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:22:49 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_ceilling_color(t_parsing *parsing, char *line) -{ - line++; - if ((parsing->ceilling_color.rgb.r = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->ceilling_color.rgb.g = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->ceilling_color.rgb.b = ft_atoi(line)) > 255) - return (FALSE); - return (TRUE); -} diff --git a/parse/parse_color.c b/parse/parse_color.c new file mode 100644 index 0000000..f602706 --- /dev/null +++ b/parse/parse_color.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 09:52:34 by cacharle #+# #+# */ +/* Updated: 2020/01/11 10:14:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_bool parse_ceilling_color(t_state *state, char *line) +{ + int tmp; + + line++; + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->ceilling_color.rgb.r = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->ceilling_color.rgb.g = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->ceilling_color.rgb.b = (t_byte)tmp; + return (TRUE); +} + +t_bool parse_floor_color(t_state *state, char *line) +{ + int tmp; + + line++; + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->floor_color.rgb.r = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->floor_color.rgb.g = (t_byte)tmp; + if ((line = ft_strchr(line, ',') + 1) == NULL) + return (FALSE); + if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + return (FALSE); + state->floor_color.rgb.b = (t_byte)tmp; + return (TRUE); +} diff --git a/parse/parse_east_texture.c b/parse/parse_east_texture.c deleted file mode 100644 index 846b36d..0000000 --- a/parse/parse_east_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_east_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:37 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:02 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_east_texture(t_parsing *parsing, char *line) -{ - parsing->east_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_floor_color.c b/parse/parse_floor_color.c deleted file mode 100644 index ab16bd9..0000000 --- a/parse/parse_floor_color.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_floor_color.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:32 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:23:26 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_floor_color(t_parsing *parsing, char *line) -{ - line++; - if ((parsing->floor_color.rgb.r = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->floor_color.rgb.g = ft_atoi(line)) > 255) - return (FALSE); - line = ft_strchr(line, ',') + 1; - if ((parsing->floor_color.rgb.b = ft_atoi(line)) > 255) - return (FALSE); - return (TRUE); -} diff --git a/parse/parse_north_texture.c b/parse/parse_north_texture.c deleted file mode 100644 index eb8cb3c..0000000 --- a/parse/parse_north_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_north_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:47 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:30:52 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_north_texture(t_parsing *parsing, char *line) -{ - parsing->north_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_resolution.c b/parse/parse_resolution.c index e63317e..d6c5759 100644 --- a/parse/parse_resolution.c +++ b/parse/parse_resolution.c @@ -6,16 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:27 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:47 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 09:44:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -t_bool parse_resolution(t_parsing *parsing, char *line) +t_bool parse_resolution(t_state *state, char *line) { - parsing->resolution_width = ft_atoi(line); - line = ft_strrchr(line, ' ') + 1; - parsing->resolution_height = ft_atoi(line); + if ((state->window.width = ft_atoi(line)) < 0) + return (FALSE); + if ((line = ft_strrchr(line, ' ') + 1) == NULL) + return (FALSE); + if ((state->window.height = ft_atoi(line)) < 0) + return (FALSE); return (TRUE); } diff --git a/parse/parse_south_texture.c b/parse/parse_south_texture.c deleted file mode 100644 index 10ee403..0000000 --- a/parse/parse_south_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_south_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:55 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:30:46 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_south_texture(t_parsing *parsing, char *line) -{ - parsing->south_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_sprite_texture.c b/parse/parse_sprite_texture.c deleted file mode 100644 index 8e3f40b..0000000 --- a/parse/parse_sprite_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_sprite_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:30:05 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:56 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_sprite_texture(t_parsing *parsing, char *line) -{ - parsing->sprite_texture_path = ft_strdup(line + 1); - return (TRUE); -} diff --git a/parse/parse_textures.c b/parse/parse_textures.c new file mode 100644 index 0000000..a0fb8f6 --- /dev/null +++ b/parse/parse_textures.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_textures.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 09:47:53 by cacharle #+# #+# */ +/* Updated: 2020/01/11 09:51:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_bool parse_north_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_NORTH] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_south_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_SOUTH] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_west_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_WEST] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_east_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_EAST] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} + +t_bool parse_sprite_texture(t_state *state, char *line) +{ + if ((state->textures_path[TEX_SPRITE] = ft_strdup(line)) == NULL) + return (FALSE); + return (TRUE); +} diff --git a/parse/parse_west_texture.c b/parse/parse_west_texture.c deleted file mode 100644 index dca1629..0000000 --- a/parse/parse_west_texture.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_west_texture.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/15 09:29:42 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:30:40 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_bool parse_west_texture(t_parsing *parsing, char *line) -{ - parsing->west_texture_path = ft_strdup(line + 1); - return (TRUE); -} |
