-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
130 lines (120 loc) · 2.86 KB
/
Copy pathparse.c
File metadata and controls
130 lines (120 loc) · 2.86 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpedraza <mpedraza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/21 16:51:00 by mpedraza #+# #+# */
/* Updated: 2026/01/24 18:55:10 by mpedraza ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static int count_lines(char *filepath)
{
int fd;
int count;
char *line;
fd = open(filepath, O_RDONLY);
if (fd < 0)
return (fd);
count = 0;
while (1)
{
line = get_next_line(fd);
if (!line)
break ;
count++;
free(line);
}
close(fd);
return (count);
}
void build_rows(t_game *g, int fd)
{
int i;
char *line;
int len;
i = 0;
while (1)
{
line = get_next_line(fd);
if (!line)
break ;
len = ft_strlen(line);
if (len > 0 && line[len - 1] == '\n')
line[len - 1] = '\0';
g->map[i++] = line;
}
g->map[i] = NULL;
if (i != g->map_h)
{
write(2, "Error: could not build map from .ber file\n", 43);
exit_game(g, 1);
}
}
void load_map(t_game *g, char *filepath)
{
int fd;
fd = open(filepath, O_RDONLY);
if (fd > 0)
g->map = malloc(sizeof(char *) * (g->map_h + 1));
if (!g->map)
{
write(2, "Error: memory allocation failed\n", 33);
exit_game(g, 1);
}
build_rows(g, fd);
close(fd);
}
void validate_map(t_game *g)
{
if (g->map_h < 3 || g->map_w < 3)
{
write(2, "Error: Map is too small\n", 25);
exit_game(g, 1);
}
if (!is_rectangle(g))
{
write(2, "Error: Map is not a rectangle\n", 31);
exit_game(g, 1);
}
if (!has_valid_objects(g))
exit_game(g, 1);
if (!has_valid_border(g))
{
write(2, "Error: Map has break in boundary wall\n", 39);
exit_game(g, 1);
}
if (!is_solvable(g))
{
write(2, "Error: Map cannot be solved\n", 29);
exit_game(g, 1);
}
}
void parse_template(t_game *g, char *filepath)
{
size_t len;
char *ext;
len = ft_strlen(filepath);
ext = &filepath[len - 4];
if (ft_strncmp(".ber", ext, 4))
{
write(2, "Error! map template must have a .ber extension\n", 48);
exit_game(g, 1);
}
g->map_h = count_lines(filepath);
if (g->map_h < 0)
{
perror("Error (Open file)");
exit_game(g, 1);
}
load_map(g, filepath);
g->map_w = ft_strlen(g->map[0]);
if (g->map_h > 15 || g->map_w > 28)
{
write(2, "Error! Map will not fit screen. Max is 28 x 15.\n", 49);
exit_game(g, 1);
}
validate_map(g);
}