

#if _WINDOWS

#include "StdHeaders.h"
#include "Debug.h"

void Debug(const char * /*format*/, ...){}


#else


//============================================================================
//
//	This is the Think C 6.0 code to send a message to the DebugWindow server
//	(in other words, this is the code to DebugWindow.Lib that came with your
//	DebugWindow package).  I'm providing this code to document the AppleEvent
//	procedures necessary to communicate with DebugWindow; those of you who
//	need a specialized version, or need to port it to another environment
//	(such as Pascal or MacApp) should find everything that you need to know
//	right here.
//
//	If you implement this into a new environment, I'd appreciate it if you
//	could send me the new modules so that I can include them with the next
//	release of DebugWindow (with the proper credits going to you, of course!).
//
//
//	Here are the necessary steps to send a string to DebugWindow:
//
//		¥ create an AppleEvent for signature 'LdbW' with a type of 'misc/dmsg'
//
//		¥ add a parameter of type 'keyDirectObject/typeChar' passing a pointer
//		  to the string to display and its length
//
//		¥ send it on its way
//
//============================================================================


#include	<stdio.h>
#include	<stdarg.h>
#include	<string.h>
#if _WINDOWS
	#include	<AppleEvents.h>
	#include	<Gestalt.h>
	#include	<Errors.h>
#else
	#include <Carbon/Carbon.h>
#endif

#if __MWERKS__
#undef OLDROUTINELOCATIONS
#define OLDROUTINELOCATIONS 0
#include <TextUtils.h>
#endif


#if THINK_C || __SC__
#include <pascal.h>
#define c2pstr CtoPstr
#endif

#include "Debug.h"


void __SendToDebugWindow ( const char *stringToSend );


void debugcstr(const char* errStr)
{
	Str255 ps;
	strncpy((char*)ps, errStr, sizeof(Str255));
	
	// GS_CARBON_REVISIT - is this CopyCStringToPascal call correct?? - PCP
	CopyCStringToPascal((char*)ps, ps);
	DebugStr(ps);
}

void Debug ( const char *format, ... )
{
	va_list		argptr;
	long		len;
	char		tDebugString[512];

	va_start ( argptr, format );
	len = (long)vsprintf ( tDebugString, format, argptr );
	va_end ( argptr );
	if ( len > 0 )
		__SendToDebugWindow ( tDebugString );
}




void ClearDebugWindow ()
{
	__SendToDebugWindow ( "~c" );
}





void DebugTimestamp ()
{
	__SendToDebugWindow ( "~t" );
}






void __SendToDebugWindow ( const char *stringToSend )
{
	AEAddressDesc	address;
	AppleEvent		appleEvent;
	AppleEvent		reply;
	OSType			targetSig;
	long			gestaltResponse;
	short			appleEventsAvailable;
	
	targetSig = 'LdbW';
	
	if(Gestalt(gestaltAppleEventsAttr, &gestaltResponse) != gestaltUndefSelectorErr) {
		appleEventsAvailable = gestaltResponse & 1<<gestaltAppleEventsPresent;
		}
	else
		appleEventsAvailable = false;
		
	
	if (appleEventsAvailable) {
		if ( AECreateDesc ( typeApplSignature, (Ptr)&targetSig, 
							sizeof targetSig, &address ) == 0 ) {
			if ( AECreateAppleEvent ( 'misc', 'dmsg', &address, kAutoGenerateReturnID,
									   kAnyTransactionID, &appleEvent ) == 0 ) {
	
				if ( AEPutParamPtr ( &appleEvent, keyDirectObject, typeChar,
									 stringToSend, strlen (stringToSend) ) == 0 ) {
	
					AESend ( &appleEvent, &reply, 
							 kAEWaitReply + kAENeverInteract,
							 kAENormalPriority, 
							 300, 								// up to 5 second wait..
							 nil, nil );
					
					AEDisposeDesc ( &reply );
				}
				AEDisposeDesc ( &appleEvent );
			}
			AEDisposeDesc ( &address );
		}
	}
}


// make the new Metrowerks MSL libraries happy & provide
// the console routines they need

// from MSL console.stubs.c
#if 0
extern "C" {

	short InstallConsole(short fd);
	void RemoveConsole(void);
	long WriteCharsToConsole(char *buffer, long n);
	long ReadCharsFromConsole(char *buffer, long n);


	short InstallConsole(short fd)
	{
	#pragma unused (fd)

		return 0;
	}

	void RemoveConsole(void)
	{
	}

	long WriteCharsToConsole(char *buffer, long n)
	{
	#pragma unused (buffer, n)

		return 0;
	}

	long ReadCharsFromConsole(char *buffer, long n)
	{
	#pragma unused (buffer, n)

		return 0;
	}
}
#endif

#endif
