summaryrefslogtreecommitdiff
path: root/Source/OpenIFF.S
blob: 2b0d026a8699515a4f0fe66b293e68f0455ccf11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
**
**	$Id: $
**	$Revision: $
**
**	$Filename: OpenIFF.S $
**	$Author: Christian A. Weber $
**	$Release: 19.1 $
**	$Date: 92/05/11 21:11:27 $
**
**	iff.library/IFFLib_OpenIFF
**
**	COPYRIGHT (C) 1987-1992 BY CHRISTIAN A. WEBER, BRUGGERWEG 2,
**	CH-8037 ZUERICH, SWITZERLAND.  ALL RIGHTS RESERVED.  NO PART
**	OF THIS SOFTWARE MAY BE COPIED, REPRODUCED,  OR  TRANSMITTED
**	IN ANY FORM OR BY ANY MEANS,  WITHOUT THE PRIOR WRITTEN PER-
**	MISSION OF THE AUTHOR. USE AT YOUR OWN RISK.
**


		IDNT	IFFLib_OpenIFF
		SECTION	text,CODE

		INCLUDE	"IFFLib.i"

		XREF	SetError,ClearError
		XDEF	OpenIFFFunc,NewOpenIFFFunc


******* iff.library/IFFLib_NewOpenIFF ***************************************
*
*   NAME
*	IFFLib_NewOpenIFF -- allocate memory for an IFF-file and read it
*
*   SYNOPSIS
*	ifffile = IFFLib_NewOpenIFF( filename, memattr )
*	D0                           A0        D0
*
*	IFFFILE IFFLib_OpenIFF( char * )
*
*   FUNCTION
*	This function opens a file on a disk and looks whether it's an IFF
*	file or not. If it is an IFF file, memory is allocated and the file
*	is read into memory. The 'memattr' argument is taken to define the
*	memory type. Normally memattr is set to MEMF_ANY or MEMF_PUBLIC.
*	If an error occurs, NULL is returned, and you can call
*	IFFLib_IFFError() to get the error number.
*
*   INPUTS
*	filename - Pointer to a null-terminated string
*	memattr  - Memory requirements as used for Exec's AllocMem(),
*	           such as MEMF_CHIP, MEMF_PUBLIC ...
*	           (MEMF_CLEAR is not necessary)
*
*   RESULT
*	ifffile - 'FileHandle', points to the beginning of the IFF file
*	("FORM...."), Zero if unsuccessful. Call IFFLib_IFFError() to get
*	the reason of the failure.
*
*   SEE ALSO
*	IFFLib_OpenIFF(), IFFLib_CloseIFF(), IFFLib_IFFError()
*
*   BUGS
*	None known
*
*****************************************************************************

OpenIFFFunc:	movem.l	d2-d6/a2/a5-a6,-(SP)
		moveq.l	#-1,d6			; Kein MemType angegeben
		bra.s	DoCont



******* iff.library/IFFLib_OpenIFF ******************************************
*
*    NAME
*	IFFLib_OpenIFF -- allocate memory for an IFF-file and read it
*
*    SYNOPSIS
*	ifffile = IFFLib_OpenIFF( filename )
*	D0                        A0
*
*	IFFFILE IFFLib_OpenIFF( char * )
*
*    FUNCTION
*	This function does the same as the IFFLib_NewOpenIFF() routine
*	above, except that you cannot specify the type of memory which is
*	allocated for the file. Normally the type is 0 which means 'any
*	memory', but if the IFF file type is '8SVX', the file is ALWAYS
*	loaded into CHIP memory. If you wish to override these defaults,
*	use IFFLib_NewOpenIFF() instead of IFFLib_OpenIFF().
*
*   INPUTS
*	filename - Pointer to a null-terminated string
*
*   RESULT
*	ifffile - 'FileHandle', points to the beginning of the IFF file
*	("FORM...."), 0 if unsuccessful. Call IFFError() to get the
*	reason of the failure.
*
*   BUGS
*	None
*
*   SEE ALSO
*	IFFLib_NewOpenIFF(), IFFLib_CloseIFF(), IFFLib_IFFError()
*
*****************************************************************************

