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
arg_parse.c
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2023
3** my_zappy
4** File description:
5** arg_parse.c
6*/
7
8#include <string.h>
9
10#include "utils.h"
11#include "arg_parse.h"
12
13static void get_multiple_value(const char **av, int idx, struct arg_s *arg)
14{
15 int i = 1;
16 char **tmp;
17
18 while (av[idx + i][0] != '-') {
19 ++arg->nb_value;
20 ++i;
21 }
22 arg->value = malloc(sizeof(char *) * (arg->nb_value + 1));
23 if (arg->value == NULL)
24 return;
25 tmp = arg->value;
26 for (size_t j = 0; j < arg->nb_value; ++j) {
27 tmp[j] = strdup(av[idx + 1 + j]);
28 if (tmp[j] == NULL) {
29 arg->value = NULL;
30 return;
31 }
32 }
33}
34
35static int param_has_mul_val(int idx, struct arg_s *arg, const char **av,
36 const struct option_s *param)
37{
38 if (strcmp(av[idx], param->name) == 0) {
39 if (param->has_multiple_arg) {
40 get_multiple_value(av, idx, arg);
41 return (0);
42 }
43 arg->value = strdup(av[idx + 1]);
44 arg->nb_value = 1;
45 return (0);
46 }
47 return (1);
48}
49
50int get_arg_value(int ac, const char **av,
51 const struct option_s *param, struct arg_s **arg)
52{
53 (*arg) = malloc(sizeof(struct arg_s) * 1);
54 if ((*arg) == NULL)
55 return (write_error_msg("Error: Internal server error.\n"));
56 (*arg)->name = strdup(param->name);
57 (*arg)->nb_value = 0;
58 if ((*arg)->name == NULL) {
59 free((*arg));
60 return (write_error_msg("Error: Internal server error.\n"));
61 }
62 for (int i = 0; i < ac; ++i) {
63 if (param_has_mul_val(i, *arg, av, param) == 0)
64 return (0);
65 }
66 free((*arg)->name);
67 free((*arg));
68 return (write_error_msg("Error: Internal server error.\n"));
69}
70
71static int *get_set_param(int ac, const char **av, int nb_param)
72{
73 int count = 0;
74 int *param = malloc(sizeof(int) * nb_param);
75 int tmp;
76
77 if (param == NULL)
78 return (NULL);
79 for (int i = 1; i < ac - 1 && count < nb_param; ++i) {
80 tmp = is_param(av[i], SERVER_OPTION);
81 if (tmp != -1) {
82 param[count] = tmp;
83 ++count;
84 }
85 }
86 return (param);
87}
88
89struct arg_s **get_zappy_args(int ac, const char **av,
90 const struct option_list_s *opt_l)
91{
92 int nb_param = get_nb_parameter(ac, av);
93 int *to_get = get_set_param(ac, av, nb_param);
94 struct arg_s **args = malloc(sizeof(struct arg_s *) * (nb_param + 1));
95
96 if (check_mandatory_set(ac, av, nb_param, opt_l) == -1 || args == NULL) {
97 free(args);
98 free(to_get);
99 return (NULL);
100 }
101 for (int i = 0; i < nb_param; ++i) {
102 if (get_arg_value(ac, av, &opt_l[to_get[i]].option, &args[i]) == -1) {
103 free_args(args, i);
104 free(to_get);
105 free(args);
106 return (NULL);
107 }
108 }
109 free(to_get);
110 args[nb_param] = NULL;
111 return (args);
112}
int get_arg_value(int ac, const char **av, const struct option_s *param, struct arg_s **arg)
Get the variables of a parameter and store it in arg_s structure.
Definition arg_parse.c:50
struct arg_s ** get_zappy_args(int ac, const char **av, const struct option_list_s *opt_l)
The principal function to get every arguments.
Definition arg_parse.c:89
int get_nb_parameter(int ac, const char **av)
Get the number of parameter passed to the program.
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 is_param(const char *str, const struct option_list_s *opt_list)
Check if a string is a parameter.
void * value
Definition arg_parse.h:19
size_t nb_value
Definition arg_parse.h:18
bool has_multiple_arg
Definition arg_parse.h:25
char * name
Definition arg_parse.h:23
void free_args(struct arg_s **args, const size_t size)
Free struct args_s of lenght size.
Definition free.c:35
int write_error_msg(const char *str)
! WRITTER !!
Definition writer.c:11