| 1 | package Filename::Backup; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | our $DATE = '2016-10-19'; # DATE | |||||||||||
| 4 | 2 | our $VERSION = '0.03'; # VERSION | |||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | require Exporter; | |||||||||||
| 11 | 2 | our @ISA = qw(Exporter); | |||||||||||
| 12 | 2 | our @EXPORT_OK = qw(check_backup_filename); | |||||||||||
| 13 | |||||||||||||
| 14 | 2 | our %SPEC; | |||||||||||
| 15 | |||||||||||||
| 16 | 2 | our %SUFFIXES = ( | |||||||||||
| 17 | '~' => 1, | ||||||||||||
| 18 | '.bak' => 1, | ||||||||||||
| 19 | '.old' => 1, | ||||||||||||
| 20 | '.orig' => 1, # patch | ||||||||||||
| 21 | '.rej' => 1, # patch | ||||||||||||
| 22 | '.swp' => 1, | ||||||||||||
| 23 | # XXX % (from /etc/mime.types) | ||||||||||||
| 24 | # XXX sik? (from /etc/mime.types) | ||||||||||||
| 25 | # XXX .dpkg* | ||||||||||||
| 26 | # XXX .rpm* | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | $SPEC{check_backup_filename} = { | ||||||||||||
| 30 | 2 | v => 1.1, | |||||||||||
| 31 | summary => 'Check whether filename indicates being a backup file', | ||||||||||||
| 32 | description => <<'_', | ||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | _ | ||||||||||||
| 36 | args => { | ||||||||||||
| 37 | filename => { | ||||||||||||
| 38 | schema => 'str*', | ||||||||||||
| 39 | req => 1, | ||||||||||||
| 40 | pos => 0, | ||||||||||||
| 41 | }, | ||||||||||||
| 42 | # XXX recurse? | ||||||||||||
| 43 | ci => { | ||||||||||||
| 44 | summary => 'Whether to match case-insensitively', | ||||||||||||
| 45 | schema => 'bool', | ||||||||||||
| 46 | default => 1, | ||||||||||||
| 47 | }, | ||||||||||||
| 48 | }, | ||||||||||||
| 49 | result_naked => 1, | ||||||||||||
| 50 | result => { | ||||||||||||
| 51 | schema => ['any*', of=>['bool*', 'hash*']], | ||||||||||||
| 52 | description => <<'_', | ||||||||||||
| 53 | |||||||||||||
| 54 | Return false if not detected as backup name. Otherwise return a hash, which may | ||||||||||||
| 55 | contain these keys: `original_filename`. In the future there will be extra | ||||||||||||
| 56 | information returned, e.g. editor name (if filename indicates backup from | ||||||||||||
| 57 | certain backup program), date (if filename contains date information), and so | ||||||||||||
| 58 | on. | ||||||||||||
| 59 | |||||||||||||
| 60 | _ | ||||||||||||
| 61 | }, | ||||||||||||
| 62 | }; | ||||||||||||
| 63 | sub check_backup_filename { | ||||||||||||
| 64 | 10 | ▶ | my %args = @_; | ||||||||||
|
Subroutine
check_backup_filename: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||
| 65 | |||||||||||||
| 66 | 10 | my $filename = $args{filename}; | |||||||||||
| 67 | 10 | my $orig_filename; | |||||||||||
| 68 | |||||||||||||
| 69 | 10 | ▶ | if ($filename =~ /\A#(.+)#\z/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 1 | $orig_filename = $1; | |||||||||||
| 71 | 1 | goto RETURN; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 9 | ▶ | $filename =~ /(~|\.\w+)\z/ or return 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 9 | ▶ | my $ci = $args{ci} // 1; | ||||||||||
|
Condition100%2/2
$args{"ci"} // 1
MC/DC100%1/1
$args{"ci"} // 1
$args{"ci"}
Truth table100%2/2
A
A= $args{"ci"}
| |||||||||||||
| 76 | |||||||||||||
| 77 | 9 | my $suffix = $1; | |||||||||||
| 78 | |||||||||||||
| 79 | 9 | my $spec; | |||||||||||
| 80 | 9 | ▶ | if ($ci) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 8 | my $suffix_lc = lc($suffix); | |||||||||||
| 82 | 8 | for (keys %SUFFIXES) { | |||||||||||
| 83 | 30 | ▶ | if (lc($_) eq $suffix_lc) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 7 | $spec = $SUFFIXES{$_}; | |||||||||||
| 85 | 7 | last; | |||||||||||
| 86 | } | ||||||||||||
| 87 | } | ||||||||||||
| 88 | } else { | ||||||||||||
| 89 | 1 | $spec = $SUFFIXES{$suffix}; | |||||||||||
| 90 | } | ||||||||||||
| 91 | 9 | ▶ | return 0 unless $spec; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | |||||||||||||
| 93 | 7 | ($orig_filename = $filename) =~ s/\Q$suffix\E\z//; | |||||||||||
| 94 | |||||||||||||
| 95 | 8 | RETURN: | |||||||||||
| 96 | return { | ||||||||||||
| 97 | original_filename => $orig_filename, | ||||||||||||
| 98 | }; | ||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 2 | 1; | |||||||||||
| 102 | # ABSTRACT: Check whether filename indicates being a backup file | ||||||||||||
| 103 | |||||||||||||
| 104 | __END__ | ||||||||||||