go ahead... be a heretic | |
PerlMonks |
Re: empty out C function bodiesby jakobi (Pilgrim) |
on Oct 24, 2009 at 12:51 UTC ( [id://803033]=note: print w/replies, xml ) | Need Help?? |
If we've permission to mangle the layout of the source with say GNU indent (so we've a canonical format and don't need to really understand C syntax), then try a more elaborate version of the following quick hack:
This one-liner slurps the whole file into $_ using a mostly useless cat (or maybe type), then matches a line starting with a word and also containing a parenthesis, followed by a line with a sole { in col1 and non-gready eating until a line with a sole } in col1, in both cases w/o blanks. Use of ^ and /mg instead of \n is required in case multiple function defs occuring w/o empty lines in between. You can also push the selection of files into Perl (-> glob), as well as reading and writing the modified files (explicitely or with an implicit -> perl -i.bak). And first try it on a copy of your files at least until both you and the compiler are happy with the output again :)
cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.
Updates: biocc's missing cases: That was the reason for asking about indent (not that indent wouldn't normally line-break long signatures, but we might hold-out and hope for a parse-friendly option-combination...) & and for my insistence on column 1 for {,}.
It might be easier to smash the source to make it conform to my assumption than to 'harden' the regex. And you probably should stop short of reimplementing the C parser anyway. s/[\t ]*$//mg; to ensure no (ASCII) whitespace at EOL s/^(\w+[^\n;]*?\([^\n;]*(?:,\n[\t ]+[^\n;]+)*\)[\t\n ]*{\n)[\s\S]+?\n(\}\n)/$1$2/mg; should do the trick for the two cases you mentioned, requiring the comma at EOL and whitespace at SOL for continuations in multiline signatures. But this regex begins to be overly cute, so you should probably rewrite it using the /x modifier (-> add comments and whitespace), and maybe split the patterns into multiple separate variables (see perlre). Extend the regex repeatedly like this, and you've found an indicator that you should have chosen some cpan module or a proper C parsing grammar :). Let me rephrase that in better words than mine: Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. (monkquips)
In Section
Seekers of Perl Wisdom
|
|