releasing/cbrtools/perl/TableFormatter.pm
changeset 602 3145852acc89
equal deleted inserted replaced
600:6d08f4a05d93 602:3145852acc89
       
     1 # Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of the License "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 # 
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 # 
       
    11 # Contributors:
       
    12 # 
       
    13 # Description:
       
    14 # 
       
    15 #
       
    16 
       
    17 package TableFormatter;
       
    18 
       
    19 use constant TABLE_MARGIN => 3;
       
    20 
       
    21 use strict;
       
    22 
       
    23 sub New {
       
    24   my $class = shift;
       
    25   my $inidata = shift;
       
    26   my $args = shift;
       
    27   # SUBCLASSES: don't store $iniData anywhere, because then you'll
       
    28   # get a reference loop and a memory leak.
       
    29 
       
    30   my $self = bless {}, (ref $class || $class);
       
    31   $self->{args} = $args;
       
    32 
       
    33   return $self;
       
    34 }
       
    35 
       
    36 ### Static methods
       
    37 
       
    38 sub CreateFormatter {
       
    39   my $type = shift;
       
    40   my $inidata = shift;
       
    41   my $args = shift;
       
    42 
       
    43   die "Error: couldn't create a formatter without a type" unless $type;
       
    44 
       
    45   $type = ucfirst($type);
       
    46   my $class = "TableFormatter::$type";
       
    47   eval "require $class";
       
    48   if ($@) {
       
    49     die "Could not load the table formatter \"$class\" for format \"$type\" because $@";
       
    50   }
       
    51   return $class->New($inidata, $args) or die "Could not create the table formatter \"$class\" for format \"$type\"";
       
    52 }
       
    53 
       
    54 ### Private
       
    55 
       
    56 sub FindColWidths {
       
    57   my $self = shift;
       
    58   my $data = shift;
       
    59   my @widths;
       
    60   return [] unless defined ($data->[0]);
       
    61   my $numCols = scalar(@{$data->[0]});
       
    62   for (my $col = 0; $col < $numCols; ++$col) {
       
    63     my $width = $self->FindWidestColElement($data, $col);
       
    64     $width += TABLE_MARGIN unless ($col == $numCols-1);
       
    65     # Don't pad the last column in case it gives us unnecessary line wrapping
       
    66     push @widths, $width;
       
    67   }
       
    68   return \@widths;
       
    69 }
       
    70 
       
    71 sub FindWidestColElement {
       
    72   my $self = shift;
       
    73   my $data = shift;
       
    74   my $col = shift;
       
    75   my $widest = 0;
       
    76   my $numRows = scalar(@{$data});
       
    77   for (my $row = 0; $row < $numRows; ++$row) {
       
    78     my $this = $data->[$row][$col];
       
    79     my $lengthThis = length($this);
       
    80     if ($lengthThis > $widest) {
       
    81       $widest = $lengthThis;
       
    82     }
       
    83   }
       
    84   return $widest;
       
    85 }
       
    86 
       
    87 1;
       
    88 
       
    89 __END__
       
    90 
       
    91 =head1 NAME
       
    92 
       
    93 TableFormatter.pm - An abstract superclass for table formatting classes.
       
    94 
       
    95 =head1 INTERFACE FOR SUBCLASSES
       
    96 
       
    97 =head2 New
       
    98 
       
    99 Creates a formatter.
       
   100 
       
   101 =head2 PrintTable
       
   102 
       
   103 Prints a table. Two arguments: firstly, a 2D array of the data. Secondly, a Boolean specifying whether the first row is a header row.
       
   104 
       
   105 =head1 KNOWN BUGS
       
   106 
       
   107 None.
       
   108 
       
   109 =head1 COPYRIGHT
       
   110 
       
   111  Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
   112  All rights reserved.
       
   113  This component and the accompanying materials are made available
       
   114  under the terms of the License "Eclipse Public License v1.0"
       
   115  which accompanies this distribution, and is available
       
   116  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
   117  
       
   118  Initial Contributors:
       
   119  Nokia Corporation - initial contribution.
       
   120  
       
   121  Contributors:
       
   122  
       
   123  Description:
       
   124  
       
   125 
       
   126 =cut