-
Notifications
You must be signed in to change notification settings - Fork 1
/
read.c
129 lines (118 loc) · 2.79 KB
/
read.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yaskour <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/28 12:48:44 by yaskour #+# #+# */
/* Updated: 2022/01/15 18:34:08 by yaskour ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int get_width(char *filename, int check)
{
int fd;
char **d;
int i;
int j;
char *line;
if (check == 1)
{
fd = open(filename, O_RDONLY);
line = get_next_line(fd);
d = ft_split(line, ' ');
}
else
d = ft_split(filename, ' ');
i = 0;
while (d[i])
i++;
j = 0;
while (d[j])
free(d[j++]);
free(d);
if (check == 1)
free(line);
return (i);
}
int get_height(char *filename)
{
int i;
int fd;
char *line;
fd = open(filename, O_RDONLY);
i = 0;
line = get_next_line(fd);
while (line != NULL)
{
free(line);
line = get_next_line(fd);
i++;
}
return (i);
}
void fill_map(int *nums, int *color, char *line)
{
char **s_line;
int i;
int j;
int k;
i = 0;
s_line = ft_split(line, ' ');
while (s_line[i])
{
nums[i] = ft_atoi(s_line[i]);
k = 0;
while (s_line[i][k] != ',' && s_line[i][k])
k++;
if (s_line[i][k++] == ',')
color[i] = ato_h(&s_line[i][k]);
else
color[i] = ft_atoi("0");
i++;
}
j = 0;
while (s_line[j])
free(s_line[j++]);
free(s_line);
}
int check_dir(char *filename)
{
int nb;
int fd;
char *buf;
fd = open(filename, O_RDONLY);
buf = malloc(sizeof(char) * 2);
nb = read(fd, buf, 2);
if (nb <= 0)
return (1);
free(buf);
return (0);
}
void read_fdf(char *filename, t_data *ptr)
{
int i;
int fd;
char *line;
fd = open(filename, O_RDONLY);
if (fd < 0 || check_dir(filename))
invalid_file();
i = 0;
ptr->width = get_width(filename, 1);
ptr->height = get_height(filename);
ptr->map = (int **) malloc(sizeof(int *) * ptr->height + 1);
ptr->color_map = (int **) malloc(sizeof(int *) * ptr->height + 1);
while (i < ptr->height)
{
ptr->map[i] = (int *) malloc(sizeof(int) * ptr->width);
ptr->color_map[i] = (int *) malloc(sizeof(int) * ptr->width);
line = get_next_line(fd);
if (ptr->width != get_width(line, 0))
invalid_file();
fill_map(ptr->map[i], ptr->color_map[i], line);
free(line);
i++;
}
ptr->map[i] = NULL;
}