⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvt_c2c.c

📁 个人曾经在工作上的源代码
💻 C
字号:
/***
*Cvt_C2C.c - 由双引号定义的字符串转换为数组定义的字符串. 
*
*Purpose:
*	用于处理中文, 韩文时, 当编译器无法识别全角字符的场合. 
*
*Copyright:
*	Modified by Denny
*	2004-12-01 11:28
*	All rights reserved.
*
*Environment:
*	Editor - SourceInsight 3.5
*	Edit font - Consolas - 12
*	Tab width - 4 (Uses spaces in place of Tabs)
*	Compiler - Turbo C 2.0
*   Run - Microsoft DOS
*
*Exceptions:
*   转换实例: 
*   .src: 
*       1234
*       "5678"
*       const char c[] = {"asdf"};
*   .obj
*       1234
*       {0x35,0x36,0x37,0x38,0x00}
*       const char c[] = {0x61,0x73,0x64,0x66,0x00};
*
****/
#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "stdlib.h"


#define	TRUE			1
#define	FALSE			0

const unsigned long Ver = (~102);		/* Encrypt the version */
const char Head_Info1[] = {"\nFile convert function(Version "};
const char Head_Info2[] = {").\x01\n\n"};
const char Head_Info3[] = {"Created by Windforce(R), at 2004-11-27 13:27.\n"};
const char Head_Info4[] = {"Modified on 2004-11-27 13:28.\n"};
const char Head_Info5[] = {"Copyright (c) WeiHua LTD.\n"};
const char Head_Info6[] = {"All rights reserverd.\n"};
const char Head_Info7[] = {"\nDetail: \n"};
const char Head_Info8[] = {"    Convert the input string to the C format string. For example:\n"};
const char Head_Info9[] = {"    The source string: ABCD; then the result: 0x41,0x42,0x43,0x44\n"};
const char Head_Info10[] = {"    The source string: \"ABCD\"; then the result: 0x41,0x42,0x43,0x44,0x00\n\n"};

