#!/usr/bin/perl -w
# (C) Jochen Puchalla 2005

use Spreadsheet::WriteExcel;

$table = 0;

    # The input record separator is defined by Perl global 
    # variable $/.  It can be anything, including multiple 
    # characters.  Normally it is "\n", newline.  Here, we
    # say there is no record separator, so the whole file
    # is read as one long record, newlines included.
#    undef $/;

    # Suppose this program was invoked with the command
    #     hystsep ax.dat bx.dat cx.dat
    # Then builtin list @ARGV would contain three elments 
    # ('ax.dat', 'bx.dat', 'cx.dat')
    # These could be accessed as $ARGV[0] $ARGV[1] $ARGV[2] 

foreach $file (@ARGV) 
{
	if (! open(INPUT,"<$file") ) 
	{
		print STDERR "Can't open input file $file\n";
		next;
	}
	$filecore=$file;
	$filecore =~ s/.dat$//g;
    
	# Create a new Excel workbook
	my $workbook = Spreadsheet::WriteExcel->new("$filecore.xls");
	# Add some worksheets
	my $summary = $workbook->addworksheet("Summary");
	my $header = $workbook->addworksheet("Header");
	
	while ($line=<INPUT>) 
	{
	
############### summary part ###############
		if ($line =~ /HysteresisResult/) 
		{
			$row=0;
			while ( ($line=<INPUT>) =~ /^\D/) #handle text lines
			{ 
				$line =~ s/[\n\r]$//g; #remove newline at end of string
				@values = split(/\s/, $line);
				$summary->write($row, 0, $values[0]); 
				$summary->write($row, 1, $values[1]); 
				$summary->write($row, 2, $values[2]); 
				$summary->write($row, 3, $values[3]); 
				$summary->write($row, 4, $values[4]); 
				$summary->write($row, 5, $values[5]); 
				$summary->write($row, 6, $values[6]); 
				$summary->write($row, 7, $values[7]); 
				$summary->write($row, 8, $values[8]);
				$row++; 
			} 
			#handle first data line:
			@values = split(/\s/, $line);
			$summary->write($row, 0, $values[0]); 
			$summary->write($row, 1, $values[1]); 
			$summary->write($row, 2, $values[2]); 
			$summary->write($row, 3, $values[3]); 
			$summary->write($row, 4, $values[4]); 
			$summary->write($row, 5, $values[5]); 
			$summary->write($row, 6, $values[6]); 
			$summary->write($row, 7, $values[7]); 
			$summary->write($row, 8, $values[8]); 
			$row++;
			while ( ($line=<INPUT>) =~ /^\d/) #handle data lines
			{ 
				@values = split(/\s/, $line);
				$summary->write($row, 0, $values[0]); 
				$summary->write($row, 1, $values[1]); 
				$summary->write($row, 2, $values[2]); 
				$summary->write($row, 3, $values[3]); 
				$summary->write($row, 4, $values[4]); 
				$summary->write($row, 5, $values[5]); 
				$summary->write($row, 6, $values[6]); 
				$summary->write($row, 7, $values[7]); 
				$summary->write($row, 8, $values[8]); 
				$row++;
			} 
		}
		
############### data header ###############
		if ($line =~ /DynamicHysteresis/) 
		{
			$row=0;
			while ( ($line=<INPUT>) =~ /^[A-Z]/i ) #handle text lines
			{ 
				$line =~ s/[\n\r]$//g; #remove newline at end of string
				@values = split(/\s/, $line);
				$header->write($row, 0, $values[0]); 
				$header->write($row, 1, $values[1]); 
				$header->write($row, 2, $values[2]); 
				$header->write($row, 3, $values[3]); 
				$header->write($row, 4, $values[4]); 
				$header->write($row, 5, $values[5]); 
				$header->write($row, 6, $values[6]); 
				$header->write($row, 7, $values[7]); 
				$header->write($row, 8, $values[8]); 
				$row++; 
			} 
		}
		
############### data part ###############
		if ($line =~ /Table/)
		{
			$table++;
			$row=0;
			$col=10;
			my $worksheet = $workbook->addworksheet("Table$table");
			while ( ($line=<INPUT>) =~ /^[A-Z]/i ) #handle text lines
			{ 
				$line =~ s/[\n\r]$//g; #remove newline at end of string
				@values = split(/\s/, $line);
				if ($line =~ /Time\[s\]/) {$row=0; $col=0;} #last text line
				$worksheet->write($row, $col, $values[0]); 
				$worksheet->write($row, $col+1, $values[1]); 
				$worksheet->write($row, $col+2, $values[2]); 
				$worksheet->write($row, $col+3, $values[3]); 
				$worksheet->write($row, $col+4, $values[4]); 
				$worksheet->write($row, $col+5, $values[5]); 
				$worksheet->write($row, $col+6, $values[6]); 
				$worksheet->write($row, $col+7, $values[7]); 
				$worksheet->write($row, $col+8, $values[8]);
				$row++; 
			} 
			$row=1;
			while ($line=<INPUT>)
			{
				if ($line =~ /^\d/) #if line starts with digit, import it
				{
					@values = split(/\s/, $line);
					$worksheet->write($row, 0, $values[0]); 
					$worksheet->write($row, 1, $values[1]); 
					$worksheet->write($row, 2, $values[2]); 
					$worksheet->write($row, 3, $values[3]); 
					$worksheet->write($row, 4, $values[4]); 
					$worksheet->write($row, 5, $values[5]); 
					$worksheet->write($row, 6, $values[6]); 
					$worksheet->write($row, 7, $values[7]); 
					$worksheet->write($row, 8, $values[8]); 
					$row++;
				}
				else { last; } #else break loop
			} 
		}
	}
	close INPUT;
#	close OUTPUT;
}
exit(0); 
