11b5d7088f505cdc4f678d3451d63bed4a3019f9 kent Fri May 26 13:28:25 2017 -0700 Adding maxCols option so can handle more than 16. diff --git src/utils/addCols/addCols.c src/utils/addCols/addCols.c index 98435b9..9c5b198 100644 --- src/utils/addCols/addCols.c +++ src/utils/addCols/addCols.c @@ -1,53 +1,63 @@ /* addCols - Add together columns. */ #include "common.h" #include "linefile.h" +#include "options.h" +int clMaxCols=16; void usage() /* Explain usage and exit. */ { errAbort( "addCols - Sum columns in a text file.\n" "usage:\n" " addCols <fileName>\n" - "adds all columns (up to 16 columns) in the given file, \n" + "adds all columns in the given file, \n" "outputs the sum of each column. <fileName> can be the\n" - "name: stdin to accept input from stdin."); + "name: stdin to accept input from stdin.\n" + "Options:\n" + " -maxCols=N - maximum number of colums (defaults to %d)\n", clMaxCols); } +/* Command line validation table. */ +static struct optionSpec options[] = { + {"maxCols", OPTION_INT}, + {NULL, 0}, +}; + + void addCols(char *fileName) /* addCols - Add together columns. */ { struct lineFile *lf; -char *words[16]; +char **words; +AllocArray(words, clMaxCols); int wordCount; -static double totals[16]; +double *totals; +AllocArray(totals, clMaxCols); int maxCount = 0; int i; -if (sameString(fileName, "stdin")) - lf = lineFileStdin(TRUE); -else lf = lineFileOpen(fileName, TRUE); -for (i=0; i<ArraySize(words); ++i) - totals[i] = 0; -while ((wordCount = lineFileChop(lf, words)) != 0) +while ((wordCount = lineFileChopNext(lf, words, clMaxCols)) != 0) { if (maxCount < wordCount) maxCount = wordCount; for (i=0; i<wordCount; ++i) totals[i] += atof(words[i]); } for (i=0; i<maxCount; ++i) printf("%6.2f ", totals[i]); printf("\n"); } int main(int argc, char *argv[]) /* Process command line. */ { +optionInit(&argc, argv, options); if (argc != 2) usage(); +clMaxCols = optionInt("maxCols", clMaxCols); addCols(argv[1]); return 0; }