script/showtable

filter: Statementstmt 36.5% 110 / 301 Branchbran 26.8% 73 / 272 Conditioncond 13.3% 6 / 45 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 63.6% 7 / 11 Podpod n/a totaltotal 29.8% 196 / 657 CC 101 SCAR 51.3
CC
101
Cov
31%
ShowData
384.4
ReadListInput
210.0
new_type
122.8
CRAP 168.4
↑ Summary lib/Data/ShowTable.pm »
1 #!/usr/bin/env perl
2 # showtable - read data and show it
3 #
4 # Copyright (C) 1996-2013 Alan K. Stebbens <aks@stebbbens.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 #
21 # showtable [options] [file]
22 #
23 # This script acts as a filter, reading data in columns and displaying
24 # the data in a nicely formatted output. The output can be either
25 # a list style, simple table, boxed table, or HTML-formatted table.
26 #
27 # The input styles accepted are: tab-delimited, comma-delimited,
28 # list-style, simple-style tables, and boxed tables.
29 #
30 # The default is tab-delimited data
31 #
32 # Use -d<char> to set the field separation character. Eg: -d,
33 #
34 # Other options: -table, -html, -box, -list
35 #
36 # If no file is given, STDIN is used by default.
37 #
38 # $Id$
39 # $Source$
40 #
41 # Author: Alan K. Stebbens <aks@stebbens.org>
42 #
43  
44 1 ($DIR,$PROG) = $0 =~ m=^(.*/)?([^/]+)$=;
45 1 ▶chop($DIR = `pwd`) unless defined($DIR);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $DIR 0 1 1
46 1 $DIR =~ s=/$==;
47  
48 1 $VERSION = '4.5';
49  
50 1 unshift(@INC,'.');
51 1 ▶unshift(@INC,'blib/lib') if -d 'blib/lib'; # for testing
Branch: 1 branch
BranchTrueFalseTotal
if -d "blib/lib" 1 0 1
52  
53 1 ▶use Data::ShowTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 sub ShowData;
56 sub ReadTableInput;
57 sub ReadListInput;
58 sub NextRow;
59 sub max;
60  
61 0 ▶sub err {
Subroutine err: not called CC=2 SCAR=17.9
62 0 my $fmt = shift;
63 0 ▶ $fmt .= "\n" unless $fmt =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt =~ /\n$/ 0 0 0
64 0 printf STDERR $fmt,@_;
65 }
66  
67 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
68 0 ▶ err @_ if $#_ >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 0 0 0
69 0 err <<EOF;
70 usage: $PROG [options] [file]
71 Read the file, or STDIN by default, and produce a nicely formatted listing
72 of columnar data. By default, columns of data should be separated by tabs,
73 and is assumed to have no titles. Lines of all spaces and any of the
74 characters "-+_=" will be ignored as separator lines, unless -nodashes is
75 given.
76  
77 Options affecting input:
78 -br(eak)=STR set the inter-column break character to "STR". The default
79 is "\t" (a tab).
80 -dSTR set the delimiter string (same as "-break=STR").
81 -nod(ashes) Do not look for lines of separators, such as dashes, equal
82 signs, or underlines (if they occur, they will be part of
83 the "data".
84 -t(itles)[=NN] Treat the first NN rows of data as column titles; multiple
85 words in the column titles may wrap vertically. If NN is
86 omitted, it defaults to 1. If no -titles is given at all,
87 NN defaults to 0.
88 -st(rip) Strip blanks from around the column values (implied with
89 $InputType eq 'box').
90 -nos(trip) Don't strip blanks around the column values.
91 -i(nput)=TYPE Set the input type; currently TYPE can be "list", "box",
92 "table", or "simple". Default is "box".
93  
94 The options controlling the output are:
95 -s(imple) use a simple table format (without wrapped columns)
96 -t(able) use a table format (handles wrapped column values)
97 -l(ist) use a list style format
98 -b(ox) use a "boxed" style table
99 -ht(ml) use HTML-formating
100 -noh(eaders) do not output any headers on the tables; -titles=0 implies
101 this option.
102 -ti(tles)=name1,name2,...,nameN
103 Define the output column titles explicitly; "name1" cannot
104 begin with a number. Implies "-titles=0".
105 -w(idth)=NN Define the maximum width of the table; if this not given,
106 the maximum is the sum of all given column widths.
107 -cw(idths)=w1,w2,...,wN
108 Define individual column widths; the last width applies to
109 any remaining columns. Each column may be given as an
110 integer, representing a character count, or a fraction
111 or percentage, representing percentages.
112 -fN1,N2... select fields to display by index; N-M is a range of fields.
113 -fields=Name1, Name2, ...
114 select fields to display by names.
115 -h(elp) Display this string.
116  
117 HTML-only options (their usage implies -html):
118 -noe(scape) Do not perform HTML escape sequences; this will allow
119 data with embedded HTML to be displayed properly. Implies
120 the -html option.
121 -attr(ibutes)='attr1 attr2 ...'
122 Define the table attributes, which get inserted into the
123 <TABLE> token.
124 -t(itle)f(ormats)=fmt1;fmt2;...;fmtN
125 Define the HTML formats for the titles.
126 Implies -html.
127 -d(ata)f(ormats)=fmt1;fmt2;...;fmtN
128 Define the HTML formats for the data values in each column.
129 Implies -html.
130 -url(s)=col1=url1,col2=url2,...
131 Define the URL mappings for given column names. Multiple
132 -url options may be given.
133 EOF
134 0 exit;
135 }
136  
137 1 $break_str = "\t"; # tab for default break str
138 1 $Titles = 0; # assume no input titles
139 1 $#Titles = -1; # assume no output titles
140 1 @Fields = (); # no explicit field selections
141 1 %URLs = (); # no URLs initially
142  
143 1 $InputType = '';
144 1 $Strip_Spaces = '';
145 1 $NoStrip_Spaces = '';
146 1 $Show_Mode = 'Box'; # by default
147 1 $Width = 0; # no width limit by default
148  
149 1 $nofile = 1;
150  
151 1 ▶while ($_ = shift(@ARGV)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @ARGV) 2 1 3
152 2 ▶ if (/^-d(\W+)?$/) { # -dSTR
Branch: 1 branch
BranchTrueFalseTotal
if (/^-d(\W+)?$/) 1 1 2
153 1 ▶ $break_str = $1 || shift || usage "$PROG: Missing argument to -d\n";
Condition33%2/6
$1 || shift @ARGV || usage("${PROG}: Missing argument to -d\n")
l!l&&r!l&&!r
100
$1 or shift @ARGV
l!l&&r!l&&!r
100
MC/DC0%0/3
$1 || shift @ARGV || usage("${PROG}: Missing argument to -d\n")
$1 shift @ARGV usage("${PROG}: Missing argument to -d\n")
Truth table25%1/4
A or B || C
A= $1B= shift @ARGVC= usage("${PROG}: Missing argument to -d\n")
ABCresult
1XX1
01X1
0011
0000
154 1 next;
155 }
156 1 ▶ if (/^-f(\d.*)/) { # -fN1,N2,...
Branch: 1 branch
BranchTrueFalseTotal
if (/^-f(\d.*)/) 0 1 1
157 0 $value = $1;
158 # convert possible range spec
159 0 $value =~ s/\b(\d+)-(\d+)\b/join(',',$1..$2)/e;
160 0 @Fields = split(/[:, ]/,$value);
161 0 next;
162 }
163 1 ▶ usage if !index('-help',$_);
Branch: 1 branch
BranchTrueFalseTotal
unless index "-help", $_ 0 1 1
164 1 ▶ if (!index('-strip',$_)) { # -strip
Branch: 12 branches
BranchTrueFalseTotal
if (not index('-strip', $_)) { } 0 1 1
elsif (not index('-nostrip', $_)) { } 0 1 1
elsif (not index('-table', $_) && index('-simple', $_)) { } 0 1 1
elsif (not index('-list', $_)) { } 0 1 1
elsif (not index('-box', $_)) { } 0 1 1
elsif (not index('-html', $_)) { } 0 1 1
elsif (not index('-noescape', $_)) { } 0 1 1
elsif (not index('-noheaders', $_)) { } 0 1 1
elsif (not index('-nodashes', $_)) { } 0 1 1
elsif (/^(-\w+)(?:[:=](.*))?/) { } 0 1 1
elsif (/^-/) { } 0 1 1
elsif (-f $_) { } 1 0 1
165 0 $Strip_Spaces = 1;
166 0 $NoStrip_Spaces = '';
167 } elsif (!index('-nostrip',$_)) { # -nostrip
168 0 $NoStrip_Spaces = 1;
169 0 $Strip_Spaces = '';
170 ▶ } elsif (!index('-table',$_) || !index('-simple',$_)) { # -table, -simple
Condition33%1/3
index('-table', $_) && index('-simple', $_)
!ll&&!rl&&r
001
MC/DC0%0/2
index('-table', $_) && index('-simple', $_)
index('-table', $_) index('-simple', $_)
Truth table33%1/3
A && B
A= index('-table', $_)B= index('-simple', $_)
ABresult
0X0
100
111
171 0 $Show_Mode = 'Table';
172 } elsif (!index('-list', $_)) { # -list
173 0 $Show_Mode = 'List';
174 } elsif (!index('-box',$_)) { # -box
175 0 $Show_Mode = 'Box';
176 0 ▶ $Strip_Spaces = 1 unless $NoStrip_Spaces;
Branch: 1 branch
BranchTrueFalseTotal
unless $NoStrip_Spaces 0 0 0
177 } elsif (!index('-html',$_)) { # -html
178 0 $Show_Mode = 'HTML';
179 } elsif (!index('-noescape',$_)) { # -noescape
180 0 $No_Escape = 1;
181 0 $Show_Mode = 'HTML';
182 } elsif (!index('-noheaders',$_)) { # -noheaders
183 0 $No_Header = 1;
184 } elsif (!index('-nodashes',$_)) { # -nodashes
185 0 $No_Dashes = 1;
186 } elsif (/^(-\w+)(?:[:=](.*))?/) { # -OPTION=VALUE
187 0 ($_, $value) = ($1, $2);
188 0 ▶ if (!index('-titles',$_)) { # -titles=NN, -titles=NAME,..
Branch: 10 branches
BranchTrueFalseTotal
if (not index('-titles', $_)) { } 0 0 0
elsif (not index('-input', $_)) { } 0 0 0
elsif (not index('-fields', $_)) { } 0 0 0
elsif (not index('-break', $_)) { } 0 0 0
elsif (not index('-width', $_)) { } 0 0 0
elsif (not index('-cwidths', $_)) { } 0 0 0
elsif (not index('-titleformats', $_) && index('-tformats', $_)) { } 0 0 0
elsif (not index('-dataformats', $_) && index('-dformats', $_)) { } 0 0 0
elsif (not index('-urls', $_)) { } 0 0 0
elsif (not index('-attributes', $_)) { } 0 0 0
189 0 ▶ if ($value =~ /^\d/ || $value eq '') { # -titles=NN?
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\d/ or $value eq '') { } 0 0 0
Condition0%0/3
$value =~ /^\d/ or $value eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$value =~ /^\d/ or $value eq ""
$value =~ /^\d/ $value eq ""
Truth table0%0/3
A or B
A= $value =~ /^\d/B= $value eq ""
ABresult
1X1
011
000
190 0 ▶ $Titles = $value ne '' ? $value : 1;
Branch: 1 branch
BranchTrueFalseTotal
$value ne '' ? : 0 0 0
191 } else { # -titles=name,name2,...,nameN
192 0 @Titles = split(/[:, ]/,$value);
193 0 $Titles = 0; # no titles in the datastream
194 }
195 } elsif (!index('-input',$_)) { # -input=TYPE?
196 0 $value = lc($value);
197 0 ▶ if (!index('box',$value)) { # -input=box?
Branch: 4 branches
BranchTrueFalseTotal
if (not index('box', $value)) { } 0 0 0
elsif (not index('list', $value)) { } 0 0 0
elsif (not index('table', $value)) { } 0 0 0
elsif (not index('simple', $value)) { } 0 0 0
198 0 $InputType = 'box';
199 0 ▶ $Titles = 1 unless $Titles >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $Titles >= 1 0 0 0
200 } elsif (!index('list',$value)) {# -input=list?
201 0 $InputType = 'list';
202 } elsif (!index('table', $value)) { # -input=table?
203 0 $InputType = 'table';
204 0 ▶ $Titles = 1 unless $Titles >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $Titles >= 1 0 0 0
205 } elsif (!index('simple', $value)) {# -input=simple?
206 0 $InputType = 'simple';
207 } else {
208 0 usage "$PROG: bad -input TYPE";
209 }
210 } elsif (!index('-fields',$_)) { # -fields=Name1, Name2, ...
211 # convert range spec
212 0 $value =~ s/\b(\d+)-(\d+)\b/join(' ',$1..$2)/e;
213 0 @Fields = split(/[:, ]+/,$value);
214 } elsif (!index('-break',$_)) { # -break=STR
215 0 ▶ $break_str = $value ne '' ? $value : "\t";
Branch: 1 branch
BranchTrueFalseTotal
$value ne '' ? : 0 0 0
216 0 ▶ if ($break_str =~ /\\/) { # any escape sequences?
Branch: 1 branch
BranchTrueFalseTotal
if ($break_str =~ /\\/) 0 0 0
217 0 $break_str =~ s/"/\\"/g; # yes, escape any quotes
218 0 $break_str = eval('"'.$break_str.'"'); # evaluate a string
219 }
220 } elsif (!index('-width',$_)) { # -width=NUM
221 0 ▶ $value =~ /^\d+$/ or usage "$PROG: width must be a number.";
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^\d+$/ or usage("${PROG}: width must be a number.") 0 0 0
222 0 $Width = 0 + $value;
223 } elsif (!index('-cwidths',$_)) { # -cwidths=N1,N2,...,NM
224 0 @Cwidths = split(/[,: ]/,$value);
225 0 ▶ if (grep(!/^(\.?\d+|\d+%)?$/,@Cwidths)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep((!/^(\.?\d+|\d+%)?$/), @Cwidths)) 0 0 0
226 0 usage "$PROG: bad format for -cwidths.";
227 }
228 ▶ } elsif (!index('-titleformats',$_) || !index('-tformats',$_)) {
Condition0%0/3
index('-titleformats', $_) && index('-tformats', $_)
!ll&&!rl&&r
000
MC/DC0%0/2
index('-titleformats', $_) && index('-tformats', $_)
index('-titleformats', $_) index('-tformats', $_)
Truth table0%0/3
A && B
A= index('-titleformats', $_)B= index('-tformats', $_)
ABresult
0X0
100
111
229 0 my @tformats = split(/[;|]/,$value); # get the title formats
230 0 foreach (@tformats) {
231 0 ▶ next if /^<?($HTML_Elements)/i; # HTML element ok?
Branch: 1 branch
BranchTrueFalseTotal
if /^<?($HTML_Elements)/i 0 0 0
232 0 usage "$PROG: bad HTML format element: '%s'", $1;
233 }
234 0 push(@Tformats,@tformats); # allow multiple options
235 0 $Show_Mode = 'HTML';
236 ▶ } elsif (!index('-dataformats',$_) || !index('-dformats',$_)) {
Condition0%0/3
index('-dataformats', $_) && index('-dformats', $_)
!ll&&!rl&&r
000
MC/DC0%0/2
index('-dataformats', $_) && index('-dformats', $_)
index('-dataformats', $_) index('-dformats', $_)
Truth table0%0/3
A && B
A= index('-dataformats', $_)B= index('-dformats', $_)
ABresult
0X0
100
111
237 0 my @dformats = split(/[;|]/,$value); # get the title formats
238 0 foreach (@dformats) {
239 0 ▶ next if /^<?(?:$HTML_Elements)/i; # HTML element ok?
Branch: 1 branch
BranchTrueFalseTotal
if /^<?(?:$HTML_Elements)/i 0 0 0
240 0 usage "$PROG: bad HTML format element '%s'", $_;
241 }
242 0 push(@Dformats,@dformats); # allow multiple options
243 0 $Show_Mode = 'HTML';
244 } elsif (!index('-urls',$_)) {
245 # parse COL1=URL1,COL2=URL2,...
246 # or: COL1:URL1,COL2:URL2,...
247 0 my @urldefs = split(/[,|]/, $value); # get the different URLs
248 0 my $urldef;
249 0 foreach $urldef (@urldefs) {
250 0 my ($col, $url) = split(/[=:]/,$urldef,2);
251 0 ▶ if ($col ne '' && $url ne '') { # both defined?
Branch: 2 branches
BranchTrueFalseTotal
if ($col ne '' and $url ne '') { } 0 0 0
elsif ($col ne '' or $url ne '') { } 0 0 0
Condition0%0/3
$col ne "" and $url ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$col ne "" and $url ne ""
$col ne "" $url ne ""
Truth table0%0/3
A and B
A= $col ne ""B= $url ne ""
ABresult
0X0
100
111
252 0 $URLs{$col} = $url; # save the mapping
253 ▶ } elsif ($col ne '' || $url ne '') {
Condition0%0/3
$col ne "" or $url ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$col ne "" or $url ne ""
$col ne "" $url ne ""
Truth table0%0/3
A or B
A= $col ne ""B= $url ne ""
ABresult
1X1
011
000
254 0 usage "$PROG: bad URL format element: '%s'", $urldef;
255 }
256 }
257 0 $Show_Mode = 'HTML'; # make sure
258 } elsif (!index('-attributes',$_)) {
259 0 $table_attributes .= ' '.$value; # build up the list
260 0 $Show_Mode = 'HTML';
261 } else {
262 0 usage "$PROG: Unknown option: $_";
263 }
264 } elsif (/^-/) {
265 0 usage "$PROG: Unknown option: $_";
266 } elsif (-f) { # does the file exist?
267 1 ▶ open(STDIN,"<$_") ||
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<$_" or die "${PROG}: Can't open $_ for input: $!\n" 1 0 1
268 die "$PROG: Can't open $_ for input: $!\n";
269 1 ShowData;
270 1 close(STDIN);
271 1 $nofile = 0;
272 } else {
273 0 err "File $_ does not exist.";
274 0 exit 1;
275 }
276 }
277  
278 1 ▶ShowData if $nofile;
Branch: 1 branch
BranchTrueFalseTotal
if $nofile 0 1 1
279  
280 1 exit;
281  
282  
283 # $type = guess_type $value
284  
285 6 ▶sub guess_type {
Subroutine guess_type: called CC=17 SCAR=33.0
286 6 local $_ = shift;
287 6 ▶ &PlainText if $Show_Mode eq 'HTML'; # remove HTML effects if -html
Branch: 1 branch
BranchTrueFalseTotal
if $Show_Mode eq "HTML" 0 6 6
288 # so they aren't part of the typing
289 6 s/^\s+//; s/\s+$//; # trim leading & trailing blanks
290 6 ▶ /^$/ && return 'null'; # null type
Branch: 1 branch
BranchTrueFalseTotal
/^$/ and return "null" 0 6 6
291 6 ▶ /^([01tf]|yes|no|on|off)$/i && return 'bool'; # 0, 1, yes, no, on, off, t, f
Branch: 1 branch
BranchTrueFalseTotal
/^([01tf]|yes|no|on|off)$/i and return "bool" 0 6 6
292 6 ▶ m=^\d{2,4}[-/.]\d{1,2}[-/.]\d{1,2}$= && return 'date'; # mm/dd/yy, yy/mm/dd, yyyy.mm.dd, yyyy-mm-dd
Branch: 1 branch
BranchTrueFalseTotal
m[^\d{2,4}[-/.]\d{1,2}[-/.]\d{1,2}$] and return "date" 1 5 6
293 5 ▶ m=^\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4}$= && return 'date'; # dd.mm.yyyy, d.m.yy, dd-mm-yy
Branch: 1 branch
BranchTrueFalseTotal
m[^\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4}$] and return "date" 1 4 5
294 4 ▶ /^\w{3,9} \d{1,2}, \d{4}/ && return 'date'; # mmmm dd, yyyy
Branch: 1 branch
BranchTrueFalseTotal
/^\w{3,9} \d{1,2}, \d{4}/ and return "date" 0 4 4
295 4 ▶ /^\d{1,2}[- ]\w{3}[- ]\d{2,4}$/ && return 'date'; # dd mmm yyyy
Branch: 1 branch
BranchTrueFalseTotal
/^\d{1,2}[- ]\w{3}[- ]\d{2,4}$/ and return "date" 0 4 4
296 4 ▶ /^\d\d:\d\d(:\d\d)?$/ && return 'time'; # hh:mm:ss
Branch: 1 branch
BranchTrueFalseTotal
/^\d\d:\d\d(:\d\d)?$/ and return "time" 0 4 4
297 4 ▶ /^[-+]?\d+$/ && return 'int'; # +-nnnn
Branch: 1 branch
BranchTrueFalseTotal
/^[-+]?\d+$/ and return "int" 0 4 4
298 4 ▶ /^[-+]?[\d.]+(E[+-]?\d+)?$/ && return 'real'; # +-nnn.nnn(E+-nn)
Branch: 1 branch
BranchTrueFalseTotal
/^[-+]?[\d.]+(E[+-]?\d+)?$/ and return "real" 0 4 4
299 4 ▶ /^[-+]?[\d.]\%$/ && return 'pct'; # nn%, nn.n%, -nn.nnn%
Branch: 1 branch
BranchTrueFalseTotal
/^[-+]?[\d.]\%$/ and return "pct" 0 4 4
300 4 ▶ /^-?\(?\$[ \d,.]+\)?(?: *CR)?$/ && return 'money'; # $ nn,nnn.nn or ($ nnn,nnn.nn)
Branch: 1 branch
BranchTrueFalseTotal
/^-?\(?\$[ \d,.]+\)?(?: *CR)?$/ and return "money" 0 4 4
301 4 ▶ /^-?\(?[\d]+,[\d,]+(\.(\d\d)?)?\)?$/&& return 'money'; # nnn,nnn.nn or ( nnn,nnn.nn)
Branch: 1 branch
BranchTrueFalseTotal
/^-?\(?[\d]+,[\d,]+(\.(\d\d)?)?\)?$/ and return "money" 0 4 4
302 4 ▶ /^['"]|["']$/ && return 'string'; # "xxxx"
Branch: 1 branch
BranchTrueFalseTotal
/^['"]|["']$/ and return "string" 0 4 4
303 4 ▶ /^\w+$/ && return 'symbol';
Branch: 1 branch
BranchTrueFalseTotal
/^\w+$/ and return "symbol" 4 0 4
304 0 ▶ /\n/ && return 'text';
Branch: 1 branch
BranchTrueFalseTotal
/\n/ and return "text" 0 0 0
305 0 return 'char';
306 }
307  
308 # Do type conversion
309  
310 6 ▶sub new_type {
Subroutine new_type: called CC=15 SCAR=48.1
311 6 my @types = @_;
312 6 my $type1 = $types[0];
313 6 my $type2 = $types[1];
314  
315 # trivial conversions
316 6 ▶ $type1 eq '' && return $type2;
Branch: 1 branch
BranchTrueFalseTotal
$type1 eq "" and return $type2 3 3 6
317 3 ▶ $type2 eq '' && return $type1;
Branch: 1 branch
BranchTrueFalseTotal
$type2 eq "" and return $type1 0 3 3
318 3 ▶ $type1 eq $type2 && return $type1;
Branch: 1 branch
BranchTrueFalseTotal
$type1 eq $type2 and return $type1 3 0 3
319  
320 # handle null types
321 0 ▶ $type1 eq 'null' && return $type2;
Branch: 1 branch
BranchTrueFalseTotal
$type1 eq "null" and return $type2 0 0 0
322 0 ▶ $type2 eq 'null' && return $type1;
Branch: 1 branch
BranchTrueFalseTotal
$type2 eq "null" and return $type1 0 0 0
323  
324 # These types supercede others
325 0 ▶ grep(/text/i, @types) && return 'text';
Branch: 1 branch
BranchTrueFalseTotal
grep /text/i, @types and return "text" 0 0 0
326 0 ▶ grep(/string/i, @types) && return 'string';
Branch: 1 branch
BranchTrueFalseTotal
grep /string/i, @types and return "string" 0 0 0
327 0 ▶ grep(/char/i, @types) && return 'char';
Branch: 1 branch
BranchTrueFalseTotal
grep /char/i, @types and return "char" 0 0 0
328  
329 # Now do finer-grain conversions
330 0 ▶ grep(/money/i, @types) && return 'money';
Branch: 1 branch
BranchTrueFalseTotal
grep /money/i, @types and return "money" 0 0 0
331 0 ▶ grep(/pct/i, @types) && return 'pct';
Branch: 1 branch
BranchTrueFalseTotal
grep /pct/i, @types and return "pct" 0 0 0
332 0 ▶ grep(/real/i, @types) && return 'real';
Branch: 1 branch
BranchTrueFalseTotal
grep /real/i, @types and return "real" 0 0 0
333 0 ▶ grep(/int/i, @types) && return 'int';
Branch: 1 branch
BranchTrueFalseTotal
grep /int/i, @types and return "int" 0 0 0
334 0 ▶ grep(/symbol/i, @types) && return 'symbol';
Branch: 1 branch
BranchTrueFalseTotal
grep /symbol/i, @types and return "symbol" 0 0 0
335 0 ▶ grep(/bool/i, @types) && return 'bool';
Branch: 1 branch
BranchTrueFalseTotal
grep /bool/i, @types and return "bool" 0 0 0
336  
337 0 $type1; # huh?
338 }
339  
340 1 ▶sub ShowData {
Subroutine ShowData: called CC=32 SCAR=59.5
341 1 @Data = ();
342 # reset titles if they are coming from the data stream
343 #@Titles = () if $Titles > 0;
344 1 @Types = ();
345 1 @Widths = ();
346 1 my ($titles, $data, $maxcols);
347 1 ▶ if ($InputType eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($InputType eq 'list') { } 0 1 1
348 0 ($titles, $data, $maxcols) = ReadListInput;
349 } else {
350 1 ($titles, $data, $maxcols) = ReadTableInput;
351 }
352 1 my @titles = @$titles;
353 1 @Data = @$data;
354  
355 # Make the fields selection
356 1 ▶ if (@Fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Fields) 0 1 1
357 # build a field index list
358 0 my ($f,$fx);
359 0 my %fields = ();
360 0 my @fields = (); # an index list
361 0 @fields{@titles} = (0..$#titles); # generate indexes
362 0 foreach $f (@Fields) {
363 0 ▶ if ($f =~ /^\d+$/) { # a numeric index?
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /^\d+$/) 0 0 0
364 0 $f--; # convert to origin zero
365 0 ▶ push(@fields,$f) if $f <= $maxcols && $f >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $f <= $maxcols and $f >= 0 0 0 0
Condition0%0/3
$f <= $maxcols and $f >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$f <= $maxcols and $f >= 0
$f <= $maxcols $f >= 0
Truth table0%0/3
A and B
A= $f <= $maxcolsB= $f >= 0
ABresult
0X0
100
111
366 0 next;
367 }
368 # Lookup a field name and use its index
369 0 ▶ if (($fx = $fields{$f}) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (($fx = $fields{$f}) ne "") 0 0 0
370 0 push(@fields,$fx);
371 }
372 }
373 # now, given the field index array, splice each row array according
374 # to the field indexes
375 0 my $r;
376 0 ▶ for ($r = 0; $r <= $#Data; $r++) { # foreach row of data..
Branch: 1 branch
BranchTrueFalseTotal
if ($r <= $#Data) 0 0 0
377 0 $row = $Data[$r]; # get the row
378 0 @$row = @$row[@fields]; # slice out the desired fields
379 }
380 0 $maxcols = $#fields; # set the other vars
381 0 @titles = @titles[@fields]; # slice out the titles
382 }
383  
384 # Now, map external titles onto the titles array, allowing
385 # user-given titles to override the built-in titles (if any).
386 1 ▶ if ($No_Header) { # if no headers, no titles
Branch: 2 branches
BranchTrueFalseTotal
if ($No_Header) { } 0 1 1
elsif ($#Titles >= 0) { } 0 1 1
387 0 @titles = ();
388 } elsif ($#Titles >= 0) { # map @Titles onto @titles
389 0 my ($c,$t);
390 0 ▶ for ($c = 0; $c <= $maxcols; $c++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $maxcols) 0 0 0
391 0 ▶ if (($t = $Titles[$c]) ne '') { # user-given title
Branch: 2 branches
BranchTrueFalseTotal
if (($t = $Titles[$c]) ne '') { } 0 0 0
elsif ($titles[$c] eq '') { } 0 0 0
392 0 $titles[$c] = $t; # overrides built-in title
393 } elsif ($titles[$c] eq '') { # otherwise, use default title
394 0 $titles[$c] = sprintf("Field_%d", $c + 1);
395 }
396 }
397 }
398  
399 # Scan the column widths, where none are given, and if a total width
400 # was given, calculate a fraction.
401 my $cols_left = length($Width) ? $Width : # maximum columns left
402 1 ▶ $Show_Mode ne 'HTML' ? $ENV{'COLUMNS'} : 0;
Branch: 2 branches
BranchTrueFalseTotal
length $Width ? : 1 0 1
$Show_Mode ne 'HTML' ? : 0 0 0
403  
404 1 ▶ if ($cols_left > 0) { # cols == 0 means no limit
Branch: 1 branch
BranchTrueFalseTotal
if ($cols_left > 0) 0 1 1
405 # We must reduce the available columns by the formatting requirements
406 # of the display format
407 0 ▶ $cols_left -= $maxcols * 3 + 2 if $Show_Mode eq 'Box';
Branch: 1 branch
BranchTrueFalseTotal
if $Show_Mode eq "Box" 0 0 0
408 0 ▶ $cols_left -= $maxcols * 2 - 1 if $Show_Mode eq 'Table';
Branch: 1 branch
BranchTrueFalseTotal
if $Show_Mode eq "Table" 0 0 0
409  
410 0 my ($c, $w, $cols);
411 0 ▶ for ($c = 0; $c <= $maxcols; $c++) { # for each column..
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $maxcols) 0 0 0
412 0 ▶ $w = $c <= $#Cwidths ? $Cwidths[$c] : $Cwidths[$#Cwidths];
Branch: 1 branch
BranchTrueFalseTotal
$c <= $#Cwidths ? : 0 0 0
413 0 ▶ if ($w ne '') { # is there a value?
Branch: 1 branch
BranchTrueFalseTotal
if ($w ne "") 0 0 0
414 # If the width is a fraction or percentage...
415 0 ▶ if ($w =~ /[.%]/ || ($w > 0 && $w < 1.0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /[.%]/ or $w > 0 and $w < 1) 0 0 0
Condition0%0/6
$w =~ /[.%]/ or $w > 0 and $w < 1
l!l&&r!l&&!r
000
$w > 0 and $w < 1
!ll&&!rl&&r
000
MC/DC0%0/3
$w =~ /[.%]/ or $w > 0 and $w < 1
$w =~ /[.%]/ $w > 0 $w < 1
Truth table0%0/4
A or B and C
A= $w =~ /[.%]/B= $w > 0C= $w < 1
ABCresult
1XX1
0111
00X0
0100
416 0 ▶ $w = $1 / 100.0 if $w =~ /^(\d+)%/; # convert NN% to fraction
Branch: 1 branch
BranchTrueFalseTotal
if $w =~ /^(\d+)%/ 0 0 0
417 0 $w = int($cols_left * $w); # convert fraction to width
418 }
419 0 ▶ $w = 1 unless $w > 0; # guarantee 1 col min
Branch: 1 branch
BranchTrueFalseTotal
unless $w > 0 0 0 0
420 0 $cols_left -= $w; # keep an accounting
421 0 ▶ $cols_left = 0 if $cols_left < 0;# keep sane
Branch: 1 branch
BranchTrueFalseTotal
if $cols_left < 0 0 0 0
422 0 $Widths[$c] = $w; # set the column width
423 }
424 }
425 }
426  
427 1 my ($c, $r, $val, $row, $type, $newtype); # some vars
428 # Now let's analyze the data
429 1 ▶ for ($c = 0; $c <= $maxcols; $c++) { # Scan each column
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $maxcols) 3 1 4
430 3 $type = ''; # assume no default
431 3 ▶ for ($r = 0; $r <= $#Data; $r++) { # scan each row
Branch: 1 branch
BranchTrueFalseTotal
if ($r <= $#Data) 6 3 9
432 6 $row = $Data[$r]; # get the row data
433 6 ▶ if (defined($row->[$c])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->[$c]) { } 6 0 6
434 6 $val = $row->[$c]; # get the column data
435 } else {
436 0 $row->[$c] = '';
437 }
438 6 $newtype = guess_type $val;
439 6 $type = new_type $type, $newtype;
440 }
441 3 $Types[$c] = $type;
442 }
443  
444 1 local $theRow;
445 1 ▶ my $theRowSub = sub { &ShowRow( $_[0], \$theRow, \@Data ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
446 1 ▶ ShowTable
Branch: 1 branch
BranchTrueFalseTotal
$Width > 0 ? : 0 1 1
447 { show_mode => $Show_Mode,
448 max_width => $Width > 0 ? $Width : '',
449 titles => \@titles,
450 types => \@Types,
451 widths => \@Widths,
452 row_sub => $theRowSub,
453 no_escape => $No_Escape,
454 table_attrs => $table_attributes,
455 tformats => \@Tformats,
456 dformats => \@Dformats,
457 url_keys => \%URLs,
458 };
459 }
460  
461 # ($titles, $data, $maxcols) = ReadTableInput
462 #
463 # $titles is a ref to an array of titles
464 # $data is a ref to an array of rows (arrays of values)
465 # $maxcols is the maximum columns index in any of the rows.
466  
467 # The global $Titles determines how many titles rows there are.
468 # Unless $NoDashes is set, this can be determined imperically
469 # if there are titles and a separator row, or explicitly with
470 # -titles=NN.
471  
472 1 ▶sub ReadTableInput {
Subroutine ReadTableInput: called CC=7 SCAR=27.1
473 1 my @data;
474 my @titles;
475 1 my $data;
476 1 ▶ if ($Titles > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Titles > 0) { } 0 1 1
477 0 @titles = @{&NextRow};
478 # if there are multiple lines of titles, combine them
479 0 my ($limit, $r, $c);
480 0 ▶ for ($r = 1; $r < $Titles; $r++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $Titles) 0 0 0
481 0 $data = NextRow; # get the next row of data
482 0 $limit = max $#titles, $#$data;
483 0 ▶ for ($c = 0; $c <= $limit; $c++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $limit) 0 0 0
484 0 $titles[$c] .= ' '.$data->[$c]; # combine titles
485 }
486 }
487 0 foreach (@titles) { # clean up the names
488 0 s/^\s+//; # trim leading &
489 0 s/\s+$//; # trailing blanks
490 0 s/\s+/ /g; # & squeeze middle blanks
491 }
492 } else {
493 1 $#titles = -1; # no titles at all
494 }
495 1 my $maxcols = 0;
496 1 ▶ while ($data = NextRow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = NextRow()) 2 1 3
497 2 push(@data, $data); # collect the data
498 2 ▶ $maxcols = $#$data if $#$data > $maxcols;
Branch: 1 branch
BranchTrueFalseTotal
if $data->$#* > $maxcols 1 1 2
499 }
500 1 (\@titles, \@data, $maxcols);
501 }
502  
503 # Read in next row of data into $_; return \@data.
504 #
505 # If $InputType is 'box', input with "| foo | bar | ... |" is
506 # boxed input, and the bars are removed. Also, if "|<" or ">|"
507 # occur, then the next row of data is appended to the current
508 # row.
509 #
510 # Unboxed input is split by tabs (or the $break_str)
511  
512 3 ▶sub NextRow {
Subroutine NextRow: called CC=17 SCAR=40.6
513 3 my @row;
514 3 my ($continue, $append);
515 3 while (1) {
516 3 $_ = <STDIN>;
517 3 ▶ last unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 1 2 3
518 2 chomp;
519 2 ▶ unless ($InputType) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($InputType) 2 0 2
520 2 ▶ $InputType = 'box' if /^\s*([\+:|*])[-+=:*|_]*$1\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*([\+:|*])[-+=:*|_]*$1\s*$/ 0 2 2
521 }
522 2 ▶ next if !$No_Dashes && /^[-_=+ \t]*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $No_Dashes and /^[-_=+ \t]*$/ 0 2 2
Condition33%1/3
not $No_Dashes and /^[-_=+ \t]*$/
!ll&&!rl&&r
020
MC/DC0%0/2
not $No_Dashes and /^[-_=+ \t]*$/
not $No_Dashes /^[-_=+ \t]*$/
Truth table33%1/3
not(A) and B
A= not $No_DashesB= /^[-_=+ \t]*$/
ABresult
0X0
100
111
523  
524 2 my @data;
525 # If boxed input, remove boxing characters and split with the
526 # box bar character
527 2 ▶ if (/^\s*([|:]).*$1\s*$/ && $InputType eq 'box') {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([|:]).*$1\s*$/ and $InputType eq 'box') { } 0 2 2
Condition33%1/3
/^\s*([|:]).*$1\s*$/ and $InputType eq "box"
!ll&&!rl&&r
200
MC/DC0%0/2
/^\s*([|:]).*$1\s*$/ and $InputType eq "box"
/^\s*([|:]).*$1\s*$/ $InputType eq "box"
Truth table33%1/3
A and B
A= /^\s*([|:]).*$1\s*$/B= $InputType eq "box"
ABresult
0X0
100
111
528 # The data is boxed--remove boxing data
529 0 my $bar = $1;
530 0 my $continue = />\Q$bar\E/; # is there a continuation?
531 0 my $append |= /\Q$bar\E</; # should this be appended?
532 0 s/^\s*\Q$bar\E<?\s*//; # remove leading bar (if any)
533 0 s/\s*>?\Q$bar\E\s*$//; # remove trailing bar (if any)
534 0 ▶ if ($NoStrip_Spaces) { # nostrip?
Branch: 1 branch
BranchTrueFalseTotal
if ($NoStrip_Spaces) { } 0 0 0
535 0 @data = split(/>?\Q$bar\E<?/);
536 } else {
537 0 @data = split(/\s*>?\Q$bar\E<?\s*/);
538 }
539 } else { # not in box mode
540 2 ▶ if ($break_str eq " \t" or $break_str eq "\t ") {
Branch: 2 branches
BranchTrueFalseTotal
if ($break_str eq " \t" or $break_str eq "\t ") { } 0 2 2
elsif ($Strip_Spaces) { } 0 2 2
Condition33%1/3
$break_str eq " \t" or $break_str eq "\t "
l!l&&r!l&&!r
002
MC/DC0%0/2
$break_str eq " \t" or $break_str eq "\t "
$break_str eq " \t" $break_str eq "\t "
Truth table33%1/3
A or B
A= $break_str eq " \t"B= $break_str eq "\t "
ABresult
1X1
011
000
541 0 @data = split(' ');
542 } elsif ($Strip_Spaces) {
543 0 @data = split(/\s*\Q$break_str\E\s*/);
544 } else {
545 2 @data = split(/\Q$break_str\E/);
546 }
547 2 ▶ if ($InputType eq 'table') { # check for table wraps
Branch: 1 branch
BranchTrueFalseTotal
if ($InputType eq "table") 0 2 2
548 0 $append |= grep(s/^<//,@data); # see if any appends
549 0 $continue = grep(s/>$//,@data); # see if any continuations
550 }
551 }
552 2 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 0 2 2
553 0 my $maxcol = max($#data, $#row);
554 0 ▶ for ($i = 0; $i < $maxcol; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $maxcol) 0 0 0
555 0 $row[$i] .= $data[$i];
556 }
557 0 $append = ''; # append done (for now)
558 } else {
559 2 @row = @data; # initialize the row data
560 }
561 # quit the loop unless we have a continuation of data
562 2 ▶ last unless $continue;
Branch: 1 branch
BranchTrueFalseTotal
unless $continue 2 0 2
563 0 $append = $continue;
564 0 $continue = '';
565 }
566 3 ▶ $#row >= 0 ? \@row : undef;
Branch: 1 branch
BranchTrueFalseTotal
$#row >= 0 ? : 2 1 3
567 }
568  
569 # ( $titles, $data, $maxcol) = ReadListInput;
570 #
571 # $titles is a ref to an array of titles.
572 # $data is a ref to an array of rows (arrays of columns) of data.
573 # $maxcol is the maximum number of columns in any one row of the data.
574  
575 # The order of the columns in the resulting array of rows is defined
576 # by the names in the @Titles array, or the order of occurance of the
577 # columns in the lists of column names.
578  
579 0 ▶sub ReadListInput {
Subroutine ReadListInput: not called CC=14 SCAR=53.5
580 0 my @data;
581 my @titles;
582 0 my ($key, $lastkey, $val);
583 0 local $_ = 'go';
584 0 my $maxcol = 0;
585 0 my $titlesref;
586 0 ▶ while (length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 0 0 0
587 0 my %row;
588 # Read another "row" of list data
589 0 while (1) {
590 0 ▶ defined($_ = <STDIN>) or last;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline STDIN) or last 0 0 0
591 0 ▶ last if /^\s*$/; # stop on the row boundary
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
592 0 chomp;
593 0 ($key, $val) = /^\s*([\w_.:\/-]+)?\s*:[ \t]?(.*)/;
594 0 ▶ if ($key ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne '') { } 0 0 0
595 0 $row{$lastkey = $key} = $val;
596 0 ▶ push(@titles,$key) unless grep(/^\Q$key\E/,@titles);
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^\Q$key\E/, @titles 0 0 0
597 } else {
598 $val = ' '.$val
599 0 ▶ unless $val =~ /^ / || $row{$lastkey} =~ / $/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^ / or $row{$lastkey} =~ / $/ 0 0 0
Condition0%0/3
$val =~ /^ / or $row{$lastkey} =~ / $/
l!l&&r!l&&!r
000
MC/DC0%0/2
$val =~ /^ / or $row{$lastkey} =~ / $/
$val =~ /^ / $row{$lastkey} =~ / $/
Truth table0%0/3
A or B
A= $val =~ /^ /B= $row{$lastkey} =~ / $/
ABresult
1X1
011
000
600 0 $row{$lastkey} .= $val;
601 }
602 }
603 0 my @row;
604 0 ▶ $titlesref = $#Titles >= 0 ? \@Titles : \@titles;
Branch: 1 branch
BranchTrueFalseTotal
$#Titles >= 0 ? : 0 0 0
605 0 foreach $column (@$titlesref) {
606 0 ▶ if (exists $row{$column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row{$column}) { } 0 0 0
607 0 push(@row,$row{$column});
608 } else {
609 0 push(@row,undef); # same as SQL NULL
610 }
611 }
612 0 push(@data,\@row); # save the row
613 0 ▶ $maxcol = $#row if $#row > $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $#row > $maxcol 0 0 0
614 }
615 0 ▶ $maxcol = $#titles if $#titles > $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $#titles > $maxcol 0 0 0
616 0 ▶ $titlesref = $#Titles >= 0 ? \@Titles : \@titles;
Branch: 1 branch
BranchTrueFalseTotal
$#Titles >= 0 ? : 0 0 0
617 0 ($titlesref, \@data, $maxcol);
618 }
619  
620 # $max = max @values;
621 # Return the maximum of a list of values
622  
623 0 ▶sub max {
Subroutine max: not called CC=3 SCAR=24.8
624 0 my $max = shift;
625 0 ▶ foreach (@_) { $max = $_ if $_ > $max; }
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 0 0 0
626 0 $max;
627 }
628  
629 __END__
↑ Summary lib/Data/ShowTable.pm »