Ticket #313: count_lines

File count_lines, 1.4 KB (added by igloo, 5 years ago)
Line 
1#! /usr/bin/perl
2#
3%DirCount = ();
4%ModCount = ();
5%DirComments = ();
6%ModComments = ();
7
8foreach $f ( @ARGV ) {
9
10    if ( $f =~ /\.lhs$/ ) {
11        open(INF, "../utils/unlit/unlit $f - |") || die "Couldn't unlit $f!\n";
12    } else {
13        open(INF, "< $f") || die "Couldn't open $f!\n";
14    }
15    $cnt = 0;
16    while (<INF>) {
17        s/--.*//;
18        s/{-.*-}//;
19        next if /^\s*$/;
20        $cnt++;
21    }
22    close(INF);
23
24    $f_wc = `wc $f`; die "wc failed: $f\n" if $? != 0;
25    if ( $f_wc =~ /\s*(\d+)\s*(\d+)\s*(\d+)/ ) {
26        $comments = $1 - $cnt;
27    } else {
28        die "Can't grok wc format: $f_wc";
29    }
30
31    if ( $f =~ /(.*)\/(.*)/ ) {
32        local($dir) = $1;
33        local($mod) = $2;
34        $DirCount{$dir} += $cnt;
35        $ModCount{$mod} += $cnt;
36        $DirComments{$dir} += $comments;
37        $ModComments{$mod} += $comments;
38    } else {
39        print STDERR "not counted in a directory: $f\n";
40        $ModCount{$f} += $cnt;
41        $ModComments{$f} += $comments;
42    }
43}
44
45# print the info
46$tot = 0;
47$totcmts = 0;
48foreach $d (sort (keys %DirCount)) {
49    printf "%-20s %6d %6d\n", $d, $DirCount{$d}, $DirComments{$d};
50    $tot     += $DirCount{$d};
51    $totcmts += $DirComments{$d};
52}
53printf "\n%-20s %6d %6d\n\n\n", 'TOTAL:', $tot, $totcmts;
54
55$tot = 0;
56$totcmts = 0;
57printf "\n                      Code  Comments\n";
58foreach $m (sort (keys %ModCount)) {
59    printf "%-20s %6d %6d\n", $m, $ModCount{$m}, $ModComments{$m};
60    $tot += $ModCount{$m};
61    $totcmts += $ModComments{$m};
62}
63printf "\n%-20s %6d %6d\n", 'TOTAL:', $tot, $totcmts;