-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdf_parsing.c
105 lines (96 loc) · 2.52 KB
/
fdf_parsing.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jimchoi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/20 11:21:49 by jimchoi #+# #+# */
/* Updated: 2024/04/12 19:01:18 by jimchoi ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int str_check(t_data *image, char **str)
{
int i;
i = 0;
if (str == NULL)
handle_exit(2);
image->width = 0;
while (str[i] != NULL)
{
if (str[i][0] != '\n')
image->width++;
free(str[i]);
i++;
}
return (image->width);
}
int is_valid_map(char *argv[], t_data *image)
{
int check;
int fd;
char *line;
char **str;
fd = open(argv[1], O_RDONLY);
if (fd == -1)
handle_exit(1);
image->height = 0;
line = get_next_line(fd);
str = ft_split(line, ' ');
check = str_check(image, str);
free(str);
while (line != NULL)
{
str = ft_split(line, ' ');
if (str_check(image, str) != check)
handle_exit(2);
image->height++;
free(line);
free(str);
line = get_next_line(fd);
}
close(fd);
return (open(argv[1], O_RDONLY));
}
void init_struct(t_data *image)
{
int i;
i = 0;
if (image->width * image->height <= 1)
handle_exit(2);
image->points = (t_point **)malloc(sizeof(t_point *) * image->height);
image->map = (t_point **)malloc(sizeof(t_point *) * image->height);
while (i < image->height)
{
image->points[i] = (t_point *)malloc(sizeof(t_point) * image->width);
image->map[i] = (t_point *)malloc(sizeof(t_point) * image->width);
i++;
}
}
void parsing(char *argv[], t_data *image, int i, int j)
{
char *line;
char **str;
int fd;
fd = is_valid_map(argv, image);
init_struct(image);
while (++i < image->height)
{
line = get_next_line(fd);
j = -1;
str = ft_split(line, ' ');
free(line);
while (str[++j] != NULL)
{
if (str[j][0] != '\n')
{
image->points[i][j].x = j;
image->points[i][j].y = i;
image->points[i][j].z = ft_atoi(str[j]);
}
free(str[j]);
}
free(str);
}
}