Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
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 meditating upon the Monastery: (5)
As of 2023-01-30 15:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found