Last active
December 17, 2015 18:29
-
-
Save akirad/5653214 to your computer and use it in GitHub Desktop.
A perl script which gets line number of Java codes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
#--------------------------------------- | |
# Arg check. | |
#--------------------------------------- | |
my $dir = $ARGV[0]; | |
my $usage = << "END_USAGE"; | |
Usage: $0 directory | |
END_USAGE | |
if((@ARGV!=1) || (! -d $dir)){ | |
die "$usage"; | |
} | |
#--------------------------------------- | |
# Main. | |
#--------------------------------------- | |
$dir =~ s|\\|/|g; | |
my @fileList; | |
getFileList($dir, \@fileList); | |
my $sum = 0; | |
foreach my $file (@fileList){ | |
my $lineNum = getSrcLineNum($file); | |
print($file . " : " . $lineNum . "\n"); | |
$sum += $lineNum; | |
} | |
print("SUM : " . $sum); | |
#--------------------------------------- | |
# Sub functions. | |
#--------------------------------------- | |
sub getSrcLineNum{ | |
my $file = shift; | |
my $lineNum = 0; | |
open(my $fh, "< $file") or die "Can not open $file: $!"; | |
my $inComment = 0; | |
while(my $line = <$fh>){ | |
chomp($line); | |
#--------------------------------------- | |
# Skip blank line and comment line. | |
#--------------------------------------- | |
if($line =~/^\s*$/){ | |
next; | |
} | |
if($line =~ /^\s*\/\//){ | |
next; | |
} | |
if($line =~ /^\s*\/\*/){ | |
$inComment = 1; | |
next; | |
} | |
if($inComment == 1){ | |
if($line =~ /\*\/\s*$/){ | |
$inComment = 0; | |
next; | |
} | |
else{ | |
next; | |
} | |
} | |
$lineNum++; | |
} | |
return $lineNum; | |
} | |
sub getFileList{ | |
my $dir = shift; | |
my $ref_fileList = shift; # for output. | |
opendir(DIR, $dir); | |
my @fileList = readdir(DIR); | |
closedir(DIR); | |
foreach my $file (sort @fileList){ | |
if($file =~ /^\.{1,2}$/){ | |
next; | |
} | |
if( -d "$dir/$file"){ | |
# For dir. | |
getFileList("$dir/$file", $ref_fileList); | |
} | |
else{ | |
# For file. | |
if($file =~ /\.java$/){ | |
push(@$ref_fileList, "$dir/$file"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment