| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
** EPITECH PROJECT, 2024 |
| 3 |
|
|
** zappy |
| 4 |
|
|
** File description: |
| 5 |
|
|
** flags.c |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include "server.h" |
| 9 |
|
|
|
| 10 |
|
3 |
static bool count_flags(size_t flags[NB_FLAGS]) |
| 11 |
|
|
{ |
| 12 |
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 |
for (size_t i = 0; i < NB_FLAGS; i++) { |
| 13 |
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
|
16 |
if (flags[i] != 1) { |
| 14 |
|
|
return false; |
| 15 |
|
|
} |
| 16 |
|
|
} |
| 17 |
|
|
return true; |
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
|
13 |
static void get_int(size_t *flag_count, int *flag, const char *next_line) |
| 21 |
|
|
{ |
| 22 |
|
13 |
(*flag_count)++; |
| 23 |
3/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 1 times.
|
13 |
if (next_line != NULL && str_is_num(next_line)) { |
| 24 |
|
12 |
*flag = atoi(next_line); |
| 25 |
|
|
} else { |
| 26 |
|
1 |
*flag = INCORRECT_FLAG_VALUE; |
| 27 |
|
|
} |
| 28 |
|
13 |
} |
| 29 |
|
|
|
| 30 |
|
2 |
static size_t count_nb_names(const char **args, size_t *idx) |
| 31 |
|
|
{ |
| 32 |
|
|
size_t nb_names = 0; |
| 33 |
|
|
|
| 34 |
|
2 |
(*idx)++; |
| 35 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
for (size_t i = *idx; args[i]; i++) { |
| 36 |
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
5 |
if (strlen(args[i]) == 2 && args[i][0] == '-') |
| 37 |
|
|
break; |
| 38 |
|
3 |
nb_names++; |
| 39 |
|
|
} |
| 40 |
|
2 |
return nb_names; |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
2 |
static void get_names( |
| 44 |
|
|
size_t *flag_count, |
| 45 |
|
|
flags_t *flags, |
| 46 |
|
|
const char **args, |
| 47 |
|
|
size_t *idx |
| 48 |
|
|
) |
| 49 |
|
|
{ |
| 50 |
|
2 |
size_t nb_names = count_nb_names(args, idx); |
| 51 |
|
|
|
| 52 |
|
2 |
(*flag_count)++; |
| 53 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (nb_names == 0) |
| 54 |
|
|
return; |
| 55 |
|
2 |
flags->names = malloc(sizeof(char *) * (nb_names + 1)); |
| 56 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (flags->names == NULL) |
| 57 |
|
|
return; |
| 58 |
|
2 |
flags->names[nb_names] = NULL; |
| 59 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
for (size_t i = 0; args[(*idx)]; (*idx)++) { |
| 60 |
|
3 |
flags->names[i] = malloc(sizeof(char *) * (strlen(args[(*idx)]) + 1)); |
| 61 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
if (flags->names[i] == NULL) |
| 62 |
|
|
break; |
| 63 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
strcpy(flags->names[i], args[(*idx)]); |
| 64 |
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
|
3 |
if (args[(*idx) + 1] != NULL && strlen(args[(*idx) + 1]) == 2 |
| 65 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
&& args[(*idx) + 1][0] == '-') |
| 66 |
|
|
break; |
| 67 |
|
1 |
i++; |
| 68 |
|
|
} |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
|
16 |
static void fill_flags( |
| 72 |
|
|
size_t f_count[NB_FLAGS], |
| 73 |
|
|
flags_t *f, |
| 74 |
|
|
const char **av, size_t i |
| 75 |
|
|
) |
| 76 |
|
|
{ |
| 77 |
7/7
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
|
16 |
switch (av[i][1]) { |
| 78 |
|
3 |
case 'p': |
| 79 |
|
3 |
get_int(&f_count[PORT], &f->port, av[i + 1]); |
| 80 |
|
3 |
break; |
| 81 |
|
3 |
case 'x': |
| 82 |
|
3 |
get_int(&f_count[WIDTH], &f->width, av[i + 1]); |
| 83 |
|
3 |
break; |
| 84 |
|
3 |
case 'y': |
| 85 |
|
3 |
get_int(&f_count[HEIGHT], &f->height, av[i + 1]); |
| 86 |
|
3 |
break; |
| 87 |
|
2 |
case 'n': |
| 88 |
|
2 |
get_names(&f_count[NAME], f, av, &i); |
| 89 |
|
2 |
break; |
| 90 |
|
2 |
case 'c': |
| 91 |
|
2 |
get_int(&f_count[CLIENT_NB], &f->nb_clients, av[i + 1]); |
| 92 |
|
2 |
break; |
| 93 |
|
2 |
case 'f': |
| 94 |
|
2 |
get_int(&f_count[FREQ], &f->frequency, av[i + 1]); |
| 95 |
|
2 |
break; |
| 96 |
|
|
} |
| 97 |
|
16 |
} |
| 98 |
|
|
|
| 99 |
|
3 |
bool is_freq_flag(const char **args) |
| 100 |
|
|
{ |
| 101 |
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
|
30 |
for (size_t i = 0; args[i] != NULL; i++) { |
| 102 |
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 2 times.
|
29 |
if (strcmp(args[i], "-f") == 0) |
| 103 |
|
|
return true; |
| 104 |
|
|
} |
| 105 |
|
|
return false; |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
3 |
static bool check_number_flags(flags_t *flags, const char **args) |
| 109 |
|
|
{ |
| 110 |
|
3 |
size_t flags_counter[NB_FLAGS] = {0}; |
| 111 |
|
|
|
| 112 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
if (args == NULL) |
| 113 |
|
3 |
return false; |
| 114 |
|
3 |
flags->is_iteration = false; |
| 115 |
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 3 times.
|
34 |
for (size_t i = 0; args[i]; i++) { |
| 116 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 |
if (strcmp(args[i], "--iteration") == 0) |
| 117 |
|
✗ |
flags->is_iteration = true; |
| 118 |
4/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 16 times.
|
31 |
if (strlen(args[i]) != 2 || args[i][0] != '-') |
| 119 |
|
15 |
continue; |
| 120 |
|
16 |
fill_flags(flags_counter, flags, args, i); |
| 121 |
|
|
} |
| 122 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 |
if (is_freq_flag(args) == false) { |
| 123 |
|
1 |
flags_counter[FREQ] = 1; |
| 124 |
|
1 |
flags->frequency = 100; |
| 125 |
|
|
} |
| 126 |
|
3 |
return count_flags(flags_counter); |
| 127 |
|
|
} |
| 128 |
|
|
|
| 129 |
|
✗ |
void print_flags(flags_t *flags) |
| 130 |
|
|
{ |
| 131 |
|
✗ |
printf("PORT: %d\n", flags->port); |
| 132 |
|
✗ |
printf("WIDTH: %d\n", flags->width); |
| 133 |
|
✗ |
printf("HEIGHT: %d\n", flags->height); |
| 134 |
|
|
printf("NAMES:\n"); |
| 135 |
|
✗ |
print_string_array(flags->names); |
| 136 |
|
✗ |
printf("NUMBER CLIENTS: %d\n", flags->nb_clients); |
| 137 |
|
✗ |
printf("FREQUENCY: %d\n", flags->frequency); |
| 138 |
|
✗ |
} |
| 139 |
|
|
|
| 140 |
|
2 |
static bool check_error_flags(flags_t *flags) |
| 141 |
|
|
{ |
| 142 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 |
if (flags->port < 0) { |
| 143 |
|
|
dprintf(2, "Invalid port\n"); |
| 144 |
|
1 |
return false; |
| 145 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
} else if (flags->names == NULL) { |
| 146 |
|
|
dprintf(2, "Couldn't find any names\n"); |
| 147 |
|
✗ |
return false; |
| 148 |
|
|
} |
| 149 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
if (flags->frequency < 2 || flags->frequency > 10000) { |
| 150 |
|
|
dprintf(2, "Frequency can only be between 2 and 10000\n"); |
| 151 |
|
✗ |
return false; |
| 152 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
} else if (flags->width < 10 || flags->width > 1000 || |
| 153 |
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 |
flags->height < 10 || flags->height > 1000) { |
| 154 |
|
|
dprintf(2, "Map values need to be between 10 and 1000\n"); |
| 155 |
|
✗ |
return false; |
| 156 |
|
|
} |
| 157 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
if (flags->nb_clients < 1 || flags->nb_clients > 200) { |
| 158 |
|
|
dprintf(2, "clientsNb can only be between 1 and 200\n"); |
| 159 |
|
✗ |
return false; |
| 160 |
|
|
} |
| 161 |
|
|
return true; |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
|
3 |
bool init_flags(flags_t *flags, const char **args) |
| 165 |
|
|
{ |
| 166 |
|
|
double width; |
| 167 |
|
|
double height; |
| 168 |
|
|
|
| 169 |
|
3 |
flags->names = NULL; |
| 170 |
4/4
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
3 |
if (!check_number_flags(flags, args) || !check_error_flags(flags)) |
| 171 |
|
2 |
return false; |
| 172 |
|
1 |
width = (double)flags->width; |
| 173 |
|
1 |
height = (double)flags->height; |
| 174 |
|
1 |
flags->max_map.food = (int)(width * height * FOOD_DENSITY); |
| 175 |
|
1 |
flags->max_map.linemate = (int)(width * height * LINEMATE_DENSITY); |
| 176 |
|
1 |
flags->max_map.deraumere = (int)(width * height * DERAUMERE_DENSITY); |
| 177 |
|
1 |
flags->max_map.sibur = (int)(width * height * SIBUR_DENSITY); |
| 178 |
|
1 |
flags->max_map.mendiane = (int)(width * height * MENDIANE_DENSITY); |
| 179 |
|
1 |
flags->max_map.phiras = (int)(width * height * PHIRAS_DENSITY); |
| 180 |
|
1 |
flags->max_map.thystame = (int)(width * height * THYSTAME_DENSITY); |
| 181 |
|
1 |
return true; |
| 182 |
|
|
} |
| 183 |
|
|
|