File: tail.pl

package info (click to toggle)
storebackup 1.19-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 780 kB
  • ctags: 368
  • sloc: perl: 11,979; makefile: 48; sh: 43
file content (138 lines) | stat: -rw-r--r-- 3,695 bytes parent folder | download | duplicates (3)
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
# -*- perl -*-

#
#   Copyright (C) Heinz-Josef Claes (2000)
#                 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 2 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, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#


require 'checkObjPar.pl';

push @VERSION, '$Id: tail.pl,v 1.4 2000/12/27 10:47:38 hjc Exp $ ';

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 (@lines);

    if ($self->{'openflag'} == 0)     # Datei "offnen
    {
	    # Datei wurde gel"oscht (oder mv), neue noch
	    # nicht angelegt, warten.
	return \@lines unless (open(FILE, $self->{'param'}{'-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 $self->{'param'}{'-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;
}

sub DESTROY
{
    my $self = shift;
    local *FILE = $self->{'filehandle'};
    close(FILE) or die "cannot close <", $self->{'param'}{'-filename'}, ">\n";
}