1 |
#!/usr/bin/perl |
2 |
# -*- Mode: Perl -*- |
3 |
# $Basename: Find.pm $ |
4 |
# $Revision: 1.4 $ |
5 |
# Author : Ulrich Pfeifer |
6 |
# Created On : Mon Sep 16 19:04:37 1996 |
7 |
# Last Modified By: Ulrich Pfeifer |
8 |
# Last Modified On: Wed Nov 5 16:50:40 1997 |
9 |
# Language : CPerl |
10 |
# Update Count : 48 |
11 |
# Status : Unknown, Use with caution! |
12 |
# |
13 |
# (C) Copyright 1997, Ulrich Pfeifer, all rights reserved. |
14 |
# |
15 |
# |
16 |
|
17 |
package WAIT::Document::Find; |
18 |
@ISA = qw(WAIT::Document::Base); |
19 |
require WAIT::Document::Base; |
20 |
|
21 |
use IO::File; |
22 |
use IO::Zlib; |
23 |
use strict; |
24 |
use Carp; |
25 |
|
26 |
sub TIEHASH { |
27 |
my $type = shift; |
28 |
my $pred = shift; |
29 |
my @files = @_; |
30 |
|
31 |
unless (ref($pred) =~ /CODE/) { |
32 |
croak "USAGE: tie %HASH, WAIT::Document::Find, coderef, file, ..."; |
33 |
} |
34 |
my $self = { |
35 |
Pred => $pred, |
36 |
Files => \@files |
37 |
}; |
38 |
bless $self, ref($type) || $type; |
39 |
} |
40 |
|
41 |
sub FETCH { |
42 |
my $self = shift; |
43 |
my $path = shift; |
44 |
|
45 |
return undef unless defined $path; |
46 |
return undef unless -f $path; |
47 |
|
48 |
my $fh; |
49 |
if ($path !~ /\.gz$/i) { |
50 |
$fh = new IO::File "< $path"; |
51 |
local($/) = undef; |
52 |
return <$fh>; |
53 |
} else { |
54 |
$fh = new IO::Zlib($path,"r"); |
55 |
# local($/) doesn't work for IO::Zip |
56 |
my $data; |
57 |
while (<$fh>) { |
58 |
$data .= $_; |
59 |
} |
60 |
return $data; |
61 |
} |
62 |
|
63 |
} |
64 |
|
65 |
sub FIRSTKEY { |
66 |
my $self = shift; |
67 |
$self->{Pending} = [@{$self->{Files}}]; |
68 |
$self->NEXTKEY; |
69 |
} |
70 |
|
71 |
sub NEXTKEY { |
72 |
my $self = shift; |
73 |
return undef unless @{$self->{Pending}}; |
74 |
my $next = pop @{$self->{Pending}}; |
75 |
while ($next and -f $next) { |
76 |
if (&{$self->{Pred}}($next)) { |
77 |
return $next; |
78 |
} |
79 |
$next = pop @{$self->{Pending}}; |
80 |
} |
81 |
if ($next and -d $next) { |
82 |
push @{$self->{Pending}}, _expand($next); |
83 |
} |
84 |
return $self->NEXTKEY; |
85 |
} |
86 |
|
87 |
sub _expand { |
88 |
my $dir = shift; |
89 |
my @result; |
90 |
return () unless -d $dir; |
91 |
opendir(DIR, $dir) or return (); |
92 |
@result = map "$dir/$_", grep $_ !~ /^\.\.?$/, readdir(DIR); |
93 |
closedir DIR; |
94 |
return @result; |
95 |
} |
96 |
|
97 |
sub close { |
98 |
my $self = shift; |
99 |
|
100 |
delete $self->{Pred}; |
101 |
delete $self->{Pending}; |
102 |
delete $self->{Files}; # no need at query time |
103 |
} |
104 |
|
105 |
1; |