NewOpenIFFFunc:	movem.l	d2-d6/a2/a5-a6,-(SP)
		move.l	d0,d6			; D6 :  MemType
DoCont:		movea.l	a6,a5			; IFFBase
		lea	-12(SP),SP		; Arbeitsspeicher reservieren
		moveq.l	#0,d5			; Fehlernummer rücksetzen

	*** File öffnen

		move.l	a0,d1			; Name
		move.l	#MODE_OLDFILE,d2	; accessmode
		movea.l	ib_DOSBase(a5),a6
		JSRLIB	Open
		move.l	d0,d4			; D4 := FileHandle
		bne.s	1$
		moveq	#IFF_CANTOPENFILE,d5
		bra.s	.Error2			; --->
1$:
	*** Erste 12 Bytes lesen, testen ob 'FORM'

		move.l	d4,d1			; File
		move.l	SP,d2			; Adr
		moveq.l	#12,d3			; Len
		JSRLIB	Read			; erste 3 Longwords lesen
		cmpi.l	#'FORM',(SP)
		beq.s	2$
		moveq	#IFF_NOTIFF,d5
		bra.s	.Error1			; --->
2$:
	*** Testen ob Chip-Speicher erwünscht (bei 8SVX-Files)

		move.l	d6,d1			; Default-Requirements
		bpl.s	3$			; sind gültig --->
		moveq.l	#MEMF_PUBLIC,d1
		cmpi.l	#'8SVX',8(SP)		; 8SVX-File ?
		bne.s	3$			; nein --->
		moveq.l	#MEMF_CHIP|MEMF_PUBLIC,d1 ; sonst CHIP-Memory!
3$:
	*** Speicher für File reservieren

		moveq.l	#12,d2			; 'Remember' + FORM....
		move.l	4(SP),d3		; D3 :  FORM-Länge
		add.l	d3,d2			; D2 :  Gesamtlänge
		subq.l	#4,d3	; FUCK!!! IFF-Typ ist ja schon gelesen!
		move.l	d2,d0
		movea.l	ib_SysBase(a5),a6
		JSRLIB	AllocMem		; requirements von D1
		movea.l	d0,a2			; A2 :  IFFFile-Adresse
		tst.l	d0
		bne.s	4$			; OK --->
		moveq	#IFF_NOMEM,d5
		bra.s	.Error1			; --->
4$:
	*** File einladen

		move.l	d2,(a2)+		; Länge des reserv. Bereichs
		move.l	d4,d1			; File
		move.l	a2,d2			; Adr
		addq.l	#8,d2			; FORM.... schon gelesen!
		addq.l	#4,d2			; IFF-Typ schon gelesen
		movea.l	ib_DOSBase(a5),a6
		JSRLIB	Read			; D3 war schon Länge
		tst.l	d0
		bpl.s	5$			; OK --->
		moveq	#IFF_READERROR,d5
		bra.s	.Error1
5$:
		movem.l	(SP),d0-d2		; FORM, Länge und Type
		movem.l	d0-d2,(a2)		; IFF-Filetyp

.Error1:	move.l	d4,d1			; File
		movea.l	ib_DOSBase(a5),a6
		JSRLIB	Close

.Error2:	move.l	d5,d0			; Error ?
		beq.s	6$			; nein --->
		bsr	SetError		; Löscht auch D0
		bra.s	Ende
6$:
		bsr	ClearError
		move.l	a2,d0			; Adresse als Return-Code
Ende:
		lea	12(SP),SP		; Lokalen Speicher freigeben
		movem.l	(SP)+,d2-d6/a2/a5-a6
		rts

		END