#!/usr/bin/perl # # This is a script to re-indent C++ source code based on { and } # # It expects your brackets to be at the beginning of the line. Otherwise it won't work. # script was written to use 'bcpp' to do the dirty work of getting the brackets where they # need to be... # # bcpp is available at : http://dickey.his.com/bcpp/bcpp.html # # # This script itself lives at http://www.elifulkerson.com. # #@@ this doesn't handle switch/case very well. In order to do that, you need to remember what *kind* of fold #you are in. $indentlevel = 0; $danglingif = 0; $keepdangle = 0; while (<>) { # replace my whitespace s/^[ \t]*//; if (/^\}/) { $indentlevel -= 1; } # sometimes people do ugly things with #define and \ if ($indentlevel < 0) { $indentlevel = 0; } # print out to our current indent level for ($x=0;$x<$indentlevel;$x++) { print "\t"; } # "if" statements that are followed with more arguments on the next line are treated differently if ( !(/^\{/) && $danglingif) { if ( (/^\|\|/) || (/^\&\&/) || (/^\(/)) { print " "; } else { print "\t"; } } if ( (/^\|\|/) || (/^\&\&/) || (/^\(/)) { $keepdangle = 1; } # counting this later, you don't increment until after you print if (/^\{/) { $indentlevel += 1; } print; # check to see if the dangling if for next line if (/^if/ || /^else/ ) { $danglingif = 1; } else { $danglingif = 0; } # keep the dangle behavior, even if this isn't an 'if' line if ($keepdangle) { $danglingif = 1; $keepdangle = 0; } }