992a12b087b6cef00ad0f699d613f2abc538018b kent Thu Apr 20 17:46:28 2017 -0700 Moving some little new useful routines to library. diff --git src/lib/fieldedTable.c src/lib/fieldedTable.c index 802cffd..78f8365 100644 --- src/lib/fieldedTable.c +++ src/lib/fieldedTable.c @@ -216,30 +216,63 @@ } /* Create fieldedTable . */ struct fieldedTable *table = fieldedTableNew(reportFileName, fields, fieldCount); table->startsSharp = startsSharp; while (lineFileRowTab(lf, fields)) { fieldedTableAdd(table, fields, fieldCount, lf->lineIx); } /* Clean up and go home. */ lineFileClose(&lf); return table; } +void fieldedTableToTabFile(struct fieldedTable *table, char *fileName) +/* Write out a fielded table back to file */ +{ +FILE *f = mustOpen(fileName, "w"); + +/* Write out header row with optional leading # */ +if (table->startsSharp) + fputc('#', f); +int i; +fputs(table->fields[0], f); +for (i=1; i<table->fieldCount; ++i) + { + fputc('\t', f); + fputs(table->fields[i], f); + } +fputc('\n', f); + +/* Write out rest. */ +struct fieldedRow *fr; +for (fr = table->rowList; fr != NULL; fr = fr->next) + { + fputs(fr->row[0], f); + for (i=1; i<table->fieldCount; ++i) + { + fputc('\t', f); + fputs(fr->row[i], f); + } + fputc('\n', f); + } + +carefulClose(&f); +} + int fieldedTableMustFindFieldIx(struct fieldedTable *table, char *field) /* Find index of field in table's row. Abort if field not found. */ { int ix = stringArrayIx(field, table->fields, table->fieldCount); if (ix < 0) errAbort("Field %s not found in table %s", field, table->name); return ix; } struct hash *fieldedTableIndex(struct fieldedTable *table, char *field) /* Return hash of fieldedRows keyed by values of given field */ { int fieldIx = fieldedTableMustFindFieldIx(table, field); struct hash *hash = hashNew(0); struct fieldedRow *fr;