aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_get_map.c
blob: a8a6fe043b2f01c668dbef1d2444061bf54572ac (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
#include <libft.h>
#include <cub3d.h>
#include <stddef.h>
#include <stdlib.h>

/* static char */
/* **ft_split_lines(char *line, int count) */
/* { */
/* } */

static int
ft_linecpy(char *line, char *mapL, size_t start)
{
	size_t	i;
	size_t	j;

	i = 0;
	j = start;
	while (i < ft_strlen(line))
	{
		mapL[j] = line[i];
		i += 2;
		j++;
	}
	mapL[j + 1] = '\n';
	return (0);
}

static int
ft_check_err(char *line, size_t size, t_win *wlist)
{
	size_t	i;

	i = 1;
	if (ft_strlen(line) != size)
		return (-1);
	while (i < ft_strlen(line) && line[i])
	{
		if (line[i] != ' ')
			return (ft_map_error(11, wlist));
		i += 2;
	}
	return (0);
}
int
ft_get_map(int fd, t_win *wlist)
{
	size_t	i;
	size_t	len;
	char	*line;
	char	*mapL;

	ft_free_words(wlist->clist->map, NULL);
	/* if (!(wlist->clist->map = (char**)malloc(40 * sizeof(char*)))) */
	/* 	return (-1); */
	if (get_next_line(fd, &line) > 0)
	{
		len = ft_strlen(line);
		ft_check_err(line, len, wlist);
		if (!(mapL = (char*)malloc((len + 1) * sizeof(char))))
			return (-1);
		ft_linecpy(line, mapL, 0);
		ft_memdel(line);
	}
	else
		return (-1);
	i = 1;
	while (get_next_line(fd, &line) > 0)
	{
		ft_check_err(line, len, wlist);
		mapL = (char*)ft_nrealloc(mapL, ((len + 1) * i) * sizeof(char), (((len + 1) * i) + ft_strlen(line) + 1) * sizeof(char));
		ft_linecpy(line, mapL, (len + 1) * i);
		ft_memdel(line);
		i++;
	}
	mapL[(i * (len + 1)) - 1] = '\0';
	ft_printf("mapL [%s]\n", mapL);
	wlist->clist->map = ft_split(mapL, '\n');
	ft_memdel(mapL);
	return (0);
}