#!/usr/bin/perl
# SourceCodeLineCounter.pl
# Rudolf Cardinal, March 2007.
# Usage: go into a directory, run this; it'll tell you how many lines of code (.C, .CPP, .H, .HPP) are in this
# and child directories.

use File::Find;
find (\&Wanted, '.');

foreach $file (sort keys %count) {
  print "$file has $count{$file} lines\n";
}
print "\nTOTAL NUMBER OF LINES: $totallines";

sub Wanted {
  # upon entry: $_ is the name of the file being processed
  #             $File::Find::dir is the name of the directory
  #             $File::Find::name is the full path/filename
  # see http://perldoc.perl.org/File/Find.html
  
  # We only want to count *.c, *.cpp, *.h
  # see http://www.stonehenge.com/merlyn/UnixReview/col02.html
  
  # in pattern matching, \. represents a dot literal (.), since a dot is also an escape character.
  # The $ anchors a pattern to the end of the string.
  /\.cpp$/ or /\.c$/ or /\.h$/ or /\.hpp$/ or return;
  
  local($linesthisfile, $filename, $localfilename);
  $fullfilename = $File::Find::name;
  $localfilename = $_;
  @ARGV = $localfilename;
  while (<>) {
    $linesthisfile++;
    $totallines++;
  }
  $count{$fullfilename} = $linesthisfile;
}
