LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2009, 07:35 AM   #1
corp1126
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
Tool to remove obsolete #ifdef statements from a C program?


Dear all,

I have an old C program that has some #ifdef foo statements which are defined in every build of the program. Since it would be exactly the same if they were there or not I'd like to get rid of them to make things easier to look at. The problem is that there are rather a lot of them, does anyone know of a pre-existing tool to remove all the #ifdef foo statements and their associated #elif, #else and #endif lines?

I could script this myself but don't want to waste the effort if a tool already exists!

Many thanks.
 
Old 12-18-2009, 02:28 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
you could write a program to do it:
Code:
-psudeo code-
file1 = open inputfile read
file2 = open outputfile write
while not end-of-file file1
getline from file1 into buffer
compare buffer with #ifdef,#else,#endif
if buffer not = #ifdef,#else,#endif then
write buffer to file2
while end
close file1,file2
return
 
Old 12-18-2009, 02:53 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What smeezekitty said, but with sed or awk.
Kevin Barry
 
Old 12-18-2009, 05:47 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
simple example
Code:
$ more file
1 text i want
#ifdef <token>
 code
#else
 code
#endif
2 text i want
3 text i want
#ifdef <token>
 code
#else
 code
#endif

$ awk -vf=0 '/#endif/{f=0;next}/#ifdef/{f=1}f==0' file
1 text i want
2 text i want
3 text i want
 
Old 12-19-2009, 09:10 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
If I understood correctly, the OP wants to keep the code contained in the ifdefs -he just wants to eliminate the ifdef statements.
If he wants to eliminate both the ifdef statements and the code they contain, then he could use 'unfidef' which is part of the linux kernel sources, or available separately.
 
Old 12-19-2009, 03:57 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ghostdog74 View Post
simple example
Code:
$ more file
1 text i want
#ifdef <token>
 code
#else
 code
#endif
2 text i want
3 text i want
#ifdef <token>
 code
#else
 code
#endif

$ awk -vf=0 '/#endif/{f=0;next}/#ifdef/{f=1}f==0' file
1 text i want
2 text i want
3 text i want
You need to keep one condition or the other; eliminating both changes the code. There should be a line that says "code" still, whether it be the first or the second.
Kevin Barry
 
Old 12-19-2009, 04:00 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You might check to see if Eclipse has some refactoring tool to do this for you (though I recommended command-line before) so that context, etc. can be taken into account.
Kevin Barry
 
Old 12-21-2009, 08:34 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
I'd suggest leaving well enough alone.
 
0 members found this post helpful.
Old 12-21-2009, 08:37 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Whenever I'm tasked with this kind of cleanup work, I just sit down with a good editor (Vim!) and crank through it one file at a time.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RHEl5 Add/Remove tool soumalya Linux - Server 3 11-13-2007 12:21 PM
Uninstall or remove obsolete kernels Cushie Linux - Newbie 2 09-15-2007 01:46 PM
yum remove obsolete apps themanwhowas Linux - Newbie 1 03-19-2007 03:57 PM
Add/Remove Packages tool blk96gt Fedora 2 07-10-2004 03:51 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration