diff options
author | FreeArtMan <dos21h@gmail.com> | 2017-05-28 17:24:42 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2017-05-28 17:24:42 +0100 |
commit | 622c73263fb0a27cf0fe77247d5a0e0db4b9fdd3 (patch) | |
tree | 72f447ec26d8fd03183c973b5cc37aa89381dae7 | |
parent | 7cf1c1d56674382ba2b10d0f0cd8b56023c9a861 (diff) | |
download | libarg-622c73263fb0a27cf0fe77247d5a0e0db4b9fdd3.tar.gz libarg-622c73263fb0a27cf0fe77247d5a0e0db4b9fdd3.zip |
working list in da params
-rw-r--r-- | arg.c | 96 | ||||
-rw-r--r-- | arg.h | 11 | ||||
-rw-r--r-- | test/test1.c | 4 |
3 files changed, 77 insertions, 34 deletions
@@ -21,7 +21,7 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) arg_t *ret = NULL; uint8_t *used; //count unused arguments, there should be no unused //elements at the end - int i = 0; + int i=0; int pos = -1; //position of argv inside def_arg table void *arg_def_val = NULL; @@ -94,8 +94,12 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) } case ARGT_LIST: { - s_arg_list *parse_params = NULL; - char *iter=NULL,*st=NULL,*s=NULL; + int j=0; + int num=0; + char **parse_params = NULL; + int param_num = 0; + s_arg_list *declared_params = argl[pos].def; + char *iter=NULL, *st=NULL, *s=NULL; //printf("%d is list\n",i); CHK_ARG(i,1); @@ -108,29 +112,37 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) } //super algo for parsing - parse_params = malloc(sizeof(arg_list)); - memset(parse_params, 0, sizeof(arg_list)); iter = argv[i+1]; st = iter; while ((*iter)!='\0') { - if (((*iter)=='|')) + if ((*iter)=='|') { if ((iter)>st) { s = malloc(iter-st+1); memcpy(s, st, iter-st); s[iter-st]=0; - //realloc and add new pointer - parse_params->num += 1; - char *realoc_ptr = realloc(parse_params->vals, sizeof(void *)*parse_params->num); - if (realoc_ptr) - { - parse_params->vals = (void **)realoc_ptr; - parse_params->vals[parse_params->num-1] = s; - } else + + //TODO double flags will be passed in =P + for (j=0;j<declared_params->num;j++) { - parse_params->num -= 1; + int n1 = strlen(s); + int n2 = strlen(declared_params->vals[j]); + int n = n1 > n2 ? n1 : n2; + if (strncmp(s,declared_params->vals[j],n)==0) + { + param_num += 1; + char **realoc_ptr = realloc(parse_params, sizeof(char *)*param_num); + if (realoc_ptr) + { + parse_params = (char **)realoc_ptr; + parse_params[param_num-1] = s; + } else + { + param_num -= 1; + } + } } st = iter+1; } else @@ -146,26 +158,46 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl ) s = malloc(iter-st+1); memcpy(s, st, iter-st); s[iter-st]=0; - parse_params->num += 1; - char *realoc_ptr = realloc(parse_params->vals, sizeof(void *)*parse_params->num); - if (realoc_ptr) - { - parse_params->vals = (void **)realoc_ptr; - parse_params->vals[parse_params->num-1] = s; - } else + + //some hash table could be nice to use here =p + //TODO double flags will be passed in =P + for (j=0;j<declared_params->num;j++) { - parse_params->num -= 1; + int n1 = strlen(s); + int n2 = strlen(declared_params->vals[j]); + int n = n1 > n2 ? n1 : n2; + if (strncmp(s,declared_params->vals[j],n)==0) + { + param_num += 1; + char **realoc_ptr = realloc(parse_params, sizeof(char *)*param_num); + if (realoc_ptr) + { + parse_params = (char **)realoc_ptr; + parse_params[param_num-1] = s; + } else + { + param_num -= 1; + } + } } + st = iter+1; } } } - arg_list *list = __create_arg(argl[pos].type, parse_params); - + arg_list *list=NULL; + { + //hack depends on __create_arg logic + s_arg_list arg; + arg.num = param_num; + arg.vals = parse_params; + list = __create_arg(argl[pos].type, &arg); + } + //check param values - if (-1==__add_arg(ret, ARGT_LIST, list)) + if (-1 == __add_arg(ret, ARGT_LIST, list)) { ENL(); } @@ -344,15 +376,23 @@ arg_float* arg_c_float( float val ) } -arg_list* arg_c_list( uint32_t num, void **vals) +arg_list* arg_c_list( uint32_t num, char **vals) { + int i; arg_list *ret = NULL; + char **ptr_c=NULL; ret = malloc( sizeof(arg_list) ); memset( ret, 0, sizeof(arg_list) ); + ptr_c = malloc(sizeof(char*)*num); + for (i=0;i<num;i++) + { + ptr_c[i] = vals[i]; + } + ret->num = num; - ret->vals = vals; + ret->vals = ptr_c; return ret; } @@ -116,19 +116,20 @@ list arguments by name supports PARAM1|PARAM2|PARAM3 */ + typedef struct s_arg_list { int used; - int def; //says if default should be used if no value in cmd + int def; //says if default should be used if no value in cmd uint32_t num; - void **vals; //list of supported arguments - char *default_val; //points to default arguments in da list + char **vals; //list of supported arguments + char *default_val; //points to default arguments in da list } s_arg_list; typedef struct arg_list { uint32_t num; - void **vals; + char **vals; } arg_list; /* @@ -186,7 +187,7 @@ argv_t* arg_get( arg_t *arg, int num ); arg_ip* arg_c_ip( uint32_t ip, uint16_t port ); arg_range* arg_c_range( uint32_t start, uint32_t end, uint32_t step ); arg_float* arg_c_float( float val ); -arg_list* arg_c_list( uint32_t num, void **vals); +arg_list* arg_c_list( uint32_t num, char **vals); arg_file* arg_c_file( char *name ); arg_val* arg_c_val( char *ptr ); arg_flag* arg_c_flag(); diff --git a/test/test1.c b/test/test1.c index f6a7a70..1b1e98e 100644 --- a/test/test1.c +++ b/test/test1.c @@ -24,11 +24,13 @@ s_arg_float ratio_float = .val = 0.8 }; +char *color_list[] = {"RED","GREEN","BLUE"}; + s_arg_list color_name_list = { .num = 3, .default_val = "RED", - .vals = {"RED","GREEN","BLUE"} + .vals = color_list }; s_arg_val number_val = |