int main(int argc, char **argv) { static const struct option options[] = { { "header-only", no_argument, NULL, 'h' }, { "no-header", no_argument, NULL, 'H' }, { "view", no_argument, NULL, 'c' }, { "help", no_argument, NULL, '?' }, { NULL, 0, NULL, 0 } }; int status = EXIT_SUCCESS; int c, i; while ((c = getopt_long(argc, argv, "chH", options, NULL)) >= 0) switch (c) { case 'c': mode = view_all; break; case 'h': mode = view_headers; show_headers = 1; break; case 'H': show_headers = 0; break; case '?': usage(stdout, EXIT_SUCCESS); break; default: usage(stderr, EXIT_FAILURE); break; } if (optind == argc) usage(stderr, EXIT_FAILURE); for (i = optind; i < argc; i++) { htsFormat fmt; hFILE *fp = hopen(argv[i], "r"); if (fp == NULL) { fprintf(stderr, "htsfile: can't open \"%s\": %s\n", argv[i], strerror(errno)); status = EXIT_FAILURE; continue; } if (hts_detect_format(fp, &fmt) < 0) { fprintf(stderr, "htsfile: detecting \"%s\" format failed: %s\n", argv[i], strerror(errno)); hclose_abruptly(fp); status = EXIT_FAILURE; continue; } if (mode == identify) { printf("%s:\t%s\n", argv[i], hts_format_description(&fmt)); } else switch (fmt.category) { case sequence_data: if (view_sam(fp, argv[i])) fp = NULL; break; case variant_data: if (view_vcf(fp, argv[i])) fp = NULL; break; default: fprintf(stderr, "htsfile: can't view %s: unknown format\n", argv[i]); status = EXIT_FAILURE; break; } if (fp && hclose(fp) < 0) { fprintf(stderr, "htsfile: closing %s failed\n", argv[i]); status = EXIT_FAILURE; } } return status; }
// Loads the contents of filename to produced a read-only, in memory, // immobile hfile. fp is the already opened file. We always close this // input fp, irrespective of whether we error or whether we return a new // immobile hfile. static hFILE *hpreload(hFILE *fp) { hFILE *mem_fp; char *buf = NULL; off_t buf_sz = 0, buf_a = 0, buf_inc = 8192, len; for (;;) { if (buf_a - buf_sz < 5000) { buf_a += buf_inc; char *t = realloc(buf, buf_a); if (!t) goto err; buf = t; if (buf_inc < 1000000) buf_inc *= 1.3; } len = hread(fp, buf+buf_sz, buf_a-buf_sz); if (len > 0) buf_sz += len; else break; } if (len < 0) goto err; mem_fp = hfile_init_fixed(sizeof(hFILE), "r", buf, buf_sz, buf_a); if (!mem_fp) goto err; mem_fp->backend = &mem_backend; if (hclose(fp) < 0) { hclose_abruptly(mem_fp); goto err; } return mem_fp; err: free(buf); hclose_abruptly(fp); return NULL; }
mFILE *find_file_url(char *file, char *url) { char buf[8192], *cp; mFILE *mf = NULL; int maxlen = 8190 - strlen(file), len; hFILE *hf; /* Expand %s for the trace name */ for (cp = buf; *url && cp - buf < maxlen; url++) { if (*url == '%' && *(url+1) == 's') { url++; cp += strlen(strcpy(cp, file)); } else { *cp++ = *url; } } *cp++ = 0; if (!(hf = hopen(buf, "r"))) return NULL; if (NULL == (mf = mfcreate(NULL, 0))) return NULL; while ((len = hread(hf, buf, 8192)) > 0) { if (mfwrite(buf, len, 1, mf) <= 0) { hclose_abruptly(hf); mfdestroy(mf); return NULL; } } if (hclose(hf) < 0 || len < 0) { mfdestroy(mf); return NULL; } mrewind(mf); return mf; }