/* $Revision Header * Header built automatically - do not edit! *************
 *
 *	(C) Copyright 1991 by Olaf 'Olsen' Barthel & MXM
 *
 *	Name .....: TermClip.c
 *	Created ..: Monday 21-Jan-91 20:12
 *	Revision .: 0
 *
 *	Date            Author          Comment
 *	=========       ========        ====================
 *	21-Jan-91       Olsen           Created this file!
 *
 * $Revision Header ********************************************************/

#include "TermGlobal.h"

	/* SaveClip(UBYTE *Buffer,LONG Size):
	 *
	 *	Send a given text buffer to the clipboard.
	 */

BYTE
SaveClip(UBYTE *Buffer,LONG Size)
{
	struct IFFHandle	*Handle;
	BYTE			 Success = FALSE;

	if(Handle = AllocIFF())
	{
		if(Handle -> iff_Stream = (ULONG)OpenClipboard(PRIMARY_CLIP))
		{
			InitIFFasClip(Handle);

			if(!OpenIFF(Handle,IFFF_WRITE))
			{
				if(!PushChunk(Handle,'FTXT','FORM',IFFSIZE_UNKNOWN))
				{
					if(!PushChunk(Handle,0,'CHRS',IFFSIZE_UNKNOWN))
					{
						if(WriteChunkBytes(Handle,Buffer,Size) == Size)
						{
							if(!PopChunk(Handle))
								Success = TRUE;
						}
					}
				}

				if(Success)
				{
					if(PopChunk(Handle))
						Success = FALSE;
				}

				CloseIFF(Handle);
			}

			CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
		}

		FreeIFF(Handle);
	}

	return(Success);
}

	/* LoadClip(UBYTE *Buffer,LONG Size):
	 *
	 *	Put the contents of the clipboard into a given
	 *	buffer.
	 */

LONG
LoadClip(UBYTE *Buffer,LONG Size)
{
	struct IFFHandle	*Handle;
	LONG			 Bytes = 0;

	if(Handle = AllocIFF())
	{
		if(Handle -> iff_Stream = (ULONG)OpenClipboard(PRIMARY_CLIP))
		{
			InitIFFasClip(Handle);

			if(!OpenIFF(Handle,IFFF_READ))
			{
				if(!StopChunk(Handle,'FTXT','CHRS'))
				{
					if(!ParseIFF(Handle,IFFPARSE_SCAN))
					{
						struct ContextNode *ContextNode;

						if(ContextNode = CurrentChunk(Handle))
						{
							if(Size > ContextNode -> cn_Size)
								Size = ContextNode -> cn_Size;

							if(ReadChunkRecords(Handle,Buffer,Size,1))
								Bytes = Size;
						}
					}
				}

				CloseIFF(Handle);
			}

			CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
		}

		FreeIFF(Handle);
	}

	return(Bytes);
}
