//
// _DBGWIN.CPP
//
//  Source file for ArchiveLib 2.0
//
//  Copyright (c) Greenleaf Software, Inc. 1994-1996
//  All Rights Reserved
//
// CONTENTS
//
//  _ALAssertFailure()
//
// DESCRIPTION
//
//  This file contains a support routine used by the assertion macros,
//
// REVISION HISTORY
//
//   May 22, 1994  1.0A  : First release
//
//   February 14, 1996  2.0A : New release
//

#include "arclib.h"
#if !defined( AL_IBM )
#pragma hdrstop
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

//
// NAME
//
//  _ALAssertFailure()
//
// PLATFORMS/ENVIRONMENTS
//
//  Windows
//  C++
//
// SHORT DESCRIPTION
//
//  Function used to print an assertion failure under Windows.
//
// C++ SYNOPSIS
//
//  #include "arclib.h"
//
//  void _ALAssertFailure( const char *condition,
//                         const char *filename,
//                         int line,
//                         const char *message,
//                         ... );
//
// C SYNOPSIS
//
//  None, this is an internal C++ support function.
//
// VB SYNOPSIS
//
//  None, this is an internal C++ support function.
//
// DELPHI SYNOPSIS
//
//  None, this is an internal C++ support function.
//
// ARGUMENTS
//
//  condition   :  A character string containing the condition that failed,
//                 leading to the assertion.
//
//  filename    :  The name of the file where the assertion error took place.
//
//  line        :  The line in the file where the assertion error took place.
//
//  message     :  The error message associated with the assertion error.
//                 This message is a sprintf() style format string.
//
//  ...         :  Any additional arguments.
//
// DESCRIPTION
//
//  The C run time library features an assert() macro, that can be used to
//  abort a program if a given condition isn't true.  It aborts the program
//  by calling a routine that looks something like this.  The AL_ASSERT()
//  macro that we use is even better, because it includes a comment
//  that gets displayed when the abort takes place.  This routine is
//  responsible for displaying that comment, along with the file name and
//  the line number, then aborting the program.  It is called by the
//  AL_ASSERT() macro when the conditional expression argument fails.
//
//  This is a special version of the function that gets called when the
//  program in question is running under Windows.  Instead of trying to
//  display the error message with a printf() statement, it formats it
//  and puts it into a message box.
//
// RETURNS
//
//  Nothing
//
// EXAMPLE
//
// SEE ALSO
//
// REVISION HISTORY
//
//   February 14, 1996  2.0A : New release
//

void AL_CFUNCTION
_ALAssertFailure( const char AL_DLL_FAR *condition,  /* Tag debug function */
                  const char AL_DLL_FAR *filename,
                  int line,
                  const char AL_DLL_FAR *message,
                  ... )
{
    char buf1[ 256 ];
    char buf2[ 128 ];
    va_list argptr;

    va_start( argptr, message );
#if defined( AL_BUILDING_DLL )
//
// Watcom is kind of annoying in that they format their variable arguments
// just a little differently than everyone else.
//
  #if defined( AL_WATCOM )
     wvsprintf( buf2, message, *argptr );
  #else
     wvsprintf( buf2, message, argptr );
  #endif
#else
     vsprintf( buf2, message, argptr );
#endif
     va_end( argptr );

     wsprintf
     ( buf1,
       "Assertion error, ArchiveLib is aborting the application.\n"
       "Condition = %s\n"
       "File = %s, line = %d\n"
       "%s",
       condition,
       filename,
       line,
       buf2 );
     MessageBox( 0,
                 buf1,
                 "                      "
                 "ArchiveLib assertion error"
                 "                      ",
                 MB_ICONSTOP );
     FatalAppExit( 0, "Application terminated" );
}