releasing/cbrtools/perl/TableFormatter/Auto.pm
author lorewang
Wed, 01 Dec 2010 16:05:36 +0800
changeset 715 e0739b8406dd
parent 602 3145852acc89
permissions -rw-r--r--
Specify extenal tool with path
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
# Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
# TableFormatter/Text.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
package TableFormatter::Auto;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
use constant MAX_WIDTH => 75;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
use constant MAX_ROWS => 10;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
use TableFormatter;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
use TableFormatter::Text;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
use vars qw/@ISA/;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
@ISA = qw(TableFormatter);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
sub New {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
  my $class = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
  my $inidata = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
  my $args = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
  my ($complex, $complexargs) = $args =~ m/^(\w+)\s*(.*)/;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
  my $self = bless {}, (ref $class || $class);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
  $self->CreateFormatters($complex, $inidata, $complexargs);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
  return $self;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
sub PrintTable {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
  my $data = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
  my $doHeading = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
  my $usecomplex = $self->UseComplexFormatter($data);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
  my $formatter = ($usecomplex?$self->{complex}:$self->{simple});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
  return $formatter->PrintTable($data, $doHeading);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
## Private
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
sub CreateFormatters {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
  my $complex = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
  my $inidata = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
  my $complexargs = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
  $self->{complex} ||= TableFormatter::CreateFormatter($complex, $inidata, $complexargs);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
  $self->{simple} ||= TableFormatter::Text->New($inidata);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
sub UseComplexFormatter {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
  my $data = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
  # Currently {maxrows} and {maxwidth} are unused
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
  my $maxrows = $self->{maxrows} || MAX_ROWS;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
  my $maxwidth = $self->{maxwidth} || MAX_WIDTH;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
  return 1 if (@$data > $maxrows);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
  return 1 if ($self->TotalWidth($data) > $maxwidth);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
  return 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
sub TotalWidth {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
  my $data = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
  my $widths = $self->FindColWidths($data);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
  my $total = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
  $total += $_ foreach (@$widths);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
  return $total;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
__END__
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
TableFormatter/Text.pm - Formats tables in text
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
=head1 INTERFACE
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
=head2 New
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
Creates a formatter.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
=head2 PrintTable 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
Prints the table. Two arguments: firstly, a 2D array of the data. Secondly, a Boolean specifying whether the first row is a header row.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
=head1 KNOWN BUGS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
None.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
 Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
 All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
 This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
 under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
 which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
 Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
 Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
 Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
 Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
=cut