http://qs1969.pair.com?node_id=9150
Category: C coding tools
Author/Contact Info djp: parksdj@miavx1.acs.muohio.edu
Description:

Do you get sick of sorting through all those conditional compilation statems of the form:


   #ifdef DEBUG
   /* C code here *.
   #endif

This short script will strip them out, making your C code more legible. Use input redirection to run your *.c files through this filtering program.

#!/usr/bin/perl -w
#
# nodebug.pl -- strips the "#ifdef YOUR_DEBUG ... #endif"
# out of C proggies

use strict;

my $state = 0;

while(<>) {
  if( ($state == 0) && ($_ =~ /#if.*DEBUG/) ) {
    $state = 1;
  }
  if( $state == 0 ) {
    print $_;
  }
  if( ($state == 1) && ($_ =~ /#endif/) ) {
    $state = 0;
  }
}
Replies are listed 'Best First'.
RE: nodebug.pl
by ZZamboni (Curate) on Apr 26, 2000 at 21:49 UTC
    This does not work if you have other #ifdef blocks inside the DEBUG block. It will resume printing at the first #ifdef it encounters. Try it with this to see what I mean:
    #include <stdio.h> #ifdef DEBUG some code here #ifdef SOLARIS solaris-specific code here #endif /* SOLARIS */ this is still debug code #endif DEBUG this is normal code
    A possible solution would be to keep track of how many #ifs and #endifs you have seen. Something like this:
    my $state = 0; my $debug = 0;; while(<>) { $debug=1,$state++,next if /^\s*#if.*DEBUG/; $state++ if /^\s*#if/; $state-- if /^\s*#endif/; $debug=0,next if $debug && $state==0; print if $debug==0; }