xml2c.c

来自「用C语言设计的EPSON LCD控制器S1D13700驱动。」· C语言 代码 · 共 76 行

C
76
字号
// BIN2C - Dump binary files in C hex

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


#define	ISIZE	16384
#define	OSIZE	2048

FILE	*pOutFD = NULL;


#define DUMPS(s)	if (pOutFD) fputs(s,pOutFD); else puts(s);
#define DUMPF(m,p)	if (pOutFD) fprintf(pOutFD,m,p); else printf(m,p);



int main(int argc, char *argv[])
{
	int		Len, i;
	char	*pInBuf;
	char	*pOutBuf;
	FILE	*pInFD;

	if ( argc<2 || argc>3 )
	{
		puts("BIN2C - Dump binary files in C hex table format.");
		puts("Usage: BIN2C file1 [file2]");
		return (1);
	}

	if ( ((pInBuf = malloc(ISIZE)) == NULL) || ((pOutBuf = malloc(OSIZE)) == NULL) )
	{
		puts("Insufficient memory for I/O buffers.");
		return (1);
	}

	if ( !(pInFD = fopen(argv[1],"rb")) )
	{
		printf("\aError opening file %s!\n", argv[1]);
		return (1);
	}

	if ( argc>2 && !(pOutFD = fopen(argv[2],"wb")) )
		setvbuf(stdout, pOutBuf, _IOFBF, OSIZE);

	DUMPS( "\"" );
	while ( (Len = fread(pInBuf, 1, ISIZE, pInFD)) )
	{
		for ( i=0; i<Len; i++ )
		{
			switch ( pInBuf[i] )
			{
				default:	DUMPF( "%c", pInBuf[i] );	break;
				case '\"':	DUMPS( "\\\"" );			break;
				case '\\':	DUMPS( "\\\\" );			break;
				case '\r':	DUMPS( "\",\r" );			break;
				case '\n':	DUMPS( "\n\"" );			break;
			}
//			if ( ((i+1)%500) == 0 )
//				DUMPS( "\"\r\n\"" );
		}
	}
	DUMPS( "\"" );

	if ( pInFD )
		fclose( pInFD );
	if ( pOutFD )
		fclose( pOutFD );

	return (0);
}


⌨️ 快捷键说明

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