源程序-删除c源文件中的注释

前一周做得一道笔试题。
 /*
* comment_strip.c - strip all the comments in a c source file.
* Created : Fri Sep 1 21:38:40 2006
* Zhang Xuecheng <zhangxuecheng1985@gmail.com>
*
* usage: comment_strip input_file output_file
 */

/* 
version: 2.0
 
changelog:
*** fix the bug of "//" , "/*" & "*//" in the former version
*** fix the new bug of '"' in the beta version of 2.0
*** optimize the performance by using if...else if... instead of seperate if()
*** add makefile and debug info.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef DEBUG
#define dump() fprintf(stderr,"%c/t",c1); /
fprintf(stderr,"%c/t",c2); /
fprintf(stderr,"%d/t",flg_token1); /
fprintf(stderr,"%d/t",flg_token2); /
fprintf(stderr,"%d/t",flg_omit_output); /
fprintf(stderr,"%d/t",flg_quote); /
fprintf(stderr,"/n");
#endif

int main (int argc, char *argv[])
{
FILE *fp_src, *fp_dst; /* stream for source & destination file */
char c1 = ' ', c2 = ' ';

/* below are flags for comments stripping,
if "//" is probed,token1 is set,omit_output is set,output is disabled,
if "/*" is probed,token2 is set,omit_output is set,output is disabled.
if token1 is set & "/n" is probed,_or_ token2 is set & "*//" is probed,
token1 or token2 is cleared,output is enabled,
the process of stripping token1&2 should be atomic,
at one time,only _one_ token should be stripped.
 */
int flg_token1 = 0;
int flg_token2 = 0;
int flg_omit_output = 0;
int flg_quote = 0;

if (3 != argc)
{
fprintf (stderr, "please specify the source & destination file/n");
return 0;
}

if (NULL == (fp_src = fopen (argv[1], "r")))
{
fprintf (stderr, "can't open source file %s /n", argv[1]);
return 0;
}

if (NULL == (fp_dst = fopen (argv[2], "w")))
{
fprintf (stderr, "can't create destination file %s /n", argv[2]);
return 0;
}

#ifdef DEBUG
fprintf(stderr,"c1/tc2/ttoken1/ttoken2/toutput/tquote/n");
#endif

while (!feof (fp_src))
{
c1 = fgetc (fp_src);

/* probe whether " is begin */
if ('"' == c1 && !flg_token2 && !flg_token1)
{
c1=fgetc(fp_src);
if('/''==c1 && '/'' == c2)
{
flg_quote = !flg_quote;
}
ungetc(c1,fp_src);
c1='"';
flg_quote = !flg_quote;
}
else if /* probe whether "//" is begin */
('/' == c2 && '/' == c1 && !flg_token2 && !flg_token1 && !flg_quote)
{
flg_token1 = 1;
flg_omit_output = 1;
}
else if /* probe whether "/*" is begin */
('/' == c2 && '*' == c1 && !flg_token1 && !flg_token2 && !flg_quote)
{
flg_token2 = 1;
flg_omit_output = 1;
}
else if /* probe whether a line begin with "//" is end */
('/n' == c2 && flg_token1 && !flg_token2 && !flg_quote)
{
flg_token1 = 0;
flg_omit_output = 0;
}
else if /* probe whether a string begin with "/*" is end */
('*' == c2 && '/' == c1 && flg_token2 && !flg_token1 && !flg_quote)
{
flg_token2 = 0;
flg_omit_output = 0;
c2 = ' ';
c1 = fgetc (fp_src);
}
#ifdef DEBUG
dump();
#endif
/* if no token is being stripped now,output the char */
if (!flg_omit_output)
{
fputc (c2, fp_dst);
}

/* use c2 as the buffer of c1 */
c2 = c1;

}

return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值