tags:

views:

116

answers:

4

I have a C source file having comments in // (C++) style. I want to change all the comments to the old /* */ (C) style. Is there a way to do this using an existing script?

+7  A: 

A substitution with you favorite editor and a regular expression like s#//(.*)#/*\1 */# should do it...

sth
Just pay attention to `x=0; // /* was x=1; */ off-by-one bug corrected`
pmg
doesn't work for me in vi (throws `Pattern not found: //(.*)`) what does hash do?
N 1.1
@N 1.1: The hash is just a placeholder, it can be any character. It's usually a forward slash, but when you have slashes in your pattern, it's easier to use a different delimiter than it is to escape all of your slashes. `s/foo/bar/` is equivalent to `s#foo#bar#`, `sXfooXbarX`, and `s,foo,bar,` etc.
Adam Rosenfield
@N 1.1: In vi, I think you need to escape the parenthesis to make them capture (unescaped they match the text) `:s#//\\(.*\\)#/*\1 */#`. The '#' serve to delimit the search and the substitution (usually done with '/', but using '#' allows to replace '/'s without escaping)
pmg
@N 1.1: The default seporator for search and replace in vi is '/' but you can actually use any character. Whcih is usefull when the search string contains the '/' character (otherwise you need to escape its usage). With vi you need to escape the '(' and ')' so you end up with `: % s#//\\(.*\\)$#/*\1*/#`
Martin York
@all Thank you.
N 1.1
+1  A: 

Unfortunately most scripts will only work the other way around. There is a decent one named "RECOMMENT" but it takes C and converts to the newer C++ style comments. I imagine your reason for wanting to do this is due to compiler errors with the C++ style comments. The usual cause of this is a line that uses a C-style comment with an C++ style comment. Perhaps looking for that particular scenario would eliminate your need to convert back to older style commenting. If not, sadly you might have to do it by hand. (I pray that you don't as I know how tedious that can be!)


Recomment Link: http://people.sc.fsu.edu/~jburkardt/cpp_src/recomment/recomment.html

D.R.
I'll keep that in mind for when I need it. You may want to provide a link to RECOMMENT in your answer.
Kedar Soparkar
Scripts that do this the other way round are scary. `/* foo */ bar++;` can't be translated correctly without changing line numbering.
nategoose
I apologize for forgetting the link! I have updated my answer with the link at the bottom!
D.R.
+3  A: 

If you are looking for something a little more generic, you could also use a source code formatting tool for this. For C, I've used uncrustify before and it worked reasonably well. There may be others as well, but I think uncrustify can change C++ style comments into C style comments with the cmt_cpp_to_c parameter.

The configuration can be a little daunting, but if you just use the example config file and change only the stuff you are interested in, it might do what you want.

bde
+1  A: 

You can do this with the Vim plugin Nerdcommenter.

This makes it easy to uncomment the text and then add a multi-line comment like you want.

Nathan Fellman