-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
112 lines (101 loc) · 2.49 KB
/
map.c
File metadata and controls
112 lines (101 loc) · 2.49 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apavelko <apavelko@student.unit.ua> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/02 17:38:00 by apavelko #+# #+# */
/* Updated: 2018/03/02 17:38:00 by apavelko ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "../includes/fdf.h"
#include "../libft/libft.h"
void ft_find_depth(t_fdf *fdf)
{
size_t i;
size_t j;
fdf->map->depth_max = MIN_INT;
fdf->map->depth_min = MAX_INT;
i = 0;
while (i < fdf->map->map_y)
{
j = 0;
while (j < fdf->map->map_x)
{
if (fdf->map->map[i][j] > fdf->map->depth_max)
fdf->map->depth_max = fdf->map->map[i][j];
if (fdf->map->map[i][j] < fdf->map->depth_min)
fdf->map->depth_min = fdf->map->map[i][j];
j++;
}
i++;
}
}
int *ft_parse_map(t_fdf *fdf, char **tab)
{
int *x;
int *ptr_x;
x = (int *)malloc(sizeof(*x) * fdf->map->map_x);
ptr_x = x;
while (*tab)
*ptr_x++ = ft_atoi(*tab++);
return (x);
}
void ft_free_tab(char **tab)
{
char **ptr;
ptr = tab;
while (*ptr)
free(*ptr++);
free(tab);
}
int ft_create_map(t_fdf *fdf, char *map)
{
int fd;
char **tab;
char *line;
size_t i;
i = 0;
if ((fd = open(map, O_RDONLY)) < 0)
return (0);
while (get_next_line(fd, &line) > 0)
{
tab = ft_strsplit(line, ' ');
fdf->map->map[i] = ft_parse_map(fdf, tab);
ft_free_tab(tab);
free(line);
i++;
}
close(fd);
return (1);
}
int ft_count_x_and_y(t_fdf *fdf, char *map)
{
int fd;
char *line;
char **tab;
fdf->map->map_x = 0;
fdf->map->map_y = 0;
if ((fd = open(map, O_RDONLY)) < 0)
return (0);
while (get_next_line(fd, &line) > 0)
{
if (!fdf->map->map_x)
{
tab = ft_strsplit(line, ' ');
while (tab[fdf->map->map_x])
fdf->map->map_x++;
}
fdf->map->map_y++;
free(line);
}
if (!fdf->map->map_x)
return (ft_bye("Map error :("));
ft_free_tab(tab);
close(fd);
return (1);
}