]> gb7djk.dxcluster.net Git - spider.git/blob - perl/RingBuf.pm
use XML::Parser (it should be adequate for the little things we are doing).
[spider.git] / perl / RingBuf.pm
1 #
2 # Finite size ring buffer creation and access routines
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 use strict;
10
11 package RingBuf;
12
13 use vars qw($VERSION $BRANCH);
14 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
15 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
16 $main::build += $VERSION;
17 $main::branch += $BRANCH;
18
19
20 sub new
21 {
22         my $pkg = shift;
23         my $size = shift;
24         return bless [$size, 0, 0, 0, 0, []], (ref $pkg || $pkg);
25 }
26
27 sub write
28 {
29         my $self = shift;
30
31         $self->[5]->[$self->[2]++] = shift;
32         $self->[2] = 0 if $self->[2] >= $self->[0];
33         if ($self->[1] < $self->[0]) {
34                 $self->[1] = ++$self->[1];
35         }
36         $self->[2] = $self->[2];
37         if ($self->[1] == $self->[0] && $self->[2] == $self->[3]) {
38                 $self->[3] = $self->[2]+1;
39                 $self->[3] = 0 if $self->[3] >= $self->[0]; 
40         }
41 }
42
43 sub read
44 {
45         my $self = shift;
46         return unless $self->[1];
47         my $r;
48         
49         if ($self->[4] != $self->[2]) {
50                 $r = $self->[5]->[$self->[4]++];
51                 $self->[4] = 0 if $self->[4] >= $self->[0];
52         }
53         return $r;
54 }
55
56 sub rewind
57 {
58         my $self = shift;
59         $self->[4] = $self->[3];
60 }
61
62 sub lth
63 {
64         my $self = shift;
65         return $self->[1];
66 }
67
68 sub readall
69 {
70         my $self = shift;
71         my @out;
72         
73         $self->rewind;
74         while (my $r = $self->read) {
75                 push @out, $r;
76         }
77         return @out;
78 }
79 1;