lib/TUI/StdDlg/Dir.pm

filter: Statementstmt 78.5% 128 / 163 Branchbran 65.0% 65 / 100 Conditioncond 55.5% 20 / 36 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 83.3% 15 / 18 Podpod 100% 7 / 7 totaltotal 70.1% 242 / 345 CC 68 SCAR 33.8
CC
68
Cov
71%
setdisk
56.0
fnsplit
39.6
getcurdir
33.9
CRAP 29.5
« lib/TUI/Dialogs/Label.pm ↑ Summary lib/TUI/App/Program.pm »
1 package TUI::StdDlg::Dir;
2 # ABSTRACT: Replacements for functions in Borland's C/C++ Run Time Library
3
4 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 11 our $VERSION = '2.000002';
9 11 $VERSION =~ tr/_//d;
10 11 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 our @EXPORT_OK = qw(
14 findfirst
15 findnext
16 fexpand
17 fnsplit
18 fnmerge
19 getdisk
20 setdisk
21 getcurdir
22 );
23
24 11 our %EXPORT_TAGS = (
25 all => \@EXPORT_OK,
26 );
27
28 11 ▶use Cwd qw( getcwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 blessed
32 readonly
33 11 );
34 11 ▶use TUI::toolkit qw( :utils );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 ▶use TUI::toolkit::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
36 Maybe
37 is_PositiveOrZeroInt
38 :types
39 11 );
40
41 11 ▶use TUI::StdDlg::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
42 :DIR
43 :MAX
44 11 );
45 11 ▶use TUI::StdDlg::Dos qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
46 _dos_findfirst
47 _dos_findnext
48 11 );
49
50 0 ▶sub findfirst { # $int ($pathname, $ffblk, $attrib)
Subroutine findfirst: not called CC=1 SCAR=6.9
Pod documented
51 0 state $sig = signature(
52 pos => [Str, ArrayLike, PositiveOrZeroInt],
53 );
54 0 my ( $pathname, $ffblk, $attrib ) = $sig->( @_ );
55 0 return _dos_findfirst( $pathname, $attrib, $ffblk );
56 }
57
58 0 ▶sub findnext { # $int ($ffblk)
Subroutine findnext: not called CC=1 SCAR=6.9
Pod documented
59 0 state $sig = signature(
60 pos => [ArrayLike],
61 );
62 0 my ( $ffblk ) = $sig->( @_ );
63 0 return _dos_findnext( $ffblk );
64 }
65
66 # Builds a path from component parts.
67 10 ▶sub fnmerge { # void ($pathP|undef, $driveP|undef, $dirP|undef, $nameP|undef, $extP|undef)
Subroutine fnmerge: called CC=12 SCAR=26.3
Pod documented
68 10 state $sig = signature(
69 pos => [ Maybe[Str], Maybe[Str], Maybe[Str], Maybe[Str], Maybe[Str] ],
70 );
71 10 my ( $pathP, $driveP, $dirP, $nameP, $extP ) = $sig->( @_ );
72 10 ▶ assert ( not readonly $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if &readonly($_[0]) 0 10 10
73 10 $pathP = '';
74
75 10 ▶ if ( $^O eq 'MSWin32' && length $driveP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and length $driveP) 0 10 10
Condition33%1/3
$^O eq "MSWin32" and length $driveP
!ll&&!rl&&r
1000
MC/DC0%0/2
$^O eq "MSWin32" and length $driveP
$^O eq "MSWin32" length $driveP
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= length $driveP
ABresult
0X0
100
111
76 0 $pathP .= $driveP;
77 0 ▶ $pathP .= ':' if substr( $pathP, -1, 1 ) ne ':';
Branch: 1 branch
BranchTrueFalseTotal
if substr($pathP, -1, 1) ne ":" 0 0 0
78 }
79
80 10 ▶ if ( length $dirP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dirP) 10 0 10
81 10 $pathP .= $dirP;
82 10 my $last = substr( $pathP, -1, 1 );
83 10 ▶ if ( $last ne '\\' && $last ne '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last ne "\\" and $last ne "/") 1 9 10
Condition100%3/3
$last ne "\\" and $last ne "/"
!ll&&!rl&&r
811
MC/DC100%2/2
$last ne "\\" and $last ne "/"
$last ne "\\" $last ne "/"
Truth table100%3/3
A and B
A= $last ne "\\"B= $last ne "/"
ABresult
0X0
100
111
84 1 $pathP .= '\\';
85 }
86 }
87 10 ▶ if ( length $nameP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nameP) 10 0 10
88 10 $pathP .= $nameP
89 }
90 10 ▶ if ( length $extP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $extP) 4 6 10
91 4 ▶ $pathP .= '.' if substr( $extP, 0, 1 ) ne '.';
Branch: 1 branch
BranchTrueFalseTotal
if substr($extP, 0, 1) ne "." 1 3 4
92 4 $pathP .= $extP;
93 }
94
95 # fnmerge is often used before accessing files, so producing a
96 # UNIX-formatted pathP fixes most cases of filesystem access.
97 10 ▶ if ( $^O ne 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 10 0 10
98 10 $pathP =~ tr{\\}{/};
99 10 $pathP =~ s{/+}{/}g;
100 }
101
102 10 $_[0] = $pathP;
103 10 return;
104 }
105
106 # Split a Full Path Name into Its Components
107 12 ▶sub fnsplit { # void ($pathP|undef, $driveP|undef, $dirP|undef, $nameP|undef, $extP|undef)
Subroutine fnsplit: called CC=39 SCAR=36.8
Pod documented
108 12 state $sig = signature(
109 pos => [ Maybe[Str], Maybe[Str], Maybe[Str], Maybe[Str], Maybe[Str] ],
110 );
111 12 my ( $pathP, $driveP, $dirP, $nameP, $extP ) = $sig->( @_ );
112 12 alias: for $driveP ( $_[1] ) {
113 12 alias: for $dirP ( $_[2] ) {
114 12 alias: for $nameP ( $_[3] ) {
115 12 alias: for $extP ( $_[4] ) {
116
117 12 my $flags = 0;
118
119 12 ▶ $driveP = '' unless readonly $driveP;
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($driveP) 1 11 12
120 12 ▶ $dirP = '' unless readonly $dirP;
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($dirP) 10 2 12
121 12 ▶ $nameP = '' unless readonly $nameP;
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($nameP) 10 2 12
122 12 ▶ $extP = '' unless readonly $extP;
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($extP) 10 2 12
123
124 12 ▶ if ( my $len = length $pathP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length $pathP) 12 0 12
125 12 my $caretP;
126 my $slashP; # Rightmost slash
127 12 my $lastDotP; # Last dot in filename
128 12 my $firstDotP; # First dot in filename
129 12 ▶ for ( my $i = $len - 1; $i >= 0; --$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 165 12 177
130 165 SWITCH: for ( substr( $pathP, $i, 1 ) ) {
131 $_ eq '?' ||
132 165 ▶ $_ eq '*' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "?" || $_ eq "*" and do { $flags |= 1 unless defined $slashP; last } 2 163 165
Condition66%2/3
$_ eq "?" or $_ eq "*"
l!l&&r!l&&!r
02163
MC/DC50%1/2
$_ eq "?" or $_ eq "*"
$_ eq "?" $_ eq "*"
Truth table66%2/3
A or B
A= $_ eq "?"B= $_ eq "*"
ABresult
1X1
011
000
133 # Wildcards are only detected in filename or extension.
134 2 ▶ $flags |= WILDCARDS if !defined $slashP;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slashP 1 1 2
135 2 last;
136 };
137
138 163 ▶ $_ eq '.' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "." and do { unless (defined $slashP) { $lastDotP = $i unless defined $lastDotP; $firstDotP = $i; } ; last } 11 152 163
139 11 ▶ if ( !defined $slashP ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slashP) 6 5 11
140 6 ▶ $lastDotP = $i if !defined $lastDotP;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lastDotP 6 0 6
141 6 $firstDotP = $i;
142 }
143 11 last;
144 };
145
146 $_ eq '\\' ||
147 152 ▶ $_ eq '/' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "\\" || $_ eq "/" and do { $slashP = $i unless defined $slashP; last } 26 126 152
Condition100%3/3
$_ eq "\\" or $_ eq "/"
l!l&&r!l&&!r
1016126
MC/DC100%2/2
$_ eq "\\" or $_ eq "/"
$_ eq "\\" $_ eq "/"
Truth table100%3/3
A or B
A= $_ eq "\\"B= $_ eq "/"
ABresult
1X1
011
000
148 26 ▶ $slashP = $i if !defined $slashP;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slashP 12 14 26
149 26 last;
150 };
151
152 126 ▶ $_ eq ':' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq ":" and do { if ($i == 1) { $caretP = $i; $i = 0; } } 2 124 126
153 2 ▶ if ( $i == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) 2 0 2
154 2 $caretP = $i;
155 2 $i = 0; # Exit loop, we don't check the drive letter.
156 }
157 };
158
159 DEFAULT:
160 126 }
161 }
162
163 # These variables point after the last character of each component.
164 12 ▶ my $driveEnd = defined $caretP ? $caretP + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $caretP ? : 2 10 12
165 12 ▶ my $dirEnd = defined $slashP ? $slashP + 1 : $driveEnd;
Branch: 1 branch
BranchTrueFalseTotal
defined $slashP ? : 12 0 12
166 12 ▶ my $nameEnd = defined $lastDotP ? $lastDotP : $len;
Branch: 1 branch
BranchTrueFalseTotal
defined $lastDotP ? : 6 6 12
167
168 # Special case: pathP ends with '.' or '..', thus there's no filename.
169 12 ▶ if ( defined $lastDotP && $lastDotP == $len - 1
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastDotP and $lastDotP == $len - 1 and $lastDotP - $firstDotP < 2 and defined $firstDotP and $firstDotP == $dirEnd) 1 11 12
Condition75%9/12
defined $lastDotP and $lastDotP == $len - 1 and $lastDotP - $firstDotP < 2 and defined $firstDotP and $firstDotP == $dirEnd
!ll&&!rl&&r
1101
defined $lastDotP and $lastDotP == $len - 1 and $lastDotP - $firstDotP < 2 and defined $firstDotP
!ll&&!rl&&r
1101
defined $lastDotP and $lastDotP == $len - 1 and $lastDotP - $firstDotP < 2
!ll&&!rl&&r
1101
defined $lastDotP and $lastDotP == $len - 1
!ll&&!rl&&r
651
MC/DC40%2/5
defined $lastDotP and $lastDotP == $len - 1 and $lastDotP - $firstDotP < 2 and defined $firstDotP and $firstDotP == $dirEnd
defined $lastDotP $lastDotP == $len - 1 $lastDotP - $firstDotP < 2 defined $firstDotP $firstDotP == $dirEnd
Truth table50%3/6
A and B and C and D and E
A= defined $lastDotPB= $lastDotP == $len - 1C= $lastDotP - $firstDotP < 2D= defined $firstDotPE= $firstDotP == $dirEnd
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
170 && ( $lastDotP - $firstDotP ) < 2
171 && defined $firstDotP && $firstDotP == $dirEnd
172 ) {
173 1 $dirEnd = $nameEnd = $len;
174 }
175
176 # Copy components and set flags.
177 12 ▶ if ( $driveEnd != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driveEnd != 0) 2 10 12
178 2 ▶ $driveP = substr( $pathP, 0, min( $driveEnd, MAXDRIVE ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($driveP) 1 1 2
179 if !readonly $driveP;
180 2 $flags |= DRIVE;
181 }
182 12 ▶ if ( $dirEnd != $driveEnd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirEnd != $driveEnd) 12 0 12
183 12 ▶ $dirP = substr( $pathP, $driveEnd, min( $dirEnd - $driveEnd, MAXDIR ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($dirP) 10 2 12
184 if !readonly $dirP;
185 12 $flags |= DIRECTORY;
186 }
187 12 ▶ if ( $nameEnd != $dirEnd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nameEnd != $dirEnd) 11 1 12
188 11 ▶ $nameP = substr( $pathP, $dirEnd, min( $nameEnd - $dirEnd, MAXFILE ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($nameP) 9 2 11
189 if !readonly $nameP;
190 11 $flags |= FILENAME;
191 }
192 12 ▶ if ( $len != $nameEnd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != $nameEnd) 5 7 12
193 5 ▶ $extP = substr( $pathP, $nameEnd, min( $len - $nameEnd, MAXEXT ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &readonly($extP) 3 2 5
194 if !readonly $extP;
195 5 $flags |= EXTENSION;
196 }
197 }
198
199 12 return $flags;
200 }}}} #/ alias:
201 }
202
203 # $direc is an Str where the directory name will be placed, without drive
204 # specification nor leading backslash.
205 # B<Note>: that drive 0 is the I<default> drive, C<1> is drive C<A>, etc.
206 10 ▶sub getcurdir { # $int ($drive, $direc|undef)
Subroutine getcurdir: called CC=11 SCAR=35.2
Pod documented
207 10 state $sig = signature(
208 pos => [ Int, Maybe[Str] ],
209 );
210 10 my ( $drive, $direc ) = $sig->( @_ );
211 10 ▶ assert ( not readonly $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if &readonly($_[1]) 0 10 10
212 10 $direc = '';
213
214 10 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 10 10
215 0 ▶ my $idx = $drive ? $drive - 1 : getdisk(); # 0-based A=0
Branch: 1 branch
BranchTrueFalseTotal
$drive ? : 0 0 0
216 0 ▶ return -1 if $idx < 0 || $idx > 25;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 or $idx > 25 0 0 0
Condition0%0/3
$idx < 0 or $idx > 25
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx < 0 or $idx > 25
$idx < 0 $idx > 25
Truth table0%0/3
A or B
A= $idx < 0B= $idx > 25
ABresult
1X1
011
000
217
218 0 my $letter = chr( ord( 'A' ) + $idx );
219 0 my $full = Cwd::getdcwd( $letter . ':' );
220 0 ▶ return -1 if !defined( $full ) || $full eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $full or $full eq "" 0 0 0
Condition0%0/3
not defined $full or $full eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $full or $full eq ""
not defined $full $full eq ""
Truth table0%0/3
not(A) or B
A= not defined $fullB= $full eq ""
ABresult
1X1
011
000
221
222 0 $full =~ tr{\/}{\\};
223 0 $full =~ s/\A[A-Za-z]:\\//;
224
225 # Ensure no leading backslash (should already be none after stripping)
226 0 $full =~ s/\A\\+//;
227
228 0 $_[1] = $direc = $full;
229 0 return 0;
230 }
231
232 10 ▶ return -1 unless $drive == 0 || ( $drive - 1 ) == getdisk();
Branch: 1 branch
BranchTrueFalseTotal
unless $drive == 0 or $drive - 1 == getdisk() 0 10 10
Condition66%2/3
$drive == 0 or $drive - 1 == getdisk()
l!l&&r!l&&!r
190
MC/DC0%0/2
$drive == 0 or $drive - 1 == getdisk()
$drive == 0 $drive - 1 == getdisk()
Truth table66%2/3
A or B
A= $drive == 0B= $drive - 1 == getdisk()
ABresult
1X1
011
000
233
234 10 my $cwd = Cwd::getcwd();
235 10 ▶ return -1 unless length $cwd;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cwd 0 10 10
236
237 10 $cwd =~ tr{/}{\\};
238 10 $cwd =~ s{\\+}{\\}g;
239 10 $cwd =~ s/\A\\//;
240
241 10 $_[1] = $direc = $cwd;
242 10 return 0;
243 }
244
245 # Return current drive index (A=0, B=1, C=2, ...)
246 17 ▶sub getdisk { # $int ()
Subroutine getdisk: called CC=3 SCAR=15.7
Pod documented
247 17 state $sig = signature(
248 pos => [],
249 );
250 17 $sig->( @_ );
251 17 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 17 17
252 0 require Win32;
253 0 ▶ if ( my $cwd = Win32::GetCwd() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cwd = Win32::GetCwd()) 0 0 0
254 0 my $letter = substr( $cwd, 0, 1 );
255 0 return ord( $letter ) - ord( 'A' );
256 }
257 }
258 # Emulate drive C.
259 17 return ord( 'C' ) - ord( 'A' );
260 }
261
262 0 ▶sub setdisk { # $int ($drive)
Subroutine setdisk: not called CC=7 SCAR=40.3
Pod documented
263 0 state $sig = signature(
264 pos => [Int],
265 );
266 0 my ( $drive ) = $sig->( @_ );
267
268 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
269 0 require Win32;
270 0 require Win32API::File;
271 0 ▶ return -1 unless $drive >= 1 && $drive <= 26;
Branch: 1 branch
BranchTrueFalseTotal
unless $drive >= 1 and $drive <= 26 0 0 0
Condition0%0/3
$drive >= 1 and $drive <= 26
!ll&&!rl&&r
000
MC/DC0%0/2
$drive >= 1 and $drive <= 26
$drive >= 1 $drive <= 26
Truth table0%0/3
A and B
A= $drive >= 1B= $drive <= 26
ABresult
0X0
100
111
272 0 my $letter = chr( ord( 'A' ) + $drive - 1 );
273 0 ▶ Win32::SetCwd( "${letter}:\\" ) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
Win32::SetCwd("${letter}:\\") or return -1 0 0 0
274 0 my $mask = Win32API::File::GetLogicalDrives();
275 0 my $count = 0;
276 0 ▶ $count++ while $mask &= $mask - 1;
Branch: 1 branch
BranchTrueFalseTotal
$mask &= $mask - 1 and do { ++$count } 0 0 0
277 0 return $count;
278 }
279
280 0 ▶ return $drive == getdisk() ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$drive == getdisk() ? : 0 0 0
281 }
282
283 11 1
284
285 __END__
« lib/TUI/Dialogs/Label.pm ↑ Summary lib/TUI/App/Program.pm »