Fix some signed-vs-unsigned-int issues; make print_aligned_vertical
authorTom Lane
Fri, 4 Apr 2003 15:48:38 +0000 (15:48 +0000)
committerTom Lane
Fri, 4 Apr 2003 15:48:38 +0000 (15:48 +0000)
safe for zero-column tables.

src/bin/psql/print.c

index 7ac365647d5c3bab55f0f39b71faf4a008963e7e..24c4614d1c4f99b4d8ecc22cddf6f6e0dccd0e1a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.36 2003/03/18 22:15:44 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.37 2003/04/04 15:48:38 tgl Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -283,13 +283,11 @@ print_aligned_text(const char *title, const char *const * headers,
    /* print title */
    if (title && !opt_barebones)
    {
-       int         tlen;
-
-       tlen = pg_wcswidth((unsigned char *) title, strlen(title), encoding);
-       if (tlen >= (int) total_w)
+       tmp = pg_wcswidth((unsigned char *) title, strlen(title), encoding);
+       if (tmp >= total_w)
            fprintf(fout, "%s\n", title);
        else
-           fprintf(fout, "%-*s%s\n", ((int) total_w - tlen) / 2, "", title);
+           fprintf(fout, "%-*s%s\n", (total_w - tmp) / 2, "", title);
    }
 
    /* print headers */
@@ -305,7 +303,7 @@ print_aligned_text(const char *title, const char *const * headers,
 
        for (i = 0; i < col_count; i++)
        {
-           int         nbspace;
+           unsigned int        nbspace;
 
            nbspace = widths[i] - head_w[i];
 
@@ -420,18 +418,27 @@ print_aligned_vertical(const char *title, const char *const * headers,
    /* count headers and find longest one */
    for (ptr = headers; *ptr; ptr++)
        col_count++;
-   head_w = calloc(col_count, sizeof(*head_w));
-   if (!head_w)
+   if (col_count > 0)
    {
-       perror("calloc");
-       exit(EXIT_FAILURE);
+       head_w = calloc(col_count, sizeof(*head_w));
+       if (!head_w)
+       {
+           perror("calloc");
+           exit(EXIT_FAILURE);
+       }
    }
+   else
+       head_w = NULL;
+
    for (i = 0; i < col_count; i++)
    {
-       if ((tmp = pg_wcswidth((unsigned char *) headers[i], strlen(headers[i]), encoding)) > hwidth)
+       tmp = pg_wcswidth((unsigned char *) headers[i], strlen(headers[i]), encoding);
+       if (tmp > hwidth)
            hwidth = tmp;
        head_w[i] = tmp;
    }
+
+   /* Count cells, find their lengths */
    for (ptr = cells; *ptr; ptr++)
        cell_count++;
 
@@ -445,12 +452,13 @@ print_aligned_vertical(const char *title, const char *const * headers,
        }
    }
    else
-       cell_w = 0;
+       cell_w = NULL;
 
    /* find longest data cell */
    for (i = 0, ptr = cells; *ptr; ptr++, i++)
    {
-       if ((tmp = pg_wcswidth((unsigned char *) *ptr, strlen(*ptr), encoding)) > dwidth)
+       tmp = pg_wcswidth((unsigned char *) *ptr, strlen(*ptr), encoding);
+       if (tmp > dwidth)
            dwidth = tmp;
        cell_w[i] = tmp;
    }
@@ -480,7 +488,6 @@ print_aligned_vertical(const char *title, const char *const * headers,
    if (opt_border == 2)
        strcat(divider, "-+");
 
-
    /* print records */
    for (i = 0, ptr = cells; *ptr; i++, ptr++)
    {
@@ -544,7 +551,6 @@ print_aligned_vertical(const char *title, const char *const * headers,
    if (opt_border == 2)
        fprintf(fout, "%s\n", divider);
 
-
    /* print footers */
 
    if (!opt_barebones && footers && *footers)