自动删除源代码里面的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


原创粉丝点击