changing C++ comments // to C comments /* 
Author Message
 changing C++ comments // to C comments /*

: I have written a lot of code using the // to signal a comment.  The
code : however is pure C so I want to revert back to regular C style
comments.

: Is there a unix utility that will make the change automatically,
while retaining : the general appearance  of the program.

Try this. It should convert 99.9+% of your comments, and tell
you about the possible ones not converted. You could add code
to deal with what is referred to below as "ambiguities", and
really pretty it up like indent does:

/* -- cppc2cc.c --
 *      c++ // comments to c style comments
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
# define EXIT_FAILURE 1
#endif

#define BUFLEN 300

main(int argc, char *argv[])
{       FILE    *in, *out;
        char    buf[BUFLEN], *cp, *qp;
        int     line, rv;

        if(argc < 3)
        {       printf("usage: $ %s cppfile cfile\n", argv[0]);
                exit(EXIT_SUCCESS);
        }
        if((in = fopen(argv[1], "r")) == NULL)
        {       perror(argv[1]);
                exit(EXIT_FAILURE);
        }
        if((out = fopen(argv[2], "w")) == NULL)
        {       perror(argv[2]);
                exit(EXIT_FAILURE);
        }
        rv = EXIT_SUCCESS;
        line = 0;
        while(fgets(buf, sizeof(buf), in))
        {       line++;
                if((cp = strstr(buf, "//")) != NULL)
                {       if((strchr(cp, '"')) != NULL
                        && (qp = strchr(buf, '"')) != NULL)
                                printf(
"** poss. ambiguous situation on line %d, manual editing req. **\n"
                                        , line);
                        else
                        {       strncpy(cp, "/*", 2);
                                if(cp = strchr(buf, '\n'))
                                        strcpy(cp, " */\n");
                                else /* shouldn't happen */
                                        strcat(buf, " */");
                        }
                }
                if(!fputs(buf, out))
                        break;
        }
        if(ferror(out))
        {       printf("\n\n** conversion incomplete, error on %s **\n"
                        , argv[2]);
                rv = EXIT_FAILURE;
        }
        if(ferror(in))
        {       printf("\n\n** read error on %s **\n", argv[1]);
                rv = EXIT_FAILURE;
        }
        fclose(in);
        fclose(out);
        exit(rv);
        return 0;

Quote:
}

: An example of a typical comment is given below

: //----
: //--- text describing what is about to happen
: //----

$ cat testfile.cpp
//
this is a line // with a comment
// this is a blank line with a comment
/* this is a c comment already */

//
// 3 line comment
//

some text
printf(" embeded \"quoted\" string\n"); // with a quote
printf(" embeded \"quoted\" string\n");
printf(" embeded // string\n");
printf(" embeded \"//\" string\n");
printf(" embeded \"//\" string\n"); // with a comment

//----
//--- text describing what is about to happen
//----

shoot(happens);  // and a comment

// finito

$ ./cppc2c testfile.cpp testfile.c
** poss. ambiguous situation on line 13, manual editing req. **
** poss. ambiguous situation on line 14, manual editing req. **
** poss. ambiguous situation on line 15, manual editing req. **
$ cat testfile.c
/* */
this is a line /* with a comment */
/* this is a blank line with a comment */
/* this is a c comment already */

/* */
/* 3 line comment */
/* */

some text
printf(" embeded \"quoted\" string\n"); /* with a quote */
printf(" embeded \"quoted\" string\n");
printf(" embeded // string\n");
printf(" embeded \"//\" string\n");
printf(" embeded \"//\" string\n"); // with a comment

/*---- */
/*--- text describing what is about to happen */
/*---- */

shoot(happens);  /* and a comment */

/* finito */

--



Tue, 05 May 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Change C++ comments to C-comments

2. changing C++ comments // to C comments /*

3. Converting C++ comments to C comments

4. Need comments on commenting program

5. Multi-Line Comments Nested in Single Line Comments

6. comments on comments

7. Newbie: separate big .cs file into small .cs files

8. MS pls comment: Undocumented MFC changes in VC6.0

9. Changing Classwizards default comments

10. C# type xml comments in C++?

11. Looking for a C/C++ comment formatting tool

12. C++ to C comment replacement

 

 
Powered by phpBB® Forum Software