/* PR-QWK - QWK Importer for UMS
 * Copyright (C) 1998 J.Ross Nicoll
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <exec/types.h>

#include <exec/memory.h>
#include <libraries/ums.h>

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/ums.h>

#include <stdio.h>
#include <string.h>

#define CATCOMP_NUMBERS
#include "locale.h"
#include "pr-qwk.h"

#define Prototype extern
#include "PR-QWK-protos.h"
Prototype BOOL HandleUMSError(VOID);
Prototype VOID DisplayError(char *, ULONG, ULONG);
Prototype VOID HandleIOError(ULONG);
Prototype VOID HandleMemoryError(ULONG, ULONG);

BOOL HandleUMSError(VOID)
{
	BOOL errorRet = TRUE;
	STRPTR errorTxt;
	UMSError errorNum;

	errorNum = UMSErrNum(Account);
	if(errorNum != 0)
	{
		errorTxt = UMSErrTxt(Account);

		if(errorNum < 200)
		{
			/* The compiler makes this a long to put the pointer onto a long
			 * boundary, this just makes it clearer.
			 */
			SPrintF(FormatBuffer, GetString(ERRMSG_UMS_ERROR), (LONG)errorNum, errorTxt);
		}
		else
		{
			errorRet = FALSE;
			if(errorNum < 300)
			{
				SPrintF(FormatBuffer, GetString(ERRMSG_SERIOUS_UMS_ERROR), (LONG)errorNum, errorTxt);
			}
			else
			{
				FatalUMSError = TRUE;
				SPrintF(FormatBuffer, GetString(ERRMSG_FATAL_UMS_ERROR), (LONG)errorNum, errorTxt);
			}
		}

   	DisplayError(FormatBuffer, REQMSG_MISC_GADGET_FORMAT, REQMSG_UMS_TITLE);
	}

	return(errorRet);
}

/* ---------------------------------------------------------------------- */

/* Displays an error to the user, either using EasyRequest, or by
 * outputting TextFormat.
 */
VOID DisplayError(char *TextFormat, ULONG GadgetFormat, ULONG Title)
{
   if(IntuitionBase != NULL)
   {
      EasyStruct.es_TextFormat = TextFormat;
      EasyStruct.es_GadgetFormat = GetString(GadgetFormat);
      EasyStruct.es_Title = GetString(Title);

      EasyRequest(NULL, &EasyStruct, &IDCMPFlags);
   }
   else
   {
      PutStr(TextFormat);
      FPutC(OutputHandle, '\n');
   }

   return;
}

/* ---------------------------------------------------------------------- */

/* Handles a DOS error. FunctionName is the name of the function called that returned
 * an error.
 */
VOID HandleIOError(ULONG FunctionName)
{
   if(Fault(IoErr(), GetString(FunctionName), FormatBuffer, 512) != DOSFALSE)
   {
      DisplayError(FormatBuffer, REQMSG_MISC_GADGET_FORMAT, REQMSG_IO_TITLE);
   }
   else
   {
      PutStr(GetString(ERRMSG_DISPLAYING_ERROR));
   }

   return;
}

/* ---------------------------------------------------------------------- */

/* Handles a memory allocation error. Length is the size of the memory that
 * the program was trying to allocate, for is the purpose for which it wanted
 * the memory.
 */
VOID HandleMemoryError(ULONG Length, ULONG For)
{
   char textLength[11];
   ULONG largestMem;
   ULONG reason;
   ULONG totalMem;

   largestMem = AvailMem(MEMF_PUBLIC | MEMF_LARGEST);
   totalMem = AvailMem(MEMF_PUBLIC);

   if(Length == -1)
   {
      strcpy(textLength, "?");
      reason = ERRMSG_UNSURE_REASON;
   }
   else
   {
      SPrintF(textLength, "%ld", Length);
      if(Length == 0)
      {
         reason = ERRMSG_ALLOCATE0;
      }
      else
      {
         if(Length <= largestMem)
         {
            reason = ERRMSG_UNKNOWN_REASON;
         }
         else
         {
            if(Length > totalMem)
            {
               reason = ERRMSG_INSUFFICIENT_FREE_MEM;
            }
            else
            {
               reason = ERRMSG_INSUFFICIENT_CONT_MEM;
            }
         }
      }
   }

   SPrintF(FormatBuffer, GetString(REQMSG_MEMORY_TEXT_FORMAT), textLength, GetString(For), GetString(reason));
   DisplayError(FormatBuffer, REQMSG_MISC_GADGET_FORMAT, REQMSG_MEMORY_TITLE);

   return;
}
