Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python remove C function body

im looking for way how to remove whole bodies from functions in some C source file.

For example I have file with this content:

1.  int func1 (int para) {
2.    return para;
3.  }
4.
5.  int func2 (int para) {
6.    if (1) {
7.      return para;
8.    }
9.    return para;
10. }

I have tried these regex:

content = re.sub('(\{[.*]?\})', '', content, flags=re.DOTALL)

But there is problem with nested { }. This regex substitute only to first }, so lines 9 and 10 are still in content. I think solution should be in counting { and } brackets and stop substitution when counter is on 0. { is found => counter++, } is found => counter--. But I have no idea how to implement this in python. Can u guys give me a kick?

like image 211
nich Avatar asked Jun 03 '26 01:06

nich


1 Answers

I think you're trying to re-invent a wheel that has already been implemented many times before. If all you want is to extract the signature of each function in a C file, there are much easier ways to do it.

The ctags utility will take care of this for you:

~/test$ ctags -x --c-types=f ./test.c
func1            function      1 ./test.c         int func1 (int para) {
func2            function      5 ./test.c         int func2 (int para) {
~/test$ # Clean up the output a little bit
~/test$ ctags -x --c-types=f ./test.c | sed -e 's/\s\+/ /g' | cut -d ' ' -f 5-
int func1 (int para) {
int func2 (int para) {
like image 93
bta Avatar answered Jun 05 '26 17:06

bta