-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
114 lines (105 loc) · 2.92 KB
/
reader.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: onapoli- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/07 14:08:48 by onapoli- #+# #+# */
/* Updated: 2020/09/11 13:01:39 by onapoli- ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "reader.h"
static int free_map_lines(t_map_line **map_lines)
{
t_map_line *current;
t_map_line *next;
current = *map_lines;
while (current)
{
next = current->next;
free(current->line);
free(current);
current = next;
}
*map_lines = 0;
return (0);
}
static int data_missing(t_session *session)
{
if (!session->win_width || !session->win_height ||
!session->textures[0].path || !session->textures[1].path ||
!session->textures[2].path || !session->textures[3].path ||
!session->sprite_texture.path || !session->level.floor_color ||
!session->level.ceiling_color)
return (1);
return (0);
}
static int generate_validate_map(t_session *session, t_map_line *map_lines)
{
if (generate_map(&(session)->level.map, map_lines) == -1)
{
write(1, "Error\ncould not generate map.\n", 30);
return (-1);
}
if (validate_map(session) == -1)
{
if (map_lines)
free_map_lines(&map_lines);
write(1, "Error\ninvalid map.\n", 19);
return (-1);
}
return (0);
}
static int get_map(int fd, int *line_result, t_map_line **map_lines,
t_session *session)
{
char *line;
line = 0;
while ((*line_result = get_next_line(fd, &line)) != -1 && *line_result)
{
if (data_missing(session))
{
if (send_to_process_data(line, session) == -1)
return (-1);
}
else
{
if (!line[0] && !map_lines[0])
continue;
if (!line[0] || add_map_line(line, map_lines) == -1)
{
free(line);
write(1, "Error\nBad map data in .cub file.\n", 33);
return (-1);
}
}
}
free(line);
return (0);
}
int ft_read_file(t_session *session, char *file_path)
{
int fd;
int line_result;
t_map_line *map_lines;
map_lines = 0;
if (ft_open_file(&fd, file_path) == -1)
return (-1);
if (get_map(fd, &line_result, &map_lines, session) == -1)
{
free_map_lines(&map_lines);
return (-1);
}
if (line_result == -1)
{
write(1, "Error\nFile read operation failed.\n", 34);
return (-1);
}
close(fd);
if (generate_validate_map(session, map_lines) == -1)
return (-1);
free_map_lines(&map_lines);
return (0);
}