File: MyStuff.pm

package info (click to toggle)
libperl-critic-pulp-perl 96-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,684 kB
  • sloc: perl: 13,643; sh: 267; makefile: 6; ansic: 1
file content (138 lines) | stat: -rw-r--r-- 3,441 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright 2009, 2011 Kevin Ryde.

# MyStuff.pm is shared by various distributions.
#
# MyStuff.pm is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# MyStuff.pm is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this file.  If not, see <http://www.gnu.org/licenses/>.

package MyStuff;
use 5.010;
use strict;
use warnings;
use Text::Tabs;

#my $verbose = 0;

sub line_at_pos {
  my ($str, $pos) = @_;
  my $start = (rindex ($str, "\n", $pos) || -1) + 1;
  my $end = (index ($str, "\n", $pos) || length($str)-1) + 1;
  return substr($str, $start, $end - $start);
}

sub pos_to_line_and_column {
  my ($str, $pos) = @_;
  $str = substr ($str, 0, $pos);
  my $nlpos = rindex ($str, "\n");
  my $lastline = substr ($str, $nlpos+1);
  $lastline = Text::Tabs::expand ($lastline);
  my $colnum = 1 + length ($lastline);
  my $linenum = 1 + scalar($str =~ tr/\n//);
  return ($linenum, $colnum);
}



package Iterator::Simple::FileUniq;
sub new {
  my ($class, $it) = (shift, shift);
  my $fu = FileUniq->new (@_);
  return Iterator::Simple::iterator
    (sub {
       my $filename;
       while (defined (my $filename = $it->next)) {
         last if $fu->uniq ($filename);
       }
     });
}

package Locator;
sub new {
  my ($class, @args) = @_;
  open my $fh, '-|', 'locate', '-0', '--', @args or die;
  return bless { fh => $fh,
               }, $class;
}
sub next {
  my ($self) = @_;
  my $fh = $self->{'fh'};
  my $filename;
  {
    local $/ = "\0";
    $filename = <$fh>;
    if (defined $filename) {
      chomp $filename;
    }
  }
  return $filename;
}

package Locator::BinScripts;
sub new {
  my ($class, $type) = @_;
  return bless { 'type' => $type,
                 'locator' => Locator->new ('/bin/*',
                                            '/usr/bin/*',
                                            '/usr/local/bin/*',
                                            '/usr/local/bin2/*',
                                           ) }, $class;
}
sub next {
  my ($self) = @_;
  my $type = $self->{'type'};
  for (;;) {
    my $filename = $self->{'locator'}->next // return undef;
    my ($fh, $buf);
    if (open($fh,'<',$filename)
        && read($fh,$buf,80)
        && $buf =~ m{^#![a-z0-9/]*/$type([ \t]|$)}) {
      return $filename;
    }
  }
}

package Locator::Concat;
sub new {
  my ($class, @locators) = @_;
  return bless { 'locators' => \@locators }, $class;
}
sub next {
  my ($self) = @_;
  my $locators = $self->{'locators'};
  for (;;) {
    @$locators or return undef;
    my $filename = $locators->[0]->next;
    if (defined $filename) { return $filename; }
    shift @$locators;
  }
}

1;
__END__
my @files = split /\n/, `locate \*.t \*.pm \*.pl`;
@files = grep {-f $_} @files;

@files = uniq_by_func (\&stat_dev_ino, @files);
sub uniq_by_func {
  my $func = shift;
  my %seen;
  return grep { $seen{$func->($_)}++ == 0 } @_;
}
sub stat_dev_ino {
  my ($filename) = @_;
  my ($dev, $ino) = stat ($filename);
  return "$dev,$ino";
}

print "look at ",scalar(@files)," files\n";