/* Included Header Files
   -------------------------------------------------------------------------- */

#include "untitled.h"
#include <windows.h>


/* Function:    error
   Description: displays a detailed error message
   Parameters:  [const char *] description of the error
   Parameters:  [const char *] the file that the error occurred (use __FILE__)
   Parameters:  [int] line of the error in the file (use __LINE__)
   Returns:     [void]
   -------------------------------------------------------------------------- */

void error ( const char * description, const char * file, int line )
{
    #ifdef DEBUG
        fprintf(stderr, "\n\n   ----------------------------------------\n");
        fprintf(stderr, "   Error!\n");
        fprintf(stderr, "   Description:  %s\n", description);
        fprintf(stderr, "   File:  %s\n", file);
        fprintf(stderr, "   Line:  %d\n", line);
        fprintf(stderr, "   ----------------------------------------\n\n");
    #endif
}

void win_error ( const char * description, const char * file, int line )
{
    DWORD error_code;
    LPVOID message_buffer;
    char error_message[5000];

    /* get the error code and find the message associated with it */
    error_code = GetLastError();
    (void) FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &message_buffer, 0, NULL);

    /* display the error message */
    (void) sprintf(error_message, "--Description--\nerror %d: %s\n\n--Microsoft® Description--\n%s\n\nFILE: %s\nLINE: %d\n", (int) error_code, description, (LPSTR) message_buffer, file, line - 1);
    MessageBox(NULL, (LPCTSTR) error_message, TEXT("Error"), MB_TOPMOST | MB_OK | MB_ICONERROR);

    LocalFree(message_buffer);
}
