-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.c
140 lines (129 loc) · 3.39 KB
/
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
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
132
133
134
135
136
137
138
139
140
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: taehkwon <taehkwon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/29 18:37:34 by taehkwon #+# #+# */
/* Updated: 2023/07/28 16:03:57 by taehkwon ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void parsing(char *file_name, t_map *map)
{
int fd;
int map_size_value;
fd = open(file_name, O_RDONLY);
if (fd < 0)
ft_perror("ERROR: Can't open");
set_map_size(fd, map, 0);
close(fd);
map_size_value = (map->height) * (map->width);
map->p_map = (t_coord *)malloc(sizeof(t_coord) * map_size_value);
if (!(map->p_map))
ft_perror("ERROR: Invalid map malloc");
fd = open(file_name, O_RDONLY);
set_map_coord(fd, map, 0);
close(fd);
}
void set_map_size(int fd, t_map *map, int i)
{
char *line;
char **arr;
int check_width;
line = get_next_line(fd);
map->width = get_width(line);
map->height = 0;
while (line)
{
arr = ft_split(line, ' ');
free(line);
if (arr == NULL || *arr[0] == '\0')
ft_perror("ERROR: Invalid map");
check_width = invalid_color_check(arr, i);
if (!check_width)
ft_perror("ERROR: Invalid map color");
free_for_split(arr);
if (check_width != map->width)
ft_perror("ERROR: Invalid map size");
map->height += 1;
line = get_next_line(fd);
}
free(line);
}
void set_map_coord(int fd, t_map *map, int y)
{
char *line;
char **arr;
int i;
t_coord coord;
line = get_next_line(fd);
while (line)
{
coord.y = y - map->height / 2;
arr = ft_split(line, ' ');
i = 0;
while (arr[i])
{
get_map_color(arr[i], &coord);
coord.x = i - map->width / 2;
map->p_map[y * map->width + i] = coord;
i++;
}
free_for_split(arr);
free(line);
line = get_next_line(fd);
y++;
}
free(line);
}
void get_map_color(char *arr, t_coord *coord)
{
char **color_split;
int check_width;
int color_value;
color_split = ft_split(arr, ',');
if (!color_split || !is_valid_num(color_split[0]))
{
free_for_split(color_split);
ft_perror("ERROR: Invalid map number");
}
coord->z = ft_atoi(color_split[0]);
check_width = is_color(arr);
if (check_width == 2)
{
color_value = input_color(color_split[1], "0123456789abcdef", 0);
if (color_value == -1)
ft_perror("ERROR: Invalid color");
coord->color = color_value;
}
else
coord->color = input_color("0x00FFFFFF", "0123456789abcdef", 0);
free_for_split(color_split);
}
int input_color(char *arr, char *hex, int len)
{
int i;
int result;
if (!arr)
return (-1);
while (arr[len] && arr[len] != '\n')
len++;
if (len % 2 == 1 || len > 10)
return (-1);
i = 0;
if (ft_strncmp(arr, "0x", 2) == 0)
i += 2;
else
return (-1);
result = 0;
while (arr[i] && arr[i] != '\n')
{
if (hex_indexing(hex, arr[i]) == -1)
return (-1);
result = result * 16 + hex_indexing(hex, arr[i]);
i++;
}
return (result);
}