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_arg_mandatory.c
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2023
3** my_zappy
4** File description:
5** check_arg_mandatory.c
6*/
7
8#include <stdio.h>
9#include <string.h>
10
11#include "utils.h"
12#include "arg_parse.h"
13
14int get_nb_mandatory_option(const struct option_list_s *opt_list)
15{
16 int nb_mandatory = 0;
17
18 for (int i = 0; i < NB_PARAM; ++i) {
19 if (opt_list[i].option.mandatory)
20 ++nb_mandatory;
21 }
22 return (nb_mandatory);
23}
24
25static void is_mandatory(int ac, int *mandatory_present, const char **av,
26 char *mandatory)
27{
28 for (int j = 1; j < ac - 1; ++j) {
29 if (strcmp(mandatory, av[j]) == 0) {
30 ++(*mandatory_present);
31 return;
32 }
33 }
34}
35
36static char **get_list_mandatory(const struct option_list_s *opt_list)
37{
38 int nb_mandatory = get_nb_mandatory_option(opt_list);
39 char **mandatory = malloc(sizeof(char *) * (nb_mandatory + 1));
40 int mandatory_idx = 0;
41
42 if (mandatory == NULL)
43 return (NULL);
44 for (int i = 0; i < NB_PARAM; ++i) {
45 if (SERVER_OPTION[i].option.mandatory) {
46 mandatory[mandatory_idx] = strdup(SERVER_OPTION[i].option.name);
47 ++mandatory_idx;
48 }
49 }
50 mandatory[nb_mandatory] = NULL;
51 return (mandatory);
52}
53
54int check_mandatory_set(int ac, const char **av, int nb_param,
55 const struct option_list_s *opt_list)
56{
57 int nb_mandatory = get_nb_mandatory_option(opt_list);
58 char **mandatory = get_list_mandatory(opt_list);
59 int mandatory_present = 0;
60
61 if (mandatory == NULL) {
62 return (write_error_msg("Error: Internal server error.\n"));
63 }
64 if (nb_param < nb_mandatory) {
65 free_array(mandatory);
66 return (write_error_msg("Error: Not enough parameters.\n"));
67 }
68 for (int i = 0; i < nb_mandatory; ++i) {
69 is_mandatory(ac, &mandatory_present, av, mandatory[i]);
70 }
71 if (nb_mandatory != mandatory_present) {
72 free_array(mandatory);
73 return (write_error_msg("Error: A parameter was not recognised.\n"));
74 }
75 free_array(mandatory);
76 return (0);
77}
int check_mandatory_set(int ac, const char **av, int nb_param, const struct option_list_s *opt_list)
Check if there's every mandatory options in the arguments passed to the program.
int get_nb_mandatory_option(const struct option_list_s *opt_list)
Get the number of mandatory options needed for the server.
#define NB_PARAM
! PARSING CONSTANTS !!
Definition constants.h:20
void free_array(void *to_free)
! FREE !!
Definition free.c:13
int write_error_msg(const char *str)
! WRITTER !!
Definition writer.c:11