aboutsummaryrefslogtreecommitdiff
path: root/parse
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-15 09:34:54 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-15 09:42:45 +0100
commit9e1f4605408821b74c9e74216fbf995d29a3921c (patch)
tree5817402f6ae0329b2015efb1379477f0a61d110a /parse
parent0433beffa8c5caae0503082b9776e1035c44f94f (diff)
downloadcub3d-9e1f4605408821b74c9e74216fbf995d29a3921c.tar.gz
cub3d-9e1f4605408821b74c9e74216fbf995d29a3921c.tar.bz2
cub3d-9e1f4605408821b74c9e74216fbf995d29a3921c.zip
.cub file parsing
Diffstat (limited to 'parse')
-rw-r--r--parse/parse.c126
-rw-r--r--parse/parse_ceilling_color.c18
-rw-r--r--parse/parse_east_texture.c15
-rw-r--r--parse/parse_floor_color.c18
-rw-r--r--parse/parse_north_texture.c15
-rw-r--r--parse/parse_resolution.c16
-rw-r--r--parse/parse_south_texture.c15
-rw-r--r--parse/parse_sprite_texture.c15
-rw-r--r--parse/parse_west_texture.c15
9 files changed, 215 insertions, 38 deletions
diff --git a/parse/parse.c b/parse/parse.c
index c882424..f543ca5 100644
--- a/parse/parse.c
+++ b/parse/parse.c
@@ -1,24 +1,64 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */
+/* Updated: 2019/11/15 09:32:54 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include "cub3d.h"
-t_parsing *parse(char *filename)
+t_parsing *parse(char *filename)
{
- int fd;
- int ret;
- char *line;
- t_parsing *parsing;
+ int i;
+ char **lines;
+ t_parsing *parsing;
+ if ((lines = get_file_lines(filename)) == NULL)
+ return (NULL);
if ((parsing = (t_parsing*)malloc(sizeof(t_parsing))) == NULL)
return (NULL);
+ parsing->map = NULL;
+ i = -1;
+ while (lines[++i] != NULL)
+ {
+ if (*lines[i] == '1')
+ break ;
+ if (!parse_line(parsing, lines[i]))
+ return (NULL);
+ }
+ if ((parsing = parse_map(parsing, lines + i)) == NULL)
+ return (NULL);
+ free(lines);
+ return (parsing);
+}
+
+char **get_file_lines(char *filename)
+{
+ int fd;
+ int ret;
+ char *line;
+ char *file;
+
fd = open(filename, O_RDONLY);
+ if ((file = ft_strdup("")) == NULL)
+ return (NULL);
while ((ret = get_next_line(fd, &line)) == 1)
- if (!parse_line(parsing, line))
+ if ((file = ft_strjoin_free(file, ft_strjoin_free(line, ft_strdup("\n"), 2), 2)) == NULL)
return (NULL);
if (ret == -1)
return (NULL);
- return (parsing);
+ free(line);
+ close(fd);
+ return (ft_split(file, '\n'));
}
-static t_option_parser option_parsers[] = {
+static t_option_parser option_parsers[] =
+{
{"R", parse_resolution},
{"NO", parse_north_texture},
{"SO", parse_south_texture},
@@ -26,29 +66,75 @@ static t_option_parser option_parsers[] = {
{"EA", parse_east_texture},
{"S", parse_sprite_texture},
{"F", parse_floor_color},
- {"C", parse_ceilling_color},
+ {"C", parse_ceilling_color}
};
-#define OPTIONS_PARSER_SIZE (sizeof(line_parsers) / sizeof(t_line_parser))
+#define OPTIONS_PARSERS_SIZE (sizeof(option_parsers) / sizeof(t_option_parser))
-t_bool parse_option(t_parsing *parsing, char *line)
+t_bool parse_line(t_parsing *parsing, char *line)
{
int i;
if (!*line)
return (TRUE);
i = -1;
- while (++i < LINE_PARSER_SIZE)
- if (ft_strchr("012", *line) != NULL)
- return (parse_map(parsing, line));
- else 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));
+ 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)));
return (FALSE);
}
-t_bool parse_map(t_parsing *parsing, char *line)
+t_parsing *parse_map(t_parsing *parsing, char **lines)
{
- return (FALSE);
+ int i;
+
+ i = -1;
+ 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)
+ return (NULL);
+ parsing->map_width = ft_strcount(*lines, '1');
+ i = -1;
+ while (lines[++i] != NULL)
+ if ((parsing->map[i] = create_map_row(lines[i])) == NULL)
+ return (NULL);
+ return (parsing);
+}
+
+t_cell *create_map_row(char *line)
+{
+ int i;
+ t_cell *row;
+
+ if ((row = (t_cell*)malloc(sizeof(t_cell) * ft_strlen(line))) == NULL)
+ return (NULL);
+ i = 0;
+ while (*line)
+ {
+ if (*line == '0')
+ row[i++] = CELL_EMPTY;
+ else if (*line == '1')
+ row[i++] = CELL_WALL;
+ else if (*line == '2')
+ row[i++] = CELL_ITEM;
+ 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
+ {
+ free(row);
+ return (NULL);
+ }
+ line++;
+ while (*line == ' ')
+ line++;
+ }
+ return (row);
}
diff --git a/parse/parse_ceilling_color.c b/parse/parse_ceilling_color.c
index 0f93ed1..67291d0 100644
--- a/parse/parse_ceilling_color.c
+++ b/parse/parse_ceilling_color.c
@@ -1,12 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse_ceilling_color.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/15 09:31:32 by cacharle #+# #+# */
+/* Updated: 2019/11/15 09:31:37 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include "cub3d.h"
-t_bool parse_ceilling_color(t_parsing *parsing, char *line)
+t_bool parse_ceilling_color(t_parsing *parsing, char *line)
{
line++;
parsing->ceilling_color.r = ft_atoi(line);
- line = ft_strchr(line, ',');
+ line = ft_strchr(line, ',') + 1;
parsing->ceilling_color.g = ft_atoi(line);
- line = ft_strchr(line, ',');
+ line = ft_strchr(line, ',') + 1;
parsing->ceilling_color.b = ft_atoi(line);
return (TRUE);
}
diff --git a/parse/parse_east_texture.c b/parse/parse_east_texture.c
index e8d356a..846b36d 100644
--- a/parse/parse_east_texture.c
+++ b/parse/parse_east_texture.c
@@ -1,8 +1,19 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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)
+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
index 62d0a06..ef2204a 100644
--- a/parse/parse_floor_color.c
+++ b/parse/parse_floor_color.c
@@ -1,12 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse_floor_color.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/15 09:29:32 by cacharle #+# #+# */
+/* Updated: 2019/11/15 09:31:16 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include "cub3d.h"
-t_bool parse_floor_color(t_parsing *parsing, char *line)
+t_bool parse_floor_color(t_parsing *parsing, char *line)
{
line++;
parsing->floor_color.r = ft_atoi(line);
- line = ft_strchr(line, ',');
+ line = ft_strchr(line, ',') + 1;
parsing->floor_color.g = ft_atoi(line);
- line = ft_strchr(line, ',');
+ line = ft_strchr(line, ',') + 1;
parsing->floor_color.b = ft_atoi(line);
return (TRUE);
}
diff --git a/parse/parse_north_texture.c b/parse/parse_north_texture.c
index 0d1af87..eb8cb3c 100644
--- a/parse/parse_north_texture.c
+++ b/parse/parse_north_texture.c
@@ -1,8 +1,19 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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)
+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 c8e0c7f..e63317e 100644
--- a/parse/parse_resolution.c
+++ b/parse/parse_resolution.c
@@ -1,9 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse_resolution.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/15 09:29:27 by cacharle #+# #+# */
+/* Updated: 2019/11/15 09:31:47 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include "cub3d.h"
-t_bool parse_resolution(t_parsing *parsing, char *line)
+t_bool parse_resolution(t_parsing *parsing, char *line)
{
parsing->resolution_width = ft_atoi(line);
- line = ft_strchr(line, ' ');
+ line = ft_strrchr(line, ' ') + 1;
parsing->resolution_height = ft_atoi(line);
return (TRUE);
}
diff --git a/parse/parse_south_texture.c b/parse/parse_south_texture.c
index 9ec89d0..10ee403 100644
--- a/parse/parse_south_texture.c
+++ b/parse/parse_south_texture.c
@@ -1,8 +1,19 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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)
+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
index 039a827..8e3f40b 100644
--- a/parse/parse_sprite_texture.c
+++ b/parse/parse_sprite_texture.c
@@ -1,8 +1,19 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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)
+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_west_texture.c b/parse/parse_west_texture.c
index 5252ab4..dca1629 100644
--- a/parse/parse_west_texture.c
+++ b/parse/parse_west_texture.c
@@ -1,8 +1,19 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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)
+t_bool parse_west_texture(t_parsing *parsing, char *line)
{
parsing->west_texture_path = ft_strdup(line + 1);
return (TRUE);
}
-