-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapparsing.c
More file actions
122 lines (114 loc) · 2.76 KB
/
mapparsing.c
File metadata and controls
122 lines (114 loc) · 2.76 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* mapparsing.c :+: :+: */
/* +:+ */
/* By: dkramer <dkramer@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/12/13 12:38:44 by dkramer #+# #+# */
/* Updated: 2021/12/13 13:42:24 by dkramer ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include "libft/libft.h"
#include "libft/ft_printf/libftprintf.h"
#include <fcntl.h>
void returnft(t_mlx *mlx)
{
mlx->error = 1;
return ;
}
void othercharacters(t_mlx *mlx)
{
int i;
int a;
i = 0;
mlx->len = (int)ft_strlen(mlx->map[0]);
while (i < mlx->nr_of_lines)
{
a = 0;
while (a < mlx->len)
{
if (!ft_strrchr("E01CP", mlx->map[i][a]))
{
ft_printf("Error\n wrong characters included");
return (returnft(mlx));
}
a++;
}
i++;
}
}
void fillmap(t_mlx *mlx, char *line, char *line2)
{
mlx->ret = 1;
mlx->b = 0;
while (mlx->ret)
{
mlx->ret = get_next_line(mlx->fd, &line);
if (mlx->ret == -1)
return (returnft(mlx));
if (line)
{
line2 = line;
line = ft_strtrim(line2, "\t\n\v\f\r ");
if (!line)
return (returnft(mlx));
free (line2);
}
if (ft_strncmp(line, "", 1) != 0)
{
mlx->map[mlx->b] = line;
mlx->b++;
}
else
free (line);
}
close (mlx->fd);
}
void countlines(t_mlx *mlx, char *line, char *line2)
{
mlx->fd = open(mlx->map_file, O_RDONLY);
if (mlx->fd == -1)
return (returnft(mlx));
while (mlx->ret)
{
mlx->ret = get_next_line(mlx->fd, &line);
if (mlx->ret == -1)
return (returnft(mlx));
if (line)
{
line2 = line;
line = ft_strtrim(line2, "\t\n\v\f\r ");
if (!line)
return (returnft(mlx));
free (line2);
}
if (ft_strncmp(line, "", ft_strlen(line)) != 0)
mlx->nr_of_lines++;
if (line)
free (line);
}
close (mlx->fd);
}
void mapparsing(t_mlx *mlx)
{
char *line;
char *line2;
mlx->ret = 1;
mlx->nr_of_lines = 0;
line = NULL;
line2 = NULL;
countlines(mlx, line, line2);
mlx->map = malloc(sizeof(char *) * (mlx->nr_of_lines + 1));
if (!mlx->map)
return (returnft(mlx));
mlx->fd = open(mlx->map_file, O_RDONLY);
if (mlx->fd == -1)
return (returnft(mlx));
fillmap(mlx, line, line2);
othercharacters(mlx);
if (mlx->error == 1)
return ;
checkmap (mlx);
}