Repository navigation
Expand file tree
/
Copy pathmap_loader.c
More file actions
123 lines (112 loc) · 3.04 KB
/
Copy pathmap_loader.c
File metadata and controls
123 lines (112 loc) · 3.04 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_loader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbani-at <hbani-at@student.42amman.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/26 10:00:00 by hbani-at #+# #+# */
/* Updated: 2025/12/29 19:55:49 by hbani-at ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line/get_next_line.h"
#include "libft/libft.h"
#include "so_long.h"
static t_error validate_file_extension(const char *file)
{
char *extension;
extension = ft_strchr(file, '.');
if (!extension)
return (ERROR_INVALID_EXTENSION);
if (ft_strncmp(extension, ".ber", 4))
return (ERROR_INVALID_EXTENSION);
return (ERROR_SUCCESS);
}
static t_error count_file_rows(int fd, int *row_count)
{
char *line;
int count;
count = 0;
line = get_next_line(fd);
while (line != NULL)
{
count++;
free(line);
line = get_next_line(fd);
}
if (count == 0)
return (ERROR_FILE_EMPTY);
*row_count = count;
return (ERROR_SUCCESS);
}
static t_error read_map(int fd, t_game_info *game_info)
{
char *line;
int i;
i = 0;
line = get_next_line(fd);
while (line != NULL)
{
if (i >= game_info->number_of_rows)
{
free(line);
return (ERROR_INVALID_ARGS);
}
game_info->map[i] = line;
i++;
line = get_next_line(fd);
}
if (i != game_info->number_of_rows)
return (ERROR_INVALID_MAP);
game_info->map[i] = NULL;
return (ERROR_SUCCESS);
}
static t_error reopen_and_read_map(const char *map_file,
t_game_info *game_info)
{
int fd;
t_error err;
fd = open(map_file, O_RDONLY);
if (fd < 0)
{
free_map(game_info->map);
game_info->map = NULL;
return (ERROR_FILE_NOT_FOUND);
}
err = read_map(fd, game_info);
close(fd);
if (err != ERROR_SUCCESS)
{
free_map(game_info->map);
game_info->map = NULL;
return (err);
}
game_info->number_of_columns = row_len(game_info->map[0]);
return (ERROR_SUCCESS);
}
t_error map_load(const char *map_file, t_game_info *game_info)
{
int fd;
t_error err;
int row_count;
if (!map_file || !game_info)
return (ERROR_INVALID_ARGS);
err = validate_file_extension(map_file);
if (err != ERROR_SUCCESS)
return (err);
fd = open(map_file, O_RDONLY);
if (fd < 0)
return (ERROR_FILE_NOT_FOUND);
err = count_file_rows(fd, &row_count);
if (err != ERROR_SUCCESS)
{
close(fd);
return (err);
}
game_info->number_of_rows = row_count;
close(fd);
game_info->map = ft_calloc((game_info->number_of_rows + 1), sizeof(char *));
if (!game_info->map)
return (ERROR_MEMORY);
return (reopen_and_read_map(map_file, game_info));
}