From 3b224458a5d539bbe00318d939c9a099f3f158e1 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 4 Feb 2020 05:34:27 +0100 Subject: better parsing --- Makefile | 1 + include/cub3d.h | 10 +++++++-- libft | 2 +- minimalist.cub | 26 ++++++++++++++++++++-- src/capture.c | 4 ++-- src/main.c | 28 +++++++++++------------ src/parse/parse.c | 53 +++++++++++++++----------------------------- src/parse/parse_check.c | 4 +++- src/parse/parse_color.c | 32 +++++++++++++++----------- src/parse/parse_file.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/parse/parse_resolution.c | 25 +++++++++++++++++---- 11 files changed, 164 insertions(+), 74 deletions(-) create mode 100644 src/parse/parse_file.c diff --git a/Makefile b/Makefile index 0ee3c0e..a6ab056 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ LDFLAGS = -L$(LIBFT_PATH) -lft \ NAME = cub3D SRCFILES = main.c \ parse/parse.c \ + parse/parse_file.c \ parse/parse_textures.c \ parse/parse_color.c \ parse/parse_resolution.c \ diff --git a/include/cub3d.h b/include/cub3d.h index 30b3b74..eed376c 100644 --- a/include/cub3d.h +++ b/include/cub3d.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2020/02/04 02:54:03 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 04:09:56 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include @@ -14,6 +14,7 @@ #ifndef CUB3D_H # define CUB3D_H +# include # include # include # include @@ -172,11 +173,16 @@ typedef struct s_option_parser */ t_state *parse(char *filename); -char **get_file_lines(char *filename); t_bool parse_line(t_state *state, char *line); t_state *parse_map(t_state *state, char **lines); t_cell *create_map_row(char *line); +/* +** parse/parse_file.c +*/ + +char **get_file_lines(char *filename); + /* ** parse/parse_check.c */ diff --git a/libft b/libft index 435fec3..f078f19 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 435fec311c0c5173ae99c32b8f4e6431bd1e43d2 +Subproject commit f078f191515d0fc65340a8722fad14f3de679d7b diff --git a/minimalist.cub b/minimalist.cub index a5470f0..24a1db0 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -1,15 +1,37 @@ -R 1201 900 + + + +R 200 200 + NO ./textures/greystone.xpm + + + SO ./textures/greystone.xpm + + + + WE ./textures/greystone.xpm + + EA ./textures/greystone.xpm -S ./textures/cacharle.xpm + + + + +S ./textures/cacharle.xpm + + + + F 10,10,10 C 40,40,40 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 2 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 1 + 1 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 2 0 1 diff --git a/src/capture.c b/src/capture.c index 59c49ec..3015621 100644 --- a/src/capture.c +++ b/src/capture.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */ -/* Updated: 2020/02/04 00:29:59 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 05:32:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ t_bool bmp_write(t_image *image, t_byte file_header[FILE_HEADER_SIZE], int fd; t_byte *bmp_data; - if ((fd = open(CAPTURE_FILENAME, O_WRONLY | O_CREAT, S_IRWXU)) < 0) + if ((fd = open(CAPTURE_FILENAME, O_WRONLY | O_CREAT, 0644)) < 0) return (FALSE); if ((bmp_data = malloc(sizeof(unsigned char) * (image->width * IMG_DEPTH))) == NULL) diff --git a/src/main.c b/src/main.c index dac0f33..f1d4f20 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2020/02/03 20:45:17 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 05:29:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int main(int argc, char **argv) return (0); } - +/* */ /* int main(int argc, char **argv) */ /* { */ /* (void)argc; */ @@ -37,11 +37,11 @@ int main(int argc, char **argv) /* if (s == NULL) */ /* 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]); */ -/* printf("WE %s\n", s->textures_path[TEX_WEST]); */ -/* printf("EA %s\n\n", s->textures_path[TEX_EAST]); */ -/* printf("S %s\n", s->textures_path[TEX_SPRITE]); */ +/* printf("NO |%s|\n", s->textures_path[TEX_NORTH]); */ +/* printf("SO |%s|\n", s->textures_path[TEX_SOUTH]); */ +/* printf("WE |%s|\n", s->textures_path[TEX_WEST]); */ +/* printf("EA |%s|\n\n", s->textures_path[TEX_EAST]); */ +/* printf("S |%s|\n", s->textures_path[TEX_SPRITE]); */ /* printf("F %d,%d,%d\n", s->floor_color.rgb.r, s->floor_color.rgb.g, s->floor_color.rgb.b); */ /* printf("C %d,%d,%d\n\n", s->ceilling_color.rgb.r, s->ceilling_color.rgb.g, s->ceilling_color.rgb.b); */ /* printf("%dx%d\n", s->map_height, s->map_width); */ @@ -61,12 +61,12 @@ int main(int argc, char **argv) /* printf("\n"); */ /* } */ /* printf("post state_new\n"); */ -/* if ((s = state_new(s)) == NULL) */ -/* { */ -/* printf("Error: state new"); */ -/* return 1; */ -/* } */ -/* printf("state->pos [%f %f]\n", s->pos.x, s->pos.y); */ -/* state_destroy(s); */ +/* #<{(| if ((s = state_new(s)) == NULL) |)}># */ +/* #<{(| { |)}># */ +/* #<{(| printf("Error: state new"); |)}># */ +/* #<{(| return 1; |)}># */ +/* #<{(| } |)}># */ +/* #<{(| printf("state->pos [%f %f]\n", s->pos.x, s->pos.y); |)}># */ +/* #<{(| state_destroy(s); |)}># */ /* return 0; */ /* } */ diff --git a/src/parse/parse.c b/src/parse/parse.c index 63ffb71..4c51945 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2020/02/04 02:28:12 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 04:41:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,54 +40,37 @@ t_state *parse(char *filename) return (state); } -char **get_file_lines(char *filename) -{ - int fd; - int ret; - char buf[CUB3D_BUFFER_SIZE + 1]; - char *file; - - if ((fd = open(filename, O_RDONLY)) < 0) - return (NULL); - if ((file = ft_strdup("")) == NULL) - return (NULL); - while ((ret = read(fd, buf, CUB3D_BUFFER_SIZE)) > 0) - { - buf[ret] = '\0'; - if ((file = ft_strjoin(file, buf)) == NULL) - return (NULL); - } - if (ret == -1) - return (NULL); - close(fd); - return (ft_split(file, '\n')); -} - static t_option_parser g_option_parsers[] = { - {"R", parse_resolution}, - {"NO", parse_north_texture}, - {"SO", parse_south_texture}, - {"WE", parse_west_texture}, - {"EA", parse_east_texture}, - {"S", parse_sprite_texture}, - {"F", parse_floor_color}, - {"C", parse_ceilling_color} + {"R ", parse_resolution}, + {"NO ", parse_north_texture}, + {"SO ", parse_south_texture}, + {"WE ", parse_west_texture}, + {"EA ", parse_east_texture}, + {"S ", parse_sprite_texture}, + {"F ", parse_floor_color}, + {"C ", parse_ceilling_color} }; t_bool parse_line(t_state *state, char *line) { - int i; + 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) - return (g_option_parsers[i].func( - state, line + ft_strlen(g_option_parsers[i].id) + 1)); + { + tmp = line + ft_strlen(g_option_parsers[i].id); + while (*tmp == ' ') + tmp++; + return (g_option_parsers[i].func(state, tmp)); + } } return (FALSE); } diff --git a/src/parse/parse_check.c b/src/parse/parse_check.c index 5a36179..976020b 100644 --- a/src/parse/parse_check.c +++ b/src/parse/parse_check.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */ -/* Updated: 2020/02/04 02:26:18 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 05:07:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,8 @@ t_state *parse_check(t_state *state) { int i; + if (state == NULL) + return (NULL); i = -1; while (++i < state->map_width) { diff --git a/src/parse/parse_color.c b/src/parse/parse_color.c index 5385c0c..d3cee05 100644 --- a/src/parse/parse_color.c +++ b/src/parse/parse_color.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 09:52:34 by cacharle #+# #+# */ -/* Updated: 2020/02/04 02:26:46 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 05:12:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,42 +14,48 @@ t_bool parse_ceilling_color(t_state *state, char *line) { - int tmp; + long tmp; + errno = 0; state->ceilling_color.hexcode = 0x0; - if ((tmp = ft_atoi(line)) > 255 || tmp < 0) // cant do atoi due to overflow stuff, use ft_strtol + if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) return (FALSE); state->ceilling_color.rgb.r = (t_byte)tmp; - if ((line = ft_strchr(line, ',') + 1) == NULL) + if (*line++ != ',' || !ft_isdigit(*line)) return (FALSE); - if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) return (FALSE); state->ceilling_color.rgb.g = (t_byte)tmp; - if ((line = ft_strchr(line, ',') + 1) == NULL) + if (*line++ != ',' || !ft_isdigit(*line)) return (FALSE); - if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) return (FALSE); state->ceilling_color.rgb.b = (t_byte)tmp; + if (errno != 0) + return (FALSE); return (TRUE); } t_bool parse_floor_color(t_state *state, char *line) { - int tmp; + long tmp; + errno = 0; state->floor_color.hexcode = 0x0; - if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) return (FALSE); state->floor_color.rgb.r = (t_byte)tmp; - if ((line = ft_strchr(line, ',') + 1) == NULL) + if (*line++ != ',' || !ft_isdigit(*line)) return (FALSE); - if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) return (FALSE); state->floor_color.rgb.g = (t_byte)tmp; - if ((line = ft_strchr(line, ',') + 1) == NULL) + if (*line++ != ',' || !ft_isdigit(*line)) return (FALSE); - if ((tmp = ft_atoi(line)) > 255 || tmp < 0) + if ((tmp = ft_strtol(line, &line, 10)) > 255 || tmp < 0) return (FALSE); state->floor_color.rgb.b = (t_byte)tmp; + if (errno != 0) + return (FALSE); return (TRUE); } diff --git a/src/parse/parse_file.c b/src/parse/parse_file.c new file mode 100644 index 0000000..443a1b5 --- /dev/null +++ b/src/parse/parse_file.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/04 03:50:53 by cacharle #+# #+# */ +/* Updated: 2020/02/04 04:28:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +static char **trim_file_lines(char **lines) +{ + int i; + char *tmp; + + if (lines == NULL) + return (NULL); + i = -1; + while (lines[++i] != NULL) + { + if ((tmp = ft_strtrim(lines[i], " ")) == NULL) + return (NULL); + lines[i] = tmp; + } + return (lines); +} + +char **get_file_lines(char *filename) +{ + int fd; + int ret; + char buf[CUB3D_BUFFER_SIZE + 1]; + char *file; + + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); + if ((file = ft_strdup("")) == NULL) + return (NULL); + while ((ret = read(fd, buf, CUB3D_BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((file = ft_strjoin(file, buf)) == NULL) + return (NULL); + } + if (ret == -1) + return (NULL); + close(fd); + return (trim_file_lines(ft_split(file, '\n'))); +} diff --git a/src/parse/parse_resolution.c b/src/parse/parse_resolution.c index d6c5759..300b461 100644 --- a/src/parse/parse_resolution.c +++ b/src/parse/parse_resolution.c @@ -6,19 +6,36 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:27 by cacharle #+# #+# */ -/* Updated: 2020/01/11 09:44:18 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 04:40:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" +#define MIN_RES 20 +#define MAX_RES_HEIGHT 2160 +#define MAX_RES_WIDTH 3840 + t_bool parse_resolution(t_state *state, char *line) { - if ((state->window.width = ft_atoi(line)) < 0) + long tmp; + + errno = 0; + if ((tmp = ft_strtol(line, &line, 10)) < MIN_RES) + return (FALSE); + if (tmp > MAX_RES_WIDTH) + return (FALSE); + state->window.width = tmp; + if (*line != ' ') + return (FALSE); + while (*line == ' ') + line++; + if ((tmp = ft_strtol(line, &line, 10)) < MIN_RES) return (FALSE); - if ((line = ft_strrchr(line, ' ') + 1) == NULL) + if (tmp > MAX_RES_HEIGHT) return (FALSE); - if ((state->window.height = ft_atoi(line)) < 0) + state->window.height = tmp; + if (*line != '\0' || errno != 0 || state->window.height > 2160) return (FALSE); return (TRUE); } -- cgit