blib/lib/File/Format/CRD/Reader.pm

filter: Statementstmt 23.7% 19 / 80 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 37.5% 6 / 16 Podpod 100% 4 / 4 totaltotal 24.3% 29 / 119 CC 10 SCAR 20.4
CC
10
Cov
19%
get_next_card
20.0
_read_from
12.0
_init
12.0
CRAP 7.7
↑ Summary Build »
1 package File::Format::CRD::Reader;
2 3 $File::Format::CRD::Reader::VERSION = '0.2.1';
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 sub new
17 {
18 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
19  
20 0 my $self = bless {}, $class;
21  
22 0 $self->_init(@_);
23  
24 0 return $self;
25 }
26  
27 sub _read_from
28 {
29 0 ▶ my ($self, $pos, $count) = @_;
Subroutine _read_from: not called CC=3 SCAR=24.8
30  
31 0 ▶ if (!seek($self->{_fh}, $pos, SEEK_SET))
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $self->{"_fh"}, $pos, 0) 0 0 0
32 {
33 0 Carp::confess("Cannot seek to $pos.");
34 }
35  
36 0 my $buffer = "";
37 0 ▶ if (read($self->{_fh}, $buffer, $count) != $count)
Branch: 1 branch
BranchTrueFalseTotal
if (read($self->{"_fh"}, $buffer, $count) != $count) 0 0 0
38 {
39 0 Carp::confess("Could not read $count bytes.");
40 }
41  
42 0 return $buffer;
43 }
44  
45 sub _read_short
46 {
47 0 ▶ my $self = shift;
Subroutine _read_short: not called CC=1 SCAR=6.9
48 0 my $pos = shift;
49  
50 0 my $buffer = $self->_read_from($pos, 2);
51  
52 0 return unpack("v", $buffer);
53 }
54  
55 sub _read_long
56 {
57 0 ▶ my $self = shift;
Subroutine _read_long: not called CC=1 SCAR=6.9
58 0 my $pos = shift;
59  
60 0 return unpack("V", $self->_read_from($pos, 4));
61 }
62  
63 sub _init
64 {
65 0 ▶ my ($self, $args) = @_;
Subroutine _init: not called CC=3 SCAR=24.8
66  
67 0 my $filename = $args->{'filename'};
68  
69 0 ▶ open my $in, "<", $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $filename or Carp::confess("Could not open '${filename}'") 0 0 0
70 or Carp::confess "Could not open '$filename'";
71  
72 0 binmode ($in);
73  
74 0 $self->{_fh} = $in;
75  
76 0 my $magic = $self->_read_from(0, 3);
77  
78 0 ▶ if ($magic ne "MGC")
Branch: 1 branch
BranchTrueFalseTotal
if ($magic ne "MGC") 0 0 0
79 {
80 0 Carp::confess("Could not find magic number in file.");
81 }
82  
83 0 my $n_cards = $self->_read_short(3);
84  
85 0 $self->{_num_cards} = $n_cards;
86  
87 0 $self->{_card_idx} = 0;
88  
89 0 return;
90 }
91  
92  
93 sub get_num_cards
94 {
95 0 ▶ return shift->{_num_cards};
Subroutine get_num_cards: not called CC=1 SCAR=6.9
Pod documented
96 }
97  
98 sub DESTROY
99 {
100 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
101  
102 0 $self->finish();
103  
104 0 return;
105 }
106  
107  
108 sub finish
109 {
110 0 ▶ my $self = shift;
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
111  
112 0 ▶ if (exists($self->{_fh}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_fh"}) 0 0 0
113 {
114 0 close($self->{_fh});
115  
116 0 delete($self->{_fh});
117 }
118  
119 0 return;
120 }
121  
122  
123 sub get_next_card
124 {
125 0 ▶ my $self = shift;
Subroutine get_next_card: not called CC=4 SCAR=30.0
Pod documented
126 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
127  
128 0 my $encoding = $args->{'encoding'};
129  
130 0 my $card_idx = $self->{_card_idx};
131  
132 0 ▶ if ($card_idx == $self->get_num_cards())
Branch: 1 branch
BranchTrueFalseTotal
if ($card_idx == $self->get_num_cards) 0 0 0
133 {
134 0 return;
135 }
136  
137 0 my $loc = 11 + $card_idx * 52;
138  
139 0 my $textloc = $self->_read_long($loc);
140  
141 0 ▶ if (! ($textloc >= 57))
Branch: 1 branch
BranchTrueFalseTotal
unless ($textloc >= 57) 0 0 0
142 {
143 0 Carp::confess("textloc is too small");
144 }
145  
146 my $transform = sub {
147 0 ▶ my $text = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
148  
149 0 ▶ if (defined($encoding))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) { } 0 0 0
150 {
151 0 return decode($encoding, $text);
152 }
153 else
154 {
155 0 return $text;
156 }
157 0 };
158  
159 0 my $title = $self->_read_from($loc+5, 52-5);
160  
161 0 my $ret = { 'title' => $transform->($title) };
162  
163 0 my $textlen = $self->_read_short($textloc+2);
164  
165 0 my $text = $self->_read_from($textloc+4, $textlen);
166  
167 0 $ret->{'body'} = $transform->($text);
168 0 ++($self->{_card_idx});
169  
170 0 return $ret;
171 }
172  
173  
174  
175 3 1; # End of File::Format::CRD::Reader
176  
177 __END__
↑ Summary Build »