-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
110 lines (100 loc) · 2.35 KB
/
utils.c
File metadata and controls
110 lines (100 loc) · 2.35 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* utils.c :+: :+: */
/* +:+ */
/* By: cstaats <cstaats@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/10/31 12:56:56 by cstaats #+# #+# */
/* Updated: 2022/11/08 15:37:55 by cstaats ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
void itsanerror(void)
{
write(1, "Error\n", 6);
exit(EXIT_FAILURE);
}
char **makeamap(int fd)
{
char **map;
char *templine;
int cnt;
char **tempmap;
cnt = 0;
map = malloc(sizeof(char *));
tempmap = malloc(sizeof(char *));
if (map == NULL || tempmap == NULL)
exit(EXIT_FAILURE);
while (get_next_line(fd, &templine) == 1)
{
map[cnt] = templine;
map = allocatemap(map, cnt);
tempmap[cnt] = ft_strdup(templine);
tempmap = allocatemap(tempmap, cnt);
cnt++;
}
isitvalid(map);
validpath(tempmap);
close(fd);
return (map);
}
char **allocatemap(char **oldmap, int cnt)
{
char **map;
map = malloc(sizeof(char *) * (cnt + 2));
if (map == NULL)
exit(EXIT_FAILURE);
map[cnt + 1] = NULL;
while (cnt >= 0)
{
map[cnt] = oldmap[cnt];
cnt--;
}
free(oldmap);
return (map);
}
void isitvalid(char **map)
{
size_t cnt;
size_t cnt2;
cnt = ft_strlen(map[0]);
cnt2 = 0;
isitones(map[0]);
while (map[cnt2] != NULL)
{
if (ft_strlen(map[cnt2]) != cnt)
itsanerror();
isitgood(map[cnt2]);
cnt2++;
}
isitones(map[cnt2 - 1]);
isitcomplete(map);
}
void isitcomplete(char **str)
{
int cnt;
int p;
int zero;
int e;
int c;
p = 0;
zero = 0;
e = 0;
c = 0;
while (*str != NULL)
{
cnt = 0;
while ((*str)[cnt] != '\0')
{
p = (((*str)[cnt] == 'P') + p);
zero = (((*str)[cnt] == '0') + zero);
e = (((*str)[cnt] == 'E') + e);
c = (((*str)[cnt] == 'C') + c);
cnt ++;
}
str++;
}
if (p != 1 || e != 1 || c == 0)
itsanerror();
}