/* cpp2c.c -- a tool to convert C++ comments to C comments This code fragment that is furnished by IBM is a simple example to provide an illustration. This example has not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of this program. All code contained herein is provided to you "AS IS". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. */ #include #include #define MAXLINELEN 512 /* cpp2c NEWDIR file1 file2 file3 ... */ void main(int argc, char* argv[]) { FILE* IN; FILE* OUT; char outfn[128]; int filenumber; /* index into command line arguments */ int idx; /* index into line buffer */ int commentpos; /* first '/' of C++ comment */ int endpos; /* newline character or end of line */ int inquotes; /* flag for current pos in quotes */ int incomment; /* flag for current pos in comment */ char line[MAXLINELEN]; if (argc < 3) { printf("Usage: %s NEWDIR file1 file2 file3 ... ", argv[0]); return; } for (filenumber = argc-1; filenumber > 1; filenumber--) { IN = fopen(argv[filenumber], "r"); if (IN != NULL) { sprintf(outfn, "%s/%s", argv[1], argv[filenumber] ); OUT = fopen(outfn, "w+"); if (OUT != NULL) { inquotes = -1; incomment = 0; while ( fgets(line, MAXLINELEN, IN) ) { endpos = strlen(line) - 1; printf("STRLEN:%d END:%d\n",strlen(line), endpos); if (line[endpos] == '\n') line[endpos] = '\0'; idx = commentpos = 0; while (line[idx] != '\0' && commentpos == 0) { if (line[idx] != '\' && line[idx+1] == '"' && incomment == 0) inquotes = -inquotes; if (line[idx] == '/' && line[idx+1] == '*' && inquotes == -1) { incomment++; if (incomment > 1) fprintf(stderr,"WARNING: nested comment\n"); } if (line[idx] == '*' && line[idx+1] == '/' && inquotes == -1) incomment--; if (line[idx] == '/' && line[idx+1] == '/' && inquotes == -1) { if (incomment == 0) commentpos = idx; else fprintf(stderr,"WARNING: C++ comment in C comment\n"); } idx++; } if (commentpos != 0) line[commentpos]='\0'; if (endpos > commentpos) fprintf(OUT, "%s/* %s */\n", line, &(line[commentpos+2])); else fprintf(OUT, "%s\n", line); } fclose(OUT); if (inquotes != -1) fprintf(stderr,"WARNING: uneven quotes\n"); if (incomment != 0) fprintf(stderr,"WARNING: open comment\n"); } else printf("Could not open output %s.\n", outfn); fclose(IN); } else printf("Could not open input %s.\n", argv[filenumber] ); } }