diff options
Diffstat (limited to 'arg.c')
-rw-r--r-- | arg.c | 96 |
1 files changed, 68 insertions, 28 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; } |