#!/bin/bash
function toHTML(){
local inFile=$1;
local outFile="${inFile}.dat";
if [ -f ${outFile} ]; then
rm ${outFile};
fi
sed 's/\t/\ \ \ \ /g' ${inFile} > ${outFile}
sed 's/$/ /g' ${outFile} > tempFile.dat;
mv tempFile.dat ${outFile};
}
fileName="";
if [ $# == 0 ]; then
fileName="\.";
else
fileName=$1;
fi
toHTML ${fileName};
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.
Friday, June 22, 2012
To Make Your Code Snippet Blogger Ready
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:
- It recursively counts the lines of code within all subdirectories of the location mentioned.
- 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.
- 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!
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!
#!/bin/bash
FALSE=0;
TRUE=1;
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;
fi
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}";
fi
else
echo "Something wrong with ${fullName}";
fi
done;
}
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
do
if [ "${line}" != "" ]; then
echo ${line} >> ${loc};
fi
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};
fi
done;
return ${FALSE};
}
# main
dirname="";
if [ $# == 0 ]; then
dirname="\.";
else
dirname=$1;
fi
locFile="${dirname}/loc.dat";
loc_dir ${dirname};
# remove_empty_lines ${dirname};
if [ -f "${locFile}" ]; then
result=( `wc -l "${locFile}"` );
else
result=0;
fi
loc=${result[0]};
echo "${loc} lines of code."
rm "${locFile}";
Subscribe to:
Posts (Atom)