// ================================================================ static int popen_file_mlr_getsdelim(char* reader, char* filename, int do_write) { char* command = mlr_malloc_or_die(strlen(reader) + 1 + strlen(filename) + 1); strcpy(command, reader); strcat(command, " "); strcat(command, filename); FILE* fp = popen(command, "r"); if (fp == NULL) { perror("popen"); exit(1); } char* irs = "\r\n"; int irslen = strlen(irs); int bc = 0; while (1) { char* line = mlr_get_sline(fp, irs, irslen); if (line == NULL) break; //bc += linelen; // available by API, but make a fair comparison bc += strlen(line); if (do_write) { fputs(line, stdout); fputc('\n', stdout); } free(line); } pclose(fp); return bc; }
static lrec_t* lrec_reader_stdio_dkvp_process_multi_irs_multi_others(void* pvstate, void* pvhandle, context_t* pctx) { lrec_reader_stdio_dkvp_state_t* pstate = pvstate; FILE* input_stream = pvhandle; char* line = mlr_get_sline(input_stream, pstate->irs, pstate->irslen); if (line == NULL) return NULL; else return lrec_parse_stdio_dkvp_multi_sep(line, pstate->ifs, pstate->ips, pstate->ifslen, pstate->ipslen, pstate->allow_repeat_ifs); }
// ================================================================ static int read_file_mlr_getsdelim(char* filename, int do_write) { FILE* fp = fopen_or_die(filename); char* irs = "\r\n"; int irslen = strlen(irs); int bc = 0; while (1) { char* line = mlr_get_sline(fp, irs, irslen); if (line == NULL) break; //bc += linelen; // available by API, but make a fair comparison bc += strlen(line); if (do_write) { fputs(line, stdout); fputc('\n', stdout); } free(line); } fclose(fp); return bc; }
// ---------------------------------------------------------------- static lrec_t* lrec_reader_stdio_xtab_process(void* pvstate, void* pvhandle, context_t* pctx) { FILE* input_stream = pvhandle; lrec_reader_stdio_xtab_state_t* pstate = pvstate; if (pstate->at_eof) return NULL; slls_t* pxtab_lines = slls_alloc(); while (TRUE) { char* line = (pstate->ifslen == 1) ? mlr_get_cline(input_stream, pstate->ifs[0]) : mlr_get_sline(input_stream, pstate->ifs, pstate->ifslen); if (line == NULL) { // EOF // EOF or blank line terminates the stanza. pstate->at_eof = TRUE; if (pxtab_lines->length == 0) { slls_free(pxtab_lines); return NULL; } else { return (pstate->ipslen == 1) ? lrec_parse_stdio_xtab_single_ips(pxtab_lines, pstate->ips[0], pstate->allow_repeat_ips) : lrec_parse_stdio_xtab_multi_ips(pxtab_lines, pstate->ips, pstate->ipslen, pstate->allow_repeat_ips); } } else if (*line == '\0') { free(line); if (pxtab_lines->length > 0) { return (pstate->ipslen == 1) ? lrec_parse_stdio_xtab_single_ips(pxtab_lines, pstate->ips[0], pstate->allow_repeat_ips) : lrec_parse_stdio_xtab_multi_ips(pxtab_lines, pstate->ips, pstate->ipslen, pstate->allow_repeat_ips); } } else { slls_add_with_free(pxtab_lines, line); } } }
// ---------------------------------------------------------------- static lrec_t* lrec_reader_stdio_csvlite_process(void* pvstate, void* pvhandle, context_t* pctx) { FILE* input_stream = pvhandle; lrec_reader_stdio_csvlite_state_t* pstate = pvstate; while (TRUE) { if (pstate->expect_header_line_next) { while (TRUE) { char* hline = (pstate->irslen == 1) ? mlr_get_cline(input_stream, pstate->irs[0]) : mlr_get_sline(input_stream, pstate->irs, pstate->irslen); if (hline == NULL) // EOF return NULL; pstate->ilno++; slls_t* pheader_fields = (pstate->ifslen == 1) ? split_csvlite_header_line_single_ifs(hline, pstate->ifs[0], pstate->allow_repeat_ifs) : split_csvlite_header_line_multi_ifs(hline, pstate->ifs, pstate->ifslen, pstate->allow_repeat_ifs); if (pheader_fields->length == 0) { pstate->expect_header_line_next = TRUE; if (pstate->pheader_keeper != NULL) { pstate->pheader_keeper = NULL; } } else { for (sllse_t* pe = pheader_fields->phead; pe != NULL; pe = pe->pnext) { if (*pe->value == 0) { fprintf(stderr, "%s: unacceptable empty CSV key at file \"%s\" line %lld.\n", MLR_GLOBALS.argv0, pctx->filename, pstate->ilno); exit(1); } } pstate->expect_header_line_next = FALSE; pstate->pheader_keeper = lhmslv_get(pstate->pheader_keepers, pheader_fields); if (pstate->pheader_keeper == NULL) { pstate->pheader_keeper = header_keeper_alloc(hline, pheader_fields); lhmslv_put(pstate->pheader_keepers, pheader_fields, pstate->pheader_keeper, NO_FREE); // freed by header-keeper } else { // Re-use the header-keeper in the header cache slls_free(pheader_fields); free(hline); } break; } } } char* line = (pstate->irslen == 1) ? mlr_get_cline(input_stream, pstate->irs[0]) : mlr_get_sline(input_stream, pstate->irs, pstate->irslen); if (line == NULL) // EOF return NULL; pstate->ilno++; if (!*line) { if (pstate->pheader_keeper != NULL) { pstate->pheader_keeper = NULL; pstate->expect_header_line_next = TRUE; free(line); continue; } } else { pstate->ifnr++; if (pstate->ifslen == 1) { return pstate->use_implicit_header ? lrec_parse_stdio_csvlite_data_line_single_ifs_implicit_header( pstate->pheader_keeper, pctx->filename, pstate->ilno, line, pstate->ifs[0], pstate->allow_repeat_ifs) : lrec_parse_stdio_csvlite_data_line_single_ifs(pstate->pheader_keeper, pctx->filename, pstate->ilno, line, pstate->ifs[0], pstate->allow_repeat_ifs); } else { return pstate->use_implicit_header ? lrec_parse_stdio_csvlite_data_line_multi_ifs_implicit_header( pstate->pheader_keeper, pctx->filename, pstate->ilno, line, pstate->ifs, pstate->ifslen, pstate->allow_repeat_ifs) : lrec_parse_stdio_csvlite_data_line_multi_ifs(pstate->pheader_keeper, pctx->filename, pstate->ilno, line, pstate->ifs, pstate->ifslen, pstate->allow_repeat_ifs); } } } }