C-c to c ++ annotation Conversion
# Include <stdio. h> typedef enum Tag {C_ANNOTAION_START = 0, // start of C annotation C_ANNOTAION_END = 1, // end of C annotation} Tag; typedef enum State {SUCCESS = 1, // successful OPEN_FILE_ERROR, // NO_MATCH when opening the FILE, // the FILE does not match OTHER, // OTHER} State; State AnnotationConvert (FILE * cIn, FILE * cOut) {char FirstCh, secondCh; Tag Is_Start = C_ANNOTAION_END; // whether to start to convert do {FirstCh = fgetc (cIn); switch (FirstCh) {// 1. in general, // * int I = 0; */case '/': Se CondCh = fgetc (cIn); // 3. matching Problem // * int I = 0;/* xxxxx */if (SecondCh = '*' & Is_Start = C_ANNOTAION_END) {fputc ('/', cOut); fputc ('/', cOut); Is_Start = C_ANNOTAION_START;} else if (SecondCh = '/') // c ++ comments {char Next = fgetc (cIn); fputc ('/', cOut); fputc ('/', cOut); while (Next! = '\ N' & Next! = EOF) {fputc (Next, cOut); Next = fgetc (cIn);} // FirstCh = Next; fseek (cIn,-1, SEEK_CUR );} else {fputc (FirstCh, cOut); fputc (SecondCh, cOut);} break; case '*': SecondCh = fgetc (cIn); // 2. line feed problem/5. continuous annotation problem // * int I = 0; */int j = 0; // * int I = 0; */if (SecondCh = '/') {char Next = 0; Next = fgetc (cIn); if (Next! = '\ N' & Next! = EOF) {fseek (cIn,-1, SEEK_CUR); fputc ('\ n', cOut);} else {fputc (' \ n', cOut );} is_Start = C_ANNOTAION_END;} else if (SecondCh = '*') {fputc (FirstCh, cOut); fseek (cIn,-1, SEEK_CUR); break ;} else {fputc (FirstCh, cOut); fputc (SecondCh, cOut);} break; // 4. multi-line comment case '\ N': fputc (' \ n', cOut); if (Is_Start = C_ANNOTAION_START) {fputc ('/', cOut ); fputc ('/', cOut);} break; default: fputc (Fir StCh, cOut); break;} while (FirstCh! = EOF); if (Is_Start = C_ANNOTAION_START) {return NO_MATCH;} return SUCCESS;} State StartAnnotationConvert (const char * input, const char * output) {FILE * cIn = fopen (input, "r"); FILE * cOut = fopen (output, "w"); State ret = 0; if (cIn = NULL) {fclose (cOut); return OPEN_FILE_ERROR;} if (cOut = NULL) {fclose (cIn); return OPEN_FILE_ERROR;} ret = AnnotationConvert (cIn, cOut ); fclose (cIn); fclos E (cOut); return ret;} int main () {State ret = StartAnnotationConvert ("input. c "," output. c "); if (ret = SUCCESS) {printf (" conversion successful! \ N ");} else if (ret = NO_MATCH) {printf (" the matching end \ n "is not found in the document);} else if (ret = OPEN_FILE_ERROR) {printf ("failed to open the file \ n") ;}return 0 ;}