views:

66

answers:

2

Does anyone know of a tool which can beautify C++ code to add brackets to conditional statements? What I need to do is change:

if ( myCondition )
    setDateTime( date, time );

to

if ( myCondition )
{
    setDateTime( date, time );
}

but I've got to do this hundreds of times. I could probably write something in perl but don't want to go down this road if there's already a tool. I've used AStyle but I couldn't find how to do this with it.

Apart from meeting the clients coding standards, the reason I want to do this is that I have to replace certain calls such as the above call to setDateTime( date, time ) with setDate( date ) and setTime( time ) which I can easily enough do with regular expressions but ends up like this:

if ( myCondition )
    setDate( date );
    setTime( time );

Obviously not right!!!

+1  A: 
s/setDateTime(date, time)/{ setDate(date); setTime(time); }/
FredOverflow
@FredOverflow: Thanks. Granted that does work, however it has the odd but usually harmless side-effect that *all* setDateTime() calls get bracketed even if they are not conditional. After this has passed again through AStyle, the resultant code looks, well, odd - A code block in the middle of no-where for no apparent reason.
Robin Welch
Isn't simple text replacement like this, what macros are for? To the extent that they're for anything, I mean ;-)
Steve Jessop
+1  A: 

static inline void setDateTime(date, time) { setDate(date); setTime(time); }

Regarding the if: does the astyle option --add-brackets not work for you, combined with brackets=break? When I've used astyle, I've found it difficult to get it to do exactly what I want. So if you're going to use it at all, it's easiest to define the coding style guidelines in terms of a set of astyle parameters and then use astyle to enforce them.

I'm not so bothered by inconsistent style that I personally think that's worth it, but then the customer is always grudgingly tolerated right.

Steve Jessop
@Steve Jessop: Thanks, looks as if --add-brackets seems to do exactly what I want. That said, I haven't tried it yet. Thanks very much.
Robin Welch