Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

nodebug.pl

by Anonymous Monk
on Apr 26, 2000 at 05:13 UTC ( #9150=sourcecode: print w/ replies, xml ) Need Help??

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;
  }
}

Comment on nodebug.pl
Download Code
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; }

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://9150]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (17)
As of 2015-07-29 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls