blib/lib/File/Glob/Windows.pm

filter: Statementstmt 36.7% 47 / 128 Branchbran 5.3% 5 / 94 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 70.5% 12 / 17 Podpod 100% 5 / 5 totaltotal 26.0% 70 / 269 CC 60 SCAR 71.1
CC
60
Cov
22%
glob
2070.0
getCodePage_A
25.3
getCodePage_B
10.8
CRAP 1229.0
↑ Summary
1 #!perl
2 package File::Glob::Windows;
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DirHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 3 our $VERSION="0.1.5";
13
14 3 our @ISA = qw(Exporter);
15 3 our @EXPORT = qw( glob );
16 3 our @EXPORT_OK = qw( glob getCodePage getCodePage_A getCodePage_B getCodePage_POSIX);
17
18 ##############################################
19 # find native encoding
20
21 3 ▶sub getCodePage_A{
Subroutine getCodePage_A: called CC=6 SCAR=32.3
Pod documented
22 3 ▶ eval "require Win32::TieRegistry"; $@ and return;
Branch: 1 branch
BranchTrueFalseTotal
$@ and return 3 0 3
23 0 ▶ my $key = Win32::TieRegistry->new('HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/CodePage',{Delimiter=>"/"}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $key = "Win32::TieRegistry"->new("HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/CodePage", {"Delimiter", "/"}) or return 0 0 0
24 0 for('Default','ACP','OEMCP'){
25 0 my $v = $key->GetValue($_);
26 0 ▶ return "cp$v" if defined($v) and $v=~/(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v =~ /(\d+)/ 0 0 0
Condition0%0/3
defined $v and $v =~ /(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and $v =~ /(\d+)/
defined $v $v =~ /(\d+)/
Truth table0%0/3
A and B
A= defined $vB= $v =~ /(\d+)/
ABresult
0X0
100
111
27 }
28 0 return;
29 }
30 3 ▶sub getCodePage_B{
Subroutine getCodePage_B: called CC=4 SCAR=23.7
Pod documented
31 3 ▶ eval "require Win32::API"; $@ and return;
Branch: 1 branch
BranchTrueFalseTotal
$@ and return 3 0 3
32 0 ▶ my $f=Win32::API->new("Kernel32", "GetACP", '', 'N') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $f = "Win32::API"->new("Kernel32", "GetACP", "", "N") or return 0 0 0
33 0 ▶ my $v= $f->Call; $v and return "cp$v";
Branch: 1 branch
BranchTrueFalseTotal
$v and return "cp$v" 0 0 0
34 0 return;
35 }
36
37 3 ▶sub getCodePage_POSIX {
Subroutine getCodePage_POSIX: called CC=3 SCAR=12.0
Pod documented
38 3 require POSIX;
39 3 my $v = POSIX::setlocale( &POSIX::LC_CTYPE );
40 #~ LC_TYPE returns
41 #~ English_United States.1252
42 #~ which matches ...Control/Nls/CodePage
43 #~ (default)=(value not set)
44 #~ ACP=1252
45 #~ OEMCP=437
46 3 ▶ return "cp$1" if defined($v) and $v=~/(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v =~ /(\d+)$/ 0 3 3
Condition33%1/3
defined $v and $v =~ /(\d+)$/
!ll&&!rl&&r
030
MC/DC0%0/2
defined $v and $v =~ /(\d+)$/
defined $v $v =~ /(\d+)$/
Truth table33%1/3
A and B
A= defined $vB= $v =~ /(\d+)$/
ABresult
0X0
100
111
47 3 return;
48 }
49
50
51 3 ▶sub getCodePage{
Subroutine getCodePage: called CC=4 SCAR=14.2
Pod documented
52 3 for( \&getCodePage_B,\&getCodePage_A,\&getCodePage_POSIX ){
53 9 my $cp = eval{ &$_ };
54 9 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
55 9 ▶ $cp and return $cp;
Branch: 1 branch
BranchTrueFalseTotal
$cp and return $cp 0 9 9
56 }
57 3 return;
58 }
59
60 ##############################################
61
62 # public options
63 3 our $encoding = getCodePage();
64 3 our $sorttype = 0;
65 3 our $nocase = 1;
66
67
68 3 our %alpha;
69 our %glob_sortfunc=(
70 0 ▶ 1=>sub{ $a->[2] cmp $b->[2] }, # name order
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 ▶ 2=>sub{$b->[0] <=> $a->[0] or $a->[2] cmp $b->[2] }, # directory and name
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$File::Glob::Windows::b->[0] <=> $File::Glob::Windows::a->[0] or $File::Glob::Windows::a->[2] cmp $File::Glob::Windows::b->[2] 0 0 0
72 0 ▶ 3=>sub{$a->[0] <=> $b->[0] or $a->[2] cmp $b->[2] }, # fine and name
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$File::Glob::Windows::a->[0] <=> $File::Glob::Windows::b->[0] or $File::Glob::Windows::a->[2] cmp $File::Glob::Windows::b->[2] 0 0 0
73 0 ▶ 4=>sub{ $b->[2] cmp $a->[2] }, # name desc
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 3 );
75
76 0 ▶sub glob{
Subroutine glob: not called CC=45 SCAR=76.4
Pod documented
77 0 my($path)=@_;
78 # check input
79 0 ▶ (not defined $path or $path eq '') and croak "path is not specified";
Branch: 1 branch
BranchTrueFalseTotal
!defined($path) || $path eq "" and croak("path is not specified") 0 0 0
Condition0%0/3
not defined $path or $path eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $path or $path eq ""
not defined $path $path eq ""
Truth table0%0/3
not(A) or B
A= not defined $pathB= $path eq ""
ABresult
1X1
011
000
80 # check encoding
81 0 my $enc = Encode::find_encoding($encoding);
82 0 ▶ ref($enc) or croak "encoding is not specified";
Branch: 1 branch
BranchTrueFalseTotal
ref $enc or croak("encoding is not specified") 0 0 0
83
84 0 my $sortfunc = $glob_sortfunc{$sorttype};
85
86 # read volume and root
87 0 ▶ utf8::is_utf8($path) or $path = Encode::decode($enc,$path);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($path) or $path = &Encode::decode($enc, $path) 0 0 0
88 0 my $top='';
89 0 ▶ $path =~s!^([^:]+:|\\\\[^\\]+)!! and $top .=$1;
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s/^([^:]+:|\\\\[^\\]+)// and $top .= $1 0 0 0
90 0 ▶ $path =~s!^([\\/]+)!! and $top .='\\';
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s[^([\\/]+)][] and $top .= '\\' 0 0 0
91 0 $top= Encode::encode($enc,$top);
92 0 ▶ ($path eq '') and return ($top);
Branch: 1 branch
BranchTrueFalseTotal
$path eq "" and return $top 0 0 0
93
94 # split path and convert wildcard to regex
95 0 my @node;
96 0 my $re1 = Encode::encode($enc,'.*?');
97 0 my $re2 = Encode::encode($enc,'.');
98 0 ▶ if($nocase and not %alpha){ $alpha{$_}=1 for 'A'..'Z','a'..'z';}
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Glob::Windows::nocase and not %File::Glob::Windows::alpha) 0 0 0
Condition0%0/3
$File::Glob::Windows::nocase and not %File::Glob::Windows::alpha
!ll&&!rl&&r
000
MC/DC0%0/2
$File::Glob::Windows::nocase and not %File::Glob::Windows::alpha
$File::Glob::Windows::nocase not %File::Glob::Windows::alpha
Truth table0%0/3
A and not(B)
A= $File::Glob::Windows::nocaseB= not %File::Glob::Windows::alpha
ABresult
0X0
100
111
99 0 for my $t (split m![\\/]+!,$path){
100 0 ▶ next if $t eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "" 0 0 0
101 0 ▶ if( not $t =~ /[*?]/ ){ push @node,Encode::encode($enc,$t); next; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($t =~ /[*?]/) 0 0 0
102 0 my $r='';
103 0 ▶ if($nocase){
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Glob::Windows::nocase) { } 0 0 0
104 0 for(split /([*?A-Za-z])/,$t){
105 0 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 0 0
106 0 ▶ if($_ eq '*' ){ $r.=$re1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($_ eq '*') { } 0 0 0
elsif ($_ eq '?') { } 0 0 0
elsif ($File::Glob::Windows::alpha{$_}) { } 0 0 0
107 0 elsif($_ eq '?' ){ $r.=$re2 }
108 0 elsif($alpha{$_} ){ $r.=Encode::encode($enc,'['.uc($_).lc($_).']') }
109 0 else{ $r .= quotemeta(Encode::encode($enc,$_)) }
110 }
111 }else{
112 0 for(split /([*?])/,$t){
113 0 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 0 0
114 0 ▶ if($_ eq '*' ){ $r.=$re1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '*') { } 0 0 0
elsif ($_ eq '?') { } 0 0 0
115 0 elsif($_ eq '?' ){ $r.=$re2 }
116 0 else{ $r .= quotemeta(Encode::encode($enc,$_)) }
117 }
118 }
119 0 ▶ utf8::is_utf8($r) and die "bad implement. pattern is_utf8 !!\n";
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($r) and die "bad implement. pattern is_utf8 !!\n" 0 0 0
120 0 push @node,qr/^$r$/;
121 }
122
123 # directory search
124 0 my @result;
125 0 my @stack=([0,'']);
126 0 ▶ while(@stack){
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 0 0
127 0 my($level,$prefix)=@{shift @stack};
128 0 ▶ if($level==-1){ push @result,$prefix; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($level == -1) 0 0 0
129 0 my($replace,$separator,$parent,$spec) = (0,'\\',$top.$prefix,$node[$level++]);
130 0 ▶ if($parent eq '' ){ ($parent,$replace)=('.',1); }
Branch: 2 branches
BranchTrueFalseTotal
if ($parent eq '') { } 0 0 0
elsif (length $top and not length $prefix) { } 0 0 0
131 0 ▶ elsif(length($top) and not length($prefix) ){ $separator =''; }
Condition0%0/3
length $top and not length $prefix
!ll&&!rl&&r
000
MC/DC0%0/2
length $top and not length $prefix
length $top not length $prefix
Truth table0%0/3
A and not(B)
A= length $topB= not length $prefix
ABresult
0X0
100
111
132
133 0 my @list;
134 0 ▶ if(ref $spec){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec) { } 0 0 0
135 0 ▶ my $d = new DirHandle($parent) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $d = "DirHandle"->new($parent) or next 0 0 0
136 0 ▶ while(defined( $_=$d->read )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $d->read)) 0 0 0
137 0 ▶ next if not $_ =~ $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$spec/ 0 0 0
138 0 ▶ my $path = ($replace?$_:"$parent$separator$_");
Branch: 1 branch
BranchTrueFalseTotal
$replace ? : 0 0 0
139 0 ▶ if($level==@node){ push @list,[-1,$path,$_]; }
Branch: 2 branches
BranchTrueFalseTotal
if ($level == @node) { } 0 0 0
elsif (-d $path) { } 0 0 0
140 0 ▶ elsif(-d $path){ push @list,[$level,($replace?$_:"$prefix$separator$_"),$_]; }
Branch: 1 branch
BranchTrueFalseTotal
$replace ? : 0 0 0
141 }
142 0 ▶ $sortfunc and @list = sort $sortfunc @list;
Branch: 1 branch
BranchTrueFalseTotal
$sortfunc and @list = sort $sortfunc @list 0 0 0
143 0 pop @$_ for @list;
144 0 splice @stack,0,0,@list;
145 }else{
146 0 ▶ my $path = ($replace?$spec:"$parent$separator$spec");
Branch: 1 branch
BranchTrueFalseTotal
$replace ? : 0 0 0
147 0 ▶ next if not -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
148 0 ▶ if($level==@node){ push @result,$path; }
Branch: 2 branches
BranchTrueFalseTotal
if ($level == @node) { } 0 0 0
elsif (-d _) { } 0 0 0
149 0 ▶ elsif(-d _){ unshift @stack,[$level,($replace?$spec:"$prefix$separator$spec")]; }
Branch: 1 branch
BranchTrueFalseTotal
$replace ? : 0 0 0
150 }
151 }
152 0 return @result;
153 }
154
155 3 1;
156
157 __END__
↑ Summary