标签:注释转换
input.c
// 1.一般情况 /* int i = 0; */ // 2.换行问题 /* int i = 0; */int j = 0; /* int i = 0; */ int j = 0; // 3.匹配问题 /*int i = 0;/*xxxxx*/ // 4.多行注释问题 /* int i=0; int j = 0; int k = 0; */int k = 0; // 5.连续注释问题 /**//**/ // 6.连续的**/问题 /***/ // 7.C++注释问题 // /*xxxxxxxxxxxx*/
AnnotationConvert.h
#pragma once #include<assert.h> #include <errno.h> #include <stdio.h> #pragma warning (disable: 4996) typedef enum ConvertState { SUCCESS, FILE_ERROR, NO_MATCH, }ConvertState; typedef enum State { C_BEGIN, C_END, CPP_BEGIN, CPP_END, }State; ConvertState Convert(FILE* fIn, FILE* fOut) { ConvertState ret = SUCCESS; char first, second; State tag = C_END; assert(fIn); assert(fOut); do{ first = fgetc(fIn); switch (first) { case ‘/‘: second = fgetc(fIn); if (second == ‘*‘) { // 3.匹配问题 if (tag == C_END) { fputc(‘/‘, fOut); fputc(‘/‘, fOut); tag = C_BEGIN; } else { fputc(‘/‘, fOut); fputc(‘*‘, fOut); } } else if (second == ‘/‘) { char next; fputc(‘/‘, fOut); fputc(‘/‘, fOut); do{ next = fgetc(fIn); fputc(next, fOut); if (next == EOF) return ret; } while (next != ‘\n‘); } else { fputc(first, fOut); fputc(second, fOut); } break; case ‘\n‘: // 4.多行注释问题 fputc(‘\n‘, fOut); if (tag == C_BEGIN) { fputc(‘/‘, fOut); fputc(‘/‘, fOut); } break; case ‘*‘: second = fgetc(fIn); if (second == ‘/‘) { // 2.换行问题 char next = fgetc(fIn); // 5.连续注释问题 if (next == ‘/‘) { fputc(‘\n‘, fOut); fseek(fIn, -1, SEEK_CUR); } else if (next != ‘\n‘ && next != EOF) { fputc(‘\n‘, fOut); fputc(next, fOut); } else { fputc(‘\n‘, fOut); } tag = C_END; } else if (second == ‘*‘) { fputc(first, fOut); fseek(fIn, -1, SEEK_CUR); } else { fputc(first, fOut); fputc(second, fOut); } break; default: if (first != EOF) fputc(first, fOut); break; } } while (first != EOF); if (tag != C_END) { ret = NO_MATCH; } return ret; } ConvertState AnnotationConvert(const char* inputFile, const char* outputFile) { ConvertState ret; FILE* fOut, *fIn; fIn = fopen(inputFile, "r"); if (fIn == NULL) { printf("打开文件%s失败, errno: %d\n", inputFile, errno); return FILE_ERROR; } fOut = fopen(outputFile, "w"); if (fOut == NULL) { fclose(fIn); printf("打开文件%s失败! errno: %d", outputFile, errno); return FILE_ERROR; } ret = Convert(fIn, fOut); fclose(fIn); fclose(fOut); return ret; }
test.c
#include"AnnotationConvert.h" int main() { ConvertState ret = AnnotationConvert("input.c", "output.c"); if (ret == SUCCESS) printf("转换成功\n"); else if (ret == FILE_ERROR) printf("打开文件错误\n"); else if (ret == NO_MATCH) printf("不匹配\n"); system("pause"); return 0; }
标签:注释转换
原文地址:http://10739786.blog.51cto.com/10729786/1772217