1#!/usr/local/bin/perl
2# generic_count - count physical lines of code, given a comment marker.
3# Usage: generic_count commentstart [-f file] [list_of_files]
4#  commentstart: string that begins a comment (continuing til end-of-line)
5#  file: file with a list of files to count (if "-", read list from stdin)
6#  list_of_files: list of files to count
7#  -f file or list_of_files can be used, or both
8# This is a trivial/naive program for scripts, etc.
9
10
11# This is part of SLOCCount, a toolsuite that counts
12# source lines of code (SLOC).
13# Copyright (C) 2001-2004 David A. Wheeler.
14#
15# This program is free software; you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation; either version 2 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program; if not, write to the Free Software
27# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28#
29# To contact David A. Wheeler, see his website at:
30#  http://www.dwheeler.com.
31#
32#
33
34$commentstart = shift @ARGV;
35$total_sloc = 0;
36
37# Do we have "-f" (read list of files from second argument)?
38if (($#ARGV >= 1) && ($ARGV[0] eq "-f")) {
39  # Yes, we have -f
40  if ($ARGV[1] eq "-") {
41    # The list of files is in STDIN
42    while (<STDIN>) {
43      chomp ($_);
44      &count_file ($_);
45    }
46  } else {
47    # The list of files is in the file $ARGV[1]
48    open (FILEWITHLIST, $ARGV[1]) || die "Error: Could not open $filewithlist\n";
49    while (<FILEWITHLIST>) {
50      chomp ($_);
51      &count_file ($_);
52    }
53    close FILEWITHLIST;
54  }
55  shift @ARGV; shift @ARGV;
56}
57# Process all (remaining) arguments as file names
58while ($file = shift @ARGV) {
59  &count_file ($file);
60}
61
62print "Total:\n";
63print "$total_sloc\n";
64
65sub count_file {
66  my ($file) = @_;
67  my $sloc = 0;
68
69  open (FILE, $file);
70  while (<FILE>) {
71    s/${commentstart}.*//;
72    if (m/\S/) {$sloc++;}
73  }
74  print "$sloc $file\n";
75  $total_sloc += $sloc;
76  close (FILE);
77}
78