* Force both PushChunk things to fail, to see if CloseIFF works.

*=- INFO -=****************************************************************************
* Routines for reading from/writting to the clipboard.				      *
* Unless otherwise stated, all routines are by Leo Davidson (Pot-Noodle of Gods'Gift) *
*=- Notes -===========================================================================*
* o Includes routines for handling IFF files. These may be moved at some point.	      *
*=- To Do -===========================================================================*
***************************************************************************************


***************************************************************************************
* Clipboard Subroutines								      *
***************************************************************************************
;Include Asm:Source/Routines/Clipboard.s


*= Write To Clipboard =************************************************= 25-Aug-1995 =*
*  Inputs: (a1) - Text to put into clipboard.					      *
*           d0  - Length of (a1).						      *
* Outputs: Text is written to the clipbaord (IFF-Text/FTXT format).		      *
*   Notes: Failure will abort the program.					      *
***************************************************************************************
WriteToClip
	Movem.l	a1/d0,-(SP)			Preserve buffer/size.

	Bsr	AllocNudelIFF			Allocate IFFHandle.
	Move.l	NudelIFFHandle(a5),a0		-._ Initialise it for
	N_CallIFFParse	InitIFFasClip		-'  the clipboard.
	Bsr	OpenNudelClipboard		Open the clipboard and setup stream.
	Moveq	#IFFF_WRITE,d0			-._ Open the IFFHandle,
	Bsr	OpenNudelIFF			-'  in WRITE mode.

	Move.l	NudelIFFHandle(a5),a0		IFFHandle,
	Move.l	#"FTXT",d0			Type,
	Move.l	#ID_FORM,d1			id,
	Moveq	#IFFSIZE_UNKNOWN,d2		Let it handle the size itself.
	N_CallIFFParse	PushChunk		Push an IFF-Text chunk.
	Tst.l	d0
	Bne	PushChunk_Error_1

	Move.l	NudelIFFHandle(a5),a0		IFFHandle,
	Moveq	#0,d0				Type,
	Move.l	#"CHRS",d1			id,
	Moveq	#IFFSIZE_UNKNOWN,d2		Let it handle the size itself.
	N_CallIFFParse	PushChunk		Push an IFF-Text chunk.
	Tst.l	d0
	Bne	PushChunk_Error_2

	Move.l	NudelIFFHandle(a5),a0		IFFHandle.
	Movem.l	(SP)+,a1/d0			Restore buffer/size.
	N_CallIFFParse	WriteChunkBytes		Write buffer to clipboard.
	Tst.l	d0				-._ If negative result,
	Blt.s	WriteChunkBytes_Error		-'  call special error routine.

	Move.l	NudelIFFHandle(a5),a0		-._ Pop the previously
	N_CallIFFParse	PopChunk		-'  pushed chunk.
	Tst.l	d0
	Bne_ErrorE_Int	ErrAct_IFFPopChunk(pc),#0

	Move.l	NudelIFFHandle(a5),a0		-._ Pop the previously
	N_CallIFFParse	PopChunk		-'  pushed chunk.
	Tst.l	d0
	Bne_ErrorE_Int	ErrAct_IFFPopChunk(pc),#0

	Bsr	CloseNudelIFF		Close our IFFhandle.
	Bsr	FreeNudelIFF		Free our IFFHandle.
	Bsr	CloseNudelClipboard	Close the Clipboard.

	RTS

WriteChunkBytes_Error
	Move.l	NudelIFFHandle(a5),a0		-._ Pop the previously
	N_CallIFFParse	PopChunk		-'  pushed chunk.
;;;;;;;	Ignore any error.
	Bra_ErrorE_Int	ErrAct_IFFWriteChunkBytes(pc),#0	Fatal error.

PushChunk_Error_2
	Move.l	NudelIFFHandle(a5),a0		-._ Pop the previously
	N_CallIFFParse	PopChunk		-'  pushed chunk.
;;;;;;;	Tst.l	d0
;;;;;;;	Bne_ErrorE_Int	ErrAct_IFFPopChunk(pc),#0
PushChunk_Error_1
	Bra_ErrorE_Int	ErrAct_IFFPushChunk(pc),#0



ErrAct_IFFPushChunk
	Dc.b	"PushChunk() failed",0
ErrAct_IFFPopChunk
	Dc.b	"PopChunk() failed",0
ErrAct_IFFWriteChunkBytes
	Dc.b	"WriteChunkBytes() failed",0
	Even


*= Allocate Nudel IFF =************************************************= 25-Aug-1995 =*
*  Inputs: NudelIFFHandle(a5) - If non-null, Internal error.			      *
* Outputs: NudelIFFHandle(a5) - Newly allocated IFFHandle.			      *
*   Notes: Errors handled automatically and will abort the program.		      *
***************************************************************************************
AllocNudelIFF
	Tst.l	NudelIFFHandle(a5)		-._ If previous use of handle
	Bne	Internal			-'  not freed, internal error.
	N_CallIFFParse	AllocIFF		Allocate IFFHandle.
	Move.l	d0,NudelIFFHandle(a5)		Store it.
	Beq_ErrorE_Int	ErrAct_AllocIFFHandle(pc),#0
	RTS
ErrAct_AllocIFFHandle
	Dc.b	"Could not allocate IFFHandle",0
	EVEN


*= Free Nudel IFF =****************************************************= 25-Aug-1995 =*
*  Inputs: NudelIFFHandle(a5) - IFFHandle to free.				      *
* Outputs: NudelIFFHandle(a5) - NULL						      *
*   Notes: Does not check that the structure has been closed with CloseIFF()	      *
***************************************************************************************
FreeNudelIFF
	Tst.l	NudelIFFHandle(a5)		-._ If no handle was allocated,
	Beq.s	FreeNudelIFF_Skip		-'  don't free anything.
	Move.l	NudelIFFHandle(a5),a0		-._ Else, free
	N_CallIFFParse	FreeIFF			-'  NudelIFFHandle.
	Move.l	#0,NudelIFFHandle(a5)		NULL handle for future use.
FreeNudelIFF_Skip
	RTS


*= Open Nudel Clipboard =**********************************************= 25-Aug-1995 =*
*  Inputs: NudelIFFHandle(a5) - Init'd IFFHandle to point to clipboard stream.	      *
* Outputs: NudelIFFHandle(a5) - iff_stream points to clipboard handle.		      *
*          NudelClipboardHandle(a5) - ClipboardHandle for closing later.	      *
*   Notes: Always opens the 'primary' clipboard device, #0.			      *
***************************************************************************************
OpenNudelClipboard
	Tst.l	NudelClipboardHandle(a5)	-._ If previous ClipboardHandle
	Bne	Internal			-'  not freed, internal error.

	Moveq	#PRIMARY_CLIP,d0		Want to open 'primary' unit (#0).
	N_CallIFFParse	OpenClipboard		Open the clipboard.
	Move.l	d0,NudelClipboardHandle(a5)	Store the handle.
	Beq_ErrorE_Int	ErrAct_OpenClipboard(pc),#0

	Move.l	NudelIFFHandle(a5),a0		-._ Setup stream
	Move.l	d0,iff_Stream(a0)		-'  in IFFHandle.

	RTS
ErrAct_OpenClipboard
	Dc.b	"Could not open Clipboard",0
	Even


*= Close Nudel Clipboard =*********************************************= 25-Aug-1995 =*
*  Inputs: NudelClipboardHandle(a5) - ClipboardHandle for closing.		      *
* Outputs: NudelClipboardHandle(a5) - NULL.					      *
*   Notes: NudelIFFHandle(a5)/iff_Stream is *NOT* altered and thus WILL be invalid    *
*	   after this routine if it was pointing to NudelClipboardHandle. This	      *
*	   routine should *only* be used after the IFFHandle has been closed first.   *
***************************************************************************************
CloseNudelClipboard
	Tst.l	NudelClipboardHandle(a5)	-._ If handle isn't open,
	Beq.s	CloseNudelClipboard_Skip	-'  skip the routine.
	Move.l	NudelClipboardHandle(a5),a0	-._ Close the
	N_CallIFFParse	CloseClipboard		-'  clipboard.
	Move.l	#0,NudelClipboardHandle(a5)	Null handle for future use.
CloseNudelClipboard_Skip
	RTS


*= Open NudelIFF =*****************************************************= 25-Aug-1995 =*
*  Inputs: NudelIFFHandle(a5) - Init'd IFFHandle, including iff_Stream		      *
*	   d0 - Mode to open with, either #IFFF_READ or #IFFF_WRITE.		      *
* Outputs: IFFHandle is open.							      *
*	   SF1_OpenIFF,STD_F_1(a5) - Set (1)					      *
***************************************************************************************
OpenNudelIFF
	BTst	#SF1_OpenIFF,STD_F_1(a5)	-._ If the handle is already
	Bne	Internal			-'  open, internal error.
	Move.l	NudelIFFHandle(a5),a0		IFFHandle to open.
;;;;;;;	Move.l	d0,d0				Mode to open with.
	N_CallIFFParse	OpenIFF			OpenIFF()
	Tst.l	d0
	Bne_ErrorE_Int	ErrAct_OpenIFF(pc),#0	#0 = SUCCESS, _NOT_ FAILURE.
	BSet	#SF1_OpenIFF,STD_F_1(a5)	Mark it as open.
	RTS
ErrAct_OpenIFF
	Dc.b	"Could not OpenIFF()",0
	Even


*= Close NudelIFF =****************************************************= 25-Aug-1995 =*
*  Inputs: NudelIFFHandle(a5) - Open IFFHandle.					      *
*	   SF1_OpenIFF,STD_F_1(a5) - Routine skipped if clear (0)		      *
* Outputs: NudelIFFHandle(a50 - Closed IFFHandle.				      *
*	   SF1_OpenIFF,STD_F_1(a5) - Cleared (0)				      *
***************************************************************************************
CloseNudelIFF
	BClr	#SF1_OpenIFF,STD_F_1(a5)	-._ Mark as closed,
	Beq.s	CloseNudelIFF_Skip		-'  and skip if no open.
	Move.l	NudelIFFHandle(a5),a0		Handle for closure.
	N_CallIFFParse	CloseIFF		Close it.
CloseNudelIFF_Skip
	RTS
