aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_parse_map.c
blob: eb69e28215c9f6edf2695f2c31443acb83d6caea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/*                                                          LE - /            */
/*                                                              /             */
/*   ft_parse_map.c                                   .::    .:/ .      .::   */
/*                                                 +:+:+   +:    +:  +:+:+    */
/*   By: rbousset <marvin@le-101.fr>                +:+   +:    +:    +:+     */
/*                                                 #+#   #+    #+    #+#      */
/*   Created: 2020/02/02 17:19:31 by rbousset     #+#   ##    ##    #+#       */
/*   Updated: 2020/02/02 17:19:31 by rbousset    ###    #+. /#+    ###.fr     */
/*                                                         /                  */
/*                                                        /                   */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

static void
	ft_check_cub(const char *map_path, t_cub *clist)
{
	char	**words;
	size_t	i;

	if (!(words = ft_split(map_path, '.')))
	{
		ft_dprintf(STDERR_FILENO, "Error\n");
		ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n");
		ft_free_words(words);
		ft_exit(2, clist);
	}
	i = 0;
	while (words[i])
		i++;
	if (ft_strcmp(words[i - 1], "cub"))
	{
		ft_dprintf(STDERR_FILENO, "Error\n");
		ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n");
		ft_free_words(words);
		ft_exit(2, clist);
	}
	ft_free_words(words);
}

static void
	ft_check_map_last_line(t_cub *clist)
{
	size_t	i;
	size_t	j;

	i = 0;
	while (clist->map[i])
		i++;
	j = 0;
	while (clist->map[i - 1][j])
	{
		if (clist->map[i - 1][j] != '1' && clist->map[i - 1][j] != '\0')
			ft_map_error(clist);
		j++;
	}
}

static int8_t
	ft_error_here(char *line, t_cub *clist)
{
	ft_memdel((void**)&line);
	return (ft_map_error(clist));
}

static int8_t
	ft_parse_it(int fd, t_cub *clist)
{
	char	*line;
	char	**words;
	int		ret;

	clist->line_chk += 1;
	if ((ret = get_next_line(fd, &line)) < 0)
		return (ft_map_error(clist));
	if (ret == 0)
		return (ft_error_here(line, clist));
	if (!line[0])
	{
		ft_memdel((void**)&line);
		return (ft_parse_it(fd, clist));
	}
	if (!ft_ischarset("RNSEWFC1\0", line[0])
		|| !(words = ft_split(line, ' ')))
		return (ft_error_here(line, clist));
	if ((ret = ft_select_get(words, clist)) == 12)
		return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (12));
	ft_memdel((void**)&line);
	return (ret);
}

void
	ft_parse_map(const char *map_path, t_cub *clist)
{
	int		fd;
	int8_t	ret;

	ft_check_cub(map_path, clist);
	fd = open(map_path, O_RDONLY);
	if (fd < 0)
	{
		ft_dprintf(STDERR_FILENO, "Error\n");
		ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n");
		ft_exit(2, clist);
	}
	ret = 1;
	while (ret != 12 && ret != -1)
		ret = ft_parse_it(fd, clist);
	if (ret == -1)
		ft_map_error(clist);
	clist->nsew = 0;
	if (ft_get_map_core(fd, clist) < 0)
		ft_map_error(clist);
	ft_check_map_last_line(clist);
	ft_check_missing(clist);
	ft_print_list(clist);
	close(fd);
}