views:

94

answers:

7

Possible Duplicates:
Code polisher / reformater for C, C++ or Fortran
Best C++ Code Formatter/Beautifier

I am working as part of a team on a C++ project. Part of the code is a highly modified version of a large-ish external library, which doesn't conform to our coding conventions.

The library uses this style:

void main ()
{
  if (somecondition)
    {
    doStuff (some, parameters);
    }
  moreStuff (parameter);
}

whereas our style is like so:

int main() {
    if(somecondition) {
        doStuff(some, parameters);
    }
    moreStuff(parameter);
}

(Also, the editor on Stack Overflow doesn't make it possible to show, but their code uses two spaces for indentation, our code uses tabs).

Given that this is nearly all just editing whitespace, I'd imagine there are some programs to automate most of it.

+2  A: 

There are lots of programs which do this for you. Like:

Some editors can also fix this stuff on run time, without modifying the actual source files. (I believe Eclipse/emacs can do this)

Still, I can't help but ask why you wish to change the style of a library.

Aviral Dasgupta
A: 

You could probably tweak it to exactly how you wanted with RegEx - though that's dependent on just how complex the changes you want to make are.

Rushyo
+1  A: 

Most modern editors do exactly what you want.

The easiest might be to use eclipse. Get the C++ plugin, and modify the way you want your code to look like in the configuration.

Starkey
+1  A: 

There are tools for this, see SO question 841075. The accepted answer was AStyle.

Here's the link to that question: http://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier
+1  A: 

Have a look at my answer to a similar question

Basically you want AStyle

Glen
A: 

As others have pointed out, AStyle is a good tool for this, but any good IDE can reformat the sources as well.

In Vim, for example, you can set your cinoptions to match your style guidelines, and then the = command will reformat your sources according to those rules.

greyfade
+2  A: 

If it were up to me, I'd leave it alone. Instead, I'd change the coding style to explicitly allow use of already-established coding styles in code that comes from external sources. Changing the code to match your coding style is going to lead to an unnecessary merging nightmare if you ever find you want or need to update the external library from upstream.

Is it really worth it to virtually prohibit your project from ever updating the library, just to make it match your (arbitrary and subjective) coding style?

Dan Moulding
The library has not been updated in 5 years. So we won't need to worry about that.
Macha