#****************************************************************************
#  ##   ##         #####   #####  ##     **       NoSQL RDBMS - islist      *
#  ###  ##        ####### ####### ##     **        $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.                                *
#****************************************************************************
#
#  Checks that a file has a valid NoSQL 'list' format.
#
#  This NoSQL operator reads a file from STDIN and returns an exit
#  code equal 0 if the file has a valid NoSQL 'list' structure, or 
#  255 otherwise. If '--edit' is specified, then a message is
#  printed to STDOUT telling the No. of the first failing row.
#
#  Usage: nosql islist [-v|--verbose] [-e|--edit] < list
#
########################################################################

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

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

  # Get command line arguments.
  split( __nosql_args, args, " " )

  while ( args[++i] != NULL )
  {
	if ( args[i] == "-v" || args[i] == "--verbose" ) verbose = 1
	else if ( args[i] == "-e" || args[i] == "--edit" ) edit = 1
  }
}

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

# First record must be a newline, with any No. of tabs and/or spaces.
NR == 1 \
{
  if ( $0 !~ /^[ \t]*$/ )
  {
	if ( verbose ) 
	  print "nosql islist: first line must be" \
		" a newline with no data" > "/dev/stderr"
	errors = 1
	if ( edit ) print "nosql islist: " NR
    exit 255
  }
}

$0 ~ /^[ \t]*$/ \
{						# This is a separator line.
  if ( ++col_num < 3 ) row_size = num_cols
  else if ( num_cols != row_size )
  {
	if ( verbose ) print "nosql islist: the input block ending at line " NR \
	  " has a different No. of rows than its predecessors" > "/dev/stderr"

	errors = 1
	if ( edit ) print "nosql islist: " NR
	exit 255
  }
  num_cols = 0
}

$0 ~ /^[A-Za-z]/ { num_cols++ }		# This is a field name.

# Last record must be a newline.
END \
{
  if ( NR == 0 ) errors=1
  if ( ! errors )
  {
    if ( $0 !~ /^[ \t]*$/ )
	{
	  if ( verbose ) 
	    print "nosql islist: last line must be" \
		  "a newline with no data" > "/dev/stderr"

	  errors = 1
	  if ( edit ) print "nosql islist: " NR
	  exit 255
	}
  }
  if ( verbose )
  {
	if ( errors ) print "nosql islist: list not ok" > "/dev/stderr"
	else print "nosql islist: list ok" > "/dev/stderr"
  }
}

