
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 */
--