Bits of Learning

Learning sometimes happens in big jumps, but mostly in little tiny steps. I share my baby steps of learning here, mostly on topics around programming, programming languages, software engineering, and computing in general. But occasionally, even on other disciplines of engineering or even science. I mostly learn through examples and doing. And this place is a logbook of my experiences in learning something. You may find several things interesting here: little cute snippets of (hopefully useful) code, a bit of backing theory, and a lot of gyan on how learning can be so much fun.

Wednesday, June 13, 2012

A Lines of Code Counter

The script below is a simple lines of code counter for you. Following interesting features of the script are:
  1. It recursively counts the lines of code within all subdirectories of the location mentioned.
  2. It counts the lines of code of only those files whose extensions are considered as source file name extensions in the list srcFileExtensions defined early in the script.
  3. Optionally, you could make it exclude the blank lines from the lines of code.
Caution: The script doesn't run like lightning! Suggested optimisations will be gratefully accepted.

Also, there are plenty of superior LOC tools out there. Just google! But for simple plain lines of code counting, this one would still be good for you to begin with. Just copy paste into a bash shell script and go!



srcFileExtensions=( \
#    java    # Java 
#    cpp    # C++
#    cc    # C++
#    h    # C, C++
#    hh    # C++
#    hpp     # C++
#    c     # C
#    py    # Python
#    pl    # Perl
    sh    # Shell
#    flex    # flex
#    lex    # lex
#    y    # yacc
#    yy    # yacc
#    cs    # C#
); # File name extensions which will be considered as source files. Remove/add as needed. Keep the list to minimum to keep the speed good.
function loc_dir(){
    local prefix=$1;
    if [ ! -d ${prefix} ]; then
        echo "${prefix} is not a directory. Quitting...";
        exit 1;

    local names=( `ls ${prefix}` );

    for name in ${names[@]}; do
        local fullName=${prefix}/${name};
        if [ -d "${fullName}" ]; then
            loc_dir "${fullName}"
        elif [ -f "${fullName}" ]; then
            isSrcFile ${fullName};
            local result=$?;
            if [ "${result}" == "${TRUE}" ]; then
                echo "including ${fullName} ...";
                cat "${fullName}" >> "${locFile}";
            echo "Something wrong with ${fullName}";

function remove_empty_lines(){
    local prefix=$1;

    echo "Removing empty lines...";

    local loc="${prefix}/loc.dat";
    local loc1="${prefix}/loc1.dat";
    mv ${loc} ${loc1};
    while read line
        if [ "${line}" != "" ]; then
            echo ${line} >> ${loc};
    done <${loc1}
    rm ${loc1};

function isSrcFile(){
    local fileName=$1;

    for ext in ${srcFileExtensions[@]}; do
        local extLength=`expr length ".${ext}"`;
        local nameLength=`expr length "${fileName}"`;
        local startPosition=`expr ${nameLength} - ${extLength}`;
        local len=`expr ${nameLength} - 1`;
        local subString=`echo ${fileName:${startPosition}:${len}}`;
        if [ "${subString}" == ".${ext}" ]; then
            return ${TRUE};
    return ${FALSE};

# main
if [ $# == 0 ]; then


loc_dir ${dirname};

# remove_empty_lines ${dirname};

if [ -f "${locFile}" ]; then
    result=( `wc -l "${locFile}"` );

echo "${loc} lines of code."
rm "${locFile}";

No comments: