#!/usr/bin/env perl

# Usage: latex-tables [ <text-file> ... ]
#
# Transform tables from text files (either in argument or via stdin),
# as generated by run-cc-tminasm + update-ratios, into LaTeX format.
#
# Copyright (C) 2012 Vincent Lefevre <vincent@vinc17.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see http://www.gnu.org/licenses/
# or write to the Free Software Foundation, Inc., 51 Franklin St,
# Fifth Floor, Boston, MA 02110-1301, USA.

use strict;

# UTF-8 settings.
use utf8;                            # for the characters in the script.
use open ':encoding(UTF-8)';         # for the file arguments.
binmode STDIN, ':encoding(UTF-8)';   # for stdin.
binmode STDOUT, ':encoding(UTF-8)';  # for stdout.

my $arx = qr/(1 [246] [56])  ([-246])/;
my $trx = qr/(\d*\.\d\d)/;
my $rrx = qr/(\d*\.\d)/;
my (@table,@s);
while (<>)
  {
    if (m!^┌──────────┬──────────────────────────────────────┬───────────┐!)
      {
        @table and die;
        @s = ();
        push @table, <<'EOF';
\begin{table}[htbp]
\makepz{~}
\[\renewcommand{\arraystretch}{1.3}
\begin{array}{|cc|cccc|cc|}
EOF
        next;
      }
    @table or next;
    if (m!^│          │           --- timings ---            │   ratio   │!)
      {
        push @table, <<'EOF';
\cline{3-8}
\multicolumn{2}{c|}{}
  & \multicolumn{4}{c|}{\text{timings (in seconds)}}
  & \multicolumn{2}{c|}{\text{ratios}} \\
EOF
        next;
      }
    if (m!^│  arg   g │ double  MPFR/12  SIPE/12/0 SIPE/12/1 │ S/D   M/S │!)
      {
        push @table, <<'EOF';
\hline
\text{args} & \text{g}
  & \texttt{double} & \text{MPFR/12} & \text{SIPE/12/0} & \text{SIPE/12/1}
  & \text{S/D} & \text{M/S} \\
EOF
        next;
      }
    if (m!^├──────────┼──────────────────────────────────────┼───────────┤!)
      {
        push @table, <<'EOF';
\hline
EOF
        next;
      }
    if (m!^└──────────┴──────────────────────────────────────┴───────────┘!)
      {
        foreach my $i (1..$#table)
          {
            ref $table[$i] or next;
            foreach my $j (2..7)
              {
                my $len = $s[$j] - length $table[$i][$j];
                $table[$i][$j] = ("~" x $len) . "$table[$i][$j]";
              }
            $table[$i] = join(' & ', @{$table[$i]})." \\\\\n";
          }
        print @table, <<'EOF';
\hline
\end{array}
\]
\endpz
\end{table}
EOF
        undef @table;
        next;
      }
    my @a = m!^│ $arx │ *$trx +$trx +$trx +$trx *│ *$rrx +$rrx *│! or die;
    $a[0] =~ tr/ /~/;
    push @table, \@a;
    foreach my $j (2..7)
      {
        my $len = length $a[$j];
        $len > $s[$j] and $s[$j] = $len;
      }
  }

# $Id: latex-tables 56954 2012-12-11 15:07:51Z vinc17/ypig $
