-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_in.c
More file actions
160 lines (142 loc) · 2.78 KB
/
Copy pathread_in.c
File metadata and controls
160 lines (142 loc) · 2.78 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "fillit.h"
/*
** Reads the tetrimino from the fd to the buffer,
** ensures that it's valid, determines the tetrimino type
** and the order
*/
t_lst *read_in(int fd, char order, t_lst *list, char *old_buf)
{
char *buf;
buf = ft_memalloc(TET_SIZE + 1);
if (!buf)
error_message();
ssize_t read_size = read(fd, (void *)buf, TET_SIZE);
buf[read_size] = '\0';
if (read_size == 0 && check_doublen(old_buf))
return (list);
else if (read_size != 21 && read_size != 20)
error_message();
else
{
free(old_buf);
old_buf = NULL;
if (!check_tet(buf) || !check_tet2(buf) || !check_tet3(buf))
error_message();
if (process_string(buf, list, order))
order++;
}
return (read_in(fd, order, list, buf));
}
int first_check(char *buf)
{
int i;
int c;
i = 0;
c = 0;
while (buf[i] != '\0')
{
if (buf[i] == '.')
c++;
i++;
}
if (c != 12)
return (0);
return (1);
}
int check_doublen(char *buf)
{
ssize_t end;
if (!buf)
return (0);
end = ft_strlen(buf) - 1;
if (buf[end] == '\n' && buf[end - 1] == '\n')
error_message();
return (1);
}
void free_vars(char *var1, char *var2)
{
free(var1);
free(var2);
var1 = NULL;
var2 = NULL;
}
int process_string(void *buf, t_lst *list, char order)
{
char *type_string;
char *final_string;
t_type *save;
create_typelist();
type_string = tet_string(buf);
final_string = remove_newlines(type_string);
if ((save = tet_types(final_string)) == NULL)
{
free_vars(type_string, final_string);
error_message();
}
to_struct(list, save, order);
free_vars(type_string, final_string);
return (1);
}
/*
** Puts type and order into a struct
*/
void to_struct(t_lst *list, t_type *type, char order)
{
if (order == 'A')
{
list->size = 1;
list->head = new_tet(type, order, 0, 0);
list->current = list->head;
}
else
{
list->size = list->size + 1;
list->current->next = new_tet(type, order, 0, 0);
list->current = list->current->next;
}
}
/*
** Defines the tetrimino type as a string
** (trimmed and with '\n's)
*/
char *tet_string(char *buf)
{
char *type_string;
int hash_count;
int index;
int index2;
index = 0;
index2 = 0;
if (!(type_string = (char *)malloc(sizeof(char) * TET_SIZE)))
return (0);
hash_count = 4;
while (buf[index] != '\0')
{
if (buf[index] == '#')
{
while (hash_count >= 1)
{
if (buf[index] == '#')
hash_count--;
type_string[index2++] = buf[index++];
}
type_string[index2] = '\0';
}
index++;
}
return (type_string);
}
/*
** Removes '\n's from the trimmed string
*/
char *remove_newlines(char *type_string)
{
char *final_string;
char **arr;
arr = ft_strsplit(type_string, '\n');
if (!(final_string = (char *)malloc(sizeof(char) * ft_strlen(type_string))))
return (NULL);
while (*arr != '\0')
ft_strcat(final_string, *arr++);
return (final_string);
}