This repository has been archived by the owner on Feb 27, 2021. It is now read-only.
forked from giacomoguiulfo/42-fillit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
121 lines (112 loc) · 2.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkalia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 12:02:04 by jkalia #+# #+# */
/* Updated: 2017/03/15 14:10:50 by jkalia ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "fillit.h"
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
void error(void)
{
ft_putstr("error\n");
}
t_bool valid_1(char *str, int bytes)
{
int n;
int i;
int count1;
int count2;
n = 0;
while (n < bytes)
{
i = -1;
count1 = 0;
count2 = 0;
while (++i < 19)
{
if (str[n + i] == '#')
count1++;
if (str[n + i] == '.')
count2++;
}
if (count1 != 4)
return (false);
if (count2 != 12)
return (false);
n += 21;
}
return (true);
}
t_bool valid_2(char *str, int bytes)
{
int n;
int i;
int k;
n = 0;
while (n < bytes)
{
i = 0;
while (i <= 15)
{
k = n + i;
if (!V1(str[k]) || !V1(str[k + 1]) || !V1(str[k + 2])
|| !V1(str[k + 3]) || !V2(str[k + 4]))
return (false);
i += 5;
}
n += 21;
}
return (true);
}
t_bool valid_0(char *str, int bytes)
{
bytes++;
if (str[0] != '.' && str[0] != '#')
return (false);
if (valid_1(str, bytes) == false)
return (false);
if (valid_2(str, bytes) == false)
return (false);
while (*str)
{
if (!V3(*str))
return (false);
str++;
}
return (true);
}
int main(int av, char **ac)
{
size_t fd;
size_t rd;
char *str;
char **tbl;
size_t blocks;
CHK1(av != 2, ft_putstr("usage: ./fillit source_file\n"), 0);
CHK1((str = ft_strnew(BUFFER_SIZE)) == NULL, error(), 0);
CHK2((fd = open(ac[1], O_RDONLY, S_IRUSR)) == -1, free(str), error(), 0);
CHK3((rd = read(fd, str, BUFFER_SIZE)) < 0, error(), free(str),
close(fd), 0);
close(fd);
CHK2(str[545] != 0, error(), free(str), 0);
CHK2(!valid_0(str, rd), error(), free(str), 0);
blocks = (rd + 1) / 21;
change_end(&str, rd);
CHK2((tbl = ft_strsplit(str, '@')) == 0, error(), free(str), 0);
trim_newline(tbl);
CHK3(!valid_pattern(tbl, blocks), error(), ft_tbldel(tbl), free(str), 0);
rename_block(tbl);
solve(tbl, blocks);
ft_tbldel(tbl);
free(str);
return (0);
}