views:

273

answers:

8

Hello, all :)

I'm looking for a tool that works on Windows to reformat some C++ code in my codebase. Essentially, I've got some code I wrote a while ago that I'd like to use, but it doesn't match the style I'm using in a more recent project.

What's the best way to reformat C++ code in a standard manner?

Billy3

+2  A: 

Visual Studio can. And most IDEs can.

Andrey
Care to explain how? I did spend some time with Visual Studio before asking the question...
Billy ONeal
-1: Without 3rd party add-ins, Visual Studio cannot.
280Z28
@280Z28 let me prove it. In VS: select the whole document, then Edit | Advanced | Format Selection. If you don't know something it doesn't mean that it doesn't exist.
Andrey
Actually, it can, to some degree, assist you with formatting of your code. You can run "Format Selection" on a part of or your whole document, and it will re-indent your code.
Dave Van den Eynde
@Andrey: It will reindent if I do that but it won't change the way brackets are handled or other types of formatting. I'll throw you a +1 -- but this really doesn't do what I'm looking for.
Billy ONeal
@Andrey, you can't possibly believe that indenting a document is what the OP is asking about.
280Z28
+5  A: 

GNU Indent should get you most of the way there

Paul Betts
Yes, I found that one on Google -- but does it work on Windows?
Billy ONeal
Yes. See http://gnuwin32.sourceforge.net/packages/indent.htm
Matthew Flaschen
@Billy: it works as well on Windows as it does anywhere else. Regardless of platform, unless you want one of the "canned" formats, getting usable output can be challenging (to the point that unless you're dealing with a *lot* of code, formatting by hand is often faster and easier).
Jerry Coffin
@Herry Coffin: I was asking because the site Paul Betts linked to contained only UNIX source code, and no description as to how to get it running on Windows.
Billy ONeal
+6  A: 

In Visual Studio: Edit / Advanced / Format Document

The format applied to the document will match the settings in: Tools / Options / Text Editor / C/C++

Visual Studio might not support all the formatting options you want applied to your document, in which case you'll need a separate tool (such as Paul Betts is suggesting) to format the way you want.

You might want to check here for a good list: http://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier

Task
+1 for the reference to SO on formatters.
Thomas Matthews
+4  A: 

Astyle is one of the more popular tools.

Edit: I don't know why I didn't think about it when I first posted this, but you can also use Vim. The = command will reformat your code according to your indentexpr and cinoptions.

greyfade
We used this a year or two ago to format our entire code base (GNU->ANSI indentation), and it worked flawlessly.
Ben
+2  A: 

I have also used AStyle in the past. I used this GUI for it to make it a bit easier (I think--it's been a while): Artistic Style for Windows: http://jimp03.zxq.net/

Nathan McDaniel
+1  A: 

Eclipse/CDT can indent your code very well...

  • Its configurable.
  • You can also share the settings within your team/group/organization.

P.S. I am writing something about that in my Book Post comment if you have some direct questions. :-)

Meera
+1  A: 

GC Great Code has been an old standby for me. It's quite configurable.

http://sourceforge.net/projects/gcgreatcode/

Mr Fooz