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

void DisplayHelp( void );

int main( int argc, char *argv[] )
{
	FILE *SrcFp;
	FILE *DstFp;
	char SrcPath[129];
	char DstPath[129];
	int cnt;
	int c;
	int i;
	int nDdFlag;
	/* ----------------------------------------	ƒwƒ‹ƒv•\Ž¦ */
	if ( argc < 2 ) DisplayHelp();
	if ( strcmp( argv[1],"/?") == 0 ) DisplayHelp();
	if ( strcmp( argv[1],"-?") == 0 ) DisplayHelp();
	/* »·Þ®³ ¶²¼ */
	strcpy( SrcPath, argv[1] );
	strcat( SrcPath, ".H" );
	strcpy( DstPath, argv[1] );
	strcat( DstPath, ".ASM" );
	if ( ( SrcFp = fopen( SrcPath, "rb" ) ) != NULL ) {
		if ( ( DstFp = fopen( DstPath, "w" ) ) != NULL ) {
			while( 1 ) {
				c = getc( SrcFp );
				if ( c == '{' ) {
					break;
				}
			}
			cnt = 0;
			nDdFlag = 0;
			fprintf( DstFp, "\tDD\t" );
			while( 1 ) {
				c = getc( SrcFp );
				if ( c == '}' ) {
					break;
				}
				if ( c == 'x' ) {
					if ( nDdFlag == 1 ) {
						fprintf( DstFp, "\tDD\t" );
						nDdFlag = 0;
					}
					fprintf( DstFp, "0" );
					for( i=0; i<8; i++ ) {
						c = getc( SrcFp );
						fprintf( DstFp, "%c", c );
					}
					fprintf( DstFp, "h" );
					if ( cnt != 7 ) {
						fprintf( DstFp, ", " );
					} else {
						fprintf( DstFp, "\n" );
						nDdFlag = 1;
					}
					cnt++;
					if ( cnt > 7 ) cnt = 0;
				}
			}
			fclose( DstFp );
		} else {
			printf( "%s ¦ ¶·ºÑ ºÄ ¶Þ ÃÞ·Ï¾Ý.", DstPath );
		}
		fclose( SrcFp );
	} else {
		printf( "%s ¦ ÖÐºÑ ºÄ ¶Þ ÃÞ·Ï¾Ý.", SrcPath );
	}
}

void DisplayHelp( void )
{
	printf( "USAGE:\n" );
	printf( "HTOASM [.H file] [Enter]\n" );
}
