#!/usr/bin/perl -0777
# This is a script to count the number of lines of source code in a  file

my @lines = ();
my $numofifs = 1;
my $commentstarted = 0;

while(<>) {
     s/\/\*[^\*\/]*\*\/?/cOmMeNt/gms;
     s/<!--[^->]*-->?/cOmMeNt/gms;
     s/\n/:/g; 
     @tmp = split(/:/);
     for(@tmp) { 
 
     if (/cOmMeNt/) {
                unless(/cOmMeNt/ ) {
			$_ = $_ . "\n";
                        push(@lines, $_);
			next;
                 } 
                 else {
                    if((/[\S]+[\s]*cOmMeNt/) || (/cOmMeNt[\s]*[\S]+/)) {
                       $_ = $_ . "\n";
                       push(@lines, $_);
                    }
	            					
                 }
         }
          else {
#slurp #if 0 comments
            if( (/[\s]*#[\s]*if[\s]+0/) || ($commentstarted == 1) ) {
                if ( $commentstarted == 0) {
                  $commentstarted = 1;
                  next;
                }	
                if(/[\s]*#[\s]*if/) {
                 $numofifs = $numofifs + 1;
                 next; 
                }
                unless(/[\s]*#[\s]*endif/) {
             	next; # slurp the comments
                }
                 else { # we found an endif, but we don't know if it is the right one...
                  $numofifs = $numofifs - 1;
                  if($numofifs == 0) {
                    $commentstarted = 0;
                    $numofifs = 1;
                  # found the end of comment , so just fall thro'
                  }
                  next;
                 }
            }
 
#leave out // comments and # comments
		if( (/[\s]*\/\//) || (/[\s]*#/)) {
	         next;		
		}
#leave out blank lines
		if(/^[\s]*$/) {
	         next;		
		}

# Now leave out =head section till =cut in perl scripts
if( (/^=head/) || ($commentstarted == 1) ) {
                if ( $commentstarted == 0) {
                  $commentstarted = 1;
                  next;
                }	
               unless(/^=cut/) {
             	next; # slurp the comments
                }
            # Now found cut, just fall thro'
            }
		$_ = $_ . "\n";
                push(@lines, $_);
my @lines = ();
          }

      }
}
print "The number of lines of code in file is [1m[4m " . @lines . "[0m\n";
exit @lines;

=head1 countlines.pl

Countlines - A script to count the number of lines of code in a C,C++,Java,perl,shell program file or html file

Usage: ./countlines.pl <sourcefile>

=head1 DESCRIPTION

This script leaves out the blank lines, C style comments, C++ style comments and #if 0 style comments and counts the remaining lines in the  C/C++ source file.

Something similar is done for perl,shell scripts and others.

  The result is printed to the terminal. The input is STDIN which means that a file can be redirected through a pipe or given in the command line.

=head1 README

This is a simple script to figure out the actual lines of code in a C,C++,perl,shell,html or Java source

=head1 PREREQUISITES

None

=head1 COREQUISITES

None

=pod OSNAMES

any

=pod SCRIPT CATEGORIES

Educational/ComputerScience

=cut

