代码格式转换-read/write

#include "../head.h"

 

/*
 * 去掉代码中的dbg整行打印信息 
 * 去掉代码中的中文注释信息
 */


void instructions(void)
{
	printf("使用说明:\n"
		"\t按比特位来使用某项功能\n"
		"\t8421 8421\n\n"
		"\t0	默认选项,全部开启\n"
		"\t1	去掉dbg() 打印信息\n"
		"\t2	去掉中文\n"
		"\t4	去掉//...\n"
		"\t8	去掉/*...*/\n\n\n");
}
 

int main(int agrc, char **argv)
{		
//	get_variable_size(); 
	instructions();

	dbg();
	
	if(agrc != 2) dbge("%d", agrc);

	if(mkdir("out", 0777) && errno != 17) dbge();  

	char outfile[50]; 
	sprintf(outfile, "out/%s", argv[1]);
 	
	dbg("%s %s", argv[1], outfile); 
	
	int ofd, ifd;
	if(-1 == (ofd = open(outfile, 2 | O_CREAT | O_TRUNC))) dbge(); 
	if(-1 == (ifd = open(argv[1], 2))) dbge(); 
	
	struct stat stat;
	if(fstat(ifd, &stat)) dbge();
	dbg("%ld %ldk", stat.st_size, stat.st_size >> 10);
	
	char *buf = malloc(stat.st_size);
	if(NULL == buf) dbge();
	if(-1 == read(ifd, buf, stat.st_size)) dbge();
	
	char *p = buf;
	char *start = buf; 
	char *end = p + stat.st_size;
	do { 
		if('d' == *p || '/' == *p) {   
			if(0x28676264 == *I(p) || 0x80202f2f == (*I(p) & 0x80ffffff)) {   	 	// dbg( 和 //						 				
				if(-1 == write(ofd, start, p - start)) dbge(); 
				
				p += 4;    
				while(*++p != '\n');   
				start = p;   	      
			} else if(0x2a2f == *S(p)) {							 
				if(-1 == write(ofd, start, p - start)) dbge();
				
				p += 2;
				while(*++p != '/');
				start = ++p;   	
			} 
		} else if(*p & 0x80) {									// 去中文  
			if(-1 == write(ofd, start, p - start)) dbge(); 
			while(*++p & 0x80); 
			start = p;   			
		}
	} while(++p < end); 
 
	if(p > start)    
		if(-1 == write(ofd, start, p - start)) dbge();   
	 
	free(buf);
	dbg("转换成功 0x%x 0x%x 0x%x %x", *I(&"dbg("), *I(&"// "), *S(&"/*"), '/');  
	 
	return 0; 
}

 

posted @ 2023-03-27 16:37  迷人的危险~  阅读(66)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css