Version 3 of Lines Of Code Counting

Updated 2006-02-09 12:40:30

if {0} {

LM 2006-02-09: I travel by car to work everyday. The journey takes three quarter of an hour about (traffic allowing). Especially on the way back, my mind uses his background activities to solve small problems.

My last evening journey small problem was: counting c lines of code in TCL way. I discarded C style solution to the problem (probably using a finite state machine) or console linux commands. Pure TCL code.

This is my straight off solution.

Comments and optimizations are appreciated.

What's about TCL LOC counting?

 }

 proc c_loc {fname} {
         set loc 0
         set cflag 0
         # Open file assuming all will be right
         set fd [open $fname r]
         while {![eof $fd]} {
                 # read a code line
                 gets $fd line

                 # cut-off comments that are totally enclosed into line 
                 set codeonly [regsub {//.*} $line ""]
                 set codeonly [regsub -all {/\*[^(\*/)]*\*/} $codeonly ""]

                 # cut-off multi-line comments
                 if {[regexp {(/\*.*)} $codeonly]} {
                         set codeonly [regsub {(/\*.*)} $codeonly ""]
                         set cflag 1
                 }
                 if {[regexp {(.*\*/)} $codeonly]} {
                         set codeonly [regsub {(.*\*/)} $codeonly ""]
                         set cflag 0
                 }

                 # count all ";" and "}" occurrences (line of code ending chars)
                 if {$cflag == 0} {
                         # For debugging uncomment the following line
                        # puts $codeonly
                         incr loc [regexp -all {[;\}]} $codeonly]
                 }
         }
         close $fd
         return $loc
 }

See also: Counting comments in a source


Category Development - Category File