Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
generic line counting tool for programming
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
correctclick
n00b
n00b


Joined: 18 Jul 2002
Posts: 71

PostPosted: Tue Jan 21, 2003 9:32 pm    Post subject: generic line counting tool for programming Reply with quote

Hi. I have a directory structure with a whole bunch of php source files and I want to count the lines. I've fooled around with grep -crh "\n" website_directory, but I just get a long string of numbers. This could of course be fixed with relatively simple script, but I figured there must be some general purpose tool for doing this.

What do people use to count lines in various programming languages?
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Tue Jan 21, 2003 9:40 pm    Post subject: Reply with quote

wc?

Code:

wc -l `find -name '*.php'`

or
Code:

find -name '*.php' -exec wc -l {} \; | sort +0n

if you want a sorted list without a total.
Back to top
View user's profile Send private message
Tarball
Tux's lil' helper
Tux's lil' helper


Joined: 19 Jun 2002
Posts: 142
Location: Cheshire, UK

PostPosted: Tue Jan 21, 2003 9:42 pm    Post subject: Reply with quote

This task is very open to interpretation as to what you consider a line of code to be. The following is a lump of Perl code I wrote to give me a *rough* idea of the number of lines of C code I have. Basically, it filters out lines that are comments and looks for the usage of keywords. This was something I knocked together very quickly (5 mins) so it is only intended to give a ball-park figure.

Code:

#!/usr/bin/perl -w

$lines_of_code = 0;

while(<ARGV>)
{
        $in_comment = rindex($_, "/*") > rindex($_, "*/");

        if( ! $in_comment )
        {
                if( /;/ ) { $lines_of_code++ ; next ; }
                if( /if/ ) { $lines_of_code++ ; next ; }
                if( /while/ ) { $lines_of_code++ ; next ; }
                if( /include/ ) { $lines_of_code++ ; next ; }
                if( /define/ ) { $lines_of_code++ ; next ; }
                if( /switch/ ) { $lines_of_code++ ; next ; }
                if( /case/ ) { $lines_of_code++ ; next ; }
                if( /struct/ ) { $lines_of_code++ ; next ; }
                if( /\(.*\)/ ) { $lines_of_code++ ; next ; }
                if( /\[.*\]/ ) { $lines_of_code++ ; next ; }
                if( /union/ ) { $lines_of_code++ ; next ; }
                if( /do/ ) { $lines_of_code++ ; next ; }
        }
}

print $lines_of_code . " lines of code\n";
Back to top
View user's profile Send private message
correctclick
n00b
n00b


Joined: 18 Jul 2002
Posts: 71

PostPosted: Tue Jan 21, 2003 9:49 pm    Post subject: Reply with quote

thanks for the quick replies. The wc thign worked great.

For anyone who knows:
what is a line of code defined as "in the industry"? A newline character? Semicolins?
Back to top
View user's profile Send private message
mmealman
Guru
Guru


Joined: 02 Nov 2002
Posts: 348
Location: Florida

PostPosted: Tue Jan 21, 2003 9:53 pm    Post subject: Reply with quote

I'm
guessing
it's
any
line
with
a
\n

Wish
I
got
paid
by
the
line.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum