/**************************************************************/
/**  C++ code Copyright (C) Nick V. King <nking@eworld.com>  **/
/**************************************************************/

#include <ids_mac.h>

void _FE(const char* cp, const char* file, const int line)
{ 
	extern GDHandle currentDevice;
	extern WindowPtr mainWindow;

	SetGWorld((CGrafPtr)mainWindow, currentDevice);
	Str255 theFunction, theFile, theLine;
	BlockMove(cp, theFunction, maxStringLength);
	c2pstr((char*)theFunction);
	BlockMove(file, theFile, maxStringLength);
	c2pstr((char*)theFile);
	NumToString(line, theLine);
	ParamText(theFunction, theFile, theLine, "\p");
	Alert(fatalErrorAlertID, 0L);
	ExitToShell();
}

void _NFE(const char* cp, const char* file, const int line)
{
	GDHandle saveDevice;
	CGrafPtr savePort;
	extern GDHandle currentDevice;
	extern WindowPtr mainWindow;

	GetGWorld(&savePort, &saveDevice);
	SetGWorld((CGrafPtr)mainWindow, currentDevice);
	Str255 theFunction, theFile, theLine;
	BlockMove(cp, theFunction, maxStringLength);
	c2pstr((char*)theFunction);
	BlockMove(file, theFile, maxStringLength);
	c2pstr((char*)theFile);
	NumToString(line, theLine);
	ParamText(theFunction, theFile, theLine, "\p");
	Alert(nonFatalErrorAlertID, 0L);
	SetGWorld(savePort, saveDevice);
}

void _CST(const unsigned long size, const unsigned long number,
const char* file, const int line)
{ if (size * number > 65000L) then
    _FE("Attempt to allocate > 64K", file, line);
}

char* ltoa(long value, char* str, int radix)
{
	if (radix == 10)
		sprintf(str, "%d", value); 
	else if (radix == 8)
		sprintf(str, "%o", value);
	return str;
}

void yield(void)
{
	extern Boolean backgroundingEnabled;
	
	if (backgroundingEnabled)
	{
		GDHandle saveDevice;
		CGrafPtr savePort;
		extern GDHandle currentDevice;
		extern WindowPtr mainWindow;
		
		GetGWorld(&savePort, &saveDevice);
		SetGWorld((CGrafPtr)mainWindow, currentDevice);
		YieldToAnyThread();
		SetGWorld(savePort, saveDevice);
	}
}

void write_binary(FILE* file, char data)
{
	fwrite(&data, sizeof(char), 1, file);
}

void write_binary(FILE* file, byte data)
{
	fwrite(&data, sizeof(byte), 1, file);
}

void write_binary(FILE* file, int16 data)
{
	fwrite(&data, sizeof(int16), 1, file);
}

void write_binary(FILE* file, uint16 data)
{
	fwrite(&data, sizeof(uint16), 1, file);
}

void write_binary(FILE* file, uint32 data)
{
	fwrite(&data, sizeof(uint32), 1, file);
}

/***********/
/**  End  **/
/***********/