*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.79 2005/10/27 13:34:47 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.79.2.1 2010/03/01 20:56:15 heikki Exp $
*/
#include "postgres_fe.h"
#include "common.h"
const char *const * cells, const char *const * footers,
const char *opt_align, const char *opt_fieldsep,
const char *opt_recordsep, bool opt_tuples_only,
- bool opt_numeric_locale, FILE *fout)
+ FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
fputs(opt_recordsep, fout);
need_recordsep = false;
}
- if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- {
- char *my_cell = format_numeric_locale(*ptr);
-
- fputs(my_cell, fout);
- free(my_cell);
- }
- else
- fputs(*ptr, fout);
+ fputs(*ptr, fout);
if ((i + 1) % col_count)
fputs(opt_fieldsep, fout);
const char *const * cells,
const char *const * footers, const char *opt_align,
const char *opt_fieldsep, const char *opt_recordsep,
- bool opt_tuples_only, bool opt_numeric_locale, FILE *fout)
+ bool opt_tuples_only, FILE *fout)
{
unsigned int col_count = 0;
unsigned int i;
fputs(headers[i % col_count], fout);
fputs(opt_fieldsep, fout);
- if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- {
- char *my_cell = format_numeric_locale(*ptr);
-
- fputs(my_cell, fout);
- free(my_cell);
- }
- else
- fputs(*ptr, fout);
+ fputs(*ptr, fout);
}
/* print footers */
static void
print_aligned_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,
+ const char *opt_align, bool opt_tuples_only,
unsigned short int opt_border, int encoding,
FILE *fout)
{
for (i = 0, ptr = cells; *ptr; ptr++, i++)
{
- int add_numeric_locale_len;
-
- if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- add_numeric_locale_len = additional_numeric_locale_len(*ptr);
- else
- add_numeric_locale_len = 0;
-
- tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding) + add_numeric_locale_len;
+ tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding);
if (tmp > widths[i % col_count])
widths[i % col_count] = tmp;
cell_w[i] = tmp;
/* content */
if (opt_align[i % col_count] == 'r')
- {
- if (opt_numeric_locale)
- {
- char *my_cell = format_numeric_locale(*ptr);
-
- fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", my_cell);
- free(my_cell);
- }
- else
- fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr);
- }
+ fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr);
else
{
if ((i + 1) % col_count == 0 && opt_border != 2)
print_aligned_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,
+ unsigned short int opt_border,
int encoding, FILE *fout)
{
unsigned int col_count = 0;
/* find longest data cell */
for (i = 0, ptr = cells; *ptr; ptr++, i++)
{
- int add_numeric_locale_len;
-
- if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- add_numeric_locale_len = additional_numeric_locale_len(*ptr);
- else
- add_numeric_locale_len = 0;
-
- tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding) + add_numeric_locale_len;
+ tmp = pg_wcswidth(*ptr, strlen(*ptr), encoding);
if (tmp > dwidth)
dwidth = tmp;
cell_w[i] = tmp;
else
fputs(" ", fout);
- if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- {
- char *my_cell = format_numeric_locale(*ptr);
-
- if (opt_border < 2)
- fprintf(fout, "%s\n", my_cell);
- else
- fprintf(fout, "%-s%*s |\n", my_cell, dwidth - cell_w[i], "");
- free(my_cell);
- }
+ if (opt_border < 2)
+ fprintf(fout, "%s\n", *ptr);
else
- {
- if (opt_border < 2)
- fprintf(fout, "%s\n", *ptr);
- else
- fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
- }
+ fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
}
if (opt_border == 2)
print_html_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,
+ unsigned short int opt_border,
const char *opt_table_attr, FILE *fout)
{
unsigned int col_count = 0;
/* is string only whitespace? */
if ((*ptr)[strspn(*ptr, " \t")] == '\0')
fputs(" ", fout);
- else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- {
- char *my_cell = format_numeric_locale(*ptr);
-
- html_escaped_print(my_cell, fout);
- free(my_cell);
- }
else
html_escaped_print(*ptr, fout);
print_html_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,
+ unsigned short int opt_border,
const char *opt_table_attr, FILE *fout)
{
unsigned int col_count = 0;
/* is string only whitespace? */
if ((*ptr)[strspn(*ptr, " \t")] == '\0')
fputs(" ", fout);
- else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
- {
- char *my_cell = format_numeric_locale(*ptr);
-
- html_escaped_print(my_cell, fout);
- free(my_cell);
- }
else
html_escaped_print(*ptr, fout);
print_latex_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,
+ unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
/* print cells */
for (i = 0, ptr = cells; *ptr; i++, 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);
+ latex_escaped_print(*ptr, fout);
if ((i + 1) % col_count == 0)
fputs(" \\\\\n", fout);
print_latex_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,
+ unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
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);
+ latex_escaped_print(*ptr, fout);
fputs(" \\\\\n", fout);
}
print_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,
+ unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
/* 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);
+ troff_ms_escaped_print(*ptr, fout);
if ((i + 1) % col_count == 0)
fputc('\n', fout);
print_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,
+ unsigned short int opt_border,
FILE *fout)
{
unsigned int col_count = 0;
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);
+ troff_ms_escaped_print(*ptr, fout);
fputc('\n', fout);
}
/* print the stuff */
if (flog)
- print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, opt->numericLocale, border, opt->encoding, flog);
+ print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, border, opt->encoding, flog);
switch (opt->format)
{
if (use_expanded)
print_unaligned_vertical(title, headers, cells, footers, align,
opt->fieldSep, opt->recordSep,
- opt->tuples_only, opt->numericLocale, output);
+ opt->tuples_only, output);
else
print_unaligned_text(title, headers, cells, footers, align,
opt->fieldSep, opt->recordSep,
- opt->tuples_only, opt->numericLocale, output);
+ opt->tuples_only, output);
break;
case PRINT_ALIGNED:
if (use_expanded)
print_aligned_vertical(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale, border,
+ opt->tuples_only, border,
opt->encoding, output);
else
print_aligned_text(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale,
+ opt->tuples_only,
border, opt->encoding, output);
break;
case PRINT_HTML:
if (use_expanded)
print_html_vertical(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale,
+ opt->tuples_only,
border, opt->tableAttr, output);
else
print_html_text(title, headers, cells, footers,
- align, opt->tuples_only, opt->numericLocale, border,
+ align, opt->tuples_only, border,
opt->tableAttr, output);
break;
case PRINT_LATEX:
if (use_expanded)
print_latex_vertical(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale,
+ opt->tuples_only,
border, output);
else
print_latex_text(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale,
+ opt->tuples_only,
border, output);
break;
case PRINT_TROFF_MS:
if (use_expanded)
print_troff_ms_vertical(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale,
+ opt->tuples_only,
border, output);
else
print_troff_ms_text(title, headers, cells, footers, align,
- opt->tuples_only, opt->numericLocale,
+ opt->tuples_only,
border, output);
break;
default:
int ncells;
const char **headers;
const char **cells;
+ bool *cellmustfree = NULL;
char **footers;
char *align;
int i;
for (i = 0; i < nfields; i++)
headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);
+ /* 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';
+ }
+
/* set cells */
ncells = PQntuples(result) * nfields;
cells = calloc(ncells + 1, sizeof(*cells));
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);
+
+ if (align[i % nfields] == 'r' && opt->topt.numericLocale)
+ {
+ cells[i] = format_numeric_locale(cells[i]);
+
+ /*
+ * format_numeric_locale() returns a malloc'd string,
+ * remember that it needs to be freed.
+ */
+ if (cellmustfree == NULL)
+ {
+ cellmustfree = calloc(ncells + 1, sizeof(bool));
+ if (!cellmustfree)
+ {
+ fprintf(stderr, _("out of memory\n"));
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ cellmustfree[i] = true;
+ }
+ }
}
/* set footers */
align, &opt->topt, fout, flog);
free(headers);
+ if (cellmustfree)
+ {
+ for (i = 0; i < ncells; i++)
+ {
+ if (cellmustfree[i])
+ free((char *) cells[i]);
+ }
+ free(cellmustfree);
+ }
free(cells);
if (footers)
{