forked from ydeinega/corwar_fin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_champs.c
122 lines (112 loc) · 2.59 KB
/
parse_champs.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_champs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ydeineha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/04 14:14:05 by ydeineha #+# #+# */
/* Updated: 2018/07/04 14:14:07 by ydeineha ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void parse_champs(void)
{
t_lst_champs *tmp;
tmp = g_game.champ;
while (tmp)
{
if ((tmp->fd = open(tmp->file_name, O_RDONLY)) == -1)
{
tmp->error = 12;
error(12);
}
read_champs_info(tmp);
tmp = tmp->next;
}
}
unsigned int read_num(t_lst_champs *champ, int len)
{
int ret;
unsigned char line[len];
ret = read(champ->fd, line, len);
if (ret == -1)
{
champ->error = 12;
error(12);
}
if (ret < len)
{
champ->error = 11;
error(11);
}
return (conv_hex(line, len));
}
void read_string(t_lst_champs *champ, char *line, int len)
{
int ret;
ret = read(champ->fd, line, len);
if (ret == -1)
{
champ->error = 12;
error(12);
}
if (ret < len)
{
champ->error = 11;
error(11);
}
line[len] = '\0';
}
void read_instructions(t_lst_champs *champ)
{
int ret;
char line;
ret = 0;
champ->comms = (unsigned char *)malloc(sizeof(unsigned char) * champ->size);
ret = read(champ->fd, champ->comms, champ->size);
if (ret == -1)
{
champ->error = 12;
error(12);
}
if ((unsigned int)ret < champ->size)
{
champ->error = 13;
error(13);
}
ret = read(champ->fd, &line, 1);
if (ret != 0)
{
champ->error = 13;
error(13);
}
}
void read_champs_info(t_lst_champs *champ)
{
champ->magic = read_num(champ, 4);
if (champ->magic != COREWAR_EXEC_MAGIC)
{
champ->error = 10;
error(10);
}
read_string(champ, champ->name, PROG_NAME_LENGTH);
if (read_num(champ, 4) != 0)
{
champ->error = 15;
error(15);
}
champ->size = read_num(champ, 4);
if (champ->size > CHAMP_MAX_SIZE)
{
champ->error = 14;
error(14);
}
read_string(champ, champ->comment, COMMENT_LENGTH);
if (read_num(champ, 4) != 0)
{
champ->error = 15;
error(15);
}
read_instructions(champ);
}