aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-04 05:34:27 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-04 05:34:27 +0100
commit3b224458a5d539bbe00318d939c9a099f3f158e1 (patch)
tree1e8533c49647120e2239904e2e0b167b408043cc /src
parentc8de182e9fa0c8a2674bf2f13d2ed9f500607ebd (diff)
downloadcub3d-3b224458a5d539bbe00318d939c9a099f3f158e1.tar.gz
cub3d-3b224458a5d539bbe00318d939c9a099f3f158e1.tar.bz2
cub3d-3b224458a5d539bbe00318d939c9a099f3f158e1.zip
better parsing
Diffstat (limited to 'src')
-rw-r--r--src/capture.c4
-rw-r--r--src/main.c28
-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
7 files changed, 130 insertions, 69 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <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);
}