-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileLoc.pm
63 lines (55 loc) · 1.29 KB
/
FileLoc.pm
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
package FileLoc;
use strict;
sub new {
my $class = shift;
my $dir = shift;
bless {dir=>$dir, dirs=>[], count=>0,max_ext_count=>0,file_loc=>{}}, $class;
}
sub scan_dir( $ ) {
my $self=shift;
my $dir = shift;
opendir my($DH), "$self->{dir}/$dir" or die "$self->{dir}/$dir: $!";
while (my $fn = readdir $DH) {
my $dfn="$dir/$fn";
my $pfn="$self->{dir}/$dfn";
if (-d $pfn) {
unless ($fn =~ /^\./) {
$self->scan_dir($dfn);
push @{$self->{dirs}}, $dfn;
};
next;
};
my $ext='';
if ($fn =~ /.\.([[:alnum:]]+)$/){
#$self->{ext_count}->{lc $1}++;
$ext = lc $1;
}
next if defined $self->{ignore_ext}{$ext};
next if defined($self->{only_ext}) and not($self->{only_ext}{$ext});
if (-f $pfn) {
++ $self->{count};
push @{$self->{file_loc}{$fn,-s $pfn}},$dfn;
if (++ $self->{ext_count}{$ext} > $self->{max_ext_count}) {
$self->{max_ext_count} = $self->{ext_count}{$ext};
$self->{max_ext} = $ext;
}
}
}
closedir $DH;
}
sub ignore_extension {
$_[0]{ignore_ext}{$_[1]}=1;
}
sub only_extension {
$_[0]{only_ext}{$_[1]}=1;
}
sub max_ext {
my $self = shift;
return $self->{count}?
($self->{max_ext}, $self->{max_ext_count}/$self->{count}):
();
}
sub ext_count ( $$ ) {
return $_[0]->{ext_count}{lc $_[1]};
}
1;