WriteExcel/OLEwriter.pm

filter: Statementstmt 97.0% 200 / 206 Branchbran 73.3% 22 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 92.6% 239 / 258 CC 21 SCAR 11.7
CC
21
Cov
94%
close
6.0
_initialize
4.4
write_header
4.0
CRAP 3.2
« WriteExcel/Properties.pm ↑ Summary WriteExcel/Chart/External.pm »
1 package Spreadsheet::WriteExcel::OLEwriter;
2  
3 ###############################################################################
4 #
5 # OLEwriter - A writer class to store BIFF data in a OLE compound storage file.
6 #
7 #
8 # Used in conjunction with Spreadsheet::WriteExcel
9 #
10 # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
11 #
12 # Documentation after __END__
13 #
14  
15 30 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 30 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21  
22  
23  
24 30 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 30 @ISA = qw(Exporter);
26  
27 30 $VERSION = '2.40';
28  
29 ###############################################################################
30 #
31 # new()
32 #
33 # Constructor
34 #
35 60 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
36  
37 60 my $class = shift;
38 60 my $self = {
39 _olefilename => $_[0],
40 _filehandle => "",
41 _fileclosed => 0,
42 _internal_fh => 0,
43 _biff_only => 0,
44 _size_allowed => 0,
45 _biffsize => 0,
46 _booksize => 0,
47 _big_blocks => 0,
48 _list_blocks => 0,
49 _root_start => 0,
50 _block_count => 4,
51 };
52  
53 60 bless $self, $class;
54 60 $self->_initialize();
55 60 return $self;
56 }
57  
58  
59 ###############################################################################
60 #
61 # _initialize()
62 #
63 # Create a new filehandle or use the provided filehandle.
64 #
65 60 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=14.7
66  
67 60 my $self = shift;
68 60 my $olefile = $self->{_olefilename};
69 60 my $fh;
70  
71 # If the filename is a reference it is assumed that it is a valid
72 # filehandle, if not we create a filehandle.
73 #
74 60 ▶ if (ref($olefile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $olefile) { } 60 0 60
75 60 $fh = $olefile;
76 }
77 else{
78  
79 # Create a new file, open for writing
80 0 $fh = FileHandle->new("> $olefile");
81  
82 # Workbook.pm also checks this but something may have happened since
83 # then.
84 0 ▶ if (not defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
85 0 croak "Can't open $olefile. It may be in use or protected.\n";
86 }
87  
88 # binmode file whether platform requires it or not
89 0 binmode($fh);
90  
91 0 $self->{_internal_fh} = 1;
92 }
93  
94 # Store filehandle
95 60 $self->{_filehandle} = $fh;
96 }
97  
98  
99 ###############################################################################
100 #
101 # set_size($biffsize)
102 #
103 # Set the size of the data to be written to the OLE stream
104 #
105 # $big_blocks = (109 depot block x (128 -1 marker word)
106 # - (1 x end words)) = 13842
107 # $maxsize = $big_blocks * 512 bytes = 7087104
108 #
109 60 ▶sub set_size {
Subroutine set_size: called CC=3 SCAR=11.1
Pod undocumented
110  
111 60 my $self = shift;
112 60 my $maxsize = 7_087_104; # Use Spreadsheet::WriteExcel::Big to exceed this
113  
114 60 ▶ if ($_[0] > $maxsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] > $maxsize) 0 60 60
115 0 return $self->{_size_allowed} = 0;
116 }
117  
118 60 $self->{_biffsize} = $_[0];
119  
120 # Set the min file size to 4k to avoid having to use small blocks
121 60 ▶ if ($_[0] > 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] > 4096) { } 1 59 60
122 1 $self->{_booksize} = $_[0];
123 }
124 else {
125 59 $self->{_booksize} = 4096;
126 }
127  
128 60 return $self->{_size_allowed} = 1;
129  
130 }
131  
132  
133 ###############################################################################
134 #
135 # _calculate_sizes()
136 #
137 # Calculate various sizes needed for the OLE stream
138 #
139 59 ▶sub _calculate_sizes {
Subroutine _calculate_sizes: called CC=2 SCAR=6.9
140  
141 59 my $self = shift;
142 59 my $datasize = $self->{_booksize};
143  
144 59 ▶ if ($datasize % 512 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($datasize % 512 == 0) { } 58 1 59
145 58 $self->{_big_blocks} = $datasize/512;
146 }
147 else {
148 1 $self->{_big_blocks} = int($datasize/512) +1;
149 }
150 # There are 127 list blocks and 1 marker blocks for each big block
151 # depot + 1 end of chain block
152 59 $self->{_list_blocks} = int(($self->{_big_blocks})/127) +1;
153 59 $self->{_root_start} = $self->{_big_blocks};
154 }
155  
156  
157 ###############################################################################
158 #
159 # close()
160 #
161 # Write root entry, big block list and close the filehandle.
162 # This routine is used to explicitly close the open filehandle without
163 # having to wait for DESTROY.
164 #
165 60 ▶sub close {
Subroutine close: called CC=6 SCAR=18.0
Pod undocumented
166  
167 60 my $self = shift;
168  
169 60 ▶ return if not $self->{_size_allowed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_size_allowed"} 0 60 60
170  
171 60 ▶ $self->_write_padding() if not $self->{_biff_only};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_biff_only"} 59 1 60
172 60 ▶ $self->_write_property_storage() if not $self->{_biff_only};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_biff_only"} 59 1 60
173 60 ▶ $self->_write_big_block_depot() if not $self->{_biff_only};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_biff_only"} 59 1 60
174  
175 60 my $close = 1; # Default to no error for external filehandles.
176  
177 # Close the filehandle if it was created internally.
178 60 ▶ $close = CORE::close($self->{_filehandle}) if $self->{_internal_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_internal_fh"} 60 0 60
179  
180 60 $self->{_fileclosed} = 1;
181  
182 60 return $close;
183 }
184  
185  
186 ###############################################################################
187 #
188 # DESTROY()
189 #
190 # Close the filehandle if it hasn't already been explicitly closed.
191 #
192 60 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.1
193  
194 60 my $self = shift;
195  
196 60 local ($@, $!, $^E, $?);
197  
198 60 ▶ $self->close() unless $self->{_fileclosed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_fileclosed"} 0 60 60
199 }
200  
201  
202 ###############################################################################
203 #
204 # write($data)
205 #
206 # Write BIFF data to OLE file.
207 #
208 368 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
209  
210 368 my $self = shift;
211  
212 # Protect print() from -l on the command line.
213 368 local $\ = undef;
214 368 print {$self->{_filehandle}} $_[0];
215 }
216  
217  
218 ###############################################################################
219 #
220 # write_header()
221 #
222 # Write OLE header block.
223 #
224 60 ▶sub write_header {
Subroutine write_header: called CC=4 SCAR=13.9
Pod undocumented
225  
226 60 my $self = shift;
227  
228 60 ▶ return if $self->{_biff_only};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_biff_only"} 1 59 60
229 59 $self->_calculate_sizes();
230  
231 59 my $root_start = $self->{_root_start};
232 59 my $num_lists = $self->{_list_blocks};
233  
234 59 my $id = pack("NN", 0xD0CF11E0, 0xA1B11AE1);
235 59 my $unknown1 = pack("VVVV", 0x00, 0x00, 0x00, 0x00);
236 59 my $unknown2 = pack("vv", 0x3E, 0x03);
237 59 my $unknown3 = pack("v", -2);
238 59 my $unknown4 = pack("v", 0x09);
239 59 my $unknown5 = pack("VVV", 0x06, 0x00, 0x00);
240 59 my $num_bbd_blocks = pack("V", $num_lists);
241 59 my $root_startblock = pack("V", $root_start);
242 59 my $unknown6 = pack("VV", 0x00, 0x1000);
243 59 my $sbd_startblock = pack("V", -2);
244 59 my $unknown7 = pack("VVV", 0x00, -2 ,0x00);
245 59 my $unused = pack("V", -1);
246  
247 # Protect print() from -l on the command line.
248 59 local $\ = undef;
249  
250 59 print {$self->{_filehandle}} $id;
251 59 print {$self->{_filehandle}} $unknown1;
252 59 print {$self->{_filehandle}} $unknown2;
253 59 print {$self->{_filehandle}} $unknown3;
254 59 print {$self->{_filehandle}} $unknown4;
255 59 print {$self->{_filehandle}} $unknown5;
256 59 print {$self->{_filehandle}} $num_bbd_blocks;
257 59 print {$self->{_filehandle}} $root_startblock;
258 59 print {$self->{_filehandle}} $unknown6;
259 59 print {$self->{_filehandle}} $sbd_startblock;
260 59 print {$self->{_filehandle}} $unknown7;
261  
262 59 for (1..$num_lists) {
263 59 $root_start++;
264 59 print {$self->{_filehandle}} pack("V", $root_start);
265 }
266  
267 59 for ($num_lists..108) {
268 6372 print {$self->{_filehandle}} $unused;
269 }
270 }
271  
272  
273 ###############################################################################
274 #
275 # _write_big_block_depot()
276 #
277 # Write big block depot.
278 #
279 59 ▶sub _write_big_block_depot {
Subroutine _write_big_block_depot: called CC=4 SCAR=13.9
280  
281 59 my $self = shift;
282 59 my $num_blocks = $self->{_big_blocks};
283 59 my $num_lists = $self->{_list_blocks};
284 59 my $total_blocks = $num_lists *128;
285 59 my $used_blocks = $num_blocks + $num_lists +2;
286  
287 59 my $marker = pack("V", -3);
288 59 my $end_of_chain = pack("V", -2);
289 59 my $unused = pack("V", -1);
290  
291  
292 # Protect print() from -l on the command line.
293 59 local $\ = undef;
294  
295 59 for my $i (1..$num_blocks-1) {
296 426 print {$self->{_filehandle}} pack("V",$i);
297 }
298  
299 59 print {$self->{_filehandle}} $end_of_chain;
300 59 print {$self->{_filehandle}} $end_of_chain;
301  
302 59 for (1..$num_lists) {
303 59 print {$self->{_filehandle}} $marker;
304 }
305  
306 59 for ($used_blocks..$total_blocks) {
307 6949 print {$self->{_filehandle}} $unused;
308 }
309 }
310  
311  
312 ###############################################################################
313 #
314 # _write_property_storage()
315 #
316 # Write property storage. TODO: add summary sheets
317 #
318 59 ▶sub _write_property_storage {
Subroutine _write_property_storage: called CC=1 SCAR=0.0
319  
320 59 my $self = shift;
321  
322 59 my $rootsize = -2;
323 59 my $booksize = $self->{_booksize};
324  
325 ################# name type dir start size
326 59 $self->_write_pps('Root Entry', 0x05, 1, -2, 0x00);
327 59 $self->_write_pps('Workbook', 0x02, -1, 0x00, $booksize);
328 59 $self->_write_pps('', 0x00, -1, 0x00, 0x0000);
329 59 $self->_write_pps('', 0x00, -1, 0x00, 0x0000);
330 }
331  
332  
333 ###############################################################################
334 #
335 # _write_pps()
336 #
337 # Write property sheet in property storage
338 #
339 236 ▶sub _write_pps {
Subroutine _write_pps: called CC=2 SCAR=6.9
340  
341 236 my $self = shift;
342  
343 236 my $name = $_[0];
344 236 my @name = ();
345 236 my $length = 0;
346  
347 236 ▶ if ($name ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne "") 118 118 236
348 118 $name = $_[0] . "\0";
349 # Simulate a Unicode string
350 118 @name = map(ord, split('', $name));
351 118 $length = length($name) * 2;
352 }
353  
354 236 my $rawname = pack("v*", @name);
355 236 my $zero = pack("C", 0);
356  
357 236 my $pps_sizeofname = pack("v", $length); #0x40
358 236 my $pps_type = pack("v", $_[1]); #0x42
359 236 my $pps_prev = pack("V", -1); #0x44
360 236 my $pps_next = pack("V", -1); #0x48
361 236 my $pps_dir = pack("V", $_[2]); #0x4c
362  
363 236 my $unknown1 = pack("V", 0);
364  
365 236 my $pps_ts1s = pack("V", 0); #0x64
366 236 my $pps_ts1d = pack("V", 0); #0x68
367 236 my $pps_ts2s = pack("V", 0); #0x6c
368 236 my $pps_ts2d = pack("V", 0); #0x70
369 236 my $pps_sb = pack("V", $_[3]); #0x74
370 236 my $pps_size = pack("V", $_[4]); #0x78
371  
372  
373 # Protect print() from -l on the command line.
374 236 local $\ = undef;
375  
376 236 print {$self->{_filehandle}} $rawname;
377 236 print {$self->{_filehandle}} $zero x (64 -$length);
378 236 print {$self->{_filehandle}} $pps_sizeofname;
379 236 print {$self->{_filehandle}} $pps_type;
380 236 print {$self->{_filehandle}} $pps_prev;
381 236 print {$self->{_filehandle}} $pps_next;
382 236 print {$self->{_filehandle}} $pps_dir;
383 236 print {$self->{_filehandle}} $unknown1 x 5;
384 236 print {$self->{_filehandle}} $pps_ts1s;
385 236 print {$self->{_filehandle}} $pps_ts1d;
386 236 print {$self->{_filehandle}} $pps_ts2d;
387 236 print {$self->{_filehandle}} $pps_ts2d;
388 236 print {$self->{_filehandle}} $pps_sb;
389 236 print {$self->{_filehandle}} $pps_size;
390 236 print {$self->{_filehandle}} $unknown1;
391 }
392  
393  
394 ###############################################################################
395 #
396 # _write_padding()
397 #
398 # Pad the end of the file
399 #
400 59 ▶sub _write_padding {
Subroutine _write_padding: called CC=3 SCAR=11.0
401  
402 59 my $self = shift;
403 59 my $biffsize = $self->{_biffsize};
404 59 my $min_size;
405  
406 59 ▶ if ($biffsize < 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($biffsize < 4096) { } 58 1 59
407 58 $min_size = 4096;
408 }
409 else {
410 1 $min_size = 512;
411 }
412  
413 # Protect print() from -l on the command line.
414 59 local $\ = undef;
415  
416 59 ▶ if ($biffsize % $min_size != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($biffsize % $min_size != 0) 59 0 59
417 59 my $padding = $min_size - ($biffsize % $min_size);
418 59 print {$self->{_filehandle}} "\0" x $padding;
419 }
420 }
421  
422  
423 30 1;
424  
425  
426 __END__
« WriteExcel/Properties.pm ↑ Summary WriteExcel/Chart/External.pm »