

		section "Recognizer",code

	incdir	"includes:"

;EXEC
	include "exec/exec_lib.i"
	include	"exec/exec.i"

	include	"libraries/datatypes_lib.i"
	include	"datatypes/datatypes.i"

;DOS
	IFND LIBRARIES_DOS_I
	include "libraries/dos.i"
	ENDC

	IFND LIBRARIES_DOSEXTENS_I
	include "libraries/dosextens.i"
	ENDC
	
	IFND LIBRARIES_DOS_LIB_I
	include	"libraries/dos_lib.i"
	ENDC

;DM
	incdir	"asm:datamaster.library/"
	include	"datamaster.i"

	incdir	"asm:datamaster.library/"
	include	"datamaster_lib.i"

CALLBASE:	MACRO
	jsr	_LVO\1(a6)
		ENDM

JMPBASE:	MACRO
	jmp	_LVO\1(a6)
		ENDM


		RSRESET		;My Little Structure for string etc.... :-)
mls_data:	rs.l	1	;=a2
mls_size:	rs.l	1	;=d2
mls_handle:	rs.l	1
mls_result:	rs.l	1	;LONG return value (0 or 1 :-)
mls_string:	rs.b	300
mls_SIZEOF:	rs.w	0

	REM
run:
	jsr	reco\.init		;some horrible tests...
	lea	adr,a0			;don't look pleaase :-))))
	move.l	#adr2-adr,d0
	move.l	a0,a2
	move.l	d0,d2
	lea	$781cf2c4,a6		;Thanx to Scout & Xopa *:-()
	move.l	a6,a5
	jsr	reco\.check
	jmp	reco\.exit

	ds.b	65000
adr:
	incbin	"Jeux:Gfx/WB1.3gfx.iff"
	even
adr2:
	EREM	




		RECOHEADER reco
reco:	dc.l	DMR_check,.check
	dc.l	DMR_name,.name
	dc.l	DMR_Descr,.descr
	dc.l	DMR_Flags,recoF_CheckData+recoF_Getname+recoF_DupString
	dc.l	DMR_Pri,-20
	dc.l	DMR_Version,1<<16+1
	dc.l	DMR_InitRoutine,.init
	dc.l	DMR_ExitRoutine,.exit
	dc.l	TAG_END

;Init/Exit routines
;-----------------
.init:	lea	dname(pc),a1
	move.l	#37,d0
	CALLEXEC Openlibrary
	move.l	d0,.dlib
	rts

.exit:	move.l	.dlib,a1
	move.l	a1,d0
	bne.s	.do
	bra.s	.err
.do:	CALLEXEC Closelibrary
.err:	moveq	#1,d0
	rts

;the checking routine
;---------------------
.check:
	move.l	#mls_SIZEOF,d0
	move.l	#MEMF_ANY+MEMF_CLEAR,d1
	CALLEXEC ALLOCMEM	
	tst.l	d0
	beq.s	.err
	move.l	d0,a4			;a4=base of the mls structure now!

	move.l	a2,mls_data(a4)		;store important stuff...
	cmp.l	#5000,d2
	blo.s	.keep
	move.l	#5000,d2		;MAX value is 5000 (or lower :-)
.keep:	move.l	d2,mls_size(a4)		;we won't have to 'protect' these regs all the time now..

	bsr	.PRINTC			;create the filename...

	bsr	.SAVEFILE		;save the temp file...
	tst.l	d0
	beq.w	.freemem


	lea	mls_string(a4),a0	;obtain read handle for datatypes
	move.l	a0,d1
	move.l	#ACCESS_READ,d2
	CALLBASE LOCK
	tst.l	d0
	beq.s	.cantopen
	move.l	d0,mls_handle(a4)	

	move.l	d0,a0
	moveq	#2,d0			;DTST_HANDLE ????
	sub.l	a1,a1
	move.l	.dlib,a6
	CALLBASE ObtainDataTypeA
	tst.l	d0
	beq.s	.notype
	move.l	d0,d5			;yeah we got the datatype

	move.l	d0,a1			;now take the string from the core...
	move.l	dtn_header(a1),a1
	move.l	dth_name(a1),a0
;	move.l	dth_basename(a1),a0
	
	move.l	a5,a6
	move.l	a0,-(sp)
	lea	binname(pc),a1		;if it's BINARY,then failure!
	CALLBASE dmCmpStringNC
	move.l	(sp)+,a0
	tst.l	d0
	beq.s	.freedtype
	CALLBASE dmDupString		;...duplicate it...
	tst.l	d0
	beq.s	.freedtype
	move.l	a0,mls_result(a4)	;save the result (=string)
	
.freedtype:
	move.l	d5,a0			;....then release the datatype!
	move.l	.dlib,a6
	CALLBASE ReleaseDatatype

.notype:
	move.l	mls_handle(a4),d1	;close read handle
	move.l	dmb_dosbase(a5),a6
	CALLBASE UNLOCK	
.cantopen:
	bsr.s	.delete			;delete file

.freemem:
	move.l	mls_result(a4),-(sp)	;free memory
	move.l	a4,a1
	move.l	#mls_SIZEOF,d0
	CALLEXEC FREEMEM
	move.l	(sp)+,d0		;yep...the result
	rts	

.SAVEFILE:
			;IN:		a0=filename
			;		a1=Address
			;		d0=Size
			;OUT:d0=error if zero 

	lea	mls_string(a4),a0		;dop_a0
	move.l	a0,d4
	move.l	mls_data(a4),d5			;dop_adr
	move.l	mls_size(a4),d6			;dop_size

	move.l	a0,d1
	move.l	#MODE_NEWFILE,d2
	move.l	dmb_dosbase(a5),a6
	CALLBASE OPEN
	tst.l	d0
	bne.s	.diskok
	rts
.diskok:move.l	d0,d7

	move.l	d0,d1
	move.l	d5,d2
	move.l	d6,d3
	CALLBASE WRITE

	move.l	d7,d1
	JMPBASE CLOSE

.delete:lea	mls_string(a4),a0
	move.l	a0,d1
	move.l	dmb_dosbase(a5),a6
	JMPBASE	DELETEFILE


.PRINTC:				;IN:		a0=format string
	lea	fname(pc),a0
	lea	mls_data(a4),a1
	lea	mls_string(a4),a3
	lea	.putch(pc),a2
	move.l	$4.w,a6
	JMPBASE RawDoFmt
.putch:	move.b	d0,(a3)+
	rts



.dlib:	dc.l	0
.name:	dc.b	"LINK-Datatypes",0
.descr:	dc.b	"A bridge to the datatypes.library!",10
	dc.b	"To gain profit of the existing datatypes ;-)",0
dname:	dc.b	"datatypes.library",0
fname:	dc.b	"T:LINK-dtc_%lx",0
binname:dc.b	"binary",0
