|
本帖最后由 zouzhichao 于 2014-1-4 17:45 编辑
自己写的,用来删除C源代码文件的注释,刚写完还热乎着,Ubuntu GCC 编译通过,做了简单测试,还不能保证无BUG,先贴代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef signed char int8_t;
- typedef unsigned char uint8_t;
- typedef signed short int16_t;
- typedef unsigned short uint16_t;
- typedef signed int int32_t;
- typedef unsigned int uint32_t;
- typedef float flt_t;
- typedef double lflt_t;
- /*********************************************************************************
- * 此处有一个逻辑锁,三种状态:正常模式,注释模式,字符串模式
- * 1,一旦进入注释模式,必须结束注释模式才能进入别的模式,此间所有代码不输出
- * 2,一旦进入字符串模式,必须结束字符串模式才能进入别的模式,此间所有代码输出
- * 3,正常模式下,检测最先进入那个模式
- *********************************************************************************/
- int8_t _note_clr_(const int8_t* file_path_src, const int8_t* file_path_dst)
- {
- enum MODE{_MODE_NORMAL_, _MODE_NOTE_, _MODE_STR_};
- uint8_t mode; // 注释状态标志,字符串状态标志位
- FILE* file_src; // 源文件
- FILE* file_dst; // 目标文件
- int8_t buffer[512]; // 文件缓存区,单行最多支持512个字符
- int8_t* buffer_beg; // 字符串起始地址
- int8_t* buffer_temp1; // 字符串指针1,用于查找
- int8_t* buffer_temp2; // 字符串指针2,用于查找
- int8_t* buffer_temp3; // 字符串指针3,用于查找
- file_src = fopen(file_path_src, "r"); // 打开源文件
- if (NULL == file_src)
- {
- printf("error = 0!");
- return -1; // 源文件打开失败,返回-1
- }
- file_dst = fopen(file_path_dst, "w"); // 打开目标文件
- if (NULL == file_dst)
- {
- printf("error = 1!");
- fclose(file_src); // 目标文件打开失败,关闭源文件
- return -1; // 目标文件打开失败,返回-1
- }
- if (NULL == fgets((int8_t*)buffer, 512, file_src))
- {
- fclose(file_src); // 关闭源文件
- fclose(file_dst); // 关闭目标文件
- return -1;
- }
- for (mode = _MODE_NORMAL_, buffer_beg = (int8_t*)buffer; ; )
- {
- if (_MODE_NOTE_ == mode) // 注释模式
- {
- buffer_temp1 = (int8_t*)strstr((const int8_t*)buffer_beg, (const int8_t*)"*/"); // 查找"*/"
- if (NULL != buffer_temp1) // 如果找到了
- {
- buffer_beg = buffer_temp1 + 2; // 找到新的起始点
- mode = _MODE_NORMAL_; // 进入正常模式
- continue;
- }
- }
- else if (_MODE_STR_ == mode)
- {
- buffer_temp1 = (int8_t*)strstr((const int8_t*)buffer_beg, (const int8_t*)""");
- if (NULL != buffer_temp1) // 如果找到了
- {
- if (buffer_temp1 == buffer_beg)
- {
- mode = _MODE_NORMAL_; // 进入正常模式
- }
- else if ('\\' != *(buffer_temp1 - 1))
- {
- mode = _MODE_NORMAL_; // 进入正常模式
- }
- *buffer_temp1 = '\0'; // 截取有效字符串
- fprintf(file_dst, (const int8_t*)"%s"", (const int8_t*)buffer_beg); // 输出有效字符串
- buffer_beg = buffer_temp1 + 1; // 找到新的起始点
- continue;
- }
- }
- else if (_MODE_NORMAL_ == mode)
- {
- buffer_temp1 = (int8_t*)strstr((const int8_t*)buffer_beg, (const int8_t*)"/*");
- buffer_temp2 = (int8_t*)strstr((const int8_t*)buffer_beg, (const int8_t*)""");
- buffer_temp3 = (int8_t*)strstr((const int8_t*)buffer_beg, (const int8_t*)"//");
- if ((NULL != buffer_temp1) && ((buffer_temp1 < buffer_temp2) || (NULL == buffer_temp2)) && ((buffer_temp1 < buffer_temp3) || (NULL == buffer_temp3)))
- {
- // 如果最先出现"/*"
- *buffer_temp1 = '\0'; // 截取有效字符串
- fprintf(file_dst, (const int8_t*)"%s", (const int8_t*)buffer_beg); // 输出有效字符串
- buffer_beg = buffer_temp1 + 2; // 找到新的起始点
- mode = _MODE_NOTE_; // 进入注释模式
- continue;
- }
- if ((NULL != buffer_temp2) && ((buffer_temp2 < buffer_temp1) || (NULL == buffer_temp1)) && ((buffer_temp2 < buffer_temp3) || (NULL == buffer_temp3)))
- {
- // 如果最先出现"""
- *buffer_temp2 = '\0'; // 截取有效字符串
- fprintf(file_dst, (const int8_t*)"%s"", (const int8_t*)buffer_beg); // 输出有效字符串
- buffer_beg = buffer_temp2 + 1; // 找到新的起始点
- mode = _MODE_STR_; // 进入字符串模式
- continue;
- }
- if ((NULL != buffer_temp3) && ((buffer_temp3 < buffer_temp1) || (NULL == buffer_temp1)) && ((buffer_temp3 < buffer_temp2) || (NULL == buffer_temp2)))
- {
- // 如果最先出现"//"
- *buffer_temp3 = '\0'; // 截取有效字符串
- }
- fprintf(file_dst, (const int8_t*)"%s", (const int8_t*)buffer_beg); // 输出有效字符串
- }
- if (feof(file_src) || (NULL == fgets((int8_t*)buffer, 512, file_src)))
- {
- fclose(file_src); // 关闭源文件
- fclose(file_dst); // 关闭目标文件
- return 0;
- }
- buffer_beg = (int8_t*)buffer;
- }
- return -1;
- }
- int main(int argc, char** argv)
- {
- if (_note_clr_((const int8_t*)"test.c", (const int8_t*)"test-dst.c"))
- {
- printf("error!");
- return EXIT_FAILURE;
- }
- printf("ok!");
- return EXIT_SUCCESS;
- }
复制代码 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|