/* errors.c */

#include <stdlib.h>
#include <string.h>
#include "errors.h"
#include "protos.h"

Prototype void err_seterror (int errnum);
Prototype void err_seterror2 (char *str);
Prototype void err_showerror (void);

char Error1[256] = "", Error2[256] = "";

/// err_seterror
/* -------------------------------- err_seterr ---------------------------------

 Comment: Set the primary error fault

*/

void err_seterror (int errnum)
{
    char *msg;

    switch (errnum) {
	case ERR_OUT_OF_MEMORY: msg = "Out of memory"; break;
	case ERR_OTHER: msg = "Unknown error"; break;
	case ERR_FILE_OPEN: msg = "Error while opening file"; break;
	case ERR_FILE_READ: msg = "Error while reading from file"; break;
	case ERR_FILE_WRITE: msg = "Error while writing to file"; break;
	case ERR_FILE_OTHER: msg = "Unknown file I/O error"; break;
	case ERR_GUI_OPEN_WINDOW: msg = "Couldn't open window"; break;
	case ERR_GUI_OPEN_SCREEN: msg = "Couldn't open screen"; break;
	default: msg = "Unknown error (internal error)"; break;
    }

    strcpy (Error1, msg);
    Error2[0] = 0;
    return;
}
///
/// err_seterror2
/* ------------------------------- err_seterror2 -------------------------------

 Comment:

*/

void err_seterror2 (char *msg)
{
    strcpy (Error2, msg);
}
///
/// err_showerror
/* ------------------------------- err_showerror -------------------------------

 Comment: alert the user to the last error condition

*/

void err_showerror (void)
{
    if (Error1[0] && !Error2[0]) {
	AlertUser ("Error: %s", "_OK", Error1);
    } else if (Error1[0] && Error2[0]) {
	AlertUser ("Error: %s\n(%s)", "_OK", Error1, Error2);
    }
}
///
