#!/usr/bin/perl
# Original code: tbl2lst,v 2.7 1993/03/29 13:34:46 hobbs
#
$RCS_ID = '$Id: nsq-t2l,v 1.0 1998/05/14 11:09:56 carlos Exp $' ;
$0 =~ s-.*/-- ;
$HelpInfo = <<EOH ;

	    NoSQL operator: $0

Usage:  $0  [options]

Options:
    -edit    Edit option. Used by nsq-ed.
    -help    Print this help info.
    -lN      Line length of N is to be used.

Converts an rdbtable to "list" format. Long data fields are folded.

This NoSQL operator reads an rdbtable from STDIN and writes the "list" format
to STDOUT.  Options may be abbreviated.

$RCS_ID

			----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
			----------------------

EOH
$: = "\n " ;    # default line break list (white space)
$LEN = 80 ;	# default line length
$frm = "format STDOUT = \n^" ;
while ( $ARGV[0] =~ /^-/ ){			# Get args
    $_ = shift ;
    if( /^-e.*/ ){ $EDT++ ; next ; }
    if( /^-h.*/ ){ print $HelpInfo ; exit 1 ; }
    if( /^-l(\d+)/ ){ $LEN = $1 ; next ; }
    die "\nBad arg: $_\n", "For help type \"$0 -help\".\n" ; }
$lim = $LEN - 16 ;		# max line space for long data fields
$frm .= '<' x ($lim -1) ;
$frm .= "\n\$x\n.\n" ;
eval $frm ;

while( <STDIN> ){  		# read col names
    if( $EDT && /^\.\.>>>/ ){ print $_ ; next ; } # control line
    if( /^\s*#/ ){ print ; next ; } 		# comment 
    last ; }
chop ;
@H = split( /\t/, $_ ) ;			# column names
$z = 13 ;	# default size for printf of list format
for (@H){ $z = length($_) if length($_) > $z ; } # get longest
$con1 = "%$z" . "s | " ; # printf control stg
$HH = $#H ;
for (1..10){ push( @H, "-DATA-ERROR-" ) ; }
while(<STDIN>){			# the data & definitions
    if( $EDT && /^\.\.>>>/ ){ $conl = $_ ; next ; }
    &conv ; }
sub conv {			# convert, output the record
    print "\n" ;		# blank line separates each record
    print $conl if $conl ; $conl = "" ;
    chop ;
    @F = split( /\t/, $_ ) ;
    for( $i=0 ; $i <= $HH || $i <= $#F ; $i++ ){
	printf( $con1, $H[$i] ) ;
	if( length($F[$i]) <= $lim ){
	    print $F[$i], "\n" ; }
	else{
	    $init = 0 ;
	    $x = $F[$i] ;
	    do {
		print "\t\t" if $init++ ;
		write ;
	    } while( $x ) ;
	}
    }
}
