JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / Filelist.pm
1 package Filelist;
2 use strict;
3 use warnings;
4 use base qw(Root Read Write);
5
6 sub read {
7         my ($self, $fh) = @_;
8
9         local $/ = "\n";
10         while (<$fh>) {
11                 chomp;
12                 my @files = split /\s+/, $_;
13                 $self->add_files(@files);
14         }
15 }
16
17 sub add_files {
18         my ($self, @files) = @_;
19         push @{ $self->{__PACKAGE__."files"} }, \@files;
20 }
21
22 sub get_files {
23         my ($self, @list) = @_;
24         if (@list) { return @{ $self->{__PACKAGE__."files"} }[@list] }
25         else { return @{ $self->{__PACKAGE__."files"} } }
26 }
27
28 sub all_files {
29         my ($self) = @_;
30         $self->{__PACKAGE__."offset"} ||= 0;
31
32         my $files = $self->get_files( $self->{__PACKAGE__."offset"}++ );
33         defined $files or undef $self->{__PACKAGE__."offset"}, return;
34         return @{$files};
35 }
36
37 1;
38
39 __END__
40
41 =head1 NAME
42
43 Filelist - Reads in a list of files.
44
45 =head1 SYNOPSIS
46
47 Reads in a file containing a list of files per line and allows you to retrieve them easily.
48
49 =head1 FILE EXAMPLE
50
51   foo0 bar0
52   foo1 bar1
53   foo2 bar2 moo2
54
55 =head1 INHERITS
56
57 Root, Read and Write.
58
59 =head1 METHODS
60
61 =head2 $list->read($filehandle)
62
63 Reads in the filelist. Splits the lines in the file on whitespace to find the files.
64
65 =head2 $list->add_files(@paths);
66
67 Add your own files to an existing Filelist object.
68
69 =head2 $list->get_files(@offsets)
70
71 If @offsets is not an empty list, returns an AoA of the files. Otherwise returns all off the files as an AoA.
72
73 =head2 $list->all_files()
74
75 Itterator for retrieving all the files held by the object. Every call returns the files as from get_files() until all of the lines have been gone through, when it returns undef.