1

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.

Community
  • 1
  • 1
Macha
  • 14,366
  • 14
  • 57
  • 69
  • somebody's gotta have a script for this... – James Aug 20 '10 at 15:41
  • 2
    While I'm sure there are utilities that will reformat your source code to change the indentation, I doubt those programs will fix undefined behavior for you (e.g., to fix the return type of `main`). :-) – James McNellis Aug 20 '10 at 15:44
  • Duplicate of [Best C++ Code Formatter/Beautifier](http://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier) – James McNellis Aug 20 '10 at 15:45
  • @James: That's why I said "most" :P – Macha Aug 20 '10 at 15:46
  • 1
    Best thing: DON'T. External code should be handled as external code, it does not need to comply with your own internal rules. Think that any fix (real, not indent) that you apply to the library to make it work (including the `main` return type) will have to be applied to any new version that you download. By only changing actual behavior you can make small diffs that convey the semantic changes as compared to the formatting. Then you can also diff the old/new versions and see which of the changes need to be applied and where. – David Rodríguez - dribeas Aug 20 '10 at 16:10
  • @David: See my reply to Dan's answer. This code is not getting updated. – Macha Aug 20 '10 at 17:13

7 Answers7

2

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.

aviraldg
  • 9,531
  • 6
  • 41
  • 56
2

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
  • 211,373
  • 23
  • 97
  • 98
1

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
  • 9,673
  • 6
  • 31
  • 51
1

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

user122302
  • 132
  • 5
  • Here's the link to that question: http://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier – user353297 Aug 20 '10 at 15:46
1

Have a look at my answer to a similar question

Basically you want AStyle

Community
  • 1
  • 1
Glen
  • 21,816
  • 3
  • 61
  • 76
0

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
  • 7,495
  • 4
  • 35
  • 42
0

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
  • 24,948
  • 7
  • 64
  • 80