-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
101 lines (89 loc) · 2.71 KB
/
tests.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
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: laranda <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:56:42 by laranda #+# #+# */
/* Updated: 2019/04/11 16:33:54 by laranda ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdio.h>
#include "ft_parse_options.h"
int g_option_1 = 0;
int g_option_2 = 0;
t_option g_options[3] = {
{ OPTION_BIT, 'z', NULL, &g_option_1 },
{ OPTION_BIT, 'q', NULL, &g_option_2 },
{ OPTION_END }
};
void print_error(char *option_name);
void print_parsing_init(t_option *options);
void print_parsing_done(t_parsing_context context, int args_index);
int main(int argc, char **argv)
{
t_parsing_context context;
int args_index;
memset(&context, 0, sizeof(context));
args_index = 0;
context.argc = argc;
context.argv = argv;
context.error_callback = &print_error;
context.options = g_options;
print_parsing_init(context.options);
args_index = ft_parse_options(&context);
print_parsing_done(context, args_index);
return (0);
}
void print_error(char *option_name)
{
if (strncmp(
option_name, OPTION_SHORT_PREFIX, strlen(OPTION_SHORT_PREFIX)) == 0)
{
option_name += strlen(OPTION_SHORT_PREFIX);
}
printf("illegal option -- %c\n", *option_name);
}
void print_parsing_init(t_option *options)
{
t_option *current_option;
int first;
current_option = options;
first = 1;
printf("==== Init parsing ====\nActivable options are : ");
while (current_option->type != OPTION_END)
{
if (first)
{
first = 0;
}
else
{
printf(", ");
}
printf("%c", current_option->short_name);
current_option++;
}
printf("\n");
}
void print_parsing_done(t_parsing_context context, int args_index)
{
t_option *current_option;
current_option = context.options;
printf("==== Parsing Done ====\nOptions State :\n");
while (current_option->type != OPTION_END)
{
printf(
"%c => %i\n",
current_option->short_name,
*(int*)(current_option->value));
current_option++;
}
printf("\nParameters are :\n");
while (args_index < context.argc)
{
printf("%s\n", context.argv[args_index++]);
}
}