Revision: 4172 http://geany.svn.sourceforge.net/geany/?rev=4172&view=rev Author: ntrel Date: 2009-09-10 16:11:13 +0000 (Thu, 10 Sep 2009)
Log Message: ----------- Take multiple files if -w argument is supplied.
Modified Paths: -------------- trunk/scripts/fix-alignment.pl
Modified: trunk/scripts/fix-alignment.pl =================================================================== --- trunk/scripts/fix-alignment.pl 2009-09-07 18:33:31 UTC (rev 4171) +++ trunk/scripts/fix-alignment.pl 2009-09-10 16:11:13 UTC (rev 4172) @@ -18,69 +18,79 @@ my $argc = $#ARGV + 1; my $scriptname = $0;
-($argc == 1) or die <<END; +(($argc == 1) or ($argc >= 1 and $opt_write)) or die <<END; Usage: $scriptname sourcefile [>outfile] Print formatted output to STDOUT or outfile. - Warning: do not use the same file for both. + Warning: do not use the same file for outfile.
-$scriptname -w sourcefile - Writes to the file in-place. +$scriptname -w sourcefile(s) + Writes to the file(s) in-place. Warning: backup your file(s) first or use clean version control files. END
-# TODO: operate on multiple files -my ($infile) = @ARGV; -my @lines;
-open(INPUT, $infile) or die "Couldn't open $infile for reading: $!\n"; +sub parse($) +{ + my ($infile) = @_; + my @lines;
-while (<INPUT>) { - my $line = $_; # read a line, including \n char + open(INPUT, $infile) or die "Couldn't open $infile for reading: $!\n";
- # strip trailing space & newline - $line =~ s/\s+$//g; + while (<INPUT>) { + my $line = $_; # read a line, including \n char
- # for now, don't process lines with comments, strings, preproc non-defines, overflowed lines or chars - # multi-line comment internal lines are skipped only if they start with '* '. - if (!($line =~ m,/*|*/|//|"|\$|',) and !($line =~ m/^\s*(*\s|#[^d])/)) { - # make binary operators have *one* space each side - # operators must have longer variants first otherwise trailing operators can be broken e.g. "+ =" - # '*' ignored as could be pointer - # '-' ignored as could be unary "-1" - # '&' ignored as could be address-of "(type*)&foo" - my $ops = '<<=,<<,>>=,>>,<=,>=,<,>,||,|=,|,&&,&=,-=,+=,+,*=,/=,/,==,!=,%=,%,^=,^,='; - $ops =~ s/([|*+])/\$1/g; # escape regex chars - $ops =~ s/,/|/g; - $line =~ s/([\w)]])\s*($ops)\s*([\w(]|$)/$1 $2 $3/g; + # strip trailing space & newline + $line =~ s/\s+$//g;
- # space ternary conditional operator - $line =~ s/\s*?\s*(.+?)\s*:\s*/ ? $1 : /g; + # for now, don't process lines with comments, strings, preproc non-defines, overflowed lines or chars + # multi-line comment internal lines are skipped only if they start with '* '. + if (!($line =~ m,/*|*/|//|"|\$|',) and !($line =~ m/^\s*(*\s|#[^d])/)) { + # make binary operators have *one* space each side + # operators must have longer variants first otherwise trailing operators can be broken e.g. "+ =" + # '*' ignored as could be pointer + # '-' ignored as could be unary "-1" + # '&' ignored as could be address-of "(type*)&foo" + my $ops = '<<=,<<,>>=,>>,<=,>=,<,>,||,|=,|,&&,&=,-=,+=,+,*=,/=,/,==,!=,%=,%,^=,^,='; + $ops =~ s/([|*+])/\$1/g; # escape regex chars + $ops =~ s/,/|/g; + $line =~ s/([\w)]])\s*($ops)\s*([\w(]|$)/$1 $2 $3/g;
- # space comma operator (allowing for possible alignment space afterwards) - $line =~ s/\s*,(\S)/, $1/g; + # space ternary conditional operator + $line =~ s/\s*?\s*(.+?)\s*:\s*/ ? $1 : /g;
- # space after statements - my $statements = 'for|if|while|switch'; - $line =~ s/\b($statements)\b\s*/$1 /g; + # space comma operator (allowing for possible alignment space afterwards) + $line =~ s/\s*,(\S)/, $1/g;
- # no space on inside of brackets - $line =~ s/(\s+/(/g; - $line =~ s/(\S)\s+)/$1)/g; + # space after statements + my $statements = 'for|if|while|switch'; + $line =~ s/\b($statements)\b\s*/$1 /g; + + # no space on inside of brackets + $line =~ s/(\s+/(/g; + $line =~ s/(\S)\s+)/$1)/g; + } + # strip trailing space again (e.g. a trailing operator now has space afterwards) + $line =~ s/\s+$//g; + + $opt_write or print $line."\n"; + $opt_write and push(@lines, $line); } - # strip trailing space again (e.g. a trailing operator now has space afterwards) - $line =~ s/\s+$//g; + close(INPUT);
- $opt_write or print $line."\n"; - $opt_write and push(@lines, $line); + $opt_write or return; + + open(OUTPUT, ">$infile") or die "Couldn't open $infile for writing: $!\n"; + foreach my $line (@lines) + { + print OUTPUT $line."\n"; + } + close(OUTPUT); } -close(INPUT);
-$opt_write or exit;
-open(OUTPUT, ">$infile") or die "Couldn't open $infile for writing: $!\n"; -foreach my $line (@lines) +foreach my $infile (@ARGV) { - print OUTPUT $line."\n"; + parse($infile); } -close(OUTPUT); +
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.