int gt_plt_fields(char *in, unsigned int query_value, unsigned int *R, unsigned int num_records, int time, int quiet, char *bim) { start(); struct plt_file pf = init_plt_file(in); unsigned int *pf_R; unsigned int len_pf_R = gt_fields_plt(pf, R, num_records, query_value, &pf_R); stop(); if (time != 0) fprintf(stderr,"%lu\n", report()); if (quiet == 0) print_result(len_pf_R, pf_R, pf.num_records, bim); free(pf_R); fclose(pf.file); return 0; }
int plt_field_freq(char *in, char *out) { struct plt_file pf = init_plt_file(in); char *line = NULL; size_t len = 0; char *pch; ssize_t read; long line_len = pf.num_fields*2*sizeof(char); int i,j; // jump past the header fseek(pf.file, pf.header_offset, SEEK_SET); struct pair *S = (struct pair *) calloc(pf.num_fields,sizeof(struct pair)); for (i = 0; i < pf.num_records; ++i) { read = getline(&line, &len, pf.file); for (j = 0; j < pf.num_fields; ++j) { S[j].a = j; S[j].b += (uint32_t)line[j*2] - 48; } } qsort(S, pf.num_fields, sizeof(struct pair), compare); // jump past the header fseek(pf.file, pf.header_offset, SEEK_SET); FILE *f = fopen(out, "w"); if (!f) err(EX_CANTCREAT, "Cannot write to \"%s\"", out); fprintf(f, "%d\n%d\n", pf.num_fields, pf.num_records); for (i = 0; i < pf.num_records; ++i) { read = getline(&line, &len, pf.file); for (j = 0; j < pf.num_fields; ++j) { if (j != 0) fprintf(f, " "); fprintf(f, "%c", line[S[j].a*2]); } fprintf(f, "\n"); } free(line); fclose(pf.file); fclose(f); return 0; }
int count_plt(char *in, uint32_t query_value, char *op, uint32_t *R, uint32_t num_records, int time, int quiet, char *bim) { start(); struct plt_file pf = init_plt_file(in); uint32_t *pf_R; uint32_t len_pf_R; if (strcmp(op,"gt") == 0) len_pf_R = gt_count_records_plt(pf, R, num_records, query_value, &pf_R); else return count_help(); stop(); if (time != 0) fprintf(stderr,"%lu\n", report()); if (quiet == 0) print_count_result(pf_R, pf.num_fields, bim); free(pf_R); fclose(pf.file); return 0; }
int main(int argc, char **argv) { int c; char *prog = argv[0]; char *in_file_name; char *record_ids; int num_r, num_records, num_fields; int i_is_set = 0, r_is_set = 0, n_is_set = 0; while ((c = getopt (argc, argv, "i:r:n:")) != -1) { switch (c) { case 'n': n_is_set = 1; num_r = atoi(optarg); break; case 'r': r_is_set = 1; record_ids = optarg; break; case 'i': i_is_set = 1; in_file_name = optarg; break; case 'h': usage(prog); return 1; case '?': if ( (optopt == 'f') || (optopt == 'r') || (optopt == 'i') ) fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); default: usage(prog); return 1; } } if ( (r_is_set == 0) || (i_is_set == 0) || (n_is_set == 0)) { usage(prog); return 1; } int R[num_r]; parse_cmd_line_int_csv(R, num_r, record_ids); struct plt_file pf = init_plt_file(in_file_name); int *G = (int *) calloc(pf.num_fields, sizeof(int)); int r = or_records_plt(pf, R, num_r, G); int i; for (i = 0; i < pf.num_fields; ++i) { if (i != 0) printf(" "); printf("%d", G[i]); } printf("\n"); fclose(pf.file); free(G); return 0; }