*** pgsql/src/bin/psql/print.c~	Wed Aug  1 20:44:54 2001
--- pgsql/src/bin/psql/print.c	Wed Sep 26 19:30:42 2001
***************
*** 33,38 ****
--- 33,41 ----
  #include <termios.h>
  #endif
  
+ #ifdef MULTIBYTE
+ #include "pg_mb_utf8.h"
+ #endif
  
  /*************************/
  /* Unaligned text		 */
***************
*** 213,218 ****
--- 216,227 ----
  				   FILE *fout)
  {
  	unsigned int col_count = 0;
+ 
+ #ifdef MULTIBYTE
+ 	unsigned int cell_count = 0;
+ 	unsigned int *head_w, *cell_w;
+ #endif
+ 
  	unsigned int i,
  				tmp;
  	unsigned int *widths,
***************
*** 230,244 ****
  		exit(EXIT_FAILURE);
  	}
  
  	/* calc column widths */
! 	for (i = 0; i < col_count; i++)
! 		if ((tmp = strlen(headers[i])) > widths[i])
  			widths[i] = tmp;	/* don't wanna call strlen twice */
! 
! 	for (i = 0, ptr = cells; *ptr; ptr++, i++)
! 		if ((tmp = strlen(*ptr)) > widths[i % col_count])
  			widths[i % col_count] = tmp;
! 
  	if (opt_border == 0)
  		total_w = col_count - 1;
  	else if (opt_border == 1)
--- 239,289 ----
  		exit(EXIT_FAILURE);
  	}
  
+ #ifdef MULTIBYTE
+ 	head_w = calloc(col_count, sizeof(*head_w));
+ 	if (!head_w) {
+ 	  perror("calloc");
+ 	  exit(EXIT_FAILURE);
+ 	}
+ 
+ 	/* count rows */
+ 	for (ptr = cells; *ptr; ptr++) {
+ 	  cell_count++;
+ 	}
+ 
+ 	cell_w = calloc(cell_count, sizeof(*cell_w));
+ 	if (!cell_w) {
+ 	  perror("calloc");
+ 	  exit(EXIT_FAILURE);
+ 	}
+ #endif
+ 	
+ 
  	/* calc column widths */
! 	for (i = 0; i < col_count; i++) {
! #ifdef MULTIBYTE
! 	    if ((tmp = pg_mb_utfs_width(headers[i], PG_MB_UTF_TRUNC)) > widths[i]) {
  			widths[i] = tmp;	/* don't wanna call strlen twice */
! 		}
! 		head_w[i] = tmp;
! #else
! 		if ((tmp = strlen(headers[i])) > widths[i]) {
! 			widths[i] = tmp;	/* don't wanna call strlen twice */
! 	    }
! #endif
! 	}
! 	for (i = 0, ptr = cells; *ptr; ptr++, i++) {
! #ifdef MULTIBYTE
! 	    if ((tmp = pg_mb_utfs_width(*ptr, PG_MB_UTF_TRUNC)) > widths[i % col_count]) {
  			widths[i % col_count] = tmp;
! 		}
! 		cell_w[i] = tmp;
! #else
! 		if ((tmp = strlen(*ptr)) > widths[i % col_count]) {
! 			widths[i % col_count] = tmp;
! 		}
! #endif
! 	}
  	if (opt_border == 0)
  		total_w = col_count - 1;
  	else if (opt_border == 1)
***************
*** 252,261 ****
  	/* print title */
  	if (title && !opt_barebones)
  	{
  		if (strlen(title) >= total_w)
  			fprintf(fout, "%s\n", title);
  		else
! 			fprintf(fout, "%-*s%s\n", (int) (total_w - strlen(title)) / 2, "", title);
  	}
  
  	/* print headers */
--- 297,315 ----
  	/* print title */
  	if (title && !opt_barebones)
  	{
+ #ifdef MULTIBYTE
+ 	    int tlen;
+ 		if ((tlen = pg_mb_utfs_width(title, PG_MB_UTF_TRUNC)) >= total_w)
+ 			fprintf(fout, "%s\n", title);
+ 		else
+ 			fprintf(fout, "%-*s%s\n", (int) (total_w - tlen) / 2, "", title);
+ #else
  		if (strlen(title) >= total_w)
  			fprintf(fout, "%s\n", title);
  		else
! 			fprintf(fout, "%-*s%s\n", (int) (total_w - strlen(title)) / 2, "",
! 					title);
! #endif
  	}
  
  	/* print headers */
