FILE *openSortOutput(char *fileName, char **sortSpec) /* Open a pipeline to sort and compress output. SortSpec is null-terminated * list of fields options to pass to sort. gSortTmpDir maybe null. * gbOutputRename is used to install and close. */ { struct gbPipeline *pipeline; char tmpPath[PATH_LEN]; char **compressor; char **sortCmd, **cmds[3]; int nSort, iSort = 0, i; /* sort cmd, nSort includes cmd and NULL */ for (nSort = 0; sortSpec[nSort] != NULL; nSort++) continue; if (gSortTmp != NULL) nSort += 2; nSort += 2; /* for "sort" and NULL */ sortCmd = alloca(nSort*sizeof(char*)); sortCmd[iSort++] = "sort"; if (gSortTmp != NULL) { sortCmd[iSort++] = "-T"; sortCmd[iSort++] = gSortTmp; } for (i = 0; sortSpec[i] != NULL; i++) sortCmd[iSort++] = sortSpec[i]; sortCmd[iSort] = NULL; assert(iSort < nSort); cmds[0] = sortCmd; cmds[1] = NULL; /* compress process, if needed */ compressor = gbGetCompressor(fileName, "w"); if (compressor != NULL) { cmds[1] = compressor; cmds[2] = NULL; } /* tmp name to output to */ gbGetOutputTmp(fileName, tmpPath); gbMakeFileDirs(tmpPath); pipeline = gbPipelineCreate(cmds, PIPELINE_WRITE|PIPELINE_FDOPEN, NULL, tmpPath); return gbPipelineFile(pipeline); }
FILE* gzMustOpen(char* fileName, char *mode) /* Open a file for read or write access. If it ends in .gz, .Z, or .bz2 it * will be piped through the approriate decompression program. Must use * gzClose() to close the file. */ { char **compressor = gbGetCompressor(fileName, mode); if (compressor != NULL) { char *argv[4], *outFile, **cmds[2]; unsigned options = PIPELINE_FDOPEN; struct gbPipeline *pipeline; argv[0] = compressor[0]; argv[1] = compressor[1]; /* compress options */ if (sameString(mode, "r")) { argv[2] = fileName; argv[3] = NULL; outFile = NULL; options |= PIPELINE_READ; } else { argv[2] = NULL; outFile = fileName; if (sameString(mode, "w")) options |= PIPELINE_WRITE; else if (sameString(mode, "a")) errAbort("gzMustOpen append mode does not work for compressed output"); else errAbort("invalid mode for gzMustOpen \"%s\"", mode); } cmds[0] = argv; cmds[1] = NULL; pipeline = gbPipelineCreate(cmds, options, NULL, outFile); return gbPipelineFile(pipeline); } else return mustOpen(fileName, mode); }