📄 print.c
字号:
fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %d}} \\\\\n", record++); } if (opt_border >= 1) fputs("\\hline\n", fout); } latex_escaped_print(headers[i % col_count], fout); fputs(" & ", fout); latex_escaped_print(*ptr, fout); fputs(" \\\\\n", fout); } if (opt_border == 2) fputs("\\hline\n", fout); fputs("\\end{tabular}\n\n\\noindent ", fout); /* print footers */ if (footers && !opt_tuples_only) for (ptr = footers; *ptr; ptr++) { if (opt_numeric_locale) { char *my_cell = format_numeric_locale(*ptr); latex_escaped_print(my_cell, fout); free(my_cell); } else latex_escaped_print(*ptr, fout); fputs(" \\\\\n", fout); } fputc('\n', fout);}/*************************//* Troff -ms *//*************************/static voidtroff_ms_escaped_print(const char *in, FILE *fout){ const char *p; for (p = in; *p; p++) switch (*p) { case '\\': fputs("\\(rs", fout); break; default: fputc(*p, fout); }}static voidprint_troff_ms_text(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, const char *opt_align, bool opt_tuples_only, bool opt_numeric_locale, unsigned short int opt_border, FILE *fout){ unsigned int col_count = 0; unsigned int i; const char *const * ptr; /* print title */ if (!opt_tuples_only && title) { fputs(".LP\n.DS C\n", fout); troff_ms_escaped_print(title, fout); fputs("\n.DE\n", fout); } /* count columns */ for (ptr = headers; *ptr; ptr++) col_count++; /* begin environment and set alignments and borders */ fputs(".LP\n.TS\n", fout); if (opt_border == 2) fputs("center box;\n", fout); else fputs("center;\n", fout); for (i = 0; i < col_count; i++) { fputc(*(opt_align + i), fout); if (opt_border > 0 && i < col_count - 1) fputs(" | ", fout); } fputs(".\n", fout); /* print headers and count columns */ for (i = 0, ptr = headers; i < col_count; i++, ptr++) { if (!opt_tuples_only) { if (i != 0) fputc('\t', fout); fputs("\\fI", fout); troff_ms_escaped_print(*ptr, fout); fputs("\\fP", fout); } } if (!opt_tuples_only) fputs("\n_\n", fout); /* print cells */ for (i = 0, ptr = cells; *ptr; i++, ptr++) { if (opt_numeric_locale) { char *my_cell = format_numeric_locale(*ptr); troff_ms_escaped_print(my_cell, fout); free(my_cell); } else troff_ms_escaped_print(*ptr, fout); if ((i + 1) % col_count == 0) fputc('\n', fout); else fputc('\t', fout); } fputs(".TE\n.DS L\n", fout); /* print footers */ if (footers && !opt_tuples_only) for (ptr = footers; *ptr; ptr++) { troff_ms_escaped_print(*ptr, fout); fputc('\n', fout); } fputs(".DE\n", fout);}static voidprint_troff_ms_vertical(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, const char *opt_align, bool opt_tuples_only, bool opt_numeric_locale, unsigned short int opt_border, FILE *fout){ unsigned int col_count = 0; unsigned int i; const char *const * ptr; unsigned int record = 1; unsigned short current_format = 0; /* 0=none, 1=header, 2=body */ (void) opt_align; /* currently unused parameter */ /* print title */ if (!opt_tuples_only && title) { fputs(".LP\n.DS C\n", fout); troff_ms_escaped_print(title, fout); fputs("\n.DE\n", fout); } /* begin environment and set alignments and borders */ fputs(".LP\n.TS\n", fout); if (opt_border == 2) fputs("center box;\n", fout); else fputs("center;\n", fout); /* basic format */ if (opt_tuples_only) fputs("c l;\n", fout); /* count columns */ for (ptr = headers; *ptr; ptr++) col_count++; /* print records */ for (i = 0, ptr = cells; *ptr; i++, ptr++) { /* new record */ if (i % col_count == 0) { if (!opt_tuples_only) { if (current_format != 1) { if (opt_border == 2 && i > 0) fputs("_\n", fout); if (current_format != 0) fputs(".T&\n", fout); fputs("c s.\n", fout); current_format = 1; } fprintf(fout, "\\fIRecord %d\\fP\n", record++); } if (opt_border >= 1) fputs("_\n", fout); } if (!opt_tuples_only) { if (current_format != 2) { if (current_format != 0) fputs(".T&\n", fout); if (opt_border != 1) fputs("c l.\n", fout); else fputs("c | l.\n", fout); current_format = 2; } } troff_ms_escaped_print(headers[i % col_count], fout); fputc('\t', fout); if (opt_numeric_locale) { char *my_cell = format_numeric_locale(*ptr); troff_ms_escaped_print(my_cell, fout); free(my_cell); } else troff_ms_escaped_print(*ptr, fout); fputc('\n', fout); } fputs(".TE\n.DS L\n", fout); /* print footers */ if (footers && !opt_tuples_only) for (ptr = footers; *ptr; ptr++) { troff_ms_escaped_print(*ptr, fout); fputc('\n', fout); } fputs(".DE\n", fout);}/********************************//* Public functions *//********************************//* * PageOutput * * Tests if pager is needed and returns appropriate FILE pointer. */FILE *PageOutput(int lines, unsigned short int pager){ /* check whether we need / can / are supposed to use pager */ if (pager#ifndef WIN32 && isatty(fileno(stdin)) && isatty(fileno(stdout))#endif ) { const char *pagerprog;#ifdef TIOCGWINSZ int result; struct winsize screen_size; result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size); /* >= accounts for a one-line prompt */ if (result == -1 || lines >= screen_size.ws_row || pager > 1) {#endif pagerprog = getenv("PAGER"); if (!pagerprog) pagerprog = DEFAULT_PAGER;#ifndef WIN32 pqsignal(SIGPIPE, SIG_IGN);#endif return popen(pagerprog, "w");#ifdef TIOCGWINSZ }#endif } return stdout;}voidprintTable(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, const char *align, const printTableOpt *opt, FILE *fout, FILE *flog){ const char *default_footer[] = {NULL}; unsigned short int border = opt->border; FILE *output; bool use_expanded; if (opt->format == PRINT_NOTHING) return; if (!footers) footers = default_footer; if (opt->format != PRINT_HTML && border > 2) border = 2; /* * We only want to display the results in "expanded" format if this is a * normal (user-submitted) query, not a table we're printing for a slash * command. */ if (opt->expanded) use_expanded = true; else use_expanded = false; if (fout == stdout) { int col_count = 0, row_count = 0, lines; const char *const * ptr; /* rough estimate of columns and rows */ if (headers) for (ptr = headers; *ptr; ptr++) col_count++; if (cells) for (ptr = cells; *ptr; ptr++) row_count++; if (col_count > 0) row_count /= col_count; if (opt->expanded) lines = (col_count + 1) * row_count; else lines = row_count + 1; if (footers && !opt->tuples_only) for (ptr = footers; *ptr; ptr++) lines++; output = PageOutput(lines, opt->pager); } else output = fout; /* print the stuff */ if (flog) print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->encoding, flog); switch (opt->format) { case PRINT_UNALIGNED: if (use_expanded) print_unaligned_vertical(title, headers, cells, footers, align, opt->fieldSep, opt->recordSep, opt->tuples_only, opt->numericLocale, output); else print_unaligned_text(title, headers, cells, footers, align, opt->fieldSep, opt->recordSep, opt->tuples_only, opt->numericLocale, output); break; case PRINT_ALIGNED: if (use_expanded) print_aligned_vertical(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->encoding, output); else print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->encoding, output); break; case PRINT_HTML: if (use_expanded) print_html_vertical(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->tableAttr, output); else print_html_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->tableAttr, output); break; case PRINT_LATEX: if (use_expanded) print_latex_vertical(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, output); else print_latex_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, output); break; case PRINT_TROFF_MS: if (use_expanded) print_troff_ms_vertical(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, output); else print_troff_ms_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, output); break; default: fprintf(stderr, _("invalid output format (internal error): %d"), opt->format); exit(EXIT_FAILURE); } /* Only close if we used the pager */ if (fout == stdout && output != stdout) { pclose(output);#ifndef WIN32 pqsignal(SIGPIPE, SIG_DFL);#endif }}voidprintQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *flog){ int nfields; int ncells; const char **headers; const char **cells; char **footers; char *align; int i; /* extract headers */ nfields = PQnfields(result); headers = calloc(nfields + 1, sizeof(*headers)); if (!headers) { fprintf(stderr, _("out of memory\n")); exit(EXIT_FAILURE); } for (i = 0; i < nfields; i++) headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding); /* set cells */ ncells = PQntuples(result) * nfields; cells = calloc(ncells + 1, sizeof(*cells)); if (!cells) { fprintf(stderr, _("out of memory\n")); exit(EXIT_FAILURE); } for (i = 0; i < ncells; i++) { if (PQgetisnull(result, i / nfields, i % nfields)) cells[i] = opt->nullPrint ? opt->nullPrint : ""; else cells[i] = mbvalidate(PQgetvalue(result, i / nfields, i % nfields), opt->topt.encoding); } /* set footers */ if (opt->footers) footers = opt->footers; else if (!opt->topt.expanded && opt->default_footer) { footers = calloc(2, sizeof(*footers)); if (!footers) { fprintf(stderr, _("out of memory\n")); exit(EXIT_FAILURE); } footers[0] = pg_local_malloc(100); if (PQntuples(result) == 1) snprintf(footers[0], 100, _("(1 row)")); else snprintf(footers[0], 100, _("(%d rows)"), PQntuples(result)); } else footers = NULL; /* set alignment */ align = calloc(nfields + 1, sizeof(*align)); if (!align) { fprintf(stderr, _("out of memory\n")); exit(EXIT_FAILURE); } for (i = 0; i < nfields; i++) { Oid ftype = PQftype(result, i); if (ftype == 20 || /* int8 */ ftype == 21 || /* int2 */ ftype == 23 || /* int4 */ (ftype >= 26 && ftype <= 30) || /* ?id */ ftype == 700 || /* float4 */ ftype == 701 || /* float8 */ ftype == 790 || /* money */ ftype == 1700 /* numeric */ ) align[i] = 'r'; else align[i] = 'l'; } /* call table printer */ printTable(opt->title, headers, cells, (const char *const *) footers, align, &opt->topt, fout, flog); free(headers); free(cells); if (footers) { free(footers[0]); free(footers); } free(align);}voidsetDecimalLocale(void){ struct lconv *extlconv; extlconv = localeconv(); if (*extlconv->decimal_point) decimal_point = strdup(extlconv->decimal_point); else decimal_point = "."; /* SQL output standard */ if (*extlconv->grouping && atoi(extlconv->grouping) > 0) grouping = strdup(extlconv->grouping); else grouping = "3"; /* most common */ if (*extlconv->thousands_sep) thousands_sep = strdup(extlconv->thousands_sep); else if (*decimal_point != ',') thousands_sep = ","; else thousands_sep = ".";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -