blib/lib/File/GlobMapper.pm

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