602
|
1 |
# Copyright (c) 2000 Ned Konz. All rights reserved. This program is free
|
|
2 |
# software; you can redistribute it and/or modify it under the same terms
|
|
3 |
# as Perl itself.
|
|
4 |
|
|
5 |
# File handle that uses a string internally and can seek
|
|
6 |
# This is given as a demo for getting a zip file written
|
|
7 |
# to a string.
|
|
8 |
# I probably should just use IO::Scalar instead.
|
|
9 |
# Ned Konz, March 2000
|
|
10 |
#
|
|
11 |
# $Revision: 1.3 $
|
|
12 |
|
|
13 |
use strict;
|
|
14 |
package Archive::Zip::BufferedFileHandle;
|
|
15 |
use FileHandle ();
|
|
16 |
use Carp;
|
|
17 |
|
|
18 |
sub new
|
|
19 |
{
|
|
20 |
my $class = shift || __PACKAGE__;
|
|
21 |
$class = ref($class) || $class;
|
|
22 |
my $self = bless( {
|
|
23 |
content => '',
|
|
24 |
position => 0,
|
|
25 |
size => 0
|
|
26 |
}, $class );
|
|
27 |
return $self;
|
|
28 |
}
|
|
29 |
|
|
30 |
# Utility method to read entire file
|
|
31 |
sub readFromFile
|
|
32 |
{
|
|
33 |
my $self = shift;
|
|
34 |
my $fileName = shift;
|
|
35 |
my $fh = FileHandle->new($fileName, "r");
|
|
36 |
if (! $fh)
|
|
37 |
{
|
|
38 |
Carp::carp("Can't open $fileName: $!\n");
|
|
39 |
return undef;
|
|
40 |
}
|
|
41 |
local $/ = undef;
|
|
42 |
$self->{content} = <$fh>;
|
|
43 |
$self->{size} = length($self->{content});
|
|
44 |
return $self;
|
|
45 |
}
|
|
46 |
|
|
47 |
sub contents
|
|
48 |
{
|
|
49 |
my $self = shift;
|
|
50 |
if (@_)
|
|
51 |
{
|
|
52 |
$self->{content} = shift;
|
|
53 |
$self->{size} = length($self->{content});
|
|
54 |
}
|
|
55 |
return $self->{content};
|
|
56 |
}
|
|
57 |
|
|
58 |
sub binmode
|
|
59 |
{ 1 }
|
|
60 |
|
|
61 |
sub close
|
|
62 |
{ 1 }
|
|
63 |
|
|
64 |
sub eof
|
|
65 |
{
|
|
66 |
my $self = shift;
|
|
67 |
return $self->{position} >= $self->{size};
|
|
68 |
}
|
|
69 |
|
|
70 |
sub seek
|
|
71 |
{
|
|
72 |
my $self = shift;
|
|
73 |
my $pos = shift;
|
|
74 |
my $whence = shift;
|
|
75 |
|
|
76 |
# SEEK_SET
|
|
77 |
if ($whence == 0) { $self->{position} = $pos; }
|
|
78 |
# SEEK_CUR
|
|
79 |
elsif ($whence == 1) { $self->{position} += $pos; }
|
|
80 |
# SEEK_END
|
|
81 |
elsif ($whence == 2) { $self->{position} = $self->{size} + $pos; }
|
|
82 |
else { return 0; }
|
|
83 |
|
|
84 |
return 1;
|
|
85 |
}
|
|
86 |
|
|
87 |
sub tell
|
|
88 |
{ return shift->{position}; }
|
|
89 |
|
|
90 |
# Copy my data to given buffer
|
|
91 |
sub read
|
|
92 |
{
|
|
93 |
my $self = shift;
|
|
94 |
my $buf = \($_[0]); shift;
|
|
95 |
my $len = shift;
|
|
96 |
my $offset = shift || 0;
|
|
97 |
|
|
98 |
$$buf = '' if not defined($$buf);
|
|
99 |
my $bytesRead = ($self->{position} + $len > $self->{size})
|
|
100 |
? ($self->{size} - $self->{position})
|
|
101 |
: $len;
|
|
102 |
substr($$buf, $offset, $bytesRead)
|
|
103 |
= substr($self->{content}, $self->{position}, $bytesRead);
|
|
104 |
$self->{position} += $bytesRead;
|
|
105 |
return $bytesRead;
|
|
106 |
}
|
|
107 |
|
|
108 |
# Copy given buffer to me
|
|
109 |
sub write
|
|
110 |
{
|
|
111 |
my $self = shift;
|
|
112 |
my $buf = \($_[0]); shift;
|
|
113 |
my $len = shift;
|
|
114 |
my $offset = shift || 0;
|
|
115 |
|
|
116 |
$$buf = '' if not defined($$buf);
|
|
117 |
my $bufLen = length($$buf);
|
|
118 |
my $bytesWritten = ($offset + $len > $bufLen)
|
|
119 |
? $bufLen - $offset
|
|
120 |
: $len;
|
|
121 |
substr($self->{content}, $self->{position}, $bytesWritten)
|
|
122 |
= substr($$buf, $offset, $bytesWritten);
|
|
123 |
$self->{size} = length($self->{content});
|
|
124 |
return $bytesWritten;
|
|
125 |
}
|
|
126 |
|
|
127 |
sub clearerr() { 1 }
|
|
128 |
|
|
129 |
# vim: ts=4 sw=4
|
|
130 |
1;
|
|
131 |
__END__
|
|
132 |
|
|
133 |
=head1 COPYRIGHT
|
|
134 |
|
|
135 |
Copyright (c) 2000 Ned Konz. All rights reserved. This program is free
|
|
136 |
software; you can redistribute it and/or modify it under the same terms
|
|
137 |
as Perl itself.
|
|
138 |
|
|
139 |
=cut
|