// ---------------------------------------------------------------- static void lrec_writer_pprint_process(FILE* output_stream, lrec_t* prec, void* pvstate) { lrec_writer_pprint_state_t* pstate = pvstate; int drain = FALSE; if (prec == NULL) { drain = TRUE; } else { if (pstate->pprev_keys != NULL && !lrec_keys_equal_list(prec, pstate->pprev_keys)) { drain = TRUE; } } if (drain) { if (pstate->num_blocks_written > 0LL) // separate blocks with empty line fputs(pstate->ors, output_stream); print_and_free_record_list(pstate->precords, output_stream, pstate->ors, pstate->ofs, pstate->left_align); if (pstate->pprev_keys != NULL) { slls_free(pstate->pprev_keys); pstate->pprev_keys = NULL; } pstate->precords = sllv_alloc(); pstate->num_blocks_written++; } if (prec != NULL) { sllv_append(pstate->precords, prec); if (pstate->pprev_keys == NULL) pstate->pprev_keys = mlr_copy_keys_from_record(prec); } }
// ---------------------------------------------------------------- static void lrec_writer_csv_process(FILE* output_stream, lrec_t* prec, void* pvstate) { if (prec == NULL) return; lrec_writer_csv_state_t* pstate = pvstate; char *ors = pstate->ors; char *ofs = pstate->ofs; if (pstate->plast_header_output != NULL) { if (!lrec_keys_equal_list(prec, pstate->plast_header_output)) { slls_free(pstate->plast_header_output); pstate->plast_header_output = NULL; if (pstate->num_header_lines_output > 0LL) fputs(ors, output_stream); } } if (pstate->plast_header_output == NULL) { int nf = 0; if (!pstate->headerless_csv_output) { for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) { if (nf > 0) fputs(ofs, output_stream); pstate->pquoted_output_func(output_stream, pe->key, pstate->ors, pstate->ofs, pstate->orslen, pstate->ofslen); nf++; } fputs(ors, output_stream); } pstate->plast_header_output = mlr_copy_keys_from_record(prec); pstate->num_header_lines_output++; } int nf = 0; for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) { if (nf > 0) fputs(ofs, output_stream); pstate->pquoted_output_func(output_stream, pe->value, pstate->ors, pstate->ofs, pstate->orslen, pstate->ofslen); nf++; } fputs(ors, output_stream); pstate->onr++; // See ../README.md for memory-management conventions lrec_free(prec); }
static void lrec_writer_csv_process(FILE* output_stream, lrec_t* prec, void* pvstate) { if (prec == NULL) return; lrec_writer_csv_state_t* pstate = pvstate; char ors = pstate->ors; char ofs = pstate->ofs; if (pstate->plast_header_output != NULL) { // xxx make a fcn to compare these w/o copy: put it in mixutil. if (!lrec_keys_equal_list(prec, pstate->plast_header_output)) { slls_free(pstate->plast_header_output); pstate->plast_header_output = NULL; if (pstate->num_header_lines_output > 0LL) fputc(ors, output_stream); } } if (pstate->plast_header_output == NULL) { int nf = 0; for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) { if (nf > 0) fputc(ofs, output_stream); fputs(pe->key, output_stream); nf++; } fputc(ors, output_stream); pstate->plast_header_output = mlr_copy_keys_from_record(prec); pstate->num_header_lines_output++; } int nf = 0; for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) { if (nf > 0) fputc(ofs, output_stream); fputs(pe->value, output_stream); nf++; } fputc(ors, output_stream); pstate->onr++; lrec_free(prec); // xxx cmt mem-mgmt }