Repository navigation
Expand file tree
/
Copy pathmap_reader.c
More file actions
78 lines (72 loc) · 2.12 KB
/
Copy pathmap_reader.c
File metadata and controls
78 lines (72 loc) · 2.12 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteichma <mteichma@student.42.fr > +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 19:52:36 by mteichma #+# #+# */
/* Updated: 2025/02/25 16:39:51 by mteichma ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static int process_line(char ***lines, char **line, int *i)
{
if ((*line)[0] == '\n')
{
free(*line);
*line = NULL;
return (1);
}
*lines = ft_realloc_tab(*lines, *i + 1);
if (!*lines)
return (free_line_return(*line, 0));
(*lines)[(*i)++] = *line;
*line = NULL;
return (1);
}
static int store_lines(t_game *game, char ***lines)
{
char *line;
int i;
*lines = ft_calloc(1, sizeof(char *));
if (!*lines)
return (0);
i = 0;
line = get_next_line(game->fd);
while (line)
{
if (!process_line(lines, &line, &i))
return (0);
if (!line)
line = get_next_line(game->fd);
}
game->height = i;
return (game->height > 0);
}
int read_and_parse_map(t_game *game)
{
char **lines;
lines = NULL;
if (!store_lines(game, &lines))
return (0);
game->width = (int)ft_strlen(lines[0]);
if (lines[0][game->width - 1] == '\n')
game->width--;
if (!check_rectangular(lines, game->height, game->width))
return (free_lines_return(lines, 0));
if (!parse_lines(lines, game))
{
free_map(game->map);
game->map = NULL;
return (free_lines_return(lines, 0));
}
if (!validate_map_content(game))
{
free_map(game->map);
game->map = NULL;
return (free_lines_return(lines, 0));
}
free_lines_return(lines, 1);
return (1);
}