implementation module scrapuse;

import	StdClass, StdInt, StdChar, StdString, StdArray,StdBool;
import	memory;
from	ioState import ::IOState, IOStateGetToolbox, IOStateSetToolbox;
import	scrap;

import StdMisc;

/* for carbon: 

IOPutScrap :: !{#Char} !(IOState s) -> IOState s;
IOPutScrap string io
	# (tb,io) = IOStateGetToolbox io;
	# (r,tb) = ClearCurrentScrap tb;
	| r<>0
		= IOStateSetToolbox tb io;
	# (r,scrap_ref,tb) = GetCurrentScrap tb;
	| r<>0
		= IOStateSetToolbox tb io;
	# (r,tb) = PutScrapFlavor scrap_ref ScrapFlavorTypeText ScrapFlavorMaskNone string tb;
	= IOStateSetToolbox tb io;

IOGetScrap :: !(IOState s) -> (!{#Char},!IOState s);
IOGetScrap io
	# (tb,io) = IOStateGetToolbox io;
	# (r,scrap_ref,tb) = GetCurrentScrap tb;
	| r<>0
		= ("",IOStateSetToolbox tb io);
	# (r,flags,tb) = GetScrapFlavorFlags scrap_ref ScrapFlavorTypeText tb;
	| r<>0
		= ("",IOStateSetToolbox tb io);
	# (r,size,tb) = GetScrapFlavorSize scrap_ref ScrapFlavorTypeText tb;
	| r<>0
		= ("",IOStateSetToolbox tb io);
	# (r,size,data,tb) = GetScrapFlavorData scrap_ref ScrapFlavorTypeText size tb;
	| r<>0
		= ("",IOStateSetToolbox tb io);
		= (data,IOStateSetToolbox tb io);
*/

IOPutScrap :: !{#Char} !(IOState s) -> IOState s;
IOPutScrap string io
	= io2;
	{
		(tb,io1)	= IOStateGetToolbox io;
		(_,tb1)		= ZeroScrap tb;
		(_,tb2)		= PutScrapText string tb1;
		io2			= IOStateSetToolbox tb2 io1;
	}

IOGetScrap :: !(IOState s) -> (!{#Char},!IOState s);
IOGetScrap io
	| handle==0
		= ("",IOStateSetToolbox tb0 io1);
	| size<=0
		= ("",IOStateSetToolbox tb2 io1);
		{
			(_,tb2)	= DisposHandle handle tb1;
		}
		= (string,IOStateSetToolbox tb3 io1);
		{
			(string,tb2)= handle_to_string handle size tb1;
			(_, tb3)	= DisposHandle handle tb2;
		}
	{
		(tb,io1)		= IOStateGetToolbox io;
		(handle,_,tb0)	= NewHandle 0 tb;
		(size,_,  tb1)	= GetScrap handle TextResourceType tb0;
	}

handle_to_string :: !Handle !Int !*Toolbox -> (!{#Char},!*Toolbox);
handle_to_string handle size tb0
	=	(string,tb1);
	{
		tb1		= copy_handle_data_to_string string handle size tb0;
		string	= createArray size ' ';
	}
