Tuesday, May 10, 2011

replaceTab, an utility program

I use PasteBin for all my online code snippets. However there is one small problem which I find about their service:
Tabs expand to 8 spaces
While many might not understand the fuss, I find this quite annoying. I use Tabs for my code and the default number of spaces I use  for Tab is 4. Expanding Tab to 8 spaces, makes my code look ugly (kind of stretched). Being a programmer, I wrote this following piece of code in C which replaces the Tab by as many characters as one wants.

I designed the program to read and write to stdin and stdout so that I can use it as a pipe for further programs (mostly to facilitate joining multiple files). The syntax is simple:
replaceTab 4 < inpFile > outFile
The first argument is the number of characters I need per Tab. The default value is 4 which is assumed when there are no arguments.

Here is the C source code for the same (yes, I have posted it in PasteBin):



PS: Before writing the code, I tried using the unix command line utility "tr" for this, but could not get it to replace one Tab by multiple spaces. However as it was pointed out to me by a friend this is simple using "sed". The corresponding command is: "sed 's/\t/    /g' < inpFile". ... Looks like I should be spending more time learning Unix utilities than writing simple programs.

No comments:

Post a Comment

I'd love to hear from you !