1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include "parser.h"
4+ #include "string.h"
5+ #include "linked-list.h"
6+ /**************************************************************
7+ * you can read the readme for more documents .
8+ * this library is just made by me (amateur) not that professional
9+ * feel free to contribute or suggest or help when you find any
10+ * fix or upgrade for this modest project .
11+ *
12+ * author : AZZEDINE LAKHDAR
13+ * EMBEDDED SYSTEMS student
14+ * 8/7/2021
15+ * thanks for using this . .
16+ . .
17+ ..
18+ *****************************************************************/
19+ linked_list_string * argument_init (){
20+ linked_list_string * new_list = malloc (sizeof (linked_list_string ));
21+ init_linked_list_string (new_list );
22+ return new_list ;
23+ }
24+ void add_argument (char * normal ,char * shor_t ,char required ,linked_list_string * my_list ){
25+ node_string * tmp = malloc (sizeof (node_string ));
26+ create_node_full (tmp ,normal ,shor_t ,required );
27+ append_node_string_to_list (tmp ,my_list );
28+ }
29+ void add_argument_with_help (char * normal ,char * shor_t ,char required ,char * help ,linked_list_string * my_list ){
30+ node_string * tmp = malloc (sizeof (node_string ));
31+ create_node_with_help (tmp ,normal ,shor_t ,help ,required );
32+ append_node_string_to_list (tmp ,my_list );
33+ }
34+ void add_argument_with_help_and_action (char * normal ,char * shor_t ,char required ,action_type type ,char * help ,void (* action_int )(int ),void (* action_char )(char * ),void (* action_bool )(void ),linked_list_string * my_list ){
35+ node_string * tmp = malloc (sizeof (node_string ));
36+ //create_node_with_help_and_action(tmp,normal,shor_t,help,required,action);
37+ create_node_with_help_and_action (tmp ,normal ,shor_t ,help ,required ,type ,action_int ,action_char ,action_bool );
38+ append_node_string_to_list (tmp ,my_list );
39+ }
40+ int manage_arguments (int argc ,char * * argv ,linked_list_string * my_arguments ){
41+ if (argc < 2 ){
42+ printf ("not enough arguments\n" );
43+ print_linked_list_string (my_arguments );
44+ return 1 ;
45+ }
46+ int argument_number_with_parameter = 0 ;
47+ node_string * tmp = my_arguments -> head ;
48+ for (int i = 0 ;i < (my_arguments -> len );i ++ ){
49+ for (int j = 0 ;j < argc ;j ++ ){
50+ //printf("--> %s\n",argv[i]+2);
51+ if ((str_cmp ((argv [j ])+ 2 ,tmp -> self )== 1 ) || (str_cmp ((argv [j ])+ 1 ,tmp -> short_self )== 1 ) ){
52+ //printf("--> %s\n",(argv[j])+2);
53+ tmp -> index = j ;
54+ tmp -> status = True ;
55+ if ((tmp -> type == INT ) || tmp -> type == CHAR )
56+ argument_number_with_parameter = argument_number_with_parameter + 2 ;
57+ else
58+ argument_number_with_parameter = argument_number_with_parameter + 1 ;
59+ //printf("hell yeah\n");
60+ }
61+ }
62+ tmp = tmp -> next ;
63+ }
64+ if (argc != (argument_number_with_parameter + 1 )){
65+ printf ("\t some arguments need paramaters \n" );
66+ print_linked_list_string (my_arguments );
67+ return 1 ;
68+ }
69+ tmp = my_arguments -> head ;
70+ for (int i = 0 ;i < my_arguments -> len ;i ++ ){
71+ if (tmp -> required == 1 ){
72+ if (tmp -> status == False ){
73+ printf ("argument %s is required\n" ,tmp -> self );
74+ return 1 ;
75+ }
76+ }
77+ /*else
78+ printf("not required\n");*/
79+ tmp = tmp -> next ;
80+ }
81+ tmp = my_arguments -> head ;
82+ for (int i = 0 ;i < my_arguments -> len ;i ++ ){
83+ //printf("index : %d \n",tmp->index);
84+ if (((tmp -> index ) != 0 ) && ((tmp -> data_fullness ) == 4 )){
85+ if (tmp -> type == INT ){
86+ (* (tmp -> action_int ))(((string_to_int (argv [(tmp -> index )+ 1 ]))));
87+ tmp -> int_value = string_to_int (argv [(tmp -> index )+ 1 ]);
88+ //printf("hooyah : %s\n",(argv[(tmp->index)+1]));
89+ }
90+ if (tmp -> type == CHAR ){
91+ (* (tmp -> action_char ))(argv [(tmp -> index )+ 1 ]);
92+ tmp -> string = argv [(tmp -> index )+ 1 ];
93+ //printf("hooyah : %s\n",(argv[(tmp->index)+1]));
94+ }
95+ if (tmp -> type == BOOL ){
96+ (* (tmp -> action_bool ))();
97+ tmp -> bool_value = True ;
98+ //printf("hooyah :\n");
99+ }
100+ }
101+ /*else if(((tmp->index) != 0) && ((tmp->data_fullness) == 3)){
102+ printf("hooyah %s : %s\n",(tmp->self),(argv[(tmp->index)+1]));
103+ }*/
104+ tmp = tmp -> next ;
105+ }
106+ return 0 ;
107+ }
0 commit comments