tags:

views:

116

answers:

2

Edit:

indent -bap foo.cpp

works

Is there any easy way to insert a new line or insert a text before the beginning of every function definition in C++ file?

int main() {
  return 0;
}
void foo() {
}

becomes

int main() {
  return 0;
}

void foo() {
}
A: 

In Perl:

while(my $line = <>)
{
  $line =~ s/^\}[ \t]*$/}\n/;
  print $line;
}

That will also insert something at the end of every namespace (but not struct or class since they have a semi-colon at the end). You could probably get more clever to avoid that, but I suspect that may be overkill.

That also notably won't catch functions that are defined inline in a class declaration. I'm not sure if that's a case that's important in your case.

scotchi
I am finding the indent program in Unix pretty useful today. See my answer above.
ajay
A: 

Parsing C++ source code with regexp is guaranteed to fail for some cases (which might not occur for you depending on your source code/coding style), so some kind of parsing is always a better strategy.

I would have started by looking into the source code of cproto to see if it simply could be changed to add a blank line when it finds a function.

Update: cproto does not handle C++, but genproto does.

hlovdal