Automated formating of old, ugly source code

I recently had to deal with tons of old and ugly C source code. Obviously many different people had worked on it over the years and the code used many different coding styles and had very weird and inconsistent indentation. Overall it was very hard to read and a pain to work with.

The tool that saved me is astyle and it works for C/C++ and Java according to the website. I’ve only used it on C so far but it really saved me tons of work. Debian and most likely many other Linux distros have a package so there’s no need to get it from the website.

You can customize that style you want it to convert the source to on the command line or just put the options into your ~/.astylerc file. Mine for C is here:

mode=c
style=java
indent-classes
indent-switches
indent-cases
break-blocks
pad-oper
add-brackets
add-one-line-brackets

With the file in place you don’t need to worry about that anymore. Just get into the directory with all the ugly code and let astyle fix it for you:


cd /old_ugly_C_code/
astyle *.c *.h

The original files still exist named filename.orig, you may want to remove them if everything worked fine.

Advertisements

About dfspspirit

PhD student in bioinformatics, interested in photography, level design, digital image manipulation, architecture and, of course, bioinformatics.
This entry was posted in coding and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s