Monday, November 30, 2009

A Perl script to count LOC (Lines Of Code)


#!/usr/bin/perl

#
# This script counts number of lines in files of specified type.
# The counting is done recursively into subdirectory.
# @Usage: perl getLOC.pl [dir|file]
# If no argument is provided, start from current dir ".".
# @By: XC
# @Created on: 11/29/2009
#

print "\n- Line Counter -\n\n";

# specify file type(s) here. Use "[]" to escape ".".
my @types = ("[.]c", "[.]h");
#my @types = ("[.]cs"); #, "[.]aspx");
#my @types = ("[.]asp");

my $dirname = ".";
my $total_loc = 0;

# Get input directory name.
my $argc = $#ARGV + 1;
if ($argc > 0) {
$dirname = $ARGV[0];
}

# Process the starting directory or file.
if (-d $dirname) {
processDIR($dirname);
} else {
if (inTypesArray($dirname)) { countLOC($dirname); }
}

# Output total line count.
print "\n[$dirname] Total Lines: $total_loc\n";

1;


#
# Recursively process directory.
#
sub processDIR() {
my ($dirname) = @_;
my $file;

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
# Exclude "." and "..".
my @files = grep { !/^\.{1,2}$/ } readdir (DIR);
closedir(DIR);
#sort @files;

foreach (@files) {
$file = "$dirname/$_";

if (-d $file) {
processDIR($file); # Is directory. Recursion.
}
elsif (inTypesArray($file)) {
countLOC($file);
}
}
}


#
# Determine if this file is of specified type.
#
sub inTypesArray() {
my ($f) = @_;
my $t;
foreach $t (@types) {
if ($f =~ /$t$/) { return 1; }
}
return 0;
}


#
# Count number of lines in the file.
#
sub countLOC() {
my ($file) = @_;
my $loc = 0;
my @lines;
my $line_ct;

open(FILE, "$file");
while(<FILE>) {
@lines = split(/\r/, $_);
$line_ct = @lines;
#$loc ++;
$loc += $line_ct;
}
close(FILE);
print "[$file] Lines: $loc\n";

$total_loc += $loc;
}

2 comments:

Unknown said...

the while in the countLOC sub is empty, resulting in an infinite loop...

Tom said...

The code was copy/pasted, so left and right brackets were treated as html tags.. Now it's ok.

Blog Archive

Followers