/* ======================================================================== *
	writecliptext.c - Subroutines to read and write text to the clipboard
							       By Talin.
				  Compiled under Manx 5.0 with small code/data
 * ======================================================================== */

#include <std_headers.h>
#include <libraries/iffparse.h>

extern struct Library *IFFParseBase;

#define LEAVE	goto exitit
#define FAIL(x)	{ error = x; goto exitit; }

int WriteClipText(char *text, int length)
{
	struct IFFHandle	*iff = NULL;
	long				error = FALSE;

	unless (iff = AllocIFF()) FAIL(-100);
	unless (iff->iff_Stream = (ULONG) OpenClipboard (PRIMARY_CLIP)) FAIL(-101);
	InitIFFasClip (iff);

	if (error = OpenIFF (iff, IFFF_WRITE)) LEAVE;

	if (error = PushChunk (iff, 'FTXT', 'FORM', IFFSIZE_UNKNOWN)) LEAVE;
	if (error = PushChunk (iff, 0L, 'CHRS', IFFSIZE_UNKNOWN)) LEAVE;

	if (WriteChunkBytes (iff, (APTR) text, length) != length)
		FAIL(IFFERR_WRITE);

	if (error = PopChunk (iff)) LEAVE;
	if (error = PopChunk (iff)) LEAVE;

exitit:
	if (iff)
	{	CloseIFF (iff);
		if (iff->iff_Stream)
			CloseClipboard ((struct ClipboardHandle *)iff->iff_Stream);
		FreeIFF (iff);
	}
	return error;
}

