1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
# -*- perl -*-
#
# Copyright (C) Heinz-Josef Claes (2000-2012)
# hjclaes@web.de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 'checkObjPar.pl';
push @VERSION, '$Id: tail.pl 362 2012-01-28 22:11:13Z hjc $ ';
use strict;
######################################################################
# Objekt zum Weiterlesen einer Datei, hierzu ist regelm"a"sig, durch
# sleep unterbrochen, die Methode read aufzurufen
package tailOneFile;
sub new
{
my $class = shift;
my $self = {};
# Defaultwerte f"ur Parameter setzen
my (%params) = ('-filename' => undef,
'-position' => 'begin',# Datei von Anfang an durchsuchen
# kann die Werte 'begin' oder
# 'end' haben
'-prefix' => '', # wird vor jede Zeile geh"angt
'-postfix' => '', # wird an jede Zeile geh"angt
'-maxlines' => undef # maximale Anzahl von Zeilen pro
# Aufruf von read
);
&::checkObjectParams(\%params, \@_, 'tailOneFile::new', ['-filename']);
$self->{'param'} = \%params;
$self->{'filesize'} = 0;
if ($params{'-position'} eq 'begin')
{
$self->{'curpos'} = 0;
$self->{'filehandle'} = undef;
$self->{'openflag'} = 0; # Datei ist nicht ge"offnet
}
elsif ($params{'-position'} eq 'end')
{
local *FILE;
if (open(FILE, $params{'-filename'}))
{
$self->{'openflag'} = 1;
$self->{'filehandle'} = *FILE;
$self->{'curpos'} = (stat FILE)[7];
seek(FILE, $self->{'curpos'}, 0);
}
}
else
{
print STDERR "wrong value for <-position>: must be 'begin' or 'end'\n";
}
bless($self, $class);
}
sub getpar
{
my $self = shift;
my $par = shift;
return $self->{'param'}{$par};
}
sub read
{
my $self = shift;
local *FILE;
my $filename = $self->{'param'}{'-filename'};
my (@lines) = ();
return(\@lines, "no read permissions for <$filename>")
if -e $filename and not -r $filename;
if ($self->{'openflag'} == 0) # Datei "offnen
{
# Datei wurde gel"oscht (oder mv), neue noch
# nicht angelegt, warten.
return \@lines unless (open(FILE, $filename));
$self->{'filehandle'} = *FILE;
$self->{'openflag'} = 1;
}
*FILE = $self->{'filehandle'};
my ($curpos) = $self->{'curpos'};
my ($pre) = $self->{'param'}{'-prefix'};
my ($post) = $self->{'param'}{'-postfix'};
my ($l, $i);
my $max = $self->{'param'}{'-maxlines'};
for ($curpos = tell(FILE), $i = 1 ; $l = <FILE> ;
$curpos = tell(FILE), $i++)
{
chomp $l;
push @lines, "$pre$l$post\n";
last if ($max and $i >= $max);
}
my ($size_old) = $self->{'filesize'};
$self->{'filesize'} = (stat $filename)[7];
if ($self->{'filesize'} < $size_old)
{
close(FILE);
$self->{'openflag'} = 0;
}
else
{
seek(FILE, $curpos, 0); # seek to where we had been
}
$self->{'curpos'} = $curpos;
return (\@lines, undef); # ok
}
sub DESTROY
{
my $self = shift;
local *FILE = $self->{'filehandle'};
close(FILE) or die "cannot close <", $self->{'param'}{'-filename'}, ">\n";
}
|