Zappy - Year end project 2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
check_values_validity.c
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** my_zappy
4** File description:
5** check_values_validity
6*/
7
8#include <string.h>
9
10#include "utils.h"
11#include "arg_parse.h"
12
13static int number_checker(const struct arg_s **arguments, const int index)
14{
15 char str[MAX_BUFFER_SIZE];
16 char *num_str = (char *)arguments[index]->value;
17
18 if (strcmp(arguments[index]->name, "-n") == 0)
19 return (0);
20 if (strlen(num_str) == 1 && strcmp(num_str, "0") == 0) {
21 strcpy(str, "Error: The number of the parameter '");
22 strcat(str, arguments[index]->name);
23 strcat(str, "' must be greater than 0.\n");
24 return (write_error_msg(str));
25 }
26 for (int i = 0; num_str[i] != '\0'; i++) {
27 if (num_str[i] < '0' || num_str[i] > '9') {
28 strcpy(str, "Error: The parameter '");
29 strcat(str, arguments[index]->name);
30 strcat(str, "' must be a number.\n");
31 return (write_error_msg(str));
32 }
33 }
34 return (0);
35}
36
37static int compare_name_2(char **array, const char *name, const size_t x)
38{
39 char str[MAX_BUFFER_SIZE];
40
41 for (size_t i = 0; i < x; i++) {
42 if (array[i] == NULL) {
43 break;
44 }
45 if (strcmp(name, array[i]) == 0) {
46 strcpy(str, "Error: The team name '");
47 strcat(str, name);
48 strcat(str, "' is repeated. A team name must be unique.\n");
49 return (write_error_msg(str));
50 }
51 }
52 return (0);
53}
54
55static int compare_name_1(const struct arg_s **arguments, const size_t index)
56{
57 char **tmp = NULL;
58 char **name = NULL;
59 size_t x = 0;
60
61 name = arguments[index]->value;
62 tmp = malloc(sizeof(char *) * arguments[index]->nb_value + 1);
63 for (size_t i = 0; i < arguments[index]->nb_value; i++) {
64 if (compare_name_2(tmp, *name, x) == -1) {
65 free(tmp);
66 return (-1);
67 }
68 tmp[x] = *name;
69 x++;
70 name++;
71 }
72 free(tmp);
73 return (0);
74}
75
76static int name_checker(const struct arg_s **arguments, const size_t index)
77{
78 if (strcmp(arguments[index]->name, "-n") != 0) {
79 return (0);
80 }
81 if (arguments[index]->nb_value > 1) {
82 if (compare_name_1(arguments, index) == -1) {
83 return (-1);
84 }
85 }
86 return (0);
87}
88
89static int map_size_checker(const struct arg_s **arguments)
90{
91 char str[MAX_BUFFER_SIZE];
92 int x = 0;
93 int y = 0;
94
95 strcpy(str, (char *)find_value_by_param(arguments, "-x"));
96 x = atoi(str);
97 strcpy(str, (char *)find_value_by_param(arguments, "-y"));
98 y = atoi(str);
99 if (x * y < MINIMUM_MAP_SIZE) {
100 write_error_msg("Error: The map size must be bigger than 20.\n");
101 return (-1);
102 }
103 return (0);
104}
105
106int check_values_validity(const struct arg_s **arguments, const size_t size)
107{
108 for (size_t i = 0; i < size; i++) {
109 if (number_checker(arguments, i) == -1) {
110 return (-1);
111 }
112 if (name_checker(arguments, i) == -1) {
113 return (-1);
114 }
115 }
116 return map_size_checker(arguments);
117}
int check_values_validity(const struct arg_s **arguments, const size_t size)
Check if the values of the parameters are correct.
#define MINIMUM_MAP_SIZE
Definition constants.h:23
#define MAX_BUFFER_SIZE
Definition constants.h:14
void * value
Definition arg_parse.h:19
size_t nb_value
Definition arg_parse.h:18
void * find_value_by_param(const struct arg_s **arguments, char *param)
! FINDER !!
int write_error_msg(const char *str)
! WRITTER !!
Definition writer.c:11