Monday, January 12, 2009

SLOC (Source Lines of Code)

Reading comments about Software Enginering, I have found a web based CocomoII (COnstructive COst MOdel II), which allows one to estimate the cost and effort when planning a new software development activity.

I have programmed a little bash program which counts the source lines of a forlder. It skips new lines and php comments (/* */) when counting.

Code
#!/bin/bash

# SLOC (Source Lines of Code)
# This application is used to count the source lines of code
# of a project
# usage: sloc /home/pron/myphpproject
# http://www.cms4site.ru/utility.php?utility=cocomoii

# awk usage
# http://www.liamdelahunty.com/tips/linux_ls_awk_totals.php
# sed usage
# http://www.gnulamp.com/sed.html
# http://www.grymoire.com/Unix/

# if $1 parameter is a folder,
# will show recursively the lines that have the files
# but will exclude lines that contents any special character:
# * : initially used to delete comments like /* or */
# ^$ : ^ means beginning of line and $ means end of line,
# so it deletes lines when only appears '/n'

function count_lines {
if [ -d $1 ]
then
for j in $1/*
do
if [ -f $j ]
then
sed -e '/\/\*/ d' -e '/\*\// d' -e '/^$/ d' $j | wc -l
fi
if [ -x $j ]
then
$0 $j
fi
done
fi
}

count_lines $1 | awk '{ SUM +=$1 } END { print SUM} '

No comments: