c - Awkward data structure -
i writing utility takes tab delimited text files , outputs html tables. part of functionality ability pass filter values @ command prompt. so, example, if have column "a" should able pass --filter "a" "foo" , see rows column "a" contained "foo."
this brings me question. i'm writing in c, , perhaps due inexperience language, i'm having trouble expressing "filter" data structure.
my initial stab @ problem store filters upfront:
//parse parameters , values, i.e. --start, --length (; i<argc; i++){ if (argv[i][1] == '-'){ argv[i]++; //strip long options if (i+1 == argc || argv[i+1][0] == '-'){ fprintf(stderr, "error: missing argument optional parameter!\n"); exit(1); } } if (strcmp(argv[i], "-start")==0) start = strtol(argv[i+1], null, 0); else if (strcmp(argv[i], "-length")==0) length = strtol(argv[i+1], null, 0); else if (strcmp(argv[i], "-filter")==0){ //store filter arguments later processing filters = realloc(filters, sizeof(char*) * (f+2)); filters[f++] = strdup(argv[i+1]); filters[f++] = strdup(argv[i+2]); } } and then, once have column names, create sparsely populated array of filter values corresponds length of file header. so, if i'm looking @ column 4, fourth value of filter array , check if populated value.
the code expands "initial" storage of filter lookup table follows:
//now know how many cols have, create sparsely populated filter array (j=0,i=0; i<ncols; i++){ filtercols = realloc(filtercols, sizeof(char*) * (ncols+1)); filtercols[i] = strdup(""); (c=0; c<f; c++){ if (strcmp(columns + j, filters[c])==0){ filtercols[i] = strdup(filters[c+1]); break; } } while (columns[j]){ j++; } j++; } other being awkward, can see if specify filter has same value both column name , filter value, won't work.
in python, i'd handle dictionaries. best idiom available situation when working in c?
thank time.
you use hash:
Comments
Post a Comment