| 1 | package uSAC::MIME; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use version; our $VERSION=version->declare("v0.2"); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | #Private storage for the internal database | ||||||||||||
| 7 | 1 | my $default_mime_to_ext; | |||||||||||
| 8 | |||||||||||||
| 9 | sub load_from_handle { | ||||||||||||
| 10 | 1 | ▶ | my ($self, $fh)=@_; | ||||||||||
|
Subroutine
load_from_handle: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||
| 11 | 1 | for(<$fh>){ | |||||||||||
| 12 | 94 | tr/;//d; | |||||||||||
| 13 | 94 | s/^\s+//; | |||||||||||
| 14 | 94 | ▶ | next if /^\s*#/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 94 | ▶ | next if /^\s*$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 87 | ▶ | next if /{|}/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | |||||||||||||
| 18 | 87 | my @fields=split /\s+/; | |||||||||||
| 19 | #first field is the mime type, remaining are extensions | ||||||||||||
| 20 | 87 | for my $ext (@fields[1..$#fields]){ | |||||||||||
| 21 | 105 | $self->add($ext=>$fields[0]); | |||||||||||
| 22 | } | ||||||||||||
| 23 | } | ||||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | sub save_to_handle { | ||||||||||||
| 27 | 1 | ▶ | my ($self, $fh)=@_; | ||||||||||
|
Subroutine
save_to_handle: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 28 | 1 | my @keys= sort keys $self->%*; | |||||||||||
| 29 | 1 | my $output=""; | |||||||||||
| 30 | 1 | for(@keys){ | |||||||||||
| 31 | 0 | $output.= "$_ ".$self->{$_}."\n"; | |||||||||||
| 32 | } | ||||||||||||
| 33 | 1 | print $fh $output; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | sub new { | ||||||||||||
| 37 | 1 | ▶ | my $package=shift//__PACKAGE__; | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
Condition50%1/2
shift() // __PACKAGE__
MC/DC0%0/1
shift() // __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 38 | 1 | my %additional=@_; | |||||||||||
| 39 | 1 | my $self={$default_mime_to_ext->%*}; | |||||||||||
| 40 | 1 | bless $self, $package; | |||||||||||
| 41 | |||||||||||||
| 42 | 1 | for(keys %additional){ | |||||||||||
| 43 | 0 | $self->add($_, $additional{$_}); | |||||||||||
| 44 | } | ||||||||||||
| 45 | 1 | $self; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | sub new_empty { | ||||||||||||
| 49 | 2 | ▶ | my $package=shift//__PACKAGE__; | ||||||||||
|
Subroutine
new_empty: called CC=3 SCAR=11.0
Pod
documented
Condition50%1/2
shift() // __PACKAGE__
MC/DC0%0/1
shift() // __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 50 | 2 | my %additional=@_; | |||||||||||
| 51 | 2 | my $self={}; | |||||||||||
| 52 | |||||||||||||
| 53 | 2 | bless $self, $package; | |||||||||||
| 54 | 2 | for(keys %additional){ | |||||||||||
| 55 | 1 | $self->add($_, $additional{$_}); | |||||||||||
| 56 | } | ||||||||||||
| 57 | 2 | $self; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | sub new_from_file { | ||||||||||||
| 61 | #ignore any line with only one word, or { or } | ||||||||||||
| 62 | 1 | ▶ | my $package=shift//__PACKAGE__; | ||||||||||
|
Subroutine
new_from_file: called CC=3 SCAR=11.3
Pod
undocumented
Condition50%1/2
shift() // __PACKAGE__
MC/DC0%0/1
shift() // __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 63 | 1 | my $path=shift; | |||||||||||
| 64 | 1 | my $self={}; | |||||||||||
| 65 | 1 | bless $self, $package; | |||||||||||
| 66 | 1 | my $res=open my $fh, "<" ,$path; | |||||||||||
| 67 | 1 | ▶ | unless($res){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 1 | warn "could not read file: $path"; | |||||||||||
| 69 | } | ||||||||||||
| 70 | else { | ||||||||||||
| 71 | 0 | $self->load_from_handle($fh); | |||||||||||
| 72 | } | ||||||||||||
| 73 | 1 | $self; | |||||||||||
| 74 | |||||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | |||||||||||||
| 78 | sub index{ | ||||||||||||
| 79 | 5 | ▶ | my $db=shift; | ||||||||||
|
Subroutine
index: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||
| 80 | 5 | my @tmp; | |||||||||||
| 81 | my @tmp2; | ||||||||||||
| 82 | 5 | for my $mime (keys $db->%*){ | |||||||||||
| 83 | 314 | for($db->{$mime}){ | |||||||||||
| 84 | 314 | my $exts=[split " "]; | |||||||||||
| 85 | 314 | push @tmp, map {$_,$mime} @$exts; | |||||||||||
| 86 | 314 | push @tmp2, $mime, $exts | |||||||||||
| 87 | |||||||||||||
| 88 | } | ||||||||||||
| 89 | } | ||||||||||||
| 90 | #first hash is forward map from extention to mime type | ||||||||||||
| 91 | #second hash is reverse map from mime to to array or extension | ||||||||||||
| 92 | 5 | ▶ | if(wantarray){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 5 | return ({@tmp},{@tmp2}); | |||||||||||
| 94 | } | ||||||||||||
| 95 | else { | ||||||||||||
| 96 | 0 | return {@tmp}; | |||||||||||
| 97 | } | ||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | |||||||||||||
| 101 | #add an ext=>mime mapping. need to reindex after | ||||||||||||
| 102 | #returns | ||||||||||||
| 103 | sub add { | ||||||||||||
| 104 | 108 | ▶ | my ($db,$ext,$mime)=@_; | ||||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 105 | 108 | ▶ | my $exts_string=$db->{$mime}//""; | ||||||||||
|
Condition100%2/2
$db->{$mime} // ""
MC/DC100%1/1
$db->{$mime} // ""
$db->{$mime}
Truth table100%2/2
A
A= $db->{$mime}
| |||||||||||||
| 106 | 108 | ▶ | unless($exts_string=~/\b$ext\b/){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 108 | my @items=split " ", $exts_string; | |||||||||||
| 108 | 108 | push @items, $ext; | |||||||||||
| 109 | 108 | $db->{$mime}=join " ", @items; | |||||||||||
| 110 | } | ||||||||||||
| 111 | 108 | $db; | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | |||||||||||||
| 115 | sub rem { | ||||||||||||
| 116 | 1 | ▶ | my ($db,$ext,$mime)=@_; | ||||||||||
|
Subroutine
rem: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||
| 117 | 1 | my $exts_string=$db->{$mime}; | |||||||||||
| 118 | 1 | ▶ | return unless defined $exts_string; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 1 | ▶ | if($exts_string=~s/\b$ext\b//){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 1 | $exts_string=~s/ +/ /; | |||||||||||
| 121 | 1 | ▶ | if($exts_string eq " "){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 0 | delete $db->{mime}; | |||||||||||
| 123 | } | ||||||||||||
| 124 | else { | ||||||||||||
| 125 | 1 | $db->{$mime}=$exts_string; | |||||||||||
| 126 | } | ||||||||||||
| 127 | } | ||||||||||||
| 128 | $db | ||||||||||||
| 129 | 1 | } | |||||||||||
| 130 | |||||||||||||
| 131 | #After this unit is compiled, initalise the default map with data from DATA file handle. | ||||||||||||
| 132 | #This is then used in the new constructor | ||||||||||||
| 133 | |||||||||||||
| 134 | UNITCHECK{ | ||||||||||||
| 135 | #Force loading of defaults | ||||||||||||
| 136 | my $dummy=uSAC::MIME->new_empty; | ||||||||||||
| 137 | $dummy->load_from_handle(\*DATA); | ||||||||||||
| 138 | $default_mime_to_ext={$dummy->%*}; | ||||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 1 | 1; | |||||||||||
| 142 | |||||||||||||
| 143 | |||||||||||||
| 144 | __DATA__ | ||||||||||||