-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
126 lines (115 loc) · 2.63 KB
/
map.c
File metadata and controls
126 lines (115 loc) · 2.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mamir <mamir@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/05 19:50:35 by mamir #+# #+# */
/* Updated: 2024/05/09 15:08:24 by mamir ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void map_loop(t_data *data, char *path)
{
int i;
int j;
int fd;
char *str;
fd = open(path, O_RDONLY);
str = get_next_line(fd);
data->map_dim[1] = ft_strlen(str) - 1;
i = 0;
while (i < data->map_dim[0])
{
j = 0;
data->map[i] = (char *)malloc(sizeof(char) * data->map_dim[1]);
while (j < data->map_dim[1])
{
data->map[i][j] = str[j];
j++;
}
free(str);
str = get_next_line(fd);
i++;
}
close(fd);
}
void check_rectangular(char *path)
{
char *str;
size_t len;
int fd;
fd = open(path, O_RDONLY);
str = get_next_line(fd);
len = ft_strlen1(str);
while (str != NULL)
{
if (ft_strlen1(str) != len)
ft_error("map not rectangular\n");
free(str);
str = get_next_line(fd);
}
close(fd);
}
void check_sides(t_data *data, char *path)
{
int i;
int fd;
char *str;
fd = open(path, O_RDONLY);
i = 0;
str = get_next_line(fd);
while (i < data->map_dim[0] && str != NULL)
{
if (str[0] != '1' || str[data->map_dim[1] - 1] != '1')
ft_error("Invalid Sides!\n");
free(str);
str = get_next_line(fd);
i++;
}
close(fd);
}
void check_valid_components(char *path, t_data *data)
{
int fd;
int i;
int j;
char *str;
i = 0;
fd = open(path, O_RDONLY);
str = get_next_line(fd);
while (i < data->map_dim[0])
{
j = 0;
while (j < data->map_dim[1])
{
if (!check_components(str[j]))
ft_error("Invalid components\n");
j++;
}
free(str);
str = get_next_line(fd);
i++;
}
close(fd);
}
void check_first_last_line(char *path, t_data *data)
{
char *str;
int i;
int fd;
fd = open(path, O_RDONLY);
i = 0;
str = get_next_line(fd);
while (str[i] != '\n')
{
if (str[i] != '1')
ft_error("first line or last line not 1\n");
i++;
}
i = 0;
while_check(data, str, path);
free(str);
close(fd);
}