-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_get_map.c
More file actions
116 lines (106 loc) · 2.77 KB
/
ft_get_map.c
File metadata and controls
116 lines (106 loc) · 2.77 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
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ehugh-be <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/29 20:09:16 by ehugh-be #+# #+# */
/* Updated: 2019/01/16 21:39:25 by ehugh-be ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "fdf.h"
static int ft_set_color(int alt)
{
if (alt >= 0 && alt < 10)
return (0x00FF00);
if (alt < 0)
return (0x0000FF);
if (alt >= 10 && alt < 20)
return (0xD2691E);
if (alt >= 10 && alt < 20)
return (0xFFFFFF);
return (0xFFFFFF);
}
static int ft_get_color(char *pt, int alt)
{
int ret;
char t;
if (!pt)
return (ft_set_color(alt));
if (*(++pt) != '0' || *(++pt) != 'x')
exit(ft_error(CONT_ERR_CS));
ret = 0;
while ((t = ft_tolower(*(++pt))) && ((t >= '0' && t <= '9') ||
(t >= 'a' && t <= 'f')))
{
if (t >= '0' && t <= '9')
ret = ret * 16 + t - '0';
else
ret = ret * 16 + t - 'a' + 10;
}
if (*pt)
exit(ft_error(CONT_ERR_CE));
return (ret);
}
static int ft_fill_row(t_map *fdf, char *line, int row)
{
char **tmp;
int i;
t_vec4 point;
tmp = ft_strsplit(line, ' ');
i = -1;
while (tmp[++i])
{
point.x = i;
point.y = row;
if ((tmp[i][0] < '0' || tmp[i][0] > '9') && tmp[i][0] != '-')
exit(ft_error(CONT_ERR_NE));
point.z = ft_atoi(tmp[i]);
if (point.z > fdf->d)
fdf->d = point.z;
point.color = ft_get_color(ft_strchr(tmp[i], ','), point.z);
fdf->vec = ft_vecpush(fdf->vec, &point, sizeof(t_vec4));
}
return (i);
}
t_map *ft_map_init(void)
{
t_map *fdf;
if (!(fdf = malloc(sizeof(t_map))))
return (NULL);
fdf->w = 0;
fdf->h = 0;
fdf->d = 0;
if (!(fdf->vec = ft_vecinit(2)))
return (NULL);
return (fdf);
}
t_map *ft_get_map(char *fname)
{
int fd;
char *line;
t_map *fdf;
int row;
if ((fd = open(fname, O_RDONLY)) < 0)
exit(ft_error(FILE_ERROR));
if (!(fdf = ft_map_init()))
return (NULL);
row = 0;
while (get_next_line(fd, &line) == 1)
{
if (fdf->w == 0)
fdf->w = ft_fill_row(fdf, line, row);
else if (fdf->w != ft_fill_row(fdf, line, row))
exit(ft_error(CONT_ERR_NO));
row++;
}
fdf->x_m = 0;
fdf->x = fdf->w;
fdf->h = row;
fdf->y = row;
fdf->y_m = 0;
ft_center_map(fdf);
return (fdf);
}