blib/lib/EBook/Ishmael/EBook/PalmDoc.pm

filter: Statementstmt 84.6% 88 / 104 Branchbran 50.0% 14 / 28 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 9 totaltotal 72.8% 126 / 173 CC 18 SCAR 14.1
CC
18
Cov
76%
new
8.1
raw
7.1
html
4.9
CRAP 4.1
« blib/lib/EBook/Ishmael/CharDet/ISO2022.pm ↑ Summary blib/lib/EBook/Ishmael/ShellQuote.pm »
1 package EBook::Ishmael::EBook::PalmDoc;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.04';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use EBook::Ishmael::CharDet;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use EBook::Ishmael::Decode qw(palmdoc_decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use EBook::Ishmael::PDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use EBook::Ishmael::HTML qw(text2html);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 my $TYPE = 'TEXt';
16 17 my $CREATOR = 'REAd';
17  
18 17 my $RECSIZE = 4096;
19  
20 sub heuristic {
21  
22 59 ▶ my $class = shift;
Subroutine heuristic: called CC=4 SCAR=13.9
Pod undocumented
23 59 my $file = shift;
24 59 my $fh = shift;
25  
26 59 ▶ return 0 unless -s $file >= 68;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $file >= 68 0 59 59
27  
28 59 seek $fh, 32, 0;
29 59 read $fh, my ($null), 1;
30  
31 # Last byte in title must be null
32 59 ▶ unless ($null eq "\0") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($null eq "\0") 35 24 59
33 35 return 0;
34 }
35  
36 24 seek $fh, 60, 0;
37 24 read $fh, my ($type), 4;
38 24 read $fh, my ($creator), 4;
39  
40 24 ▶ return $type eq $TYPE && $creator eq $CREATOR;
Condition66%2/3
$type eq $TYPE && $creator eq $CREATOR
!ll&&!rl&&r
12012
MC/DC50%1/2
$type eq $TYPE && $creator eq $CREATOR
$type eq $TYPE $creator eq $CREATOR
Truth table66%2/3
A && B
A= $type eq $TYPEB= $creator eq $CREATOR
ABresult
0X0
100
111
41  
42 }
43  
44 sub _decode_record {
45  
46 63 ▶ my $self = shift;
Subroutine _decode_record: called CC=3 SCAR=11.6
47 63 my $rec = shift;
48  
49 63 $rec++;
50  
51 63 my $encode = $self->{_pdb}->record($rec)->data;
52  
53 63 ▶ if ($self->{_compression} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_compression'} == 1) { } 0 63 63
elsif ($self->{'_compression'} == 2) { } 63 0 63
54 0 return $encode;
55 } elsif ($self->{_compression} == 2) {
56 63 return palmdoc_decode($encode);
57 }
58  
59 }
60  
61 sub new {
62  
63 11 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=21.0
Pod undocumented
64 11 my $file = shift;
65 11 my $enc = shift;
66  
67 11 my $self = {
68 Source => undef,
69 Metadata => EBook::Ishmael::EBook::Metadata->new,
70 Encoding => $enc,
71 _pdb => undef,
72 _compression => undef,
73 _textlen => undef,
74 _recnum => undef,
75 _recsize => undef,
76 _curpos => undef,
77 };
78  
79 11 bless $self, $class;
80  
81 11 $self->{Source} = File::Spec->rel2abs($file);
82  
83 11 $self->{_pdb} = EBook::Ishmael::PDB->new($file);
84  
85 11 my $hdr = $self->{_pdb}->record(0)->data;
86  
87 (
88 $self->{_compression},
89 undef,
90 $self->{_textlen},
91 $self->{_recnum},
92 $self->{_recsize},
93 $self->{_curpos},
94 11 ) = unpack "n n N n n N", $hdr;
95  
96 11 ▶ if ($self->{_compression} != 1 and $self->{_compression} != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_compression"} != 1 and $self->{"_compression"} != 2) 0 11 11
Condition33%1/3
$self->{"_compression"} != 1 and $self->{"_compression"} != 2
!ll&&!rl&&r
0110
MC/DC0%0/2
$self->{"_compression"} != 1 and $self->{"_compression"} != 2
$self->{"_compression"} != 1 $self->{"_compression"} != 2
Truth table33%1/3
A and B
A= $self->{"_compression"} != 1B= $self->{"_compression"} != 2
ABresult
0X0
100
111
97 0 die "$self->{Source} is not a PalmDoc file\n";
98 }
99  
100 11 ▶ if ($self->{_recsize} != 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_recsize"} != 4096) 0 11 11
101 0 die "$self->{Source} is not a PalmDoc file\n";
102 }
103  
104 11 $self->{Metadata}->set_title($self->{_pdb}->name);
105  
106 11 ▶ if ($self->{_pdb}->cdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pdb"}->cdate) 11 0 11
107 11 $self->{Metadata}->set_created($self->{_pdb}->cdate);
108 }
109 11 ▶ if ($self->{_pdb}->mdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pdb"}->mdate) 11 0 11
110 11 $self->{Metadata}->set_modified($self->{_pdb}->mdate);
111 }
112  
113 11 ▶ if ($self->{_pdb}->version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_pdb'}->version) { } 0 11 11
114 $self->{Metadata}->set_format(
115 sprintf(
116 "PalmDOC %s.%s",
117 ($self->{_pdb}->version >> 8) & 0xff,
118 0 $self->{_pdb}->version & 0xff
119 )
120 );
121 } else {
122 11 $self->{Metadata}->set_format('PalmDOC');
123 }
124  
125 11 return $self;
126  
127 }
128  
129 sub html {
130  
131 3 ▶ my $self = shift;
Subroutine html: called CC=3 SCAR=16.0
Pod undocumented
132 3 my $out = shift;
133  
134 3 my $html = text2html($self->raw);
135  
136 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
137 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
138 or die "Failed to open $out for writing: $!\n";
139 0 binmode $fh, ':utf8';
140 0 print { $fh } $html;
141 0 close $fh;
142 0 return $out;
143 } else {
144 3 return $html;
145 }
146  
147 }
148  
149 sub raw {
150  
151 6 ▶ my $self = shift;
Subroutine raw: called CC=5 SCAR=19.5
Pod undocumented
152 6 my $out = shift;
153  
154 my $raw = join '',
155 54 map { $self->_decode_record($_) }
156 6 (0 .. $self->{_recnum} - 1);
157 6 ▶ if (not defined $self->{Encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encoding"}) 5 1 6
158 5 ▶ $self->{Encoding} = chardet($raw) // 'ASCII';
Condition50%1/2
chardet($raw) // "ASCII"
l!l
50
MC/DC0%0/1
chardet($raw) // "ASCII"
chardet($raw)
Truth table50%1/2
A
A= chardet($raw)
Aresult
11
00
159 }
160  
161 6 $raw = decode($self->{Encoding}, $raw);
162  
163 6 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 6 6
164 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
165 or die "Failed to open $out for writing: $!\n";
166 0 binmode $fh, ':utf8';
167 0 print { $fh } $raw;
168 0 close $fh;
169 0 return $out;
170 } else {
171 6 return $raw;
172 }
173  
174 }
175  
176 sub metadata {
177  
178 4 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
179  
180 4 return $self->{Metadata};
181  
182 }
183  
184 2 ▶sub has_cover { 0 }
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
185  
186 1 ▶sub cover { (undef, undef) }
Subroutine cover: called CC=1 SCAR=0.0
Pod undocumented
187  
188 2 ▶sub image_num { 0 }
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
189  
190 1 ▶sub image { (undef, undef) }
Subroutine image: called CC=1 SCAR=0.0
Pod undocumented
191  
192 17 1;
« blib/lib/EBook/Ishmael/CharDet/ISO2022.pm ↑ Summary blib/lib/EBook/Ishmael/ShellQuote.pm »