-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
108 lines (97 loc) · 2.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/26 02:56:49 by rvan-der #+# #+# */
/* Updated: 2017/02/27 16:36:57 by rvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
void find_offset(t_piece *ret, char **piece, int size)
{
int x;
int y;
(ret->offset).y = -1;
(ret->offset).x = -1;
y = -1;
while (++y < size && (ret->offset).x)
{
x = 0;
while (piece[y][x] != '\0' && piece[y][x] != 42)
x++;
if (piece[y][x] == 42 && (ret->offset).y == -1)
{
(ret->offset).y = y;
(ret->offset).x = x;
}
else if (piece[y][x] == 42)
(ret->offset).x = (x < (ret->offset).x ? x : (ret->offset).x);
}
}
t_coord *get_pce_crd(int xmin, int ymin, char **piece, int size)
{
int x;
int y;
t_coord *ret;
ret = NULL;
y = -1;
while (++y < size)
{
x = -1;
while (piece[y][++x] != '\0')
if (piece[y][x] == 42)
crdlist_pushback(&ret, new_crdlist(x - xmin, y - ymin));
}
return (ret);
}
t_coord get_size(char *buff)
{
t_coord ret;
ret.y = ft_atoi(buff);
while (ft_isdigit(*(buff)))
buff++;
ret.x = ft_atoi(buff);
return (ret);
}
t_piece read_piece(void)
{
t_piece ret;
int i;
char *buff;
char **piece;
get_next_line(0, &buff);
ret.size = get_size(buff + 6);
piece = (char**)malloc(sizeof(char*) * (ret.size).y);
free(buff);
i = -1;
while (++i < (ret.size).y)
{
get_next_line(0, &buff);
piece[i] = ft_strdup(buff);
free(buff);
}
find_offset(&ret, piece, (ret.size).y);
ret.crd = get_pce_crd((ret.offset).x, (ret.offset).y, piece, (ret.size).y);
delete_map(piece, (ret.size).y);
return (ret);
}
char **read_map(t_coord size)
{
char **ret;
char *buff;
int i;
get_next_line(0, &buff);
free(buff);
ret = (char**)malloc(sizeof(char*) * size.y);
i = -1;
while (++i < size.y)
{
get_next_line(0, &buff);
ret[i] = ft_strdup(buff + 4);
free(buff);
}
return (ret);
}