Storage/Io.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 379 CC 49 SCAR 34.9
CC
49
Cov
0%
_init_file
132.0
default_iosize
42.0
read
42.0
CRAP 32.8
1 #
2 # $Id: Io.pm,v 1.2 1998/03/23 02:53:45 schwartz Exp $
3 #
4 # Io.pm
5 #
6 # Copyright (C) 1996, 1997, 1998 Martin Schwartz
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, you should find it at:
20 #
21 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/COPYING
22 #
23 # Contact: schwartz@cs.tu-berlin.de
24 #
25  
26 package OLE::Storage::Io;
27 use strict;
28 my $VERSION=do{my@R=('$Revision: 1.2 $'=~/\d+/g);sprintf"%d."."%d"x$#R,@R};
29 my $IOnum = 0;
30  
31 use Symbol;
32 use OLE::Storage::Iolist();
33  
34 sub close {
35 #
36 # 1 = close([\$streambuf])
37 #
38 my ($S, $bufR) = @_;
39  
40 if ($S->_mode & 0x10) {
41 $$bufR = ${$S->_cache()} if defined $bufR;
42 } else {
43 $S->flush();
44 close ($S->_io);
45 }
46 1}
47  
48 sub default_iosize {
49 #
50 # ($offset, $size) || 0 =
51 # default_iosize (defsize, "r"||"w", offset, size)
52 #
53 my ($S, $defsize, $rw, $o, $l) = @_;
54 return 0 if !$S->_io_method($rw);
55  
56 if (!$l) {
57 if ($rw =~ /r/) {
58 if (!defined $l) {
59 # read default
60 $o=0; $l=$defsize;
61 } else {
62 # read zero size: no problem
63 }
64 } else {
65 if (!defined $l) {
66 # write default: not allowed!
67 return _error("Write error! Unknown size.");
68 } else {
69 # write zero size: no problem
70 }
71 }
72 }
73 ($o, $l);
74 }
75  
76 sub flush {
77 #
78 # 1 = flush()
79 #
80 # flush io cache, if caching is turned on
81 #
82 my $S = shift;
83 my $bufR = $S->_cache();
84  
85 return if !$bufR;
86  
87 $S->_cache(undef);
88 $S->rw_iolist("w", $bufR, $S->_iolist->aggregate(2));
89  
90 $S->_iolist(new OLE::Storage::Iolist());
91 1}
92  
93 sub name {
94 shift->_name()
95 }
96  
97 sub open {
98 #
99 # $Io||0 = open ($Startup, $filename [,$openmode [,\$streambuf]]);
100 #
101 # openmode bitmask (0 is default):
102 #
103 # Bit 0: 0 read only 1 read and write
104 # Bit 4: 0 file mode 1 buffer mode
105 #
106 my ($proto, $Startup, $filename, $openmode, $bufR) = @_;
107 my $class = ref($proto) || $proto;
108 my $S = bless ({}, $class);
109  
110 $S->_Startup ($Startup); # startup object
111 $S->_iolist (new OLE::Storage::Iolist()); # iolist object
112 $S->_mode ($openmode || 0); # s.a.
113 $S->_name ($filename || ""); # name of file or buffer
114 $S->_size (-s $filename); # size of file or buffer
115 $S->_writable (0); # file/buffer is read-only
116  
117 if ($openmode & 0x10) {
118 return 0 if !$S->_init_stream($bufR);
119 } else {
120 return 0 if !$S->_init_file($class);
121 }
122  
123 $S;
124 }
125  
126 sub read {
127 #
128 # 1||0 = read($file_offset, $num_of_chars, \$buf [,$var_offset])
129 #
130 my ($S, $fo, $num, $buf_or_bufR, $vo) = @_;
131 my $bufR = _buf_or_bufR($buf_or_bufR);
132 my $status = 0;
133  
134 if (!$vo) {
135 $vo = 0;
136 if (!$num) {
137 $$bufR = ""; $status = 1;
138 }
139 }
140  
141 if (!$status) {
142 if ($S->_cache()) {
143 substr($$bufR, $vo, $num) = substr(${$S->_cache()}, $fo, $num);
144 $status = 1;
145 } else {
146 $status = seek($S->_io, $fo, 0)
147 && (read($S->_io, $$bufR, $num, $vo) == $num)
148 || $S->_error("Read error!")
149 ;
150 }
151 }
152  
153 if (defined $buf_or_bufR) {
154 return $status;
155 } else {
156 return $status && $$bufR || undef;
157 }
158 }
159  
160 sub read_iolist {
161 shift->rw_iolist("r", @_);
162 }
163  
164 sub rw_iolist {
165 #
166 # 1||0 = rw_iolist($S, "r"||"w", \$buf||$buf, $iolistO);
167 # 1||0 = rw_iolist($S, "w" , undef, $iolistO);
168 # $buf||undef = rw_iolist($S, "r" , undef, $iolistO);
169 #
170 my ($S, $rw, $buf_or_bufR, $ioR) = @_;
171 my $bufR = _buf_or_bufR($buf_or_bufR);
172 my ($o, $l);
173 my $done = 0;
174 my $status = 1;
175  
176 for (0 .. $ioR->max()) {
177 ($o, $l) = $ioR->entry($_);
178 next if !$l;
179 if ($S->_rw_io($rw, $o, $l, $bufR, $done)) {
180 $done += $l;
181 } else {
182 # io error!
183 $status = 0; last;
184 }
185 }
186 if ($rw =~ /r/ && !defined $buf_or_bufR) {
187 return $status && $$bufR || undef;
188 }
189 $status;
190 }
191  
192 sub size {
193 shift->_size()
194 }
195  
196 sub writable {
197 shift->_writable()
198 }
199  
200 sub write {
201 #
202 # 1||0 = write($file_offset, $num_of_bytes, \$buf||$buf||undef [,$var_offset])
203 #
204 # Tries to write a $num_of_bytes long string. If $buf ($$buf) is too short,
205 # the missing rest will be filled with zero chars "\0".
206 #
207 my ($S, $fo, $num, $buf_or_bufR, $vo) = @_;
208 my $bufR = _buf_or_bufR($buf_or_bufR);
209  
210 return $S->_error ("Cannot write! (File is opened as read-only)")
211 if !$S->_writable
212 ;
213 if (!$vo) {
214 return 1 if !$num; $vo = 0;
215 }
216  
217 my $tmp = substr($$bufR, $vo, $num);
218 $tmp .= "\0" x ($num-length($tmp));
219  
220 if ($S->_cache()) {
221 substr(${$S->_cache()}, $fo, $num) = $tmp;
222 $S->_iolist->append($fo, $num);
223 return 1;
224 } else {
225 seek($S->_io, $fo, 0)
226 && (print {$S->_io} $tmp)
227 || $S->_error("Write error!")
228 ;
229 }
230 }
231  
232 sub write_iolist {
233 shift->rw_iolist("w", @_);
234 }
235  
236 #
237 # -- Private ---------------------------------------------------------------
238 #
239  
240 sub _buf_or_bufR {
241 #
242 # Allow to pass references and scalars as argument. (nice for small scalars)
243 #
244 my $buf_or_bufR = shift;
245 my $bufR;
246 if (defined $buf_or_bufR) {
247 $bufR = ref($buf_or_bufR) && $buf_or_bufR || \$buf_or_bufR;
248 } else {
249 my $buf="";
250 $bufR = \$buf;
251 }
252 }
253  
254 sub _error {
255 my $S = shift;
256 $S->_Startup->error(@_) if $S->_Startup;
257 }
258  
259 sub _init_file {
260 #
261 # 1||0 = _init_file ($S)
262 #
263 no strict "refs";
264 my ($S, $class) = @_;
265  
266 my $status;
267 my $fn = $S->_name;
268 my $IOname = gensym;
269  
270 return $S->_error("File \"$fn\" does not exist!") if ! -e $fn;
271 return $S->_error("\"$fn\" is a directory!") if -d $fn;
272 return $S->_error("\"$fn\" is no proper file!") if ! -f $fn;
273 return $S->_error("Cannot read \"$fn\"!") if ! -r $fn;
274  
275 if ($S->_mode & 1) {
276 return $S->_error("\"$fn\" is write protected!") if ! -w $fn;
277 $S->_writable(1);
278 $status = open($IOname, '+<'.$fn);
279 } else {
280 $S->_writable(0);
281 $status = open($IOname, $fn);
282 }
283 return $S->_error("Cannot open \"$fn\"!") if !$status;
284 $S->_io($IOname);
285  
286 binmode($S->_io);
287 if ($S->_writable) {
288 select($S->_io); $|=1; select(STDOUT);
289 }
290  
291 # here extern check routine belonged...
292  
293 if ($S->_cache()) {
294 if (!$S->read(0, $S->_size, $S->_cache, 0)) {
295 $S->_cache(undef);
296 }
297 }
298 1}
299  
300 sub _init_stream {
301 #
302 # 1||0 = _init_stream ($S, \$streambuf);
303 #
304 my ($S, $bufR) = @_;
305 return $S->_error("No stream data available!") if !defined $bufR;
306 $S->_size(length($$bufR));
307 $S->_cache($bufR);
308 1}
309  
310 sub _io_method {
311 my ($S, $rw) = @_;
312 return $S->_error("Bad IO method \"$rw\"!") if !($rw =~ /^[rw]$/);
313 1}
314  
315 sub _rw_io {
316 #
317 # 1||0= _rw_io($S,"r"||"w", $file_offset, $num_of_chars, \$buf [,$var_offset])
318 #
319 my ($S, $rw) = (shift, shift);
320 return 0 if !$S->_io_method($rw);
321  
322 if ($rw =~ /r/) {
323 $S->read(@_);
324 } elsif ($rw =~ /w/) {
325 $S->write(@_);
326 }
327 }
328  
329 #
330 # -- memberEss methods -----------------------------------------------------
331 #
332  
333 sub _cache { my $S = shift; $S->{IOBUFR} = shift if @_; $S->{IOBUFR} }
334 sub _Startup { my $S = shift; $S->{STARTUP} = shift if @_; $S->{STARTUP} }
335 sub _io { my $S = shift; $S->{IO} = shift if @_; $S->{IO} }
336 sub _iolist { my $S = shift; $S->{IOLIST} = shift if @_; $S->{IOLIST} }
337 sub _mode { my $S = shift; $S->{MODE} = shift if @_; $S->{MODE} }
338 sub _name { my $S = shift; $S->{NAME} = shift if @_; $S->{NAME} }
339 sub _size { my $S = shift; $S->{SIZE} = shift if @_; $S->{SIZE} }
340 sub _writable { my $S = shift; $S->{WRITE} = shift if @_; $S->{WRITE} }
341  
342 "Atomkraft? Nein, danke!"
343  
344 __END__