aboutsummaryrefslogtreecommitdiff
path: root/src/parse
diff options
context:
space:
mode:
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/parse.c53
-rw-r--r--src/parse/parse_check.c4
-rw-r--r--src/parse/parse_color.c32
-rw-r--r--src/parse/parse_file.c53
-rw-r--r--src/parse/parse_resolution.c25
5 files changed, 114 insertions, 53 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}