File Coverage

blib/lib/CAD/Drawing/IO/Compressed.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package CAD::Drawing::IO::Compressed;
2             our $VERSION = '0.02';
3              
4 1     1   3911 use CAD::Drawing;
  0            
  0            
5              
6             use Stream::FileInputStream;
7             use Compress::Zlib;
8             use File::Temp qw(tempfile unlink0);
9              
10             use warnings;
11             use strict;
12             use Carp;
13             =pod
14              
15             =head1 NAME
16              
17             CAD::Drawing::IO::Compressed - load and save compressed data
18              
19             =head1 NOTICE
20              
21             This works well for single-file formats like dxf and dwg, but currently
22             has no support for directory formats (which would need to be saved in
23             'tarball' form.)
24              
25             =head1 AUTHOR
26              
27             Eric L. Wilhelm
28              
29             http://scratchcomputing.com
30              
31             =head1 COPYRIGHT
32              
33             This module is copyright (C) 2004-2006 by Eric L. Wilhelm. Portions
34             copyright (C) 2003 by Eric L. Wilhelm and A. Zahner Co.
35              
36             =head1 LICENSE
37              
38             This module is distributed under the same terms as Perl. See the Perl
39             source package for details.
40              
41             You may use this software under one of the following licenses:
42              
43             (1) GNU General Public License
44             (found at http://www.gnu.org/copyleft/gpl.html)
45             (2) Artistic License
46             (found at http://www.perl.com/pub/language/misc/Artistic.html)
47              
48             =head1 NO WARRANTY
49              
50             This software is distributed with ABSOLUTELY NO WARRANTY. The author,
51             his former employer, and any other contributors will in no way be held
52             liable for any loss or damages resulting from its use.
53              
54             =head1 Modifications
55              
56             The source code of this module is made freely available and
57             distributable under the GPL or Artistic License. Modifications to and
58             use of this software must adhere to one of these licenses. Changes to
59             the code should be noted as such and this notification (as well as the
60             above copyright information) must remain intact on all copies of the
61             code.
62              
63             Additionally, while the author is actively developing this code,
64             notification of any intended changes or extensions would be most helpful
65             in avoiding repeated work for all parties involved. Please contact the
66             author with any such development plans.
67              
68             =cut
69             ########################################################################
70              
71             =head1 Requisite Plug-in Functions
72              
73             See CAD::Drawing::IO for a description of the plug-in architecture.
74              
75             =cut
76             ########################################################################
77             # the following are required to be a disc I/O plugin:
78             our $can_save_type = "compressed";
79             our $can_load_type = $can_save_type;
80             our $is_inherited = 0;
81              
82             =head2 check_type
83              
84             Returns true if $type is "compressed" or $filename has a ".gz" extension
85             (probably the best way.)
86              
87             $fact = check_type($filename, $type);
88              
89             =cut
90             sub check_type {
91             my ($filename, $type) = @_;
92             my $extension;
93             if($filename =~ m/.*\.(\w+)$/) {
94             $extension = $1;
95             }
96             $extension = lc($extension);
97             if(defined($type)) {
98             # print "type was defined\n";
99             ($type eq "compressed") && return("compressed");
100             return();
101             }
102             elsif($extension eq "gz") {
103             return("compressed");
104             }
105             return();
106             } # end subroutine check_type definition
107             ########################################################################
108             =head1 Compressed I/O functions
109              
110             These use File::Temp and compression modules to create a compressed
111             version of most supported I/O types (FIXME: need a tar scheme for
112             directory-based formats (currently unsupported))
113              
114             =head2 save
115              
116             $drw->save($filename, \%opts);
117              
118             =cut
119             sub save {
120             my $self = shift;
121             my($filename, $opt) = @_;
122             my $savedebug = 0;
123             my $suffix = $filename;
124             $suffix =~ s/^.*(\..*)\.gz$/$1/;
125             $suffix = ".drwpm" . $suffix;
126             my($fh, $tmpfilename) = tempfile(SUFFIX => $suffix);
127             $savedebug && print "tempfile is named: $tmpfilename\n";
128             close($fh);
129             my @returnval = $self->save($tmpfilename, $opt);
130             $savedebug && print "temp save complete\n";
131             my $stream = Stream::FileInputStream->new( $tmpfilename);
132             my $string = Compress::Zlib::memGzip( $stream->readAll() );
133             defined($string) || croak("compression failed\n");
134             unlink($tmpfilename) or
135             carp("failed to unlink $tmpfilename\n");
136             $fh = FileHandle->new;
137             open($fh, ">$filename") or croak("can't write to $filename\n");
138             print $fh $string;
139             $fh->close;
140             return(@returnval);
141             } # end subroutine save definition
142             ########################################################################
143              
144             =head2 load
145              
146             $drw->load($filename, \%opts);
147              
148             =cut
149             sub load {
150             my $self = shift;
151             my($filename, $opt) = @_;
152             my $loaddebug = 0;
153             (-e $filename) or croak("$filename does not exist\n");
154             my $stream = Stream::FileInputStream->new( $filename);
155             my $string = Compress::Zlib::memGunzip( $stream->readAll);
156             defined($string) || croak("decompression failed ($Compress::Zlib::gzerrno)\n");
157             my $suffix = $filename;
158             $suffix =~ s/^.*(\..*)\.gz$/$1/;
159             $suffix = ".drwpm" . $suffix;
160             # warn "using $suffix\n";
161             my($fh, $tmpfilename) = tempfile(SUFFIX => $suffix);
162             $loaddebug && print "tempfile is named: $tmpfilename\n";
163             print $fh $string;
164             $fh->close();
165             ($opt->{type} eq "compressed") and delete($opt->{type});
166             my @returnval = $self->load($tmpfilename, $opt);
167             unlink($tmpfilename) or
168             carp("failed to unlink $tmpfilename\n");
169             return(@returnval);
170             } # end subroutine load definition
171             ########################################################################
172              
173             1;