***************
*** 271,280 ****
  
  		for (i = 0; i < col_count; i++)
  		{
  			/* centered */
! 			fprintf(fout, "%-*s%s%-*s",
! 				 (int) floor((widths[i] - strlen(headers[i])) / 2.0), "",
! 					headers[i], (int) ceil((widths[i] - strlen(headers[i])) / 2.0), "");
  
  			if (i < col_count - 1)
  			{
--- 325,340 ----
  
  		for (i = 0; i < col_count; i++)
  		{
+ 		    int nbspace;
+ #ifdef MULTIBYTE
+ 		    nbspace = widths[i] - head_w[i];
+ #else
+ 		    nbspace = widths[i] - strlen(headers[i]);
+ #endif
+ 
  			/* centered */
! 			fprintf(fout, "%-*s%s%-*s", 
! 					nbspace / 2, "", headers[i], (nbspace+1) / 2, "");
  
  			if (i < col_count - 1)
  			{
***************
*** 307,320 ****
  		}
  
  		/* content */
! 		if (opt_align[(i) % col_count] == 'r')
  			fprintf(fout, "%*s", widths[i % col_count], cells[i]);
  		else
  		{
  			if ((i + 1) % col_count == 0 && opt_border != 2)
  				fputs(cells[i], fout);
! 			else
  				fprintf(fout, "%-*s", widths[i % col_count], cells[i]);
  		}
  
  		/* divider */
--- 367,392 ----
  		}
  
  		/* content */
! 		if (opt_align[(i) % col_count] == 'r') {
! #ifdef MULTIBYTE
! 		  fprintf(fout, "%*s%s",
! 				  widths[i % col_count] - cell_w[i], "", cells[i] );
! #else
  			fprintf(fout, "%*s", widths[i % col_count], cells[i]);
+ #endif
+ 		}
  		else
  		{
  			if ((i + 1) % col_count == 0 && opt_border != 2)
  				fputs(cells[i], fout);
! 			else {
! #ifdef MULTIBYTE
! 			  fprintf(fout, "%-s%*s", cells[i], 
! 					  widths[i % col_count] - cell_w[i], "" );
! #else
  				fprintf(fout, "%-*s", widths[i % col_count], cells[i]);
+ #endif
+ 			}
  		}
  
  		/* divider */
***************
*** 345,350 ****
--- 417,426 ----
  	fputc('\n', fout);
  
  	/* clean up */
+ #ifdef MULTIBYTE
+ 	free(cell_w);
+ 	free(head_w);
+ #endif
  	free(widths);
  }
  
***************
*** 364,369 ****
--- 440,449 ----
  				hwidth = 0,
  				dwidth = 0;
  	char	   *divider;
+ #ifdef MULTIBYTE
+ 	unsigned int cell_count = 0;
+ 	unsigned int *cell_w,*head_w;
+ #endif
  
  	if (cells[0] == NULL)
  	{
***************
*** 371,376 ****
--- 451,490 ----
  		return;
  	}
  
+ #ifdef MULTIBYTE
+ 	/* pre-count headers */
+ 	for (ptr = headers; *ptr; ptr++) {
+ 	  col_count++;
+ 	}
+ 	head_w = calloc(col_count, sizeof(*head_w));
+ 	if (!head_w) {
+ 	  perror("calloc");
+ 	  exit(EXIT_FAILURE);
+ 	}
+ 	for (i = 0; i < col_count; i++)
+ 	{
+ 		if ((tmp = pg_mb_utfs_width(headers[i], PG_MB_UTF_TRUNC)) > hwidth)
+ 			hwidth = tmp;		/* don't wanna call strlen twice */
+ 		head_w[i] = tmp;
+ 	}
+ 	for (ptr = cells; *ptr; ptr++) {
+ 	  cell_count++;
+ 	}
+ 
+ 	cell_w = calloc(cell_count, sizeof(*cell_w));
+ 	if (!cell_w) {
+ 	  perror("calloc");
+ 	  exit(EXIT_FAILURE);
+ 	}	
+ 
+ 	/* find longest data cell */
+ 	for (i = 0, ptr = cells; *ptr; ptr++, i++) {
+ 	  if ((tmp = pg_mb_utfs_width(*ptr, PG_MB_UTF_TRUNC)) > dwidth) {
+ 			dwidth = tmp;
+ 	  }
+ 	  cell_w[i] = tmp;
+ 	}
+ #else
  	/* count columns and find longest header */
  	for (ptr = headers; *ptr; ptr++)
  	{
***************
*** 380,388 ****
  	}
  
  	/* find longest data cell */
! 	for (ptr = cells; *ptr; ptr++)
  		if ((tmp = strlen(*ptr)) > dwidth)
  			dwidth = tmp;
  
  	/* print title */
  	if (!opt_barebones && title)
--- 494,504 ----
  	}
  
  	/* find longest data cell */
! 	for (ptr = cells; *ptr; ptr++) {
  		if ((tmp = strlen(*ptr)) > dwidth)
  			dwidth = tmp;
+ 	}
+ #endif
  
  	/* print title */
  	if (!opt_barebones && title)
***************
*** 456,462 ****
--- 572,583 ----
  
  		if (opt_border == 2)
  			fputs("| ", fout);
+ #if MULTIBYTE
+ 		fprintf(fout, "%-s%*s", headers[i % col_count],
+ 				hwidth - head_w[i % col_count], "");
+ #else
  		fprintf(fout, "%-*s", hwidth, headers[i % col_count]);
+ #endif
  
  		if (opt_border > 0)
  			fputs(" | ", fout);
***************
*** 465,472 ****
  
  		if (opt_border < 2)
  			fprintf(fout, "%s\n", *ptr);
! 		else
  			fprintf(fout, "%-*s |\n", dwidth, *ptr);
  	}
  
  	if (opt_border == 2)
--- 586,598 ----
  
  		if (opt_border < 2)
  			fprintf(fout, "%s\n", *ptr);
! 		else {
! #ifdef MULTIBYTE
! 			fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
! #else
  			fprintf(fout, "%-*s |\n", dwidth, *ptr);
+ #endif
+ 		}
  	}
  
  	if (opt_border == 2)
***************
*** 485,490 ****
--- 611,620 ----
  
  	fputc('\n', fout);
  	free(divider);
+ 
+ #ifdef MULTIBYTE
+ 	free(cell_w);
+ #endif
  }
  
  
