Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9102319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:21:20+00:00 2026-06-17T01:21:20+00:00

I have a code file that has some #ifdefs I would like removed in

  • 0

I have a code file that has some #ifdefs I would like removed in the header file after building a library. My first thought was to do this as a perl script that XCode can run. While I can certainly open the header file and read all content of it into a string in perl, I’m curious as to the best way to do the following

  1. Find any occurrence of #ifdef EXAMPLE

  2. Remove it and anything in between the following #endif

So the example is:

int i;
NSString *someString;
#ifdef EXAMPLE
NSString *exampleString;
#endif
bool done;

and the output would be:

int i;
NSString *someString;
bool done;

Options I’m considering:

  • finding index of every #ifdef EXAMPLE and removing it via substring with the next found #endif
  • Write a regex that can somehow remove these occurences.

Considering I haven’t written Perl before (Objective-C is my primary language) I was curious if any XCode or Perl developers had any suggestions on what the best approach would be

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:21:21+00:00Added an answer on June 17, 2026 at 1:21 am

    I’m not sure why you want to strip out ifdefs, and you can probably use a C pre-processor to do this, but here’s how you’d do it in Perl because it means I get to play with the flip-flop operator.

    First thing is to craft a sufficient regex to match the ifdefs. IIRC they can be indented and there can be indentation between the # and the word.

    #ifdef
    #    ifdef
        #ifdef
    

    Not sure if that last one is valid, but I’m going with it anyway.

    my $ifdef_re = qr{^\s*#\s*ifdef\b};
    my $endif_re = qr{^\s*#\s*endif\b};
    

    If it was just removing text between #ifdef and #endif, Perl has the little used flip flop scalar .. operator.

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my $ifdef_re = qr{^\s*#\s*ifdef\b};
    my $endif_re = qr{^\s*#\s*endif\b};
    
    while(<DATA>) {
        my $in_ifdef = /$ifdef_re/ .. /$endif_re/;
    
        print if !$in_ifdef;
    }
    
    __DATA__
    int i;
    NSString *someString;
    #ifdef EXAMPLE
    NSString *exampleString;
    #endif
    bool done;
    

    But since we need to worry about nested ifdefs, its insufficient. A depth counter takes care of that.

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my $ifdef_re = qr{^\s*#\s*ifdef\b};
    my $endif_re = qr{^\s*#\s*endif\b};
    
    my $ifdef_count = 0;
    while(<DATA>) {
        $ifdef_count++ if /$ifdef_re/;
    
        print if $ifdef_count <= 0;
    
        $ifdef_count-- if /$endif_re/;
    }
    
    __DATA__
    int i;
    NSString *someString;
    #ifdef EXAMPLE
    NSString *exampleString;
    #    ifdef FOO
    this should not appear
    #    endif
    nor should this
    #endif
    bool done;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some php/html code. I have a css file that has several backgrounds.
I have a file (with extension.hgx) that has some data like this: length =
I have a header file that has some static variables for all of my
I have written a module that has some generic, reusable code that I would
I have a Xaml file that does not have any code behind. I would
I have some setup code in my functions.php file that sets permalinks and adds
I have a simple assembly code file called exit.s that looks like the following:
I have some code that loads an image file off the web and puts
I have a native library for Android that has some files that include NEON
I have added a simple .js file to my page that has some pretty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.