-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapval_utils.c
130 lines (119 loc) · 2.45 KB
/
mapval_utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mapval_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaher <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 11:24:58 by azaher #+# #+# */
/* Updated: 2023/01/19 16:29:20 by azaher ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
char **map_fill(char *path)
{
char *tab[2];
char **map;
int fd;
tab[0] = "";
tab[1] = ft_strdup("");
fd = open(path, O_RDONLY, 0444);
while (1)
{
tab[0] = get_next_line(fd);
if (!tab[0])
break ;
tab[1] = ft_strjoin(tab[1], tab[0]);
free(tab[0]);
}
map = ft_split(tab[1], '\n');
free(tab[0]);
free(tab[1]);
return (map);
}
int rows_and_collumns(char *line, int rows, int fd)
{
int ncollumns;
int collumns;
collumns = ft_nln_strlen(line);
while (line)
{
line = get_next_line(fd);
if (line && ft_strcmp(line, "\n") == 0)
{
perror("Error\nInvalid map NL");
exit (1);
}
if (line)
{
ncollumns = ft_nln_strlen(line);
rows++;
}
free(line);
if (ncollumns != collumns)
return (1);
}
if (collumns < rows)
return (1);
free(line);
return (0);
}
int map_is_rectangular(int fd)
{
char *line;
int rows;
rows = 1;
line = get_next_line(fd);
if (line)
{
rows_and_collumns(line, rows, fd);
}
else
{
free(line);
return (1);
}
free(line);
return (0);
}
void valid_fl_line(char **map)
{
int i;
int j;
i = 0;
j = 0;
while (map[0][j] != 0)
{
if (map[0][j] == '1')
j++;
else
error_exit(map, "Error\nInvalid top border\n");
}
while (map[i] != NULL)
{
i++;
}
j = 0;
while (map[i - 1][j] != 0)
{
if (map[i - 1][j] == '1')
j++;
else
error_exit(map, "Error\nInvalid bottom border\n");
}
}
int *map_size(char **map)
{
int *coord;
coord = malloc(2 * sizeof(int));
ft_bzero(coord, 2);
while (map[coord[0]])
{
coord[0]++;
}
while (map[1][coord[1]])
{
coord[1]++;
}
return (coord);
}