-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
109 lines (100 loc) · 3.01 KB
/
input.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sadamant <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/20 16:10:11 by sadamant #+# #+# */
/* Updated: 2017/11/20 16:10:12 by sadamant ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
/*
** accepts hexas in the format ",0xFFFFFF"
** rejects everything else.
*/
static int valid_chars(char *z_value)
{
int n;
n = 0;
while (*z_value && (ft_isdigit(*z_value) || *z_value == '-'))
z_value++;
if (*z_value)
{
if (*z_value == ',')
{
z_value++;
return (ft_ishexa(z_value) ? 1 : 0);
}
else
return (0);
}
return (1);
}
/*
** grabs variables about the map so that you can figure out how much to malloc.
** checks:
** 1. if the successive number of columns are the same as the first row's
** columns.
** 2. if the file only contains numbers and spaces
** 3. that you've got the same number of points in each line.
*/
t_map *grab_input_parameters(int fd, char *line)
{
int rows;
char **split_points;
t_map *map;
rows = 1;
if (!(map = (t_map *)malloc(sizeof(t_map))))
return (NULL);
if (get_next_line(fd, &line) <= 0)
exit_error("error: input file is either empty or does not exist");
split_points = ft_strsplit(line, ' ');
map->cols = ft_arrlen(split_points);
ft_freedarray((void **)split_points);
free(line);
while (get_next_line(fd, &line) > 0)
{
rows++;
split_points = ft_strsplit(line, ' ');
if (ft_arrlen(split_points) != map->cols)
exit_error("error: differing numbers of points per line in input");
ft_freedarray((void **)split_points);
free(line);
}
free(line);
map->rows = rows;
return (map);
}
t_point ***grab_points(int fd, char *line, t_map *map)
{
int i;
int j;
char **zvalues;
t_point ***points;
points = ft_memalloc(sizeof(t_point **) * (map->rows + 1));
j = -1;
while (get_next_line(fd, &line) > 0)
{
i = -1;
zvalues = ft_strsplit(line, ' ');
points[++j] = ft_memalloc(sizeof(t_point *) * (map->cols + 1));
while (zvalues[++i])
{
points[j][i] = ft_memalloc(sizeof(t_point));
points[j][i]->x = i * TILE_SIZE;
points[j][i]->y = j * TILE_SIZE;
valid_chars(zvalues[i]) ? points[j][i]->z = ft_atoi(zvalues[i]) \
* TILE_Z : exit_error("error: invalid characters");
}
ft_freedarray((void **)zvalues);
free(line);
}
map->x_l = 0;
map->x_r = points[j][i - 1]->x;
map->y_t = 0;
map->y_b = points[j][i - 1]->y;
free(line);
return (points);
}