/**************************************
 *   Command for batch file : isbin   *
 *      Program made by  K,Ajima      *
 *    Copyright ajiyan soft l.t.d.    *
 **************************************/



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "isbin.dtv"



/* 16進 1文字 > int */
#define chhex(_c) (_c>64 ? _c-55 : _c-48)



/*
 * バイナリテーブル
 * 0:text   1:binary   2:kanji
 */
static char bint[256]={

	1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,1,
	1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

	0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
	2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
	2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,2

};



/*
 * shift-JIS 漢字
 * 0:有効   1:無効
 */

static char kanji=0;



/********************************************************************
 *   前振り                                                         *
 ********************************************************************/
void _fastcall maehuri(void)
{
	static const char * msg[] = {
		"Command for batch file : isbin ver." DTV_ADDVER(),
		"Program made by K,Ajima",
		"Copyright ajiyan soft l.t.d.   " DTV_ADDTS(),
		"",
		NULL
	};

	int i;

	for(i=0;msg[i]!=NULL;i++) {
		cputs(msg[i]); cputs("\x0d\x0a");
	}
}



/********************************************************************
 *   パラメータエラー                                               *
 ********************************************************************/
void _fastcall nperr(void)
{
	maehuri();

	cputs("パラメータの指定がちがいます\x0d\x0a");
	exit(2);
}



/********************************************************************
 *   パラメータエラー                                               *
 ********************************************************************/
void _fastcall perr(char *para)
{
	maehuri();

	cputs("パラメータ "); cputs(para); cputs(" がちがいます\x0d\x0a");
	exit(2);
}



/********************************************************************
 *   ファイル無しエラー                                             *
 ********************************************************************/
void _fastcall fnfnd(char *fname)
{
	maehuri();

	cputs("ファイル "); cputs(fname); cputs(" がオープンできません\x0d\x0a");
	exit(2);
}



/********************************************************************
 *   ヘルプ                                                         *
 ********************************************************************/
void _fastcall help(void)
{
	maehuri();

	cputs("Syntax : isbin [ /k ] [ /b< ASCII code > | /t< ASCII code > ]... < Filename >\x0d\x0a\x0d\x0a");

	cputs("/k       shift-JIS 漢字コードを バイナリと 見なします\x0d\x0a");
	cputs("/b       指定した ASCII code を バイナリと 見なします\x0d\x0a");
	cputs("/t       指定した ASCII code を テキストと 見なします\x0d\x0a\x0d\x0a");

	cputs("ERRORLEVEL\x0d\x0a");
	cputs("0        テキストファイルである\x0d\x0a");
	cputs("1        バイナリファイルである\x0d\x0a");
	cputs("2        その他\x0d\x0a");

	exit(2);
}



/********************************************************************
 *   16進文字列 > int                                               *
 ********************************************************************/
int _fastcall tohex(char *strings)
{
	int ret;

	ret=chhex(toupper(strings[0]))<<8;
	ret+=chhex(toupper(strings[1]));

	return(ret);
}



void _cdecl main(int argc,char **argv)
{
	int i;      /* 雑用 */
	FILE *fp;   /* ハンドラ */
	int c=0x1a; /* 読み込み文字 */
	int sb=0;   /* ^Z フラグ */

	/* オプション判定 */
	if(argc==1) nperr();

	/* /? 判定 */
	if(argc==2 && !strcmp(argv[1],"/?")) help();

	/* その他 オプション */
	for(i=1;i<argc-1;i++) {
		if(argv[i][0]!='/') perr(argv[i]);

		switch(tolower(argv[i][1])) {
			case 'k':
				kanji=0;
				break;

			case 'b':
				bint[tohex(argv[i]+2)]=1;
				break;

			case 't':
				bint[tohex(argv[i]+2)]=0;
				break;

			default:
				perr(argv[i]);
		}
	}

	/* ファイルオープン */
	if((fp=fopen(argv[argc-1],"rb"))==NULL)
		fnfnd(argv[argc-1]);

	/* 判定 */
	while(!feof(fp)) {
		if((c=getc(fp))==-1) exit(0);

		switch(bint[c]) {
			case 0:
				if(sb)		exit(1);
				if(c==0x1a) sb++;
				break;

			case 1:
				exit(1);

			case 2:
				/* 漢字無効 */
				if(kanji) break;

				/* 次の文字判定 */
				c=getc(fp);
				if(c>0x3f && c<0xfd) break;
				exit(1);
		}
	}

	exit(0);
}
