blib/lib/File/GlobMapper.pm

filter: Statementstmt 92.8% 156 / 168 Branchbran 82.9% 68 / 82 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 4 totaltotal 83.6% 241 / 288 CC 48 SCAR 23.5
CC
48
Cov
87%
_parseBit
16.3
_parseInputGlob
15.0
new
9.2
CRAP 10.5
« blib/lib/IO/Compress/Base.pm ↑ Summary blib/lib/IO/Compress/Base/Common.pm »
1 package File::GlobMapper;
2  
3 130 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 130 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 130 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 130 our ($CSH_GLOB);
8  
9 BEGIN
10 {
11 130 ▶ if ($] < 5.006)
Subroutine BEGIN: called CC=2 SCAR=8.7
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.006) { } 0 130 130
12 {
13 0 require File::BSDGlob; File::BSDGlob->import(':glob');
14 0 $CSH_GLOB = File::BSDGlob::GLOB_CSH();
15 0 *globber = \&File::BSDGlob::csh_glob;
16 }
17 else
18 {
19 130 require File::Glob; File::Glob->import(':glob');
20 130 $CSH_GLOB = File::Glob::GLOB_CSH();
21 #*globber = \&File::Glob::bsd_glob;
22 130 *globber = \&File::Glob::csh_glob;
23 }
24 }
25  
26 130 our ($Error);
27  
28 130 our ($VERSION, @EXPORT_OK);
29 130 $VERSION = '1.001';
30 130 @EXPORT_OK = qw( globmap );
31  
32 130 our $BEGIN_DELIM = "\xFF";
33 130 our $END_DELIM = "\xFE";
34 130 our $BACKSLASH_ESC = "\xFD";
35 130 our $HASH_ESC = "\xFC";
36 130 our $STAR_ESC = "\xFB";
37  
38 130 our ($noPreBS, $metachars, $matchMetaRE, %mapping, %wildCount);
39 130 $noPreBS = '(?<!\\\)' ; # no preceding backslash
40 130 $metachars = '.*?[](){}';
41 130 $matchMetaRE = '[' . quotemeta($metachars) . ']';
42  
43 130 %mapping = (
44 '*' => '([^/]*)',
45 '?' => '([^/])',
46 '.' => '\.',
47 '[' => '([',
48 '(' => '(',
49 ')' => ')',
50 );
51  
52 130 %wildCount = map { $_ => 1 } qw/ * ? . { ( [ /;
53  
54 sub globmap ($$;)
55 {
56 4 ▶ my $inputGlob = shift ;
Subroutine globmap: called CC=2 SCAR=7.0
Pod undocumented
57 4 my $outputGlob = shift ;
58  
59 4 ▶ my $obj = File::GlobMapper->new($inputGlob, $outputGlob, @_)
Branch: 1 branch
BranchTrueFalseTotal
my $obj = "File::GlobMapper"->new($inputGlob, $outputGlob, @_) or croak("globmap: $File::GlobMapper::Error") 0 4 4
60 or croak "globmap: $Error" ;
61 4 return $obj->getFileMap();
62 }
63  
64 sub new
65 {
66 59 ▶ my $class = shift ;
Subroutine new: called CC=8 SCAR=22.2
Pod undocumented
67 59 my $inputGlob = shift ;
68 59 my $outputGlob = shift ;
69 # TODO -- flags needs to default to whatever File::Glob does
70 59 ▶ my $flags = shift || $CSH_GLOB ;
Condition33%1/3
shift() || $File::GlobMapper::CSH_GLOB
l!l&&r!l&&!r
0590
MC/DC0%0/2
shift() || $File::GlobMapper::CSH_GLOB
shift() $File::GlobMapper::CSH_GLOB
Truth table33%1/3
A || B
A= shift()B= $File::GlobMapper::CSH_GLOB
ABresult
1X1
011
000
71 #my $flags = shift ;
72  
73 59 $inputGlob =~ s/^\s*\<\s*//;
74 59 $inputGlob =~ s/\s*\>\s*$//;
75  
76 59 $outputGlob =~ s/^\s*\<\s*//;
77 59 $outputGlob =~ s/\s*\>\s*$//;
78  
79 59 my %object =
80 ( InputGlob => $inputGlob,
81 OutputGlob => $outputGlob,
82 GlobFlags => $flags,
83 Braces => 0,
84 WildCount => 0,
85 Pairs => [],
86 Sigil => '#',
87 );
88  
89 59 ▶ my $self = bless \%object, ref($class) || $class ;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0590
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
90  
91 59 ▶ $self->_parseInputGlob()
Branch: 1 branch
BranchTrueFalseTotal
$self->_parseInputGlob or return undef 25 34 59
92 or return undef ;
93  
94 34 ▶ $self->_parseOutputGlob()
Branch: 1 branch
BranchTrueFalseTotal
$self->_parseOutputGlob or return undef 0 34 34
95 or return undef ;
96  
97 34 my @inputFiles = globber($self->{InputGlob}, $flags) ;
98  
99 34 ▶ if (GLOB_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if (GLOB_ERROR()) 0 34 34
100 {
101 0 $Error = $!;
102 0 return undef ;
103 }
104  
105 #if (whatever)
106 {
107 34 my $missing = grep { ! -e $_ } @inputFiles ;
108  
109 34 ▶ if ($missing)
Branch: 1 branch
BranchTrueFalseTotal
if ($missing) 0 34 34
110 {
111 0 $Error = "$missing input files do not exist";
112 0 return undef ;
113 }
114 }
115  
116 34 $self->{InputFiles} = \@inputFiles ;
117  
118 34 ▶ $self->_getFiles()
Branch: 1 branch
BranchTrueFalseTotal
$self->_getFiles or return undef 1 33 34
119 or return undef ;
120  
121 33 return $self;
122 }
123  
124 sub _retError
125 {
126 25 ▶ my $string = shift ;
Subroutine _retError: called CC=1 SCAR=0.0
127 25 $Error = "$string in input fileglob" ;
128 25 return undef ;
129 }
130  
131 sub _unmatched
132 {
133 25 ▶ my $delimeter = shift ;
Subroutine _unmatched: called CC=1 SCAR=0.0
134  
135 25 _retError("Unmatched $delimeter");
136 25 return undef ;
137 }
138  
139 sub _parseBit
140 {
141 7 ▶ my $self = shift ;
Subroutine _parseBit: called CC=15 SCAR=27.9
142  
143 7 my $string = shift ;
144  
145 7 my $out = '';
146 7 my $depth = 0 ;
147  
148 7 ▶ while ($string =~ s/(.*?)$noPreBS(,|$matchMetaRE)//)
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/(.*?)$File::GlobMapper::noPreBS(,|$File::GlobMapper::matchMetaRE)//) 9 4 13
149 {
150 9 $out .= quotemeta($1) ;
151 9 ▶ $out .= $mapping{$2} if defined $mapping{$2};
Branch: 1 branch
BranchTrueFalseTotal
if defined $File::GlobMapper::mapping{$2} 5 4 9
152  
153 9 ▶ ++ $self->{WildCount} if $wildCount{$2} ;
Branch: 1 branch
BranchTrueFalseTotal
if $File::GlobMapper::wildCount{$2} 4 5 9
154  
155 9 ▶ if ($2 eq ',')
Branch: 6 branches
BranchTrueFalseTotal
if ($2 eq ',') { } 3 6 9
elsif ($2 eq '(') { } 1 5 6
elsif ($2 eq ')') { } 1 4 5
elsif ($2 eq '[') { } 1 3 4
elsif ($2 eq ']') { } 1 2 3
elsif ($2 eq '{' or $2 eq '}') { } 0 2 2
156 {
157 3 ▶ return _unmatched("(")
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 3 3
158 if $depth ;
159  
160 3 $out .= '|';
161 }
162 elsif ($2 eq '(')
163 {
164 1 ++ $depth ;
165 }
166 elsif ($2 eq ')')
167 {
168 1 ▶ return _unmatched(")")
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 1 0 1
169 if ! $depth ;
170  
171 0 -- $depth ;
172 }
173 elsif ($2 eq '[')
174 {
175 # TODO -- quotemeta & check no '/'
176 # TODO -- check for \] & other \ within the []
177 1 ▶ $string =~ s#(.*?\])##
Branch: 1 branch
BranchTrueFalseTotal
$string =~ s/(.*?\])// or return _unmatched("[") 1 0 1
178 or return _unmatched("[");
179 0 $out .= "$1)" ;
180 }
181 elsif ($2 eq ']')
182 {
183 1 return _unmatched("]");
184 }
185 ▶ elsif ($2 eq '{' || $2 eq '}')
Condition33%1/3
$2 eq "{" or $2 eq "}"
l!l&&r!l&&!r
002
MC/DC0%0/2
$2 eq "{" or $2 eq "}"
$2 eq "{" $2 eq "}"
Truth table33%1/3
A or B
A= $2 eq "{"B= $2 eq "}"
ABresult
1X1
011
000
186 {
187 0 return _retError("Nested {} not allowed");
188 }
189 }
190  
191 4 $out .= quotemeta $string;
192  
193 4 ▶ return _unmatched("(")
Branch: 1 branch
BranchTrueFalseTotal
if $depth 1 3 4
194 if $depth ;
195  
196 3 return $out ;
197 }
198  
199 sub _parseInputGlob
200 {
201 59 ▶ my $self = shift ;
Subroutine _parseInputGlob: called CC=15 SCAR=27.1
202  
203 59 my $string = $self->{InputGlob} ;
204 59 my $inGlob = '';
205  
206 # Multiple concatenated *'s don't make sense
207 #$string =~ s#\*\*+#*# ;
208  
209 # TODO -- Allow space to delimit patterns?
210 #my @strings = split /\s+/, $string ;
211 #for my $str (@strings)
212 59 my $out = '';
213 59 my $depth = 0 ;
214  
215 59 ▶ while ($string =~ s/(.*?)$noPreBS($matchMetaRE)//)
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/(.*?)$File::GlobMapper::noPreBS($File::GlobMapper::matchMetaRE)//) 135 35 170
216 {
217 135 $out .= quotemeta($1) ;
218 135 ▶ $out .= $mapping{$2} if defined $mapping{$2};
Branch: 1 branch
BranchTrueFalseTotal
if defined $File::GlobMapper::mapping{$2} 125 10 135
219 135 ▶ ++ $self->{WildCount} if $wildCount{$2} ;
Branch: 1 branch
BranchTrueFalseTotal
if $File::GlobMapper::wildCount{$2} 115 20 135
220  
221 135 ▶ if ($2 eq '(')
Branch: 6 branches
BranchTrueFalseTotal
if ($2 eq '(') { } 3 132 135
elsif ($2 eq ')') { } 18 114 132
elsif ($2 eq '[') { } 2 112 114
elsif ($2 eq ']') { } 1 111 112
elsif ($2 eq '}') { } 1 110 111
elsif ($2 eq '{') { } 8 102 110
222 {
223 3 ++ $depth ;
224 }
225 elsif ($2 eq ')')
226 {
227 18 ▶ return _unmatched(")")
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 16 2 18
228 if ! $depth ;
229  
230 2 -- $depth ;
231 }
232 elsif ($2 eq '[')
233 {
234 # TODO -- quotemeta & check no '/' or '(' or ')'
235 # TODO -- check for \] & other \ within the []
236 2 ▶ $string =~ s#(.*?\])##
Branch: 1 branch
BranchTrueFalseTotal
$string =~ s/(.*?\])// or return _unmatched("[") 1 1 2
237 or return _unmatched("[");
238 1 $out .= "$1)" ;
239 }
240 elsif ($2 eq ']')
241 {
242 1 return _unmatched("]");
243 }
244 elsif ($2 eq '}')
245 {
246 1 return _unmatched("}");
247 }
248 elsif ($2 eq '{')
249 {
250 # TODO -- check no '/' within the {}
251 # TODO -- check for \} & other \ within the {}
252  
253 8 my $tmp ;
254 8 ▶ unless ( $string =~ s/(.*?)$noPreBS\}//)
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ s/(.*?)$File::GlobMapper::noPreBS\}//) 1 7 8
255 {
256 1 return _unmatched("{");
257 }
258 #$string =~ s#(.*?)\}##;
259  
260 #my $alt = join '|',
261 # map { quotemeta $_ }
262 # split "$noPreBS,", $1 ;
263 7 my $alt = $self->_parseBit($1);
264 7 ▶ defined $alt or return 0 ;
Branch: 1 branch
BranchTrueFalseTotal
defined $alt or return 0 4 3 7
265 3 $out .= "($alt)" ;
266  
267 3 ++ $self->{Braces} ;
268 }
269 }
270  
271 35 ▶ return _unmatched("(")
Branch: 1 branch
BranchTrueFalseTotal
if $depth 1 34 35
272 if $depth ;
273  
274 34 $out .= quotemeta $string ;
275  
276  
277 34 $self->{InputGlob} =~ s/$noPreBS[\(\)]//g;
278 34 $self->{InputPattern} = $out ;
279  
280 #print "# INPUT '$self->{InputGlob}' => '$out'\n";
281  
282 34 return 1 ;
283  
284 }
285  
286 sub _parseOutputGlob
287 {
288 34 ▶ my $self = shift ;
Subroutine _parseOutputGlob: called CC=6 SCAR=19.5
289  
290 34 my $string = $self->{OutputGlob} ;
291 34 my $maxwild = $self->{WildCount};
292  
293 34 ▶ if ($self->{GlobFlags} & GLOB_TILDE)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"GlobFlags"} & 2048) 34 0 34
294 #if (1)
295 {
296 34 $string =~ s{
297 ^ ~ # find a leading tilde
298 ( # save this in $1
299 [^/] # a non-slash character
300 * # repeated 0 or more times (0 means me)
301 )
302 }{
303 $1
304 ? (getpwnam($1))[7]
305 : ( $ENV{HOME} || $ENV{LOGDIR} )
306 0 ▶ }ex;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
Condition0%0/3
$ENV{"HOME"} or $ENV{"LOGDIR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"HOME"} or $ENV{"LOGDIR"}
$ENV{"HOME"} $ENV{"LOGDIR"}
Truth table0%0/3
A or B
A= $ENV{"HOME"}B= $ENV{"LOGDIR"}
ABresult
1X1
011
000
307  
308 }
309  
310 # max #1 must be == to max no of '*' in input
311 34 ▶ while ( $string =~ m/#(\d)/g )
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /#(\d)/g) 35 34 69
312 {
313 35 ▶ croak "Max wild is #$maxwild, you tried #$1"
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $maxwild 0 35 35
314 if $1 > $maxwild ;
315 }
316  
317 34 my $noPreBS = '(?<!\\\)' ; # no preceding backslash
318  
319 # escape any use of the delimiter symbols
320 # $string =~ s/(${BEGIN_DELIM}|${END_DELIM}|${BACKSLASH_ESC})/$1$1/g;
321  
322 # escape \# and \*
323 34 $string =~ s/\\#/${HASH_ESC}/g;
324 34 $string =~ s/\\\*/${STAR_ESC}/g;
325  
326 # Transform "#3" to BEGIN_DELIM 3 END_DELIM
327 34 $string =~ s/#(\d)/${BEGIN_DELIM}${1}${END_DELIM}/g;
328  
329 34 $string =~ s#\*#${BEGIN_DELIM}${END_DELIM}#g;
330  
331 # print "INPUT '$self->{InputPattern}'\n";
332 # print "OUTPUT '$self->{OutputGlob}' => '$string'\n";
333  
334 34 $self->{OutputPattern} = $string ;
335  
336 34 return 1 ;
337 }
338  
339 sub _getFiles
340 {
341 34 ▶ my $self = shift ;
Subroutine _getFiles: called CC=6 SCAR=17.9
342  
343 34 my %outInMapping = ();
344 34 my %inFiles = () ;
345  
346 34 foreach my $inFile (@{ $self->{InputFiles} })
347 {
348 81 ▶ next if $inFiles{$inFile} ++ ;
Branch: 1 branch
BranchTrueFalseTotal
if $inFiles{$inFile}++ 1 80 81
349  
350 80 my $outFile = $inFile ;
351 80 my @matches ;
352  
353 80 ▶ if (@matches = ($inFile =~ m/$self->{InputPattern}/ ))
Branch: 1 branch
BranchTrueFalseTotal
if (@matches = $inFile =~ /$self->{"InputPattern"}/) 80 0 80
354 {
355 80 $outFile = $self->{OutputPattern};
356 80 my $ix = 1;
357  
358 # get the filename glob
359 80 $outFile =~ s/${BEGIN_DELIM}${END_DELIM}/$inFile/g;
360  
361 # now each of the #1, #2,...
362 80 for my $pattern (@matches)
363 {
364 109 $outFile =~ s/${BEGIN_DELIM}${ix}${END_DELIM}/$pattern/g;
365  
366 109 ++ $ix;
367 }
368  
369 # unescape
370 80 $outFile =~ s/${BEGIN_DELIM}${BEGIN_DELIM}/${BEGIN_DELIM}/g;
371 80 $outFile =~ s/${END_DELIM}${END_DELIM}/${END_DELIM}/g;
372 80 $outFile =~ s/${HASH_ESC}/#/g;
373 80 $outFile =~ s/${STAR_ESC}/*/g;
374  
375 80 ▶ if (defined $outInMapping{$outFile})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outInMapping{$outFile}) 1 79 80
376 {
377 1 $Error = "multiple input files map to one output file";
378 1 return undef ;
379 }
380 79 $outInMapping{$outFile} = $inFile;
381 79 push @{ $self->{Pairs} }, [$inFile, $outFile];
382 }
383 }
384  
385 33 return 1 ;
386 }
387  
388 sub getFileMap
389 {
390 33 ▶ my $self = shift ;
Subroutine getFileMap: called CC=1 SCAR=0.0
Pod undocumented
391  
392 33 return $self->{Pairs} ;
393 }
394  
395 sub getHash
396 {
397 4 ▶ my $self = shift ;
Subroutine getHash: called CC=1 SCAR=0.0
Pod undocumented
398  
399 4 return { map { $_->[0] => $_->[1] } @{ $self->{Pairs} } } ;
400 }
401  
402 130 1;
403  
404 __END__
« blib/lib/IO/Compress/Base.pm ↑ Summary blib/lib/IO/Compress/Base/Common.pm »