1#!/usr/bin/perl
2
3# Given USC output as standard input, find the # of physical and logical SLOC, and save in
4# "physical.sloc" and "logical.sloc".
5
6#
7# This is part of SLOCCount, a toolsuite that counts
8# source lines of code (SLOC).
9# Copyright (C) 2001-2004 David A. Wheeler.
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24#
25# To contact David A. Wheeler, see his website at:
26#  http://www.dwheeler.com.
27#
28#
29
30$found =0;
31
32while (<>) {
33  if (m/^The Totals/) {
34    $found = 1;
35    last;
36  }
37}
38
39if (!$found) {
40  die "FAILED to find the totals section in code output.\n";
41}
42
43while (<>) {
44  # DEBUG: print "Read line: $_\n";
45  if (m/Physical/ || m/Logical/) {
46    s/^ *//;
47    ($total, $blank, $whole, $embedded, $compiler, $datadecl, $execinstruction,
48     $number_of_files, $sloc, $file_type, $sloc_definition ) =   split(/[ \|]+/);
49    # DEBUG: print "Found match; file_type='${file_type}', sloc_definition='${sloc_definition}'\n";
50    if ($file_type =~ m/code/i) {
51       if ($sloc_definition =~ m/Physical/i) {
52              `echo $sloc > physical.sloc`
53       }
54       if ($sloc_definition =~ m/Logical/i) {
55              `echo $sloc > logical.sloc`
56       }
57    } elsif ($file_type =~ m/DATA/i) {
58       if ($number_of_files > 0) {
59         print STDERR "WARNING! NONZERO NUMBER OF DATA FILES!\n";
60         $pwd = `pwd`;
61         chomp($pwd);
62         print STDERR "Extract-count in directory ${pwd}.\n";
63         # The mere existence of this file is reason to check it out:
64         `echo $number_of_files > data.count`
65       }
66    }
67  }
68}
69
70
71__END__
72
73Here's a sample output (the beginning chopped off):
74
75                                    Temporary Project Name
76The Totals
77   Total   Blank |      Comments     |  Compiler  Data    Exec.  |  Number  |          File  SLOC
78   Lines   Lines |   Whole  Embedded |  Direct.   Decl.   Instr. | of Files |   SLOC   Type  Definition
79------------------------------------------------------------------------------------------------------------------------------------
80 1938455  359776 |  146182    164828 |        0   12359  1420138 |   3172   | 1432497  CODE  Physical
81 1938455  359776 |  146182    164828 |        0    6507   613235 |   3172   |  619742  CODE  Logical
82       0       0 |       0         0 |        0       0        0 |      0   |       0  DATA  Physical
83
84