自动删除源代码里面的debug代码
来源:互联网 发布:linux显示乱码 编辑:程序博客网 时间:2024/04/29 20:17
/*
* debug代码要用宏括起来
* 例如
* #if xxx
* #endi
* 自动删除#if xxx 和#endif之间的代码
*/
#include <stdio.h>
#include <string.h>
#define DEBUG 1
#if DEBUG
#define dbg_out(x,y) do{printf("[- %d -] ",__LINE__);printf(x,y);}while(0);
#else
#define dbg_out(x,y)
#endif
enum
{
TRUE,
FALSE,
PARAM_ERROR,
FORWARD,
BACK,
FOUND_NO_STR,
STR_FOUND,
FSEEK_ERR,
HEADER_OF_FILE,
FILE_OPERATE_API_ERR,
};
/*****************************************************************/
/****** copy file tmp.txt to source file *****/
/*****************************************************************/
int Cp_File(FILE *source_file,FILE *dst_file)
{
char ch;
ch = fgetc(source_file);
while(!feof(source_file))
{
fputc(ch,dst_file);
ch = fgetc(source_file);
}
return TRUE;
}/*end Cp_File*/
/*****************************************************************/
/****** Del_Words_In_File *****/
/*****************************************************************/
int Del_Words_In_File(char *filename,int start,int end)
{
FILE *fp;
FILE *tmp_fp;
char buf[8000];
char ch;
int ret;
if(start > end)/*param judge*/
{
#if DEBUG
printf("Param error,start > end\n");
#endif
return PARAM_ERROR;
}
/*open dst_file*/
fp = fopen(filename,"rt+");
if(fp == NULL)
{
#if DEBUG
printf("[- %d -]Open file error,filename:%s\n",filename,__LINE__);
#endif
return FALSE;
}
/*create tmp_file*/
tmp_fp = fopen("tmp.txt","at+");
if(tmp_fp == NULL)
{
printf("[- %d -]Open file error\,filename:tmp.txt\n",__LINE__);
return FALSE;
}
/*pre half*/
ch = fgetc(fp);
while(ftell(fp) <= start)
{
fputc(ch,tmp_fp);
ch = fgetc(fp);
}
/*end half*/
fseek(fp,end + 1,SEEK_SET);
ch = fgetc(fp);
while(!feof(fp))
{
fputc(ch,tmp_fp);
ch = fgetc(fp);
}
fclose(fp);
fp = fopen(filename,"w");
rewind(tmp_fp);
/*tmp_file to dst_file*/
Cp_File(tmp_fp,fp);
fclose(fp);
fclose(tmp_fp);
/*remove tmp_file*/
ret = remove("tmp.txt");/*del a file in disk*/
if(ret != 0)
{
#if DEBUG
printf("remove file error\n");
#endif
}
}/*end Del_Words_In_File*/
/*****************************************************************/
/****** Search_Str *****/
/*****************************************************************/
int Search_Str(FILE *fp,char *str,int method,int *site,int *line_t)
{
char buf[10000];
int len = strlen(str);
int ret;
int line = 0;
if(BACK == method)
{
fseek(fp,-(len+1),SEEK_CUR);
}
fgets(buf,len + 1,fp);/*want to read "len+1" words but the truth is 'len' words*/
while(!feof(fp))
{
if(!strcmp(buf,str))
{
fseek(fp,-len,SEEK_CUR);
*site = ftell(fp);
*line_t = line;
return STR_FOUND;
}
if( FORWARD == method)/*pointer go back*/
{
ret = fseek(fp,-(len-1),SEEK_CUR);
if(ret != 0)
{
#if DEBUG
printf("--FORWARD--Error,fseek error\n");
#endif
return FSEEK_ERR;
}
}
else
{
if(ftell(fp) < len + 1)
{
#if DEBUG
printf("Come to start of file ,return\n");
#endif
return HEADER_OF_FILE;
}
ret = fseek(fp,-(len+1),SEEK_CUR);
if(ret != 0)
{
#if DEBUG
printf("--BACK--Error,fseek error\n");
#endif
return 0;
}
}
fgets(buf,len + 1,fp);
if(strlen(buf) < len)
{
line ++;
if(feof(fp))
{
return FOUND_NO_STR;
}
if(FORWARD == method)
{
fseek(fp,len- 1,SEEK_CUR);
}
else
{
fseek(fp, -(len + 2), SEEK_CUR);
}
}
}
if(!feof(fp))
{
return FOUND_NO_STR;
}
return STR_FOUND;
}
/*****************************************************************/
/****** main *****/
/*****************************************************************/
int main(int argc,char *argv[])
{
FILE *fp;
char buf[1000];
int start;
int end;
int cnt = 0;
int site;
char ch;
int site_bak;
int index;
int ret;
FILE *fp_tmp;
char str[100];
char filename[100];
int line;
strcpy(filename, argv[2]);
strcpy(str,"#if ");
strcat(str,argv[1]);
if(NULL == argv[1])
{
#if DEBUG
printf("Param is NULL,return\n");
#endif
return 0;
}
fp = fopen(filename, "r");
if(NULL == fp)
{
printf("[- %d -]Open file error,filename:test.c\n",__LINE__);
return FALSE;
}
while(STR_FOUND == Search_Str(fp,str,FORWARD,&start, &line))
{
#if DEBUG
printf("delete [------------- %04d -------------]\n",line);
#endif
Search_Str(fp, "#endif", FORWARD, &end, &line);
end += strlen("#endif") + 1;
Del_Words_In_File(filename, start, end);
}
return 0;
#if 0
if(STR_FOUND == Search_Str(fp,"#if",FORWARD,&site))
{
cnt++;
while(cnt)
{
printf("while\n");
if(STR_FOUND == Search_Str(fp,"#if",FORWARD,&site))
{
fseek( fp, site_bak, SEEK_SET);
cnt++;
}
else
{
fseek( fp, site_bak, SEEK_SET);
if(STR_FOUND == Search_Str(fp,"#endif",FORWARD,&site))
{
fseek( fp, site_bak, SEEK_SET);
cnt--;
}
}
}
end = site;
}
else
{
Search_Str(fp, "#endif", FORWARD, &end);
end += strlen("#endif") + 1;
Del_Words_In_File(filename, start, end);
}
return 0;
#endif
}/*end main*/
/***********************************************/
/************ delete.bat **********************/
/**********************************************/
@echo off
D:\MyProject\Del_Debug_Code\Debug\Del_Debug_Code.exe %1 D:\\MyProject\\Del_Debug_Code\\test.c
- 自动删除源代码里面的debug代码
- JB2005里面的SWING代码的自动生成
- Python代码实现:删除一个list里面的重复元素
- Android删除工程里面无用的代码和资源
- Android Studio删除工程里面无用的代码和资源
- c#代码获取web.config配置文件里面设置的 <compilation debug="true"节点的debug值
- word 里面如何自动删除空行
- android源代码在mac上的自动生成代码
- java读取excel文件里面的标准代码,并自动生成枚举类的java代码
- 删除编辑器里面的图片
- 删除centos7里面的openjdk
- C语言实现代码注释的自动删除
- C++语言实现代码注释的自动删除
- 代码自动删除QQ空间里的说说
- phpfmt自动删除代码解决方案
- html的里面自动换行
- 去掉源代码里的debug标致
- 无法Debug JDK源代码的解决方案
- RGB转灰度图的几种算法
- taspnet 实现右下角小贴士
- Ubuntu使用中的一些问题总结
- HDU--3592[World Exhibition] 差分约束
- POJ--1201[Intervals] 基础的差分约束
- 自动删除源代码里面的debug代码
- Erlang不能错过的盛宴
- oracle9i下载列表
- PendingIntent学习小结
- 使用无限生命期Session的方法
- toj1572_Yekaterinozavodsk Great Well
- 编译和链接 - #define POINTER_64 __ptr64
- 起点
- Mysql的自动事件调度