Page 1 of 1

counting line numbers in my project

Posted: Fri Nov 01, 2002 6:35 am
by mydimension
has anyone tried doing something like this? i would like to have a "fun facts section of my site and on this page show the current number of lines of code in my project. couls someone point me in a direction to go with this? i would really appreciate it.

(note: i am not asking you to code this for me ;) )

Posted: Fri Nov 01, 2002 6:39 am
by jonsyd
how about just counting the number of ";" s? Thats what I do when a client asks why it takes 2 weeks to program something. Telling them you've written 3500 LOC tends to shut them up :)

Posted: Fri Nov 01, 2002 6:54 am
by davro
Try some thing like this
Only had a moment to test it, but it should return your selected file $file
In a textarea, enlightened by the length of the file.

Code: Select all

<? 

$file = "links.php"; 
$contents = file($file); 
$length = sizeof($contents); 

for($i=0; $i<$length; $i++) 
&#123; 
// buffers length, textarea for resizing 
&#125; 

// display enlightened form for display 
print "<p>File: $file has $length lines of text</p>\n"; 
print "<textarea border='0' name='text_area' wrap='off' rows='$i' cols='85'>\n"; 

for($j=0; $j<$length; $j++) 
&#123; 
// show the file line by line, in the textarea. 
print "$contents&#1111;$j]"; 
&#125; 

// finish textarea 
print "</textarea>\n"; 
?>
Hope helped

davro

Posted: Fri Nov 01, 2002 6:55 am
by seg
Well. if the file was named .phps, or anything but .php then it is easy to do. A simple 'while !eof $fs' scheme will work fine. If it is named .php, and constantly make a renamed copy is just too much work, (i can sympathize with that) then I'm not sure. I would think that if you used PHP to access another PHP file, even just to read the number of lines, it might try to execute the code. I could be wrong, though, I've never tried.

Code: Select all

&lt;?php
 $fd = fopen($bufferfile, "r");
 while (!feof ($fd)) {
// count code
}
close($fd);
?&gt;
that example has a LOT of overhead, and I don't recommend using it literally. I grabbed it from one of my corporate projects here. But the concept of it would work great for counting lines.

Posted: Fri Nov 01, 2002 7:59 am
by volka
when a script is executed a struct _zend_op_array is given as parameter to the worker C-function. It contains a count for all known opcodes. If only one could access this :D

Posted: Fri Nov 01, 2002 9:29 am
by lc
or simply do a:

Code: Select all

Currently there are: <b>

<?php

$file = ("yourfile.whatever");
$count = count($file);

print "$count";

?>

</b> lines in the project file

Posted: Fri Nov 01, 2002 9:36 am
by jonsyd
sorry to repeat myself, but why not count the ";" s?

Surely we want to count the number of LOC, not the number of lines in each file (unless you count blank lines, HTML, brackets and <?php / ?> as LOC)

Posted: Fri Nov 01, 2002 9:38 am
by seg
jonsyd wrote:sorry to repeat myself, but why not count the ";" s?

Surely we want to count the number of LOC, not the number of lines in each file (unless you count blank lines, HTML, brackets and <?php / ?> as LOC)
I'm glad you expounded on that, good point. As long as you can read a .php file via a php script without executing it, then yeah, its easy to count the ';'s I'm tempted to write the code when I get home and post it.

Posted: Fri Nov 01, 2002 11:34 am
by mydimension
thanx for the ideas. im leaning towards counting the semi-colons. i have about ten files to count through in two seperate directores, so the overhead is going to be substatial. good thing its only going to be one one page.

thx again guys/gals

Posted: Fri Nov 01, 2002 7:10 pm
by mydimension
for anyone interested, i have written a script which counts the number semi-colons and the number of lines containg either an opened brace or a closed brace BUT NOT both. here it is for your viewing pleasure.

(edit: i mod'ed the function so that all variables were declared inside the function)

Code: Select all

&lt;?php
function countCodeLines ($directory) {
	static $totalCodeLines;
	$dir = opendir($directory);
	
	while ($item = readdir($dir)) {
		if ((is_dir($item)) &amp;&amp; ($item != ".") &amp;&amp; ($item != "..")) {
			countCodeLines($item); //recursive directory walking
		} elseif (strrchr($item, ".") == ".php") { //count only php files
			$lines = file($directory . "/" . $item);
			
			foreach ($lines as $line) {
				//count lines containg a semi-colon or
				//lines containing either an opened brace or a closed brace BUT NOT both
				if (preg_match("/\;/", $line) xor (preg_match("/\{/", $line) xor preg_match("/\}/", $line))) {
					$totalCodeLines++;
				}
			}
		}
	}
	return $totalCodeLines; //return the final result
}

print countCodeLines($_SERVER&#1111;'DOCUMENT_ROOT']);
?&gt;
note: wherever you see & replace with &

again i thank you all for your input. consider this your pay ;) :D