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

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

while ( <> ) {
     s/\/\*.*\*\//cOmMeNt/gs;
     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;
                 }
            }
 
		if(/[\s]*\/\//) {
	         next;		
		}
		$_ = $_ . "\n";
                push(@lines, $_);
          }

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

=head1 countlines.pl

Countlines - A script to count the number of lines of code in a C program file

=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 source file.  Theresult 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

If there is any text in this section, it will be extracted into
a separate README file.

=head1 PREREQUISITES

None

=head1 COREQUISITES

None

=pod OSNAMES

any

=pod SCRIPT CATEGORIES

Educational/ComputerScience

=cut

