tags:

views:

1220

answers:

9

How can I automatically replace all C style comments (/* comment */) by C++ style comments (// comment)? This has to be done automatically in several files. Any solution is ok, as long as it works.

+1  A: 

Why don't you write a C app to parse it's own source files? You could find the /* comments */ sections with a relatively easy Regex query. You could then replace the new line characters with new line character + "//".

Anyway, just a thought. Good luck with that.

regex
Why on earth would you do this in C? There are loads of languages far more suited to regex search-replace than C. Plus, why would it parse "it's own" source files?
Draemon
Well I could say doing it in C just because presumably the OP already knows C. Having said that, I would say that avoiding the relative pain of doing it in C would be well worth the effort of learning a better language for this task. Personally I would suggest Python, but there are lots of choices.
EBGreen
Regular expressions are insufficient anyway, a real parser will be needed.
Darron
Of course, if you could get your hands on the source code for an ANSI compliant C++ compiler, then you have a parser. :)
EBGreen
Agree with Darron - you can't use regex on, for example, printf("////**** this better not be changed, it's a string literal!\n");A real C parser is needed.
Adam Davis
I would worry about a source repository where you couldn't hack together a regex that was "good enough", and then fix up any problems by hand.
Draemon
+11  A: 

This is not a trivial problem.

int * /* foo 
  /* this is not the beginning of a comment.

int * */ var = NULL;

What do you want to replace that with? Any real substitution requires sometimes splitting lines.

int * // foo
  // this is not the beginning of a comment.
// int *
var = NULL;
Darron
A: 

If you write an application/script to process the C source files, here are some things to be careful of:

  • comment characters within strings
  • comment characters in the middle of a line (you might not want to split the code line)

You might be better off trying to find an application that understands how to actually parse the code as code.

CodeSavvyGeek
A: 

If there are just "several files" is it really necessary to write a program? Opening it up in a text editor might do the trick quicker in practice, unless there's a whole load of comments. emacs has a comment-region command that (unsurprisingly) comments a region, so it'd just be a case of ditching the offending '/*' and '*/'.

Dominic Rodger
+1. Many text editors will also ‘find and replace in files’. You will in any case need a quick visual check, for all the corner-cases mentioned by other posters.
bobince
+5  A: 

How do you intend to handle situations like this:

void CreateExportableDataTable(/*[out, retval]*/ IDispatch **ppVal)
{
 //blah
}

Note the comment inside the parens... this is a common way of documenting things in generated code, or mentioning default parameter values in the implementation of a class, etc. I'm usually not a fan of such uses of comments, but they are common and need to be considered. I don't think you can convert them to C++ style comments without doing some heavy thinking.

rmeador
there is a fairly easy (but undesirable solution). Every comment you replace if it doesn't end with 0 or whitespace chars and a newline, you insert a newline. So in other words "IDispatch **ppVal)" would be on a new line.
Evan Teran
don't get me wrong, i agree with you that the conversion is a bad idea
Evan Teran
+1  A: 

Here's a Python script that will (mostly) do the job. It handles most edge cases, but it does not handle comment characters inside of strings, although that should be easy to fix.

#!/usr/bin/python

import sys

out = ''
in_comment = False

file = open(sys.argv[1], 'r+')
for line in file:
    if in_comment:
        end = line.find('*/')
        if end != -1:
            out += '//' + line[:end] + '\n'
            out += ' ' * (end + 2) + line[end+2:]
            in_comment = False
        else:
            out += '//' + line
    else:
        start = line.find('/*')
        cpp_start = line.find('//')
        if start != -1 and (cpp_start == -1 or cpp_start > start):
            out += line[:start] + '//' + line[start+2:]
            in_comment = True
        else:
            out += line

file.seek(0)
file.write(out)
Adam Rosenfield
A: 
batbrat
+5  A: 

I'm with the people who commented in your question. Why do it? Just leave it.

it wastes time, adds useless commits to version control, risk of screwing up

EDIT: Adding details from the comments from the OP

The fundamental reason of preferring C++-style comment is that you can comment out a block of code which may have comments in it. If that comment is in C-style, this block-comment-out of code is not straight forward. – unknown (yahoo)

that might be a fair/ok thing to want to do, but I have two comments about that:

  • I know of no one who would advocate changing all existing code - that is a preference for new code. (IMO)
  • If you feel the need to "comment out code" (another iffy practice) then you can do it as needed - not before

It also appears that you want to use the c-style comments to block out a section of code? Or are you going to use the // to block out many lines?

One alternative is a preprocessor #ifdef for that situation. I cringe at that but it is just as bad as commenting out lines/blocks. Neither should be left in the production code.

Tim
The fundamental reason of preferring C++-style comment is that you can comment out a block of code which may have comments in it. If that comment is in C-style, this block-comment-out of code is not straight forward.
compie
Fair enough. two points: 1. I know of no one who would advocate changing all existing code - that is a preference for new code. (IMO) 2. If you feel the need to "comment out code" (another iffy practice) then you can do it as needed - not before.
Tim
In my editor, commenting out a block of code which may have comments in it is straightforward and requires only two keystrokes. Uncommenting is equally straightforward. Why go through so much trouble when you can just use a decent editor?
JasonTrue
+7  A: 

This tool does the job: http://people.sc.fsu.edu/~burkardt/cpp_src/recomment/recomment.html

RECOMMENT is a C++ program which converts C style comments to C++ style comments.

It also handles all the non-trivial cases mentioned by other people:

This code incorporates suggestions and coding provided on 28 April 2005 by Steven Martin of JDS Uniphase, Melbourne Florida. These suggestions allow the program to ignore the internal contents of strings, (which might otherwise seem to begin or end comments), to handle lines of code with trailing comments, and to handle comments with trailing bits of code.

compie