line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package uSAC::MIME; |
2
|
1
|
|
|
1
|
|
83388
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
18
|
|
4
|
1
|
|
|
1
|
|
381
|
use version; our $VERSION=version->declare("v0.2.2"); |
|
1
|
|
|
|
|
1609
|
|
|
1
|
|
|
|
|
4
|
|
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
|
|
|
|
|
43
|
for(<$fh>){ |
12
|
96
|
|
|
|
|
119
|
tr/;//d; |
13
|
96
|
|
|
|
|
185
|
s/^\s+//; |
14
|
96
|
50
|
|
|
|
153
|
next if /^\s*#/; |
15
|
96
|
100
|
|
|
|
167
|
next if /^\s*$/; |
16
|
89
|
50
|
|
|
|
168
|
next if /{|}/; |
17
|
|
|
|
|
|
|
|
18
|
89
|
|
|
|
|
210
|
my @fields=split /\s+/; |
19
|
|
|
|
|
|
|
#first field is the mime type, remaining are extensions |
20
|
89
|
|
|
|
|
136
|
for my $ext (@fields[1..$#fields]){ |
21
|
107
|
|
|
|
|
154
|
$self->add($ext=>$fields[0]); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub save_to_handle { |
27
|
1
|
|
|
1
|
1
|
6
|
my ($self, $fh)=@_; |
28
|
1
|
|
|
|
|
4
|
my @keys= sort keys $self->%*; |
29
|
1
|
|
|
|
|
2
|
my $output=""; |
30
|
1
|
|
|
|
|
3
|
for(@keys){ |
31
|
0
|
|
|
|
|
0
|
$output.= "$_ ".$self->{$_}."\n"; |
32
|
|
|
|
|
|
|
} |
33
|
1
|
|
|
|
|
171
|
print $fh $output; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new { |
37
|
1
|
|
50
|
1
|
1
|
68
|
my $package=shift//__PACKAGE__; |
38
|
1
|
|
|
|
|
3
|
my %additional=@_; |
39
|
1
|
|
|
|
|
35
|
my $self={$default_mime_to_ext->%*}; |
40
|
1
|
|
|
|
|
4
|
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
|
300
|
my $package=shift//__PACKAGE__; |
50
|
2
|
|
|
|
|
8
|
my %additional=@_; |
51
|
2
|
|
|
|
|
3
|
my $self={}; |
52
|
|
|
|
|
|
|
|
53
|
2
|
|
|
|
|
4
|
bless $self, $package; |
54
|
2
|
|
|
|
|
4
|
for(keys %additional){ |
55
|
1
|
|
|
|
|
5
|
$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
|
1062
|
my $package=shift//__PACKAGE__; |
63
|
1
|
|
|
|
|
2
|
my $path=shift; |
64
|
1
|
|
|
|
|
2
|
my $self={}; |
65
|
1
|
|
|
|
|
1
|
bless $self, $package; |
66
|
1
|
|
|
|
|
43
|
my $res=open my $fh, "<" ,$path; |
67
|
1
|
50
|
|
|
|
5
|
unless($res){ |
68
|
1
|
|
|
|
|
60
|
warn "could not read file: $path"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
0
|
|
|
|
|
0
|
$self->load_from_handle($fh); |
72
|
|
|
|
|
|
|
} |
73
|
1
|
|
|
|
|
8
|
$self; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub index{ |
79
|
5
|
|
|
5
|
1
|
12
|
my $db=shift; |
80
|
5
|
|
|
|
|
7
|
my @tmp; |
81
|
|
|
|
|
|
|
my @tmp2; |
82
|
5
|
|
|
|
|
51
|
for my $mime (keys $db->%*){ |
83
|
322
|
|
|
|
|
373
|
for($db->{$mime}){ |
84
|
322
|
|
|
|
|
521
|
my $exts=[split " "]; |
85
|
322
|
|
|
|
|
425
|
push @tmp, map {$_,$mime} @$exts; |
|
433
|
|
|
|
|
677
|
|
86
|
322
|
|
|
|
|
503
|
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
|
|
|
|
20
|
if(wantarray){ |
93
|
5
|
|
|
|
|
280
|
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
|
110
|
|
|
110
|
1
|
2242
|
my ($db,$ext,$mime)=@_; |
105
|
110
|
|
100
|
|
|
240
|
my $exts_string=$db->{$mime}//""; |
106
|
110
|
50
|
|
|
|
670
|
unless($exts_string=~/\b$ext\b/){ |
107
|
110
|
|
|
|
|
173
|
my @items=split " ", $exts_string; |
108
|
110
|
|
|
|
|
148
|
push @items, $ext; |
109
|
110
|
|
|
|
|
266
|
$db->{$mime}=join " ", @items; |
110
|
|
|
|
|
|
|
} |
111
|
110
|
|
|
|
|
199
|
$db; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub rem { |
116
|
1
|
|
|
1
|
1
|
267
|
my ($db,$ext,$mime)=@_; |
117
|
1
|
|
|
|
|
3
|
my $exts_string=$db->{$mime}; |
118
|
1
|
50
|
|
|
|
3
|
return unless defined $exts_string; |
119
|
1
|
50
|
|
|
|
18
|
if($exts_string=~s/\b$ext\b//){ |
120
|
1
|
|
|
|
|
4
|
$exts_string=~s/ +/ /; |
121
|
1
|
50
|
|
|
|
3
|
if($exts_string eq " "){ |
122
|
0
|
|
|
|
|
0
|
delete $db->{mime}; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else { |
125
|
1
|
|
|
|
|
3
|
$db->{$mime}=$exts_string; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
$db |
129
|
1
|
|
|
|
|
2
|
} |
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__ |