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?
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: