aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_get_map.c
blob: 720f412a7558534186e708d3fe0c57ce481222ef (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
/* ************************************************************************** */
/*                                                          LE - /            */
/*                                                              /             */
/*   ft_get_map.c                                     .::    .:/ .      .::   */
/*                                                 +:+:+   +:    +:  +:+:+    */
/*   By: rbousset <marvin@le-101.fr>                +:+   +:    +:    +:+     */
/*                                                 #+#   #+    #+    #+#      */
/*   Created: 2020/02/02 17:19:26 by rbousset     #+#   ##    ##    #+#       */
/*   Updated: 2020/02/02 17:19:26 by rbousset    ###    #+. /#+    ###.fr     */
/*                                                         /                  */
/*                                                        /                   */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stddef.h>
#include <stdint.h>

static size_t
	ft_get_map_h(char **map)
{
	size_t	i;

	i = 0;
	while (map[i])
		i++;
	return (i);
}

static void
	ft_linecpy(char *line, char *mapl, size_t start)
{
	size_t	i;
	size_t	j;
	size_t	slen;

	i = 0;
	j = start;
	slen = ft_strlen(line);
	while (i < slen && line[i])
	{
		while (line[i] == ' ')
			i++;
		mapl[j] = line[i];
		i++;
		j++;
	}
	mapl[j] = '\n';
}

static int8_t
	ft_cat_mapl(char *line, size_t i, t_cub *clist)
{
	if (!(clist->mapl = (char *)ft_nrealloc(clist->mapl,
				((clist->map_w + 1) * i) * sizeof(char),
				((clist->map_w + 1) * (i + 1)) * sizeof(char))))
		return (-1);
	ft_linecpy(line, clist->mapl, (clist->map_w + 1) * i);
	return (0);
}

int
	ft_get_map_first_line(char *line, t_cub *clist)
{
	clist->map_start = clist->line_chk;
	if (!line[0])
	{
		ft_memdel((void**)&line);
		return (-1);
	}
	clist->map_w = ft_get_line_len(line);
	if (ft_check_map_line(line, 1, clist) < 0)
	{
		ft_memdel((void**)&line);
		return (-1);
	}
	ft_memdel((void**)&clist->mapl);
	if (ft_cat_mapl(line, 0, clist) < 0)
	{
		ft_memdel((void**)&line);
		return (-1);
	}
	ft_memdel((void**)&line);
	return (1);
}

int
	ft_get_map_core(int fd, t_cub *clist)
{
	int		ret;
	size_t	i;
	char	*line;

	i = 1;
	ret = 1;
	while ((ret = get_next_line(fd, &line)) > 0)
	{
		clist->line_chk += 1;
		if (!line[0] || ft_check_map_line(line, 0, clist) < 0
			|| ft_cat_mapl(line, i, clist) < 0)
		{
			ft_memdel((void**)&line);
			return (-1);
		}
		ft_memdel((void**)&line);
		i++;
	}
	ft_memdel((void**)&line);
	clist->mapl[((clist->map_w + 1) * i) - 1] = '\0';
	ft_free_words(clist->map);
	clist->map = ft_split(clist->mapl, '\n');
	clist->map_h = ft_get_map_h(clist->map);
	ft_memdel((void**)&clist->mapl);
	return (0);
}