#!/usr/local/bin/perl -w

#
# dbcolize
# Copyright (C) 1997-1998 by John Heidemann <johnh@isi.edu>
# $Id: dbcolize,v 1.11 1999/11/23 02:04:29 johnh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblibdir for details.
#
sub usage {
    print <<END;
usage: $0 [-e EmptyValue] [-d]

    Convert the list-based format back to tabular format.

Options:
    -e value	Give VALUE for unspecified columns.

Sample input:
#L      experiment      mean    stddev  pct_rsd conf_range      conf_low       conf_high        conf_pct        sum     sum_squared     min     max     n
experiment:  ufs_mab_sys
mean:        37.25
stddev:      0.070711
pct_rsd:     0.18983
conf_range:  0.6353
conf_low:    36.615
conf_high:   37.885
conf_pct:    0.95
sum:         74.5
sum_squared: 2775.1
min:         37.2
max:         37.3
n:           2

experiment:  ufs_rcp_real
mean:        271.2
stddev:      9.4752
pct_rsd:     3.4938
conf_range:  85.13
conf_low:    186.07
conf_high:   356.33
conf_pct:    0.95
sum:         542.4
sum_squared: 1.4719e+05
min:         264.5
max:         277.9
n:           2

#  | /home/johnh/BIN/DB/dbmultistats experiment duration
#  | /home/johnh/BIN/DB/dblistize 

Sample command:
cat data.jdb | dbcolize

Sample output:
#h      experiment      mean    stddev  pct_rsd conf_range      conf_low       conf_high        conf_pct        sum     sum_squared     min     max     n
ufs_mab_sys     37.25   0.070711        0.18983 0.6353  36.615  37.885  0.95   74.5     2775.1  37.2    37.3    2
ufs_rcp_real    271.2   9.4752  3.4938  85.13   186.07  356.33  0.95    542.4  1.4719e+05       264.5   277.9   2
#  | /home/johnh/BIN/DB/dbmultistats experiment duration
#  | /home/johnh/BIN/DB/dblistize 
#  | /home/johnh/BIN/DB/dbcolize 

Related programs:
    dblistize

END
    exit 1;
}

BEGIN {
    $dblibdir = "/usr/local/lib/jdb";
    push(@INC, $dblibdir);
}
use DbGetopt;
require "$dblibdir/dblib.pl";

$debug = 0;
$empty_value = "-";
@orig_argv = @ARGV;
my($prog) = &progname;
my($dbopts) = new DbGetopt("de:?", \@ARGV);
my($ch);
while ($dbopts->getopt) {
    $ch = $dbopts->opt;
    if ($ch eq 'd') {
	$debug++;
    } elsif ($ch eq 'e') {
	$empty_value = $dbopts->optarg;
    } else {
	&usage;
    };
};
&usage if ($#ARGV != -1);


&readprocess_header($list_headertag);

&write_header();
&begin_row();
while (<STDIN>) {
    if (/^\s*$/) {
	&end_row();
	&begin_row();
	next;
    };
    &pass_comments && next;
    chomp;
    my($key, $value) = /^([^:]+):\s+(.*)$/;
    die("$prog: unparsable line $_\n") if (!defined($key));
    die("$prog: contents of line contain separator: <$_>\n") if ($value =~ /$fsre/);
    $value = $empty_value if (!defined($value) || $value eq '');
    die ("$prog: unknown column <$key>.\n") if (!defined($colnametonum{$key}));
    $something_set++;
    $f[$colnametonum{$key}] = $value;
};
&end_row();

sub begin_row {
    @f = ($empty_value) x ($#colnames+1);
    $something_set = 0;
}

sub end_row {
    return if (!$something_set);
    &write_cols;
}

print "#  | $prog ", join(" ", @orig_argv), "\n";
exit 0;

if (0) {
    my $x;
    $x = $list_headertag = $colnames[0] = $fsre;
}
