Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1943174
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: C/C++

2011-12-26 00:06:39

本方法适用于分离C语言风格的注释。

> gcc parse.c -o parse

> ./parse -s parse.c

源文件parse.c


  1. #include <stdio.h>
  2. #include <string.h>
  3. #define CODE_FILE "code_file.txt"
  4. #define COMMENT_FILE "comment_file.txt"

  5. void split(const char* filename)
  6. {
  7.     FILE* fin = fopen(filename, "r");
  8.     FILE* fcode = fopen(CODE_FILE, "w");
  9.     FILE* fcomm = fopen(COMMENT_FILE, "w");
  10.     //测试数据
  11.     "123456测试数据";
  12.     /*这是测试数据*/
  13.     "````````````\"" "1111111111";
  14.     while (!feof(fin))
  15.     {
  16.         char c = fgetc(fin);
  17.         if (c == EOF) break;
  18.        
  19.         if (c == '"' || c == '\'')
  20.         {
  21.             char t = c;
  22.             fputc(c, fcode);
  23.             while ((c = fgetc(fin)) != EOF && c != t)
  24.             {
  25.                 fputc(c, fcode);
  26.                 if (c == '\\')
  27.                 {
  28.                     if ((c = fgetc(fin)) != EOF)
  29.                         fputc(c, fcode);
  30.                 }
  31.             }
  32.             if (c == t) fputc(c, fcode);
  33.             else break;
  34.         }/***********************/
  35.         else if (c == '/')
  36.         {/*
  37.        
  38.         换行测试
  39.         灭哈哈
  40.         */
  41.             char c1 = fgetc(fin);
  42.             if (c1 == '*')
  43.             {
  44.                 fprintf(fcomm, "@%d@%d@\n", ftell(fcode), 0);
  45.                 fprintf(fcomm, "/*");
  46.                 c1 = 0;
  47.                 while (1)
  48.                 {
  49.                     while (c1 != '*' && c1 != EOF)
  50.                     {
  51.                         if (c1) fputc(c1, fcomm);
  52.                         c1 = fgetc(fin);
  53.                     }
  54.                     if (c1 == EOF) break;
  55.                     c1 = fgetc(fin);
  56.                     if (c1 == '/' || c1 == EOF) break;
  57.                     else fputc('*', fcomm);
  58.                 }
  59.                 fprintf(fcomm, "*/\n");
  60.             }
  61.             else if (c1 == '/')
  62.             {
  63.                 fprintf(fcomm, "@%d@%d@\n", ftell(fcode), 1);
  64.                 fprintf(fcomm, "//");
  65.                 while ((c1 = fgetc(fin)) != EOF && c1 != '\n') fputc(c1, fcomm);
  66.                 fprintf(fcomm, "\n");
  67.             }
  68.             else
  69.             {
  70.                 fprintf(fcode, "%c%c", c, c1);
  71.             }
  72.         }
  73.         else
  74.         {/*飞雪
  75.        
  76.         //
  77.         //测试
  78.         //
  79.         */
  80.             fputc(c, fcode);
  81.         }
  82.     }
  83.     fclose(fcomm);
  84.     fclose(fcode);
  85.     fclose(fin);
  86. }
  87. //测试一下
  88. void merge(const char* filename)
  89. {
  90.     FILE* fout = fopen(filename, "w");
  91.     FILE* fcode = fopen(CODE_FILE, "r");
  92.     FILE* fcomm = fopen(COMMENT_FILE, "r");
  93.    
  94.     char temp[256] = {0};
  95.     while (!feof(fcomm))
  96.     {
  97.         int pos, type;
  98.         char c;
  99.         if (fscanf(fcomm, "@%d@%d@\n", &pos, &type) == EOF) break;
  100.         while (ftell(fcode) != pos)
  101.         {
  102.             c = fgetc(fcode);
  103.             fputc(c, fout);
  104.         }
  105.         if (type == 1)
  106.         {
  107.             while ((c = getc(fcomm)) != '\n') fputc(c, fout);
  108.             fputc('\n', fout);
  109.         }
  110.         else
  111.         {
  112.             char c = 0;
  113.             while (1)
  114.             {
  115.                 while (c != '*' && c != EOF)
  116.                 {
  117.                     if (c) fputc(c, fout);
  118.                     c = fgetc(fcomm);
  119.                 }
  120.                 if (c == EOF) break;
  121.                 c = fgetc(fcomm);
  122.                 if (c == '/' || c == EOF) break;
  123.                 else fputc('*', fout);
  124.             }
  125.             fprintf(fout, "*/");
  126.             fgetc(fcomm);
  127.         }
  128.     }
  129.     while (!feof(fcode))
  130.     {
  131.         char c = fgetc(fcode);
  132.         if (c == EOF) break;
  133.         fputc(c, fout);
  134.     }
  135.     fclose(fcomm);
  136.     fclose(fcode);
  137.     fclose(fout);
  138. }
  139. int main(int cnt, char* args[])
  140. {
  141.     if (cnt != 3)
  142.     {
  143.         puts("usage:\n -s filename\n -m filename\n");
  144.         return 1;
  145.     }
  146.     if (args[1][0] == '-' && args[1][1] == 's')
  147.     {
  148.         split(args[2]);
  149.     }
  150.     else if (args[1][0] == '-' && args[1][1] == 'm')
  151.     {
  152.         merge(args[2]);
  153.     }
  154.     else
  155.     {
  156.         puts("usage:\n -s filename\n -m filename\n");
  157.         return 1;
  158.     }
  159.     return 0;
  160. }
  161. //测试数据
  162.     //"123456测试数据";
  163.     /*这是测试数据*/
转载自:http://www.cnblogs.com/chengxin1982/archive/2009/01/18/1377781.html
阅读(1431) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~