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