Skip to content

Commit

Permalink
Started basic parsing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Aug 15, 2024
1 parent f144e04 commit fced34e
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 6 deletions.
9 changes: 7 additions & 2 deletions includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */
/* Updated: 2024/08/15 00:34:12 by tsuchen ### ########.fr */
/* Updated: 2024/08/15 15:05:11 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,6 +30,11 @@
# include "ft_printf.h"
# include "get_next_line_bonus.h"

# include "parser.h"

# define SUCCESS 0
# define PANIC 1

# define OUT STDOUT_FILENO
# define IN STDIN_FILENO
# define ER STDERR_FILENO
Expand Down
17 changes: 17 additions & 0 deletions includes/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 15:00:57 by jteissie #+# #+# */
/* Updated: 2024/08/15 15:04:21 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_H
# define PARSER_H
# include "cub3d.h"
# define MAP_ERR 1
#endif
Empty file removed parser.c
Empty file.
12 changes: 10 additions & 2 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */
/* Updated: 2024/08/15 12:04:17 by tsuchen ### ########.fr */
/* Updated: 2024/08/15 16:57:49 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

int main(int ac, char *av[])
{
int map_fd;
(void)ac;
(void)av;
//verify arguments
map_fd = open(av[1], O_RDONLY);
if (map_fd < 0)
return (EXIT_FAILURE);
if (parse_map(map_fd) == PANIC)
return (EXIT_FAILURE);
close(map_fd);
return (0);
}
160 changes: 158 additions & 2 deletions srcs/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,167 @@
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:12:00 by tsuchen #+# #+# */
/* Updated: 2024/08/15 14:25:48 by jteissie ### ########.fr */
/* Updated: 2024/08/15 18:03:39 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

// 1 are walls, 0 are walkable spaces, whitespaces are walkable spaces. Map is invalid if no walkable spaces or not enclosed by walls

static char **build_map(int map_fd)
{
char **map;
char *line;
u_int16_t index;

index = 0;
line = get_next_line(map_fd);
if (!line)
return (NULL);
while (line)
{
map[index] == ft_calloc(1, sizeof(char *));
if (!map[index])
{
free(line);
panic_free(map);
return (NULL);
}
map[index] = line;
line = get_next_line(map_fd);
index++;
}
map[index] = NULL;
return (map);
}

static int find_start(u_int16_t *coordinates, char **map)
{
u_int16_t x;
u_int16_t y;

x = 0;
y = 0;
coordinates[0] = -1;
coordinates[1] = -1;
while (map[x][y])
{
while (map[x][y])
{
if (map[x][y] == 'N' || map[x][y] == 'S' || map[x][y] == 'W' || map[x][y] == 'E') //enums please
{
if (coordinates[0] != -1 || coordinates[1] != 1)
return (PANIC);
coordinates[0] = x;
coordinates[1] = y;
}
y++;
}
x++;
y = 0;
}
if (coordinates[0] == -1)
return (PANIC);
return (SUCCESS);
}


t_bool is_invalid_char(char c)
{
if (c == '1' || c == '0')
return (FALSE);
else if (c == ' ')
return (FALSE);
else if (c == 'N' || c == 'S' || c == 'W' || c == 'E')
return (FALSE);
return (TRUE);
}

int check_invalid_chars(char **map)
{
u_int16_t x;
u_int16_t y;

x = 0;
y = 0;
while (map[x][y])
{
while (map[x][y])
{
if (is_invalid_char(map[x][y]) == TRUE)
return (PANIC);
y++;
}
x++;
y = 0;
}
return (SUCCESS);
}

void fill_whitespaces(char **map)
{
u_int16_t x;
u_int16_t y;

x = 0;
y = 0;
while (map[x][y])
{
while (map[x][y])
{
if (map[x][y] == ' ')
map[x][y] == '1';
y++;
}
x++;
y = 0;
}
}

int check_walls(char **map)
{
u_int16_t x;
u_int16_t y;
char **control;

control = dup_array(map);
if (!control)
return (PANIC);
x = 0;
y = 0;
while ()
{

}
}

int verify_map(char **map)
{
u_int16_t start[2];

if (check_invalid_chars(map) == PANIC)
return (PANIC);
if (find_start(&start, map) == PANIC)
return (PANIC);
fill_whitespaces(map);
if (check_walls(map) == PANIC)
return (PANIC);
if (error)
return (MAP_ERR);
return (SUCCESS);
}

int parse_map(int map_fd)
{
char **map;

map = build_map(map_fd);
if (!map)
return (ft_free(map), PANIC);
if (verify_map(map) == MAP_ERR)
return (ft_free(map), PANIC);
}

0 comments on commit fced34e

Please sign in to comment.