| 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
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $key = Win32::TieRegistry->new('HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/CodePage',{Delimiter=>"/"}) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $v and $v =~ /(\d+)/
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+)/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $f=Win32::API->new("Kernel32", "GetACP", '', 'N') or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $v= $f->Call; $v and return "cp$v"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $v and $v =~ /(\d+)$/
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+)$/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | $cp and return $cp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $path or $path eq ""
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 ""
| |||||||||||||||||||||||||||||
| 80 | # check encoding | ||||||||||||||||||||||||||||
| 81 | 0 | my $enc = Encode::find_encoding($encoding); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | ref($enc) or croak "encoding is not specified"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 0 | my $top=''; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | $path =~s!^([^:]+:|\\\\[^\\]+)!! and $top .=$1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $path =~s!^([\\/]+)!! and $top .='\\'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $top= Encode::encode($enc,$top); | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | ($path eq '') and return ($top); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$File::Glob::Windows::nocase and not %File::Glob::Windows::alpha
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
| |||||||||||||||||||||||||||||
| 99 | 0 | for my $t (split m![\\/]+!,$path){ | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | next if $t eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if( not $t =~ /[*?]/ ){ push @node,Encode::encode($enc,$t); next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my $r=''; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | if($nocase){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | for(split /([*?A-Za-z])/,$t){ | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | next if $_ eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if($_ eq '*' ){ $r.=$re1 } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if($_ eq '*' ){ $r.=$re1 } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | 0 | my($level,$prefix)=@{shift @stack}; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | if($level==-1){ push @result,$prefix; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | my($replace,$separator,$parent,$spec) = (0,'\\',$top.$prefix,$node[$level++]); | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if($parent eq '' ){ ($parent,$replace)=('.',1); } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | elsif(length($top) and not length($prefix) ){ $separator =''; } | ||||||||||||||||||||||||||
|
Condition0%0/3
length $top and not length $prefix
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
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my @list; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | if(ref $spec){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $d = new DirHandle($parent) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | while(defined( $_=$d->read )){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | next if not $_ =~ $spec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $path = ($replace?$_:"$parent$separator$_"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if($level==@node){ push @list,[-1,$path,$_]; } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | elsif(-d $path){ push @list,[$level,($replace?$_:"$prefix$separator$_"),$_]; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $sortfunc and @list = sort $sortfunc @list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | next if not -e $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if($level==@node){ push @result,$path; } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | elsif(-d _){ unshift @stack,[$level,($replace?$spec:"$prefix$separator$spec")]; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 0 | return @result; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 3 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||