#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - preprint     *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
#  Pre-processes a table in a way similar to 'justify', but no blanks are
#  added around fields and the column definition line is not turned into
#  dashes. This is used to pre-process a table that is to be fed into the
#  'print' operator.
#
#  Warning: if the input list contains duplicated entries, i.e. multiple
#  rows with the same label (column name), the resulting table may
#  be broken.
#
#  This NoSQL operator reads a table from STDIN and writes it to STDOUT
#  in a format suitable for the 'print' operator.
#
#  Note: this operator must be preceded by a pass through 'addtypes'.
#
##########################################################################

########################################################################
# BEGIN block
########################################################################

BEGIN { NULL = ""; FS = OFS = "\t"; }

########################################################################
# Main loop
########################################################################

NR == 1 \
{
  for ( i = 1; i <= NF; i++ )
  {
	# Set default column widths and types.
	col_w[i] = length( $i )
	col_t[i] = "S"
  }
  split( $0, c_names ) ; next
}

NR == 2 \
{
  # Handle widths and column names.
  for ( i = 1; i <= NF; i++ )
  {
	tmp = $i ; sub( /[A-Za-z].*$/, NULL, tmp )
	# Make tmp a number. This is mandatory.
	tmp += 0
	if ( tmp > col_w[i] ) col_w[i] = tmp
	tmp = $i ; gsub( /[^A-Za-z]+/, NULL, tmp )
	precision[i] = $i; float[i] = sub( /^.*\./, NULL, precision[i] )
	precision[i] += 0
	if ( tmp != "S" ) col_t[i] = "N"
	if ( i == 1 ) printf( "%s", c_names[i] )
	else { printf( OFS ) ; printf( "%s", c_names[i] ) }
  }
  printf( "\n" )

  # Handle column definiton line..
  for ( i = 1; i <= NF; i++ )
  {
	if ( i == 1 ) printf( "%s", col_w[i] col_t[i] )
	else { printf( OFS ) ; printf( "%s", col_w[i] col_t[i] ) }
  }
  printf( "\n" )
}

NR > 2 \
{
  # Handle table body.
  for ( i = 1; i <= NF; i++ )
  {
	if ( float[i] ) j_mode = "%." precision[i] "f"
	else j_mode = "%s"
	if ( i == 1 ) printf( j_mode, $i )
	else { printf( OFS ) ; printf( j_mode, $i ) }
  }
  printf( "\n" )
}

