-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_generator.c
96 lines (90 loc) · 2.75 KB
/
script_generator.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
/*
** EPITECH PROJECT, 2022
** B-MUL-100-LYN-1-1-myradar-mael.rabot
** File description:
** script_generator
*/
#include "include/my.h"
#include "include/bonus.h"
void *print_man(void)
{
int fd = open("README.txt", O_RDONLY);
if (fd == -1)
return (NULL);
int size = 3000;
char buff[size + 1];
read(fd, buff, size);
my_printf("%s", buff);
return (NULL);
}
int parser(int ac, char **av)
{
if (ac > 1 && av[1][0] == '-' && av[1][1] == 'h') {
print_man();
return (0);
}
if (ac < 3 || ac > 3) {
my_printf("./script_generator: Bad arguments: %d given but 1 is " \
"required\nRetry with -h\n", ac - 1);
return (84);
}
return (1);
}
void create_file(char *plane, char *tower, char **tower_pos)
{
char *filepath = malloc(sizeof(char) *30);
my_strcpy(filepath, plane);
my_strcat(filepath, "_plane_");
my_strcat(filepath, tower);
my_strcat(filepath, "_tower.rdr");
int fd = open(filepath, O_WRONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH \
| S_IWUSR | S_IWGRP | S_IWOTH);
char *line = malloc(sizeof(char) * 100);
for (int i = 0; i < my_getnbr(tower); i++) {
my_strcpy(line, create_tower(tower_pos, i));
write(fd, line, my_strlen(line));
}
for (int i = 0; i < my_getnbr(plane); i++) {
my_strcpy(line, create_airplane(tower_pos, my_getnbr(tower)));
write(fd, line, my_strlen(line));
}
free(line);
free(filepath);
}
int args_checker(char *plane_nb, char *tower_nb)
{
if (my_getnbr(plane_nb) < 0) {
my_printf("/script_generator: Bad arguments: %d given but number "
"higher than 0 required\nRetry with -h\n",
my_getnbr(plane_nb));
return (84);
}
if (my_getnbr(tower_nb) < 0) {
my_printf("/script_generator: Bad arguments: %d given but number "
"higher than 0 required\nRetry with -h\n",
my_getnbr(tower_nb));
return (84);
}
if (my_getnbr(tower_nb) > 10) {
my_printf("/script_generator: Bad arguments: %d given but number "
"smaller than 10 required\nRetry with -h\n",
my_getnbr(tower_nb));
return (84);
}
return (0);
}
int main(int ac, char **av)
{
time_t timer;
int pars;
char *tower_pos[] = {"660 570", "1300 1000", "1300 130", "750 160",
"1620 330", "150 260", "400 530",
"1000 620", "1500 800", "180 950", "1850 530"};
srand(time(&timer));
if ((pars = parser(ac, av)) != 1)
return (pars);
if (args_checker(av[1], av[2]) == 84)
return (84);
create_file(av[1], av[2], tower_pos);
return (0);
}