Friday, June 17, 2011

Formatting Source Code with astyle

astyle is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages. It is cross-platform and gets the job done pretty well.

I use a combination of these switches to get code formatted to my liking:

#!/bin/sh
#
# Name: indent++
#
astyle \
    --style=1tbs \
    --indent=tab=4 \
    --indent-switches \
    --indent-preprocessor \
    --convert-tabs \
    --break-blocks \
    --break-closing-brackets \
    --unpad-paren \
    --pad-header \
    --pad-oper \
    --pad-paren-out \
    --add-brackets \
    --align-pointer=type \
    --align-reference=type < $file > $file.indented

mv $file $file.orig
mv $file.indented $file