Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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:

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

   #ifdef DEBUG
   /* C code here *.

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
# -- 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
Download Code
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2015-10-04 04:36 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (98 votes), past polls