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 FileHandle; |
22 |
use strict; |
23 |
use Carp; |
24 |
|
25 |
sub TIEHASH { |
26 |
my $type = shift; |
27 |
my $pred = shift; |
28 |
my @files = @_; |
29 |
|
30 |
unless (ref($pred) =~ /CODE/) { |
31 |
croak "USAGE: tie %HASH, WAIT::Document::Find, coderef, file, ..."; |
32 |
} |
33 |
my $self = { |
34 |
Pred => $pred, |
35 |
Files => \@files |
36 |
}; |
37 |
bless $self, ref($type) || $type; |
38 |
} |
39 |
|
40 |
sub FETCH { |
41 |
my $self = shift; |
42 |
my $path = shift; |
43 |
|
44 |
return undef unless defined $path; |
45 |
return undef unless -f $path; |
46 |
|
47 |
my $fh = new FileHandle "< $path"; |
48 |
|
49 |
local($/) = undef; |
50 |
<$fh>; |
51 |
} |
52 |
|
53 |
sub FIRSTKEY { |
54 |
my $self = shift; |
55 |
$self->{Pending} = [@{$self->{Files}}]; |
56 |
$self->NEXTKEY; |
57 |
} |
58 |
|
59 |
sub NEXTKEY { |
60 |
my $self = shift; |
61 |
return undef unless @{$self->{Pending}}; |
62 |
my $next = pop @{$self->{Pending}}; |
63 |
while ($next and -f $next) { |
64 |
if (&{$self->{Pred}}($next)) { |
65 |
return $next; |
66 |
} |
67 |
$next = pop @{$self->{Pending}}; |
68 |
} |
69 |
if ($next and -d $next) { |
70 |
push @{$self->{Pending}}, _expand($next); |
71 |
} |
72 |
return $self->NEXTKEY; |
73 |
} |
74 |
|
75 |
sub _expand { |
76 |
my $dir = shift; |
77 |
my @result; |
78 |
return () unless -d $dir; |
79 |
opendir(DIR, $dir) or return (); |
80 |
@result = map "$dir/$_", grep $_ !~ /^\.\.?$/, readdir(DIR); |
81 |
closedir DIR; |
82 |
return @result; |
83 |
} |
84 |
|
85 |
sub close { |
86 |
my $self = shift; |
87 |
|
88 |
delete $self->{Pred}; |
89 |
delete $self->{Pending}; |
90 |
delete $self->{Files}; # no need at query time |
91 |
} |
92 |
|
93 |
1; |