/* errors.c - Output various error messages
** Copyright (C) 1997,1998 Karl J. Ots
** 
** 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/errors.h>
#include <devices/trackdisk.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>

#include "errors.h"
#include "main.h"
#include "zlib.h"


/*
** Output a DOS error value, as returned by IoErr(), as a text string
** to StdErr.
*/
void reportDOSError (LONG DOSError)
{
  char ErrorString[80];
  
  if (Fault (DOSError, "DOS Error", ErrorString, 79))
    FPrintf (StdErr, "%s.\n", ErrorString);
  else
    FPrintf (StdErr, "DOS Error: %ld.\n", DOSError);
}


/* 
** Output an IO Error, as returned by OpenDevice()/DoIO(), as a text 
** string to stderr.
*/
void reportIOError (BYTE IOError)
{
  STRPTR ErrStr;
  
  switch (IOError) {
  case IOERR_OPENFAIL:
    ErrStr = "Open Failure";
    break;
  case IOERR_ABORTED:
    ErrStr = "Aborted";
    break;
  case IOERR_NOCMD:
    ErrStr = "No Command";
    break;
  case IOERR_BADLENGTH:
    ErrStr = "Bad Length";
    break;
  case IOERR_BADADDRESS:
    ErrStr = "Bad Address";
    break;
  case IOERR_UNITBUSY:
    ErrStr = "Unit Busy";
    break;
  case IOERR_SELFTEST:
    ErrStr = "Self Test Failure";
    break;
  default:
    ErrStr = NULL;
  }
  
  FPuts (StdErr, "IO Error: ");
  if (ErrStr)
    FPuts (StdErr, ErrStr);
  else
    FPrintf (StdErr, "Unknown (%ld)", IOError);
  FPuts (StdErr, ".\n");
}


/*
** Output a TrackDisk IO Error, as returned by OpenDevice()/DoIO(),
** as a text string to stderr.
*/
void reportTDError (BYTE TDError)
{
  STRPTR ErrStr;

  /* Check to see if this is a `standard' error */
  if (TDError < 0)
  {
    reportIOError (TDError);
    return;
  }
  
  switch (TDError) {
  case TDERR_NotSpecified:
    ErrStr = "Not Specified";
    break;
  case TDERR_NoSecHdr:
    ErrStr = "No Sector Header";
    break;
  case TDERR_BadSecPreamble:
    ErrStr = "Bad Sector Preamble";
    break;
  case TDERR_BadSecID:
    ErrStr = "Bad Sector ID";
    break;
  case TDERR_BadHdrSum:
    ErrStr = "Bad Header Checksum";
    break;
  case TDERR_BadSecSum:
    ErrStr = "Bad Sector Checksum";
    break;
  case TDERR_TooFewSecs:
    ErrStr = "Too Few Sectors";
    break;
  case TDERR_BadSecHdr:
    ErrStr = "Bad Sector Header";
    break;
  case TDERR_WriteProt:
    ErrStr = "Write Protected";
    break;
  case TDERR_DiskChanged:
    ErrStr = "Disk Changed";
    break;
  case TDERR_SeekError:
    ErrStr = "Seek Error";
    break;
  case TDERR_NoMem:
    ErrStr = "No Memory";
    break;
  case TDERR_BadUnitNum:
    ErrStr = "Bad Unit Number";
    break;
  case TDERR_BadDriveType:
    ErrStr = "Bad Drive Type";
    break;
  case TDERR_DriveInUse:
    ErrStr = "Drive In Use";
    break;
  case TDERR_PostReset:
    ErrStr = "Post Reset";
    break;
  default:
    ErrStr = NULL;
  }
  
  FPuts (StdErr, "TrackDisk Error: ");
  if (ErrStr)
    FPuts (StdErr, ErrStr);
  else
    FPrintf (StdErr, "Unknown (%ld)", TDError);
  FPuts (StdErr, ".\n");
}


/*
** Output a Zlib error as a test string.
*/
void reportZLibError (LONG ZLibError)
{
  STRPTR ErrStr;
  
  switch (ZLibError) {
  case Z_ERRNO:
    ErrStr = "Error Number";
    break;
  case Z_STREAM_ERROR:
    ErrStr = "Stream Error";
    break;
  case Z_DATA_ERROR:
    ErrStr = "Data Error";
    break;
  case Z_MEM_ERROR:
    ErrStr = "Memory Error";
    break;
  case Z_BUF_ERROR:
    ErrStr = "Buffer Error";
    break;
  case Z_VERSION_ERROR:
    ErrStr = "Version Error";
    break;  
  default:
    ErrStr = NULL;
  }

  FPuts (StdErr, "ZLib Error: ");
  if (ErrStr)
    FPuts (StdErr, ErrStr);
  else
    FPrintf (StdErr, "Unknown (%ld)", ZLibError);
  FPuts (StdErr, ".\n");
}
