Debian Codegraph @

alc

raw
1 #!/usr/bin/perl
2
3 # Copyright (c) 2011 Erik Aronesty ([email protected])
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 #
23 # NOTE: Please let me know if you use it or like it, AKA: "Thank You Mask Man"
24
25 use strict;
26 use Getopt::Long;
27 our $VERSION = 1.2;
28
29 my %nl;
30 my $w=200000; # window size
31 my $x=10; # segment count
32 my $only;
33 GetOptions("only"=>\$only, "window=i"=>\$w, "segs=i"=>\$x);
34
35 die usage() unless @ARGV;
36 die usage() if $only && @ARGV > 1;
37 $|=1;
38 #$x-=1;
39 my $tl=0;
40 for my $f (@ARGV) {
41 my $s = -s $f;
42 my $gz = $f =~ /\.gz$/;
43 open (IN, ($gz ? "gunzip -c '$f'|" : $f)) || (warn("$f: $!\n"), next);
44 my $nl = 0;
45 my $gzratio = 1;
46 if ($gz) {
47 if ( $s > $w ) {
48 # todo, this can be done all-in-one stream, by buffering the read and gzipping in a loop
49 # it will be much faster!
50 my (@bz, @lz);
51 $x = 5; # 5 points
52 my $ss = $w/$x;
53 for (my $i=0; $i<$x; ++$i) {
54 my $o = int($ss * ($i+1));
55 $bz[$i]=`gunzip -c '$f' | head -$o | gzip -c | wc -c`;
56 if (abs($s-$bz[$i]) < ($ss+length($f))) {
57 $nl=`gunzip -c '$f' | wc -l`;
58 goto MAXXED;
59 } else {
60 $lz[$i]=$s*$o/$bz[$i];
61 }
62 # warn("o: ", $o, ", lzi:", $lz[$i], ", bzi:", $bz[$i], "\n");
63 }
64
65 # simple linear model log(bytes)~lines
66 my ($xxs, $xys, $xs, $ys);
67 for (my $i=0; $i<$x; ++$i) {
68 $xs+=log($bz[$i]);
69 $ys+=$lz[$i];
70 $xxs+=log($bz[$i])*log($bz[$i]);
71 $xys+=$lz[$i]*log($bz[$i]);
72 }
73 my $slope = ($xys - $xs*$ys/$x) / ($xxs - $xs*$xs/$x);
74 my $intercept = ($ys - ($slope*$xs))/$x;
75 # warn("intercept: $intercept, slope: $slope, s: $s\n");
76 # log size predictive of lines.....
77 $nl = $intercept + $slope * log($s);
78 MAXXED:
79 } else {
80 close(IN);
81 $nl = `gunzip -c '$f' | wc -l` + 0;
82 }
83 } else {
84 my $rc = 0;
85 if ($s > ($w*2)) {
86 my $ss = $x == 1 ? $w : (($s-($w/$x))/($x-1));
87 my $d;
88 # warn("segments: $x, window: $w, ss:$ss\n");
89 my $tweight;
90 for (my $i=0;$i<$x;++$i) {
91 seek(IN, $i*$ss, 0) if $i > 0;
92 read(IN, $d, $w/$x);
93 #whole lines only, prevents overcounting
94 if ($i > 0) {
95 # seek before current block, getting more accurate "long-first-lines"
96 my $z;
97 seek(IN, $i*$ss-1000, 0);
98 read(IN, $z, 1000);
99 $z=substr($z, rindex($z, "\n")+1);
100 $d=$z.$d;
101 } else {
102 #$d=substr($d, index($d, "\n")+1); # remove first line
103 }
104 $d=substr($d, 0, rindex($d, "\n")+1); # remove last line
105 $rc += length($d);
106 $nl += xlc($d);
107 # printf STDERR "seek: %d, len: %d, xlc: %d, rc: %d, nl: %d\n", $i*$ss, length($d), xlc($d), $rc, $nl;
108 }
109 } else {
110 my $d;
111 read(IN, $d, $w);
112 $d=substr($d, 0, rindex($d, "\n")+1); # remove last line
113 $rc += length($d);
114 $nl += xlc($d);
115 }
116 $nl = int($rc > 0 ? $nl * $s / $rc : 0);
117 }
118 $tl += $nl;
119 print "$nl" if $only;
120 print "\n" if $only && -t STDOUT;
121 $nl{$f} = $nl if !$only;
122 }
123
124 if (!$only) {
125 my $m=length($tl)+1;
126 for my $f (@ARGV) {
127 printf "%*d %s\n", $m, $nl{$f}, $f;
128 }
129 printf "%*d %s\n", $m, $tl, "total" if @ARGV > 1;
130 }
131
132 sub xlc {
133 my $d = $_[0];
134 my $p=0;
135 my $c=0;
136 my $i=0;
137 while (($i=index($d, "\n", $p))>=0) {
138 ++$c;
139 $p=$i+1;
140 }
141 return $p<length($d) ? $c+1 : $c; # correct count
142 }
143
144 sub usage {<<EOF
145 Usage: alc [-o] <file1> [<file2>] ...
146
147 Approximate line counts for each file. Attempts to be
148 somewhat compatible with "wc -l" by default.
149
150 -o|--only Output line count only for a single file.
151 -w|--window <int> Read <int> bytes from head, mid, and tail.
152 -s|--segs <int> Divide file & window into <int> segments.
153 EOF
154 };
155
156 __END__
157
158 =head1 NAME
159
160 alc - Approximate line count
161
162 =head1 DESCRIPTION
163
164 Approximate line counts for each file. Attempts to be
165 somewhat compatible with "wc -l" by default.
166
167 =head1 AUTHOR
168
169 Erik Aronesty C<[email protected]>
170
171 =head1 LICENSE
172
173 This program is free software; you can redistribute it and/or
174 modify it under the same terms as Perl itself.
175
176 See L<http://www.perl.com/perl/misc/Artistic.html>.
177
178 =cut