QChunk.pm

filter: Statementstmt 34.4% 31 / 90 Branchbran 22.2% 4 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 43.4% 10 / 23 Podpod 0.0% 0 / 22 totaltotal 29.4% 45 / 153 CC 10 SCAR 12.6
CC
10
Cov
32%
write
12.0
pointer
6.0
load
6.0
CRAP 3.5
« ZIO_Generic.pm ↑ Summary ZIO_Color.pm »
1 package Games::Rezrov::QChunk;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #use SelfLoader;
6 #use Carp qw(confess);
7 1 1;
8  
9 #__DATA__
10  
11 12 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
12 12 my ($type, $chunk_type) = @_;
13 12 my $self = {};
14 12 bless $self, $type;
15 12 ▶ $self->id($chunk_type) if $chunk_type;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_type 12 0 12
16 12 my $buf = "";
17 12 $self->buffer(\$buf);
18 12 return $self;
19 }
20  
21 0 ▶sub pointer {
Subroutine pointer: not called CC=2 SCAR=17.9
Pod undocumented
22 # return the current pointer position.
23 # with argument, increments the pointer that number of bytes
24 # (does NOT affect return value).
25 0 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 0 0
26 0 my $value = $_[0]->{"pointer"};
27 0 $_[0]->{"pointer"} = $value + $_[1];
28 0 return $value;
29 } else {
30 0 return $_[0]->{"pointer"};
31 }
32 }
33  
34 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
Pod undocumented
35 # load a chunk from file.
36 0 my ($self, $fh) = @_;
37 0 $self->id(read_chunk_id($fh));
38 0 my $len = read_int_4($fh);
39 0 $self->reset_read_pointer();
40 0 my $buf;
41 0 my $read = read($fh, $buf, $len);
42 0 ▶ if ($read != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read != $len) { } 0 0 0
43 # error
44 0 print STDERR "Read $read, expected $len\n";
45 0 return -1;
46 } else {
47 0 $self->buffer(\$buf);
48 }
49 0 return $len;
50 }
51  
52 0 ▶sub read_chunk_id {
Subroutine read_chunk_id: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my ($fh) = @_;
54 0 my $buf;
55 0 read($fh, $buf, 4);
56 0 return $buf;
57 }
58  
59 0 ▶sub read_int_4 {
Subroutine read_int_4: not called CC=1 SCAR=6.9
Pod undocumented
60 # read a signed 4-byte int
61 0 my ($fh) = @_;
62 0 my $buf;
63 0 read($fh, $buf, 4);
64 0 return unpack 'N', $buf;
65 }
66  
67 0 ▶sub get_word {
Subroutine get_word: not called CC=1 SCAR=6.9
Pod undocumented
68 # 1.2: 16-bit unsigned
69 0 my ($self) = @_;
70 0 my $buffer = $self->buffer();
71 0 my $pointer = $self->pointer(2);
72 0 my $result = unpack "x${pointer}n", $$buffer;
73 # $self->pointer($pointer + 2);
74 0 return $result;
75 }
76  
77 0 ▶sub get_byte {
Subroutine get_byte: not called CC=1 SCAR=6.9
Pod undocumented
78 # get a single byte, incrementing pointer
79 0 return unpack "x" . $_[0]->pointer(1) . "C", ${$_[0]->buffer()};
80 }
81  
82 0 ▶sub get_word_3 {
Subroutine get_word_3: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my ($self) = @_;
84 0 return ($self->get_byte() << 16 | $self->get_byte() << 8 | $self->get_byte());
85 }
86  
87 0 ▶sub get_string {
Subroutine get_string: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my ($self, $length) = @_;
89 0 my $buffer = $self->buffer();
90 0 my $pointer = $self->pointer();
91 0 my $result = unpack "x${pointer}a$length", $$buffer;
92 # $self->pointer($pointer + $length);
93 0 $self->pointer($length);
94 0 return $result;
95 }
96  
97 12 ▶sub reset_read_pointer {
Subroutine reset_read_pointer: called CC=1 SCAR=0.0
Pod undocumented
98 12 $_[0]->{"pointer"} = 0;
99 }
100  
101 0 ▶sub eof {
Subroutine eof: not called CC=2 SCAR=17.9
Pod undocumented
102 # return true if pointer at end of data
103 0 ▶ return ($_[0]->pointer() >= length(${$_[0]->buffer()}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->pointer >= length ${$_[0]->buffer;} ? : 0 0 0
104 }
105  
106 12 ▶sub id {
Subroutine id: called CC=2 SCAR=7.6
Pod undocumented
107 12 ▶ return (defined $_[1] ? $_[0]->{"id"} = $_[1] : $_[0]->{"id"});
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 12 0 12
108 }
109  
110 12 ▶sub buffer {
Subroutine buffer: called CC=2 SCAR=7.6
Pod undocumented
111 12 ▶ return (defined $_[1] ? $_[0]->{"buffer"} = $_[1] : $_[0]->{"buffer"});
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 12 0 12
112 }
113  
114 126 ▶sub add_byte {
Subroutine add_byte: called CC=1 SCAR=0.0
Pod undocumented
115 126 my $buf = $_[0]->{"buffer"};
116 126 $$buf .= pack 'C', $_[1];
117 }
118  
119 269 ▶sub add_word {
Subroutine add_word: called CC=1 SCAR=0.0
Pod undocumented
120 269 my ($self, $value) = @_;
121 269 my $buf = $_[0]->{"buffer"};
122 # confess unless defined $value;
123 269 $$buf .= pack 'n', $value;
124 }
125  
126 4 ▶sub add_string {
Subroutine add_string: called CC=2 SCAR=7.0
Pod undocumented
127 4 my ($self, $value, $length) = @_;
128 4 my $buf = $_[0]->{"buffer"};
129 4 ▶ die if length($value) > $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $length 0 4 4
130 4 $$buf .= sprintf "%${length}s", $value;
131 }
132  
133 23 ▶sub add_word_3 {
Subroutine add_word_3: called CC=1 SCAR=0.0
Pod undocumented
134 # add a 3-byte unsigned word
135 23 my ($self, $value) = @_;
136 23 $self->add_byte($value >> 16 & 0xff);
137 23 $self->add_byte($value >> 8 & 0xff);
138 23 $self->add_byte($value & 0xff);
139 }
140  
141 4 ▶sub add_data {
Subroutine add_data: called CC=1 SCAR=0.0
Pod undocumented
142 # add an arbitrary chunk
143 4 my ($self, $value) = @_;
144 4 my $buf = $_[0]->{"buffer"};
145 4 $$buf .= $value;
146 }
147  
148 0 ▶sub get_chunk_length {
Subroutine get_chunk_length: not called CC=1 SCAR=6.9
Pod undocumented
149 # return length of chunk, including id, byte count, and any required pad
150 0 my ($self) = @_;
151 0 my $buf = $self->buffer();
152 0 my $size = length $$buf;
153 0 return $size + 4 + 4 + ($size % 2);
154 # 4 for byte count, 4 for ID, plus optional pad byte
155 }
156  
157 0 ▶sub get_data_length {
Subroutine get_data_length: not called CC=1 SCAR=6.9
Pod undocumented
158 # return length of just the data
159 0 return length(${$_[0]->buffer()});
160 }
161  
162 0 ▶sub get_data {
Subroutine get_data: not called CC=1 SCAR=6.9
Pod undocumented
163 # return all data
164 0 return $_[0]->buffer();
165 }
166  
167 0 ▶sub write {
Subroutine write: not called CC=3 SCAR=24.8
Pod undocumented
168 # write this chunk to stream
169 0 my ($self, $fh) = @_;
170
171 0 my ($i, $len, $data_len);
172
173 0 my $buf = $self->buffer();
174 0 $data_len = length $$buf;
175
176 # write chunk id:
177 0 my $id = $self->id();
178 0 ▶ die if length($id) != 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $id != 4 0 0 0
179 0 print $fh $id;
180
181 # write data size (any pad is not included in length)
182 0 print $fh pack('N', $data_len);
183
184 0 print $fh $$buf;
185
186 0 ▶ if ($data_len % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_len % 2) 0 0 0
187 # pad byte required; spec 8.4.1
188 0 print $fh pack('C', 0);
189 }
190 }
191  
192 1 1;
« ZIO_Generic.pm ↑ Summary ZIO_Color.pm »