#include"pdriver.h"

int		getOneSegment( const char *p, char *q )
{
	if( *p++ != '(' )
		return ERR;

	while( *p != ')' )
	{
		if( *p == 0 )
			return ERR;
		*q++ = *p++;
	}

	return NOERR;
}

int		xfgets( char *fn, FILE *fp )
{
	if( fgets( fn, 256, fp ) == NULL )
		return ERR;

	while( *fn != '\n' && *fn != 0 )
		++ fn;

	*fn = '\0';

	return NOERR;
}

int		addstrings( const char *s, char *t )
{
	int		i = 0;

	while( *s )
	{
		*t++ = *s++;
		i++;
	}
	return i;
}

int		cmpstrings( const char *s, const char *t )
{
	while( *t )
	{
		if( *s++ != *t++ )
			return 0;
	}
	return 1;
}

int		printStrings( int n, char *p )
{
	int		i,e;
/*	FILE	*fp;
	fp = fopen( "i:\\debug.bin", "ab" );*/

	if( n )
	{
		for( i=0; i<n; i++ )
		{
			while( e = PRN_putc( *(p+i) ) )
			{
				if( printReady( e ) )
				{
					end_();
					exit(1);
				}
			}
/*			fputc( *(p+i), fp );*/
		}
	}
	else
	{
		while( *p )
		{
			while( e = PRN_putc( *p ) )
			{
				if( printReady( e ) )
				{
					end_();
					exit(1);
				}
			}
/*			fputc( *p, fp );*/
			p++;
		}
	}
/*	fclose( fp );*/
	return 0;
}

typedef struct
{
	char	*command;
	int		flag;	/* 引き数フラグ flag & 1  ネストフラグ flag & 0x10 */
	int		ret;
} COMM ;

COMM	comdata[]={ { "h1byte", 0, 1 },
					{ "up", 16, 2 },
					{ "down", 16, 4 },
					{ "kanjipitch", 1, 6 },
					{ "retpitch", 1, 8 },
					{ "normal", 16, 10 },
					{ "wide", 16, 11 },
					{ "tall", 16, 12 },
					{ "large", 16, 13 },
					{ "halftall", 16, 14 },
					{ "halfwide", 16, 15 },
					{ "h_t_wide", 16, 16 },
					{ "h_w_tall", 16, 17 },
					{ "small", 16, 18 },
					{ "gothic", 16, 19 },
					{ "mintyo", 16, 20 },
					{ "underline", 16, 21 },
					{ "connect_on", 0, 27 },
					{ "connect_off", 0, 28 },
					{ "set_top", 0, 29 },
					{ "set_bottom", 0, 30 },
					{ "set_center", 0, 31 },
					{ "proportion", 16, 32 },
					{ "next", 0, 38 },
					{ "leftmargin", 1, 39 },
					{ "net", 17, 40 },
					{ "v_write", 0, 41 },
					{ "h_write", 0, 42 },
					{ "mouhitu", 16, 43 },
					{ "maru", 16, 44 },
					{ "kyou", 16, 45 },
					{ "jis", 1, 46 },
					{ "col_rev", 16, 47 },
					{ "tabsize", 1, 48 },
					{ NULL, 0, 0 } };

int		getCommand2()
{
	int		i,j;
	char	buf[256];
	double	w;

	i = 0;
	while( comdata[i].command != NULL )
	{
		if( cmpstrings( file, comdata[i].command ) )
		{
			file += strlen( comdata[i].command );
			if( comdata[i].flag & 1 )
			{
				pool[1] = 1;
				while(1)
				{
					if( getOneSegment( file, buf ) )
					{
						pool[1] = 0;
						break;
					}
					if( calcvalue( buf, &w ) )
					{
						pool[1] = 0;
						break;
					}
					j = w;
					DWORD( pool+2 ) = j;
					file += strlen( buf ) + 2;
					break;
				}
			}
			if( comdata[i].flag & 16 )
			{
				if( *file == '{' )
				{
					pool[0] = 1;
					++ file;
				}
				else
					pool[0] = 0;
			}
			return comdata[i].ret;
		}
		++ i;
	}

	return 0xffff;
}

int		printReady( int e )
{
	int		ch,ec;

	if( e > 6 )
		e = 7;

	printf( "%s\n準備が出来たら何かキーを押してください｡(<a> abort)\n", errmes[e] );
	KYB_clrbuf();
	ch = KYB_read( 0, &ec );
	if( ch == 'a' || ch == 'A' )
		return 1;

	return 0;
}

int		end_()
{
	printf( "See you again!\n" );
	if( mint_flg == ON )
		fclose( mint_fp );
	if( goth_flg == ON )
		fclose( goth_fp );
	if( mouh_flg == ON )
		fclose( mouh_fp );
	if( maru_flg == ON )
		fclose( maru_fp );
	if( kyou_flg == ON )
		fclose( kyou_fp );

	return;
}
