-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_file.c
106 lines (94 loc) · 2.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhasan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/03 12:47:52 by mhasan #+# #+# */
/* Updated: 2020/02/13 20:58:47 by mhasan ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
/*
** Map_size function calculate the width and height of the map
** and assign it to t_fdf structure height and width.
*/
void map_size(char *file, t_fdf *data)
{
char *line;
int fd;
int height;
int width;
if ((fd = open(file, O_RDONLY)) < 0)
{
ft_error("Invalid Map");
}
height = 0;
while (get_next_line(fd, &line) > 0)
{
height++;
width = ft_words_counter(line);
free(line);
}
if (close(fd) == -1)
{
ft_error("File can not be closed");
}
data->height = height;
data->width = width;
}
/*
** In this malloc-z function I allocate
** the memory based on height and width
*/
void malloc_z(t_fdf *data)
{
int i;
if (!(data->map = (int **)malloc(sizeof(t_fdf *) * (data->height + 1))))
exit(1);
i = 0;
while (i < data->height)
{
data->map[i] = (int *)malloc(sizeof(t_fdf) * (data->width + 1));
if (!data->map[i])
exit(1);
i++;
}
}
/*
** This fill_map function converts strings to integers
*/
void fill_map(int *line_z, char *line)
{
int i;
char **map;
map = ft_strsplit(line, ' ');
i = 0;
while (map[i])
{
line_z[i] = ft_atoi(map[i]);
free(map[i]);
i++;
}
free(map);
}
void read_file(char *file, t_fdf *data)
{
char *line;
int fd;
int i;
map_size(file, data);
if (data->width == 0 || data->height == 0)
ft_error("Your map is not valid");
malloc_z(data);
if ((fd = open(file, O_RDONLY)) < 0)
exit(1);
i = 0;
while (get_next_line(fd, &line) > 0)
{
fill_map(data->map[i], line);
free(line);
i++;
}
}