* Chunks.asm
* Listet die Chunks einer IFF-Datei auf. Ignoriert die Struktur, da ohne
* iffparse.library gearbeitet wird.
* nur für CLI.
* benötigt OS2

	NOLIST
	INCLUDE "dos/LVO.i"
	INCLUDE "exec/LVO.i"
	INCLUDE "dos/dos.i"
	LIST

;Register-Variablen:

_DOSBase:	equr a5
FH:		equr d7
RDArgs: 	equr d6
NextNamePtr:	equr a4
ThisName:	equr d5
FileRest:	equr d4 ;noch zu bearbeitender Teil des Files (Bytes)

;*********************************************************************

	SECTION prg,CODE

;Dos-Library öffnen:
_main:	OPENLIB DOSName(pc),36
	tst.l	d0
	beq	wrongdos
	move.l	d0,_DOSBase
; Parameter abfragen
	lea	Template(pc),a0
	move.l	a0,d1
	clr.l	-(sp)
	move.l	sp,d2
	moveq	#0,d3
	CALLDOS ReadArgs
	move.l	(sp)+,NextNamePtr
	move.l	d0,RDArgs
	beq	CloseDos
	move.l	NextNamePtr,d0 ;tst.l NextNamePtr
	beq	NoMoreArgs

NextIFFFile:
	move.l	(NextNamePtr)+,ThisName
	beq	NoMoreArgs
	move.l	ThisName,d1
	move.l	#MODE_OLDFILE,d2
	CALL	Open
	move.l	d1,FH
	bne	1$
; Fehler: Datei läßt sich nicht öffnen
	lea	Error1(pc),a0
	move.l	a0,d1
	move.l	ThisName,-(sp)
	move.l	sp,d2
	CALL	VPrintf
	addq.l	#4,sp
	bra	NextIFFFile
1$:	move.l	FH,d1
	moveq	#12,d3
	sub.l	d3,sp
	move.l	sp,d2
	CALL	Read		;Kopf lesen
	cmp.l	d3,d0
	beq	l1		;ein IFF-File kann nicht
				;kürzer als 12 Bytes sein
	add.l	d3,sp
;kein IFF-File
IFFError: lea	Error2(pc),a0
FileError:
	move.l	a0,d1
	move.l	ThisName,-(sp)
	move.l	sp,d2
	CALL	VPrintf
	addq.l	#4,sp
EndOfIFF:
	move.l	FH,d1
	CALL	Close
	bra	NextIFFFile
l1:	cmp.l	#'FORM',(sp)+
	beq	4$
	addq.l	#8,sp
	bra	IFFError
;die Datei ist ein IFF-File
4$:	move.l	ThisName,-(sp)
	lea	HeadTxt1(pc),a0
	move.l	a0,d1
	move.l	sp,d2
	CALL	VPrintf
	addq.l	#4,sp
	move.l	(sp)+,FileRest
	subq.l	#4,FileRest	;die Format-Kennung
	move.l	(sp)+,d0
	bsr	ChunkIDOut
	lea	HeadTxt2(pc),a0
	move.l	a0,d1
	CALL	PutStr

;Schleife über die Chunks:
ChunkLoop:
	move.l	FH,d1
	moveq	#8,d3
	sub.l	d3,sp
	move.l	sp,d2
	CALL	Read
	cmp.l	d3,d0
	beq	l2
;Lesefehler (zumindest nach IFF-Protokoll)
	add.l	d3,sp
ReadError:
	lea	Error3(pc),a0
	bra	FileError
l2:	move.l	(sp)+,d0
	bsr	ChunkIDOut
	lea	ChunkTxt(pc),a0
	move.l	a0,d1
	move.l	sp,d2
	CALL	VPrintf
	move.l	(sp)+,d2
;Rest des Chunks überspringen:
	move.l	FH,d1
	addq.l	#1,d2
	and.w	#$fffe,d2		;auf gerade Anzahl aufrunden
	sub.l	d2,FileRest
	subq.l	#8,FileRest		;die Chunk-Kennung & -Länge
	beq	EndOfIFF
	move.l	#OFFSET_CURRENT,d3
	CALL	Seek
	bra	ChunkLoop

NoMoreArgs:
	move.l	RDArgs,d1
	CALL	FreeArgs
CloseDos:
	CLOSELIB _DOSBase
nodos:	moveq	#0,d0
	rts

;keine dos-library V36+
wrongdos:
	OPENLIB DOSName(pc),33
	tst.l	d0
	beq	nodos
	move.l	d0,_DOSBase
	CALLDOS Output
	move.l	d0,d1
	beq	CloseDos
	lea	DOSName(pc),a0
	move.l	a0,d2
	move.l	#DOS36Len,d3
	CALL	Write
	bra	CloseDos

; Unterroutine: Chunk-ID ausgeben:
ChunkIDOut:
	move.w	d0,-(sp)
	moveq	#0,d1
	move.b	(sp),d0
	move.b	d1,(sp)
	move.w	d0,-(sp)
	move.b	d1,(sp)
	swap	d0
	move.w	d0,-(sp)
	move.b	(sp),d0
	move.b	d1,(sp)
	move.w	d0,-(sp)
	move.b	d1,(sp)
	lea	IDFormat(pc),a0
	move.l	a0,d1
	move.l	sp,d2
	CALL	VPrintf
	addq.l	#8,sp
	rts

;*********************************************************************

DOSName:	DOSNAME
		dc.b	" v36+ required",10
DOS36Len:	equ	*-DOSName
Template:	dc.b	"Files/A/M",0

HeadTxt1:	dc.b "IFF-File %s, Length %ld+8 Bytes. Format: ",0
HeadTxt2:	dc.b 10,"Chunk",9,"Length (-8) in Bytes",10,0
ChunkTxt:	dc.b 9,"%ld",10,0
Error1: 	dc.b "File %s not found",10,0
Error2: 	dc.b "%s is no IFF-File",10,0
Error3: 	dc.b "Read-Error (early EOF)",10,0
IDFormat:	dc.b "%c%c%c%c",0

	END

