-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_map.c
131 lines (121 loc) · 3.12 KB
/
init_map.c
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
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbaela <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 11:41:56 by lbaela #+# #+# */
/* Updated: 2021/10/27 16:32:47 by lbaela ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include "error_messages.h"
static void free_and_exit(char *str, t_point *current_line, t_point *start)
{
free(str);
free_points(current_line);
free_points(start);
exit_on_error(ERR_MEM);
}
/*
** Parse and free line, return number of points created.
*/
int parse_line(char **str, t_point **start)
{
char **nums;
t_point *tmp;
t_point *current_line;
int i;
i = 0;
tmp = NULL;
current_line = NULL;
nums = ft_split(*str, ' ');
while (nums && nums[i])
{
tmp = ft_point_new(nums[i]);
if (!tmp)
break ;
ft_point_add_back(¤t_line, tmp);
i++;
}
ft_split_free(nums);
if (!tmp)
free_and_exit(*str, current_line, *start);
add_line_front(start, current_line);
free(*str);
*str = NULL;
return (i);
}
/*
** Read file, create and return ordered t_point structure of all map values.
** Returns NULL on bad lines formatting.
*/
t_point *read_map(int fd, t_map *map_i)
{
int res;
t_point *flat_map;
char *line;
int width;
width = 0;
flat_map = NULL;
line = NULL;
res = get_next_line(fd, &line);
while (res != 0 && ++map_i->map_h)
{
if (res == -1 && free_points(flat_map))
exit_on_error(ERR_MEM);
width = parse_line(&line, &flat_map);
if (map_i->map_h == 1)
map_i->map_w = width;
if (map_i->map_h != 1 && width != map_i->map_w && free_points(flat_map))
return (NULL);
res = get_next_line(fd, &line);
}
free(line);
return (flat_map);
}
/*
** Check if map contained colors. Returns 0 if no colors are present
*/
static int colors_empty(int **map, int x, int y)
{
int i;
int j;
i = 0;
while (i < y)
{
j = 0;
while (j < x)
{
if (map[i][j] != COL_VIOLET)
return (0);
j++;
}
i++;
}
return (1);
}
/*
** Check file permissions, malloc map array inside the structure.
*/
void init_map(char *map_file, t_fdf *fdf)
{
int fd;
t_point *flat_map;
flat_map = NULL;
if (open(map_file, O_DIRECTORY) != -1)
exit_on_error(ERR_FD_IS_DIR);
fd = open(map_file, O_RDONLY);
if (fd == -1)
exit_on_error(ERR_FD_OPEN);
flat_map = read_map(fd, &fdf->map_i);
if (close(fd) == -1)
exit_on_error(ERR_FD_CLOSE);
if (!flat_map)
exit_on_error(ERR_LINE_W);
list_to_arr(flat_map, fdf);
free_points(flat_map);
if (colors_empty(fdf->map_i.color, fdf->map_i.map_w, fdf->map_i.map_h))
update_colors(fdf);
}