#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - trim       *
#  ###  ##        ####### ####### ##     **        $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.                                *
#****************************************************************************
#
#  Strip surrounding blanks from table columns (including header and
#  dashline).
#
#  Removes surrounding blanks from column values. If option '-m' is 
#  specified then multiple blanks between the words are stripped
#  to one single blank. If option '-e' is specified then empty rows
#  are removed from the input table.
#
#  This NoSQL operator reads a table STDIN and writes the modified
#  table to STDOUT.
#
########################################################################

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

BEGIN \
{
  NULL = ""; FS = OFS = "\t";
  split( __nosql_args, args, " " )

  while ( args[++i] != NULL )
  {
	if ( args[i] == "-m" || args[i] == "--strip-all" ) strip_middle = 1
	else
	{
	  if ( args[i] == "-e" || args[i] == "--empty-rows" ) rm_empty = 1
	}
  }
}

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

rm_empty == 1 && $0 ~ /^[ \t]*$/ { next }

# Reduce middle blanks to one single blank if requested.
strip_middle == 1 { gsub( / +/, " " ) }

{ sub( /^ */, NULL ); sub( / *$/, NULL ); gsub( / *\t */, OFS ); print }

