Perl VC6 Add-in : A VC6 add-in that runs perl scripts

Download perlvcaddin.zip

Synopsis:

formatcpp.pl


formatcpp.pl

Synopsis
use strict;
#x = = 1;
#die "hi there\n";

my $inc = '  ';
my $indent = 0;
my $nextline = 0;
my $dec = 0;
my $lastblank = 0;
my $openb = 0;
my $closeb = 0;
my $outsb = 0;
my $prevoutsb = 0;
my $extra = 0;
my $adjustextra = 0;

@_ = split /^/m;
foreach (@_)
  {
  ###remove trailing whitespace
  s/\s+$//;
  ###temporarily remove leading spaces
  s/^\s+//;

  ###convert tabs to spaces; assume tabstop at 8
  1 while (s/\t+/' ' x (((length($&)+1) * 8 - length($') % 8 - 1) - 1)/e);

  ### line up #includes and #imports
  s/#include\s+/#include /;
  s/#import\s+/#import  /;
  s/#if\s+/#if /;
  s/#else\s+/#else /;
  s/#ifdef\s+/#ifdef /;
  s/return\s+/return /;
  
  ###leave preprocessor lines at col. 0
  if (/^#/)
    {
    s/$/\n/;
    next;
    }
  
  ### remove extra blank lines
  if (/^$/)
    {
    next if $lastblank;
    $lastblank = 1;
    }
  else
    {
    $lastblank = 0;
    }

  ###bump increment if opening brace
  if (/^{$/)
    {
    $nextline = 0;  ## prev. line was an if,else,for,etc, but we found a brace
    $indent++;
    }

  ###if prev. line was an if,else,for,etc. then 
  ###  increment current line
  ###  and restore for next line
  if ($nextline)
    {
    $indent++;
    $dec++;
    $nextline = 0;
    }

  ###increment next line if its possible to be a one line stmt
  $nextline++ if /^if\s*\(/;
  $nextline++ if /^else/;
  $nextline++ if /^for/;
  $nextline++ if /^while/;

  ###remember to decrement if closing brace
  $dec++ if /^}$/;

####todo:
####  switches
####  inline comments
 
  ### count opening and closing brackets...
  $openb  = ( s/\(/\(/g ) || 0;
  $closeb = ( s/\)/\)/g ) || 0;
  $prevoutsb = $outsb;
  $adjustextra = 0;
  $adjustextra = index $_, "(" if ($outsb == 0 && $openb - $closeb);
  $outsb += $openb-$closeb;
    
  #s/$/' ' x 5 . "\/\/outsb:$outsb"/e if $outsb != 0;

  ###restore indentation
  s/^/$inc x $indent . ' ' x $extra/e;
  s/$/\n/;

  if ($adjustextra)
    {
    $extra = $adjustextra + 1;
    }
  elsif ($outsb == 0)
    {
    $extra = 0;
    }

  ##decrement for next line
  $indent -= $dec;
  $dec = 0;
  }

my $s =  join "", @_;
return $s;






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010