//	M060 : File copy & verify
option double;

//------------------------------------------------------------------------------//
//	File copy & verify															//
//																				//
//	Return	: None																//
//------------------------------------------------------------------------------//
function main()
{
	define	bno, finp, fout, fcmp, bytes, seek;
	define	$InpFile, $BinFile, $TxtFile;
	define	$txt, $cmp;
	define	fUnmatch;
	define	count;

	_Cls();

	bno = _MemAlloc(128);						//	Memory block allocation

	//	File path setting
	$InpFile = $_StrCat($_MyPath, "TestData1.txt");
	$BinFile = $_StrCat($_MyPath, "BinData.txt");
	$TxtFile = $_StrCat($_MyPath, "TxtData.txt");

	_Print("Copy file by binary access(TestData1.txt -> BinData.txt)\n");
	count = 0;
	if ((finp = _FOpen($InpFile, "rb")) && (fout = _FOpen($BinFile, "wb"))) {
		while (bytes = _FRead(finp, bno, 0, 128)) {
			_FWrite(fout, bno, 0, bytes);
			//	Reopen the input file and execute the SEEK test
			if (count == 5) {
				seek = _FGetSeek(finp);
				_FClose(finp);
				finp = _FOpen($InpFile, "rb");
				_FSetSeek(finp, seek);
			}
			count++;
		}
		_FClose(finp);
		_FClose(fout);
	}

	_Print("Copy file by text access(TestData1.txt -> TxtData.txt)\n");
	if ((finp = _FOpen($InpFile, "rt")) && (fout = _FOpen($TxtFile, "wt"))) {
		while ($txt = $_FGetS(finp)) {
			_FPutS(fout, $txt);
		}
		_FClose(finp);
		_FClose(fout);
	}

	_Print("File Compare(BinFile)\n");
	fUnmatch = _FALSE;
	if ((finp = _FOpen($InpFile, "rt")) && (fcmp = _FOpen($BinFile, "rt"))) {
		while ($txt = $_FGetS(finp)) {
			$cmp = $_FGetS(fcmp);
			if ($txt != $cmp) {
				fUnmatch = _TRUE;
				break;
			}
		}
		_FClose(finp);
		_FClose(fcmp);
	}
	_Print("TestData1.txt VS BinData.txt --> ");
	if (fUnmatch) _Print("Unmatch(NG)");
	else		  _Print("Match(OK)\n");

	_Print("File Compare(TxtFile)\n");
	fUnmatch = _FALSE;
	if ((finp = _FOpen($InpFile, "rt")) && (fcmp = _FOpen($TxtFile, "rt"))) {
		while ($txt = $_FGetS(finp)) {
			$cmp = $_FGetS(fcmp);
			if ($txt != $cmp) {
				fUnmatch = _TRUE;
				break;
			}
		}
		_FClose(finp);
		_FClose(fcmp);
	}
	_Print("TestData1.txt VS TxtData.txt --> ");
	if (fUnmatch) _Print("Unmatch(NG)");
	else		  _Print("Match(OK)\n");

	_Print("Delete file(BinData.txt, TxtData.txt)\n");
	_FDelete($BinFile);
	_FDelete($TxtFile);

	_MemFree(bno);								//	Memory block release

}