void main(int argc, char *argv[])
{
	char	src_buf[80];			/* Source file input buffer */
	char	obj_buf[80];			/* Object file input buffer */
	char	*src_file;				/* Source file string */
	char	*obj_file;				/* Object file string */
	FILE	*src_fp;				/* Source file pointer */
	FILE	*obj_fp;				/* Object file pointer */

	printf(Head_Info1);				/* Print the head information */
	putchar((~Ver)/100%10 + '0');
	putchar('.');
	putchar((~Ver)/10%10 + '0');
	putchar((~Ver)/1%10 + '0');
	printf(Head_Info2);
	printf(Head_Info3);
	printf(Head_Info4);
	printf(Head_Info5);
	printf(Head_Info6);
	printf(Head_Info7);
	printf(Head_Info8);
	printf(Head_Info9);
	printf(Head_Info10);
	if(argc <= 1)					/* Did't input the source file */
	{
		printf("Source file:");
		src_file = src_buf;
		scanf("%s", src_file);
	}
	else /* argc > 1 */
	{
		if((strcmp(argv[1], "/?") == 0) || (strcmp(argv[1], "/H") == 0) || (strcmp(argv[1], "/h") == 0))
									/* Display the usage */
		{
			printf("Usage:\n");
			printf("    .\\Cvt.exe     [Source filename]     [Destination filename]\n");
			return;
		}
		src_file = argv[1];
	}
	do{
		if((strcmp(src_file, "q") == 0) || (strcmp(src_file, "Q") == 0) || (strcmp(src_file, "exit") == 0)
			|| (strcmp(src_file, "EXIT") == 0) || (strcmp(src_file, "Exit") == 0) || (strcmp(src_file, "eXIT") == 0))
		{
			return;
		}
		src_fp = fopen(src_file, "rb");
		if(src_fp == NULL)
		{
			printf("Source file not found!");
			getch();
			putchar('\n');
			printf("Source file:");
			src_file = src_buf;
			scanf("%s", src_file);
		}
		else
			break;
	}while(TRUE);

	if(argc <= 2)					/* Did't input the object file */
	{
		printf("Object file:");
		obj_file = obj_buf;
		scanf("%s", obj_file);
	}
	else
		obj_file = argv[2];
	if(strcmp(src_file, obj_file) == 0)			/* The same file name */
	{
		fclose(src_fp);
		return;
	}
	do{
		if((strcmp(obj_file, "q") == 0) || (strcmp(obj_file, "Q") == 0) || (strcmp(obj_file, "exit") == 0)
			|| (strcmp(obj_file, "EXIT") == 0) || (strcmp(obj_file, "Exit") == 0) || (strcmp(obj_file, "eXIT") == 0))
		{
			fclose(src_fp);
			return;
		}
		system("cd .");
		obj_fp = fopen(obj_file, "wb");
		if(obj_fp == NULL)
		{
			printf("Object file can't be created!");
			getch();
			putchar('\n');
			printf("Object file:");
			obj_file = obj_buf;
			scanf("%s", obj_file);
		}
		else
			break;
	}while(TRUE);

	{
		int			src_byte;
		char			cvt_byte;	/* Object is the ASCII code */
		char			wr_buf[6+1];
		unsigned char	posi;
		unsigned char	head_flag = 1;
		unsigned char	i;
		unsigned char	spec_char_cnt = 0;
		unsigned char	spec_char2 = 0x00;

		printf("Destination file: %s\n", obj_file);
		printf("Converts result:\n");
		for(i = 0; i < 60; i ++)
			putchar(0xC4);
		putchar('\n');
		while((src_byte=fgetc(src_fp)) != EOF)
		{
			switch(src_byte)
			{
				case	0x0D:
				case	0x0A:
						head_flag = 1;
						spec_char_cnt = 0;
						if(spec_char2 == '{')
						{
							fputc(spec_char2, obj_fp);
							putchar(spec_char2);
						}
						spec_char2 = 0x00;
						fputc(src_byte, obj_fp);
						putchar(src_byte);
						break;

				case	'{':
						spec_char2 = src_byte;
						break;

				case	'}':
						if(!spec_char2)
						{
							fputc(src_byte, obj_fp);
							putchar(src_byte);
						}
						spec_char2 = 0x00;
						break;

				default:
						posi = 0;
						if(src_byte == '\"')
						{
							spec_char_cnt ++;
							if(spec_char_cnt == 1)
							{
								fputc('{', obj_fp);
								putchar('{');
								spec_char2 = 0;
							}
							else if(spec_char_cnt == 2)
							{
								spec_char_cnt = 0;
								if(head_flag)
								{
									strcpy(wr_buf, "0x00}");
								}
								else
								{
									head_flag = 1;
									strcpy(wr_buf, ",0x00}");
								}
								posi = 6;
								spec_char2 = '}';
								fputs(wr_buf, obj_fp);
								printf("%s", wr_buf);
							}
						}
						else
						{
							if(spec_char_cnt)
							{
								if(head_flag)		/* Is the head */
									head_flag = 0;
								else
									wr_buf[posi++] = ',';
								wr_buf[posi ++] = '0';
								wr_buf[posi ++] = 'x';
								cvt_byte = (src_byte>>4) + '0';
								if(cvt_byte > '9')
									cvt_byte += 7;	/* Character 'A' ~ 'F' */
								wr_buf[posi ++] = cvt_byte;
								cvt_byte = (src_byte&0x0F) + '0';
								if(cvt_byte > '9')
									cvt_byte += 7;	/* Character 'A' ~ 'F' */
								wr_buf[posi ++] = cvt_byte;
								wr_buf[posi ++] = '\0';
								fputs(wr_buf, obj_fp);
								printf("%s", wr_buf);
							}
							else
							{
								if(spec_char2 == '{')
								{
									fputc(spec_char2, obj_fp);
									putchar(spec_char2);
								}
								spec_char2 = 0x00;
								fputc(src_byte, obj_fp);
								putchar(src_byte);
							}
						}
						break;
			}
		}
		putchar('\n');
		for(i = 0; i < 60; i ++)
			putchar(0xC4);
		putchar('\n');
	}
	printf("File convert finish!\n");
	fclose(obj_fp);
	fclose(src_fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -