File Coverage

blib/lib/uSAC/MIME.pm
Criterion Covered Total %
statement 70 75 93.3
branch 10 18 55.5
condition 5 8 62.5
subroutine 11 11 100.0
pod 7 8 87.5
total 103 120 85.8


line stmt bran cond sub pod time code
1             package uSAC::MIME;
2 1     1   85278 use strict;
  1         2  
  1         26  
3 1     1   4 use warnings;
  1         2  
  1         22  
4 1     1   373 use version; our $VERSION=version->declare("v0.2.1");
  1         1603  
  1         5  
5              
6             #Private storage for the internal database
7             my $default_mime_to_ext;
8              
9             sub load_from_handle {
10 1     1 1 2 my ($self, $fh)=@_;
11 1         55 for(<$fh>){
12 95         122 tr/;//d;
13 95         165 s/^\s+//;
14 95 50       151 next if /^\s*#/;
15 95 100       179 next if /^\s*$/;
16 88 50       168 next if /{|}/;
17              
18 88         205 my @fields=split /\s+/;
19             #first field is the mime type, remaining are extensions
20 88         145 for my $ext (@fields[1..$#fields]){
21 106         152 $self->add($ext=>$fields[0]);
22             }
23             }
24             }
25              
26             sub save_to_handle {
27 1     1 1 7 my ($self, $fh)=@_;
28 1         4 my @keys= sort keys $self->%*;
29 1         3 my $output="";
30 1         4 for(@keys){
31 0         0 $output.= "$_ ".$self->{$_}."\n";
32             }
33 1         132 print $fh $output;
34             }
35              
36             sub new {
37 1   50 1 1 78 my $package=shift//__PACKAGE__;
38 1         3 my %additional=@_;
39 1         59 my $self={$default_mime_to_ext->%*};
40 1         5 bless $self, $package;
41              
42 1         4 for(keys %additional){
43 0         0 $self->add($_, $additional{$_});
44             }
45 1         3 $self;
46             }
47              
48             sub new_empty {
49 2   50 2 1 259 my $package=shift//__PACKAGE__;
50 2         5 my %additional=@_;
51 2         5 my $self={};
52              
53 2         4 bless $self, $package;
54 2         6 for(keys %additional){
55 1         6 $self->add($_, $additional{$_});
56             }
57 2         12 $self;
58             }
59              
60             sub new_from_file {
61             #ignore any line with only one word, or { or }
62 1   50 1 0 1103 my $package=shift//__PACKAGE__;
63 1         3 my $path=shift;
64 1         2 my $self={};
65 1         3 bless $self, $package;
66 1         42 my $res=open my $fh, "<" ,$path;
67 1 50       6 unless($res){
68 1         61 warn "could not read file: $path";
69             }
70             else {
71 0         0 $self->load_from_handle($fh);
72             }
73 1         10 $self;
74            
75             }
76              
77              
78             sub index{
79 5     5 1 15 my $db=shift;
80 5         6 my @tmp;
81             my @tmp2;
82 5         56 for my $mime (keys $db->%*){
83 318         357 for($db->{$mime}){
84 318         506 my $exts=[split " "];
85 318         432 push @tmp, map {$_,$mime} @$exts;
  429         697  
86 318         481 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 50       17 if(wantarray){
93 5         300 return ({@tmp},{@tmp2});
94             }
95             else {
96 0         0 return {@tmp};
97             }
98             }
99              
100              
101             #add an ext=>mime mapping. need to reindex after
102             #returns
103             sub add {
104 109     109 1 2448 my ($db,$ext,$mime)=@_;
105 109   100     230 my $exts_string=$db->{$mime}//"";
106 109 50       714 unless($exts_string=~/\b$ext\b/){
107 109         173 my @items=split " ", $exts_string;
108 109         146 push @items, $ext;
109 109         237 $db->{$mime}=join " ", @items;
110             }
111 109         201 $db;
112             }
113              
114              
115             sub rem {
116 1     1 1 286 my ($db,$ext,$mime)=@_;
117 1         3 my $exts_string=$db->{$mime};
118 1 50       4 return unless defined $exts_string;
119 1 50       17 if($exts_string=~s/\b$ext\b//){
120 1         2 $exts_string=~s/ +/ /;
121 1 50       4 if($exts_string eq " "){
122 0         0 delete $db->{mime};
123             }
124             else {
125 1         2 $db->{$mime}=$exts_string;
126             }
127             }
128             $db
129 1         4 }
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;
142              
143              
144             __DATA__