-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_file.c
110 lines (102 loc) · 2.04 KB
/
read_file.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
#include "cub3d.h"
void read_file(t_info *info)
{
int fd;
int ret;
char *line;
t_list *lista;
lista = NULL;
info->save = NULL;
ret = 1;
line = NULL;
fd = open(info->file_path, O_RDONLY);
if (fd == -1)
{
printf("ERROR = NOT ABLE TO READ THE FILE");
exit(0);
}
while (ret > 0)
{
ret = get_next_line(fd, &line);
parse_file(info, &lista, line);
}
close(fd);
check_width(info);
init_map(info, lista);
check_errors_map(info->map, info->size_list);
init_pos(info);
}
void parse_file(t_info *info, t_list **lista, char *line)
{
if (line[0] == 'R')
parse_r(info, line);
else if (line[0] == 'F')
parse_f(info, line);
else if (line[0] == 'C')
parse_c(info, line);
else if (line[0] == 'N' && line[1] == 'O')
parse_variable(info, line, 1);
else if (line[0] == 'S' && line[1] == 'O')
parse_variable(info, line, 2);
else if (line[0] == 'W' && line[1] == 'E')
parse_variable(info, line, 3);
else if (line[0] == 'E' && line[1] == 'A')
parse_variable(info, line, 4);
else if (line[0] == 'S' && line[1] == ' ')
parse_variable(info, line, 5);
else
ft_lstadd_back(lista, ft_lstnew((void *)line));
}
void check_width(t_info *info)
{
if (info->counter != 8)
{
printf("ERROR = PROBLEM WITH THE FILE. Change it.");
exit(0);
}
if (info->width == 1920)
info->width = 1921;
if (info->width >= 2560)
info->width = 2559;
if (info->height >= 1440)
info->height = 1439;
if (info->width <= 9)
info->width = 10;
if (info->height <= 9)
info->height = 10;
}
void init_map(t_info *info, t_list *lista)
{
int x;
x = 0;
info->size_list = ft_lstsize(lista);
info->map = ft_calloc(info->size_list, sizeof(char *));
while (lista)
{
info->map[x] = ft_strdup(lista->content);
lista = lista->next;
x++;
}
}
void init_pos(t_info *info)
{
int x;
int y;
x = 0;
while (x < info->size_list)
{
y = 0;
while (y < ft_strlen(info->map[x]))
{
if (ft_isalpha(info->map[x][y]) == 1)
{
info->posX = x + 0.5;
info->posY = y + 0.5;
info->direction = info->map[x][y];
info->map[x][y] = '0';
}
y++;
}
x++;
}
}