// utf-8

/*	Copyright  (c)	Günter Woigk   1999-2003
  					mailto:kio@little-bat.de

	This file is free software

 	This program is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

 	Permission to use, copy, modify, distribute, and sell this software and
 	its documentation for any purpose is hereby granted without fee, provided
 	that the above copyright notice appear in all copies and that both that
 	copyright notice and this permission notice appear in supporting
 	documentation, and that the name of the copyright holder not be used
 	in advertising or publicity pertaining to distribution of the software
 	without specific, written prior permission. The copyright holder makes no
 	representations about the suitability of this software for any purpose.
 	It is provided "as is" without express or implied warranty.

 	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 	PERFORMANCE OF THIS SOFTWARE.

 
	display alert + log + abort
	display alert + log
	display alert + confirm
	---------------------------
	

	01.jan.1999	kio	first work on this file
	19.mar.1999	kio	added license
	25.aug.2001 kio	added option for Abort() to suspend/terminate thread instead of quit appl
	05.feb.2002 kio	cocoa version
	2002-03-02	kio	classic/cocoa/unix stuff
	2002-09-15	kio	put _USE_KIOS_THREADS arounf thread stuff
*/

#define	SAFE	3
#define	LOG		0


#include	"kio.h"

#if defined(_USE_KIOS_THREADS)
#include	"disp/disp.h"
#endif



/* ----	display an alert box with replacement texts -------------------------
*/
#if defined(_MACINTOSH)
	int ShowAlert ( short n, cstr s, cstr t, cstr u, cstr v )
	{
		Host::Init();	// assure basic initialization: InitGraf(QDPort) ... InitDialogs()
		
		uchar a[256], b[256], c[256], d[256];
	
		a[0] = s?strlen(s):0; memcpy(a+1,s,a[0]); 
		b[0] = t?strlen(t):0; memcpy(b+1,t,b[0]); 
		c[0] = u?strlen(u):0; memcpy(c+1,u,c[0]); 
		d[0] = v?strlen(v):0; memcpy(d+1,v,d[0]);
	
		SetCursor ( &qd.arrow );
		ParamText ( a,b,c,d );
		return Alert(n,NULL);
	}
#endif



/* ----	display an alert and quit application -------------------------------
		Print error message via Log(),
		display a requester and abort.
*/
#if defined(_MACINTOSH)
	#if defined(_USE_KIOS_THREADS)

		void Abort ( cstr t, cstr u, cstr v )
		{
		a:	char s[64]={0};
			if (MyTid()) { strncpy(s,MyTid()->Name(),32); s[32]=0; }
			strcat(s,": Sorry, a fatal error occured:");
		
			LogLine(s); 
			if(t) LogLine(t); 
			if(u) LogLine(u); 
			if(v) LogLine(v); 
			Log("");
		
		// -> hint for debugging: put a break point here <-
		
			if (!MyTid()||MyTid()==RootTid())
			{
				ShowAlert(alrt_alert,s,t,u,v);
			}
			else
			{
				int n = ShowAlert(alrt_susquit,s,t,u,v);
				switch(n)
				{
				case 3:		Suspend();
							t="Thread was suspended due to an error; has been resumed!";
							u=v=NULL;
							goto a;
				case 2:		Termi();
			//	case 1:		quit
				}
			}
			ExitToShell();
		}

	#else /* don't _USE_KIOS_THREADS */

		void Abort ( cstr t, cstr u, cstr v )
		{
			LogLine("Sorry, a fatal error occured:"); 
			if(t) LogLine(t); if(u) LogLine(u); if(v) LogLine(v); Log("");
		
		// -> hint for debugging: put a break point here <-
		
			ShowAlert(alrt_alert,"Sorry, a fatal error occured:",t,u,v);
			ExitToShell();
		}

	#endif

#elif defined(_UNIX)||defined(_COCOA)

	void Abort ( cstr t, cstr u, cstr v  )
	{
		fputs("\nSorry, a fatal error occured:\n",stderr);
		if(t) fputs(t,stderr);
		if(u) fputs(u,stderr);
		if(v) fputs(v,stderr);
		fputs("\n",stderr);
		abort();
	}

#else
	#error   platform not yet supported
#endif



/* ----	display an alert and quit application -------------------------------
		Print file name, line number and message via Log()
		display a requester and abort.
*/
void Abort ( cstr file, long line, cstr text )
{
	char t[30]; sprintf(t," in line %ld: ",line);
	Abort(file,t,text);
}



