# Block.pm: Class Used for Managing Report Blocks package RDA::Handle::Block; # $Id: Block.pm,v 1.10 2015/08/19 21:05:29 RDA Exp $ # ARCS: $Header: /home/cvs/cvs/RDA_8/src/scripting/lib/RDA/Handle/Block.pm,v 1.10 2015/08/19 21:05:29 RDA Exp $ # # Change History # 20150819 MSC Improve the getline method. =head1 NAME RDA::Handle::Block - Class Used for Managing Report Blocks =head1 SYNOPSIS require RDA::Handle::Block; =head1 DESCRIPTION The objects of the C class are used for managing report blocks. The following methods are available: =cut use strict; BEGIN { use Exporter; use IO::File; use RDA::Text qw(get_string); use Symbol; } # Define the global public variables use vars qw($STRINGS $VERSION @ISA); $VERSION = sprintf('%d.%02d', q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/); @ISA = qw(Exporter); # Define the global private constants my $BLOCK = 8192; # Define the global private variables # Report the package version sub Version { return $VERSION; } =head2 S<$h = RDA::Handle::Block-Enew($path,$offset,$length[,$partial])> The object constructor. C is represented by a symbol, which can be used as a file handle. The following special keys are used: =over 12 =item S< B<'blk' > > Blocking indicator =item S< B<'buf' > > Read buffer =item S< B<'eol' > > End-of-line characters protection indicator =item S< B<'flg' > > Partial information indicator =item S< B<'ifh' > > Input file handle =item S< B<'lgt' > > Block length =item S< B<'lin' > > Line number =item S< B<'max' > > Data size =item S< B<'pos' > > Current position =back =cut sub new ## no critic (Unpack) { my $cls = shift; my ($slf); # Create the buffer object $slf = bless Symbol::gensym(), ref($cls) || $cls; tie *$slf, $slf; ## no critic (Tie) $slf->open(@_); # Return the object reference return $slf; } sub open ## no critic (Builtin) { my ($slf, $pth, $off, $lgt, $flg) = @_; my ($ifh); # Create the object when not yet done return $slf->new($pth, $off, $lgt, $flg) unless ref($slf); # Create the block $off = 0 unless defined($off) && $off > 0; ## no critic (Unless) if (ref($pth)) { $ifh = $pth; die get_string('ERR_SEEK', $pth, $!) unless defined($ifh->seek($off, 0)); } else { $ifh = IO::File->new; die get_string('ERR_OPEN', $pth, $!) unless $ifh->open("<$pth"); die get_string('ERR_SEEK', $pth, $!) unless defined(CORE::seek($ifh, $off, 0)); } # Create the block *$slf->{'blk'} = 0; *$slf->{'buf'} = q{}; *$slf->{'eol'} = 1; *$slf->{'flg'} = $flg; *$slf->{'ifh'} = $ifh; *$slf->{'lgt'} = 0; *$slf->{'lin'} = 0; *$slf->{'max'} = (defined($lgt) && $lgt > 0) ? $lgt : 0; *$slf->{'pos'} = 0; # Return the object reference return $slf; } =head2 S<$h-Eis_partial> This method indicates whether the block contains incomplete information. =cut sub is_partial { my ($slf) = @_; return *$slf->{'flg'} ? 1 : 0; } # Manage object attributes sub setinfo { my ($slf, $key, $val) = @_; my ($old); $old = *$slf->{$key}; *$slf->{$key} = $val if defined($val); return $old; } # Declare a routine for an undefined functionality my $und = sub { return }; =head1 BASIC I/O METHODS See L for complete descriptions of each of the following methods, which are front ends for the corresponding built-in functions: $io->close $io->eof $io->fileno $io->getc $io->read(BUF,LEN,[OFFSET]) $io->print(ARGS) $io->printf(FMT,[ARGS]) $io->stat $io->sysread(BUF,LEN,[OFFSET]) $io->syswrite(BUF,[LEN,[OFFSET]]) $io->truncate(LEN) =cut sub close ## no critic (Ambiguous,Builtin) { my ($slf) = @_; delete *$slf->{'blk'}; delete *$slf->{'buf'}; delete *$slf->{'eol'}; delete *$slf->{'flg'}; delete *$slf->{'ifh'}; delete *$slf->{'lgt'}; delete *$slf->{'lin'}; delete *$slf->{'max'}; delete *$slf->{'pos'}; undef *$slf; return 1; } sub eof ## no critic (Builtin) { my ($slf) = @_; return (defined(*$slf->{'ifh'}) && (*$slf->{'max'} > 0 || *$slf->{'lgt'} > 0)) ? 0 : 1; } *fileno = $und; sub getc ## no critic (Builtin) { my ($slf) = @_; my ($buf); return $buf if $slf->read($buf, 1); return; } *print = $und; *printf = $und; sub read ## no critic (Builtin,Unpack) { my ($slf, undef, $siz, $off) = @_; my ($buf, $lgt); return 0 unless defined(*$slf->{'ifh'}); # Read from line buffer $lgt = *$slf->{'lgt'}; if ($lgt > 0) { if ($siz > $lgt) { $buf = *$slf->{'buf'}; if ($siz = _read_block($slf, $siz - $lgt)) { $buf .= *$slf->{'buf'}; *$slf->{'buf'} = q{}; *$slf->{'lgt'} = 0; $lgt += $siz; } } else { $buf = substr(*$slf->{'buf'}, 0, $lgt = $siz); *$slf->{'buf'} = substr(*$slf->{'buf'}, $lgt); *$slf->{'lgt'} -= $lgt; } if (defined($off)) { substr($_[1], $off) = $buf; } else { $_[1] = $buf; } *$slf->{'pos'} += $lgt; return $lgt; } # Read from file $lgt = ($siz > *$slf->{'max'}) ? *$slf->{'max'} : $siz; return 0 unless $lgt > 0 ## no critic (Unless) && defined($lgt = *$slf->{'ifh'}->read($_[1], $lgt, $off)) && $lgt > 0; *$slf->{'max'} -= $lgt; *$slf->{'pos'} += $lgt; return $lgt; } *stat = $und; *sysread = \&read; *syswrite = $und; *truncate = $und; =head1 I/O METHODS RELATED TO PERL VARIABLES See L for complete descriptions of each of the following methods. The methods return the previous value of the attribute and take an optional single argument that, when given, sets the value. If no argument is given, then the previous value is unchanged. $| $io-Eautoflush([BOOL]) $. $io-Einput_line_number([NUM]) =cut *autoflush = $und; sub input_line_number { my ($slf, $val) = @_; return $slf->setinfo('lin', $val); } =head1 IO::HANDLE LIKE METHODS See L for complete descriptions of each of the following methods: $io->blocking([BOOL]) $io->clearerr $io->error $io->flush $io->getline $io->getlines $io->opened $io->printflush(ARGS) $io->sync $io->ungetc(ORD) $io->untaint $io->write(BUF,LEN[,OFFSET]) =cut sub blocking { my ($slf, $val) = @_; return $slf->setinfo('blk', $val); } *clearerr = $und; *error = $und; *fcntl = $und; *flush = $und; sub getline { my ($slf) = @_; my ($eol, $lgt, $lin, $str); return unless defined(*$slf->{'ifh'}) ## no critic (Unless) && (*$slf->{'max'} > 0 || *$slf->{'lgt'} > 0); unless (defined($/)) # No line separator defined { $str = *$slf->{'buf'}; *$slf->{'pos'} += *$slf->{'lgt'}; while ($lgt = _read_block($slf, $BLOCK)) { $str .= *$slf->{'buf'}; *$slf->{'pos'} += *$slf->{'lgt'}; } *$slf->{'buf'} = q{}; *$slf->{'lgt'} = *$slf->{'max'} = 0; return $str; } if (length($/)) # Line mode { return unless defined($str = _read_line($slf, $/)); $. = ++*$slf->{'lin'}; } else # Paragraph mode { return unless defined($str = _read_line($slf, qq{\n})); $eol = 0; while (defined($lin = _read_line($slf, qq{\n}))) { if ($lin eq qq{\n}) { $eol++; next if $eol > 1; } elsif ($eol) { $lgt = length($lin); *$slf->{'buf'} = $lin.*$slf->{'buf'}; *$slf->{'lgt'} += $lgt; *$slf->{'pos'} -= $lgt; last; } else { $eol = 0; } $str .= $lin; } } unless (*$slf->{'eol'}) { chomp($str); $str =~ s/[\n\r\s]+$//; } return $str; } sub _read_block { my ($slf, $siz) = @_; my ($lgt); $lgt = (*$slf->{'max'} < $siz) ? *$slf->{'max'} : $siz; if ($lgt > 0 && defined($lgt = *$slf->{'ifh'}->read(*$slf->{'buf'}, $lgt)) && $lgt > 0) { *$slf->{'max'} -= $lgt; return *$slf->{'lgt'} = $lgt; } *$slf->{'buf'} = q{}; return *$slf->{'lgt'} = 0; } sub _read_line { my ($slf, $eol) = @_; my ($buf, $cor, $lgt, $off); # Check if the line is in the current block $cor = length($eol); if (($off = index($buf = *$slf->{'buf'}, $eol)) >= 0) { $off += $cor; *$slf->{'buf'} = substr($buf, $off); *$slf->{'lgt'} -= $off; *$slf->{'pos'} += $off; return substr($buf, 0, $off); } # Extract an overlapping line $lgt = *$slf->{'lgt'}; while (_read_block($slf, $BLOCK)) { $buf .= *$slf->{'buf'}; if (($off = index($buf, $eol, ($lgt < $cor) ? 0 : $lgt - $cor)) >= 0) { $off += $cor; *$slf->{'buf'} = substr(*$slf->{'buf'}, $cor = $off - $lgt); *$slf->{'lgt'} -= $cor; *$slf->{'pos'} += $off; return substr($buf, 0, $off); } $lgt += *$slf->{'lgt'}; } # Accept uncomplete last line *$slf->{'pos'} += $lgt; return $buf; } sub getlines { my ($slf) = @_; my ($lin, @tbl); die get_string('BAD_GETLINES') unless wantarray; push(@tbl, $lin) while defined($lin = $slf->getline); return @tbl; } *ioctl = $und; sub opened { my ($slf) = @_; return defined(*$slf->{'ifh'}) ? 1 : 0; } *printflush = $und; *setbuf = $und; *setvbuf = $und; *sync = $und; sub ungetc { my ($slf) = @_; --*$slf->{'cur'}; return 1; } *untaint = $und; *write = $und; =head1 SEEK METHODS See L for complete descriptions of each of the following methods: $io->getpos $io->setpos($pos) $io->seek($pos,$whence) $io->sysseek($pos,$whence) $io->tell =cut sub getpos { my ($slf) = @_; return unless defined(*$slf->{'ifh'}); return *$slf->{'pos'}; } sub seek ## no critic (Builtin) { my ($slf, $off, $typ) = @_; my ($ret, $siz); return 0 unless defined(*$slf->{'ifh'}); # Determine the offset if ($typ == 0) { $siz = $off - *$slf->{'pos'}; } elsif ($typ == 1) { $siz = $off; } else { die get_string('BAD_WHENCE', $typ); } # Move the current position if ($siz < 0) { $siz = - *$slf->{'pos'} unless (*$slf->{'pos'} + $siz) > 0; ## no critic (Unless) return 1 unless $siz < 0; ## no critic (Unless) *$slf->{'pos'} += $siz; $siz -= *$slf->{'lgt'}; } else { unless ($siz > *$slf->{'lgt'}) ## no critic (Unless) { *$slf->{'buf'} = substr(*$slf->{'buf'}, $siz); *$slf->{'lgt'} -= $siz; *$slf->{'pos'} += $siz; return 1; } $siz -= *$slf->{'lgt'}; $siz = *$slf->{'max'} if $siz > *$slf->{'max'}; return 1 unless $siz > 0; ## no critic (Unless) } $ret = CORE::seek(*$slf->{'ifh'}, $siz, 1); *$slf->{'buf'} = q{}; *$slf->{'lgt'} = 0; *$slf->{'max'} -= $siz; return $ret; } sub setpos { my ($slf, $pos) = @_; my ($max); return unless defined(*$slf->{'ifh'}); $max = *$slf->{'pos'} + *$slf->{'lgt'} + *$slf->{'max'}; $pos = ($pos < 0) ? 0 : ($pos > $max) ? $max : $pos; return CORE::seek(*$slf->{'ifh'}, $pos, 0); } *sysseek = \&seek; *tell = \&getpos; =head1 OTHER I/O METHODS =head2 S<$h-Edump([$level[,$text[,$trace]]])> This method returns a string containing the object dump. You can provide an indentation level, a prefix text, and a trace indicator as extra parameters. =cut sub dump ## no critic (Builtin) { my ($slf, $lvl, $txt) = @_; my ($buf, $pre); $lvl = 0 unless defined($lvl); $pre = q{ } x $lvl; $buf = $pre.$txt."bless {\n"; foreach my $key (sort keys(%{*$slf})) { if ($key eq 'buf') { $buf .= "$pre $key => '...'\n"; } elsif ($key ne 'ifh') { $buf .= "$pre $key => ".*$slf->{$key}.qq{\n}; } } $buf .= "$pre}, RDA::Handle::Block"; return $buf; } =head1 TIE METHODS The following methods are implemented to emulate a file handle: BINMODE this CLOSE this DESTROY this EOF this FILENO this GETC this OPEN this, mode, LIST PRINT this, LIST PRINTF this, format, LIST READ this, scalar, length, offset READLINE this SEEK this, position, whence TELL this TIEHANDLE classname, LIST WRITE this, scalar, length, offset =cut sub BINMODE { my ($slf, @arg) = shift; return (@arg) ? 0 : 1; } *CLOSE = \&close; sub DESTROY { } *EOF = \&eof; *FILENO = $und; *GETC = \&getc; *OPEN = \&open; *PRINT = $und; *PRINTF = $und; *READ = \&read; sub READLINE { goto &getlines if wantarray; goto &getline; } *SEEK = \&seek; *TELL = \&getpos; sub TIEHANDLE { my $slf = shift; return ref($slf) ? $slf : $slf->new(@_); } *WRITE = $und; 1; __END__ =head1 SEE ALSO L, L =head1 COPYRIGHT NOTICE Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. =head1 TRADEMARK NOTICE Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. =cut