//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// 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.
//-----------------------------------------------------------------------------
// package	libh/avl
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// G. M. Adelson-Velskii and E. M. Landis are the inventors of the algorithm
// bearing their initials, AVL.  Their algorithm for height-balanced binary
// search trees is the basis of the code in this package.  Some modification
// is made to the balancing operations, and features are added for practical
// purposes.
//-----------------------------------------------------------------------------

#include "avl_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	avl_abort
//
// purpose	Abort program or process execution after outputting diagnostic
//		information about the cause for the problem.
//
// arguments	1 (AVL *) pointer to binary tree where problem was found
//		2 (avl_link *) pointer to link around which problem was found
//		3 (const char *) message format string
//		4 ... variable arguments for message
//
// returns	(int) 1 only if environment variable AVL_ABORT_BYPASS is set
//		otherwise does not return
//-----------------------------------------------------------------------------
int
avl_abort (
    AVL *		arg_tree
    ,
    avl_link *		arg_link
    ,
    const char *	arg_message
    ,
    ...
    )
__PROTO_END__
{
    va_list		arg_list		;

    const char *	avl_abort_bypass	;


    //-----------------------------------
    // Output formatted message if given.
    //-----------------------------------
    if ( arg_message ) {
	va_start( arg_list, arg_message );
	vfprintf( stderr, arg_message, arg_list );
	va_end( arg_list );
    }

    //---------------------------------
    // Output message identifying node.
    //---------------------------------
    if ( arg_link ) {
	avl_print_link_detail( stderr, arg_tree, arg_link );
    }

    //-------------------------
    // Run check on whole tree.
    //-------------------------
    avl_check_tree( arg_tree );

    //-----------------------------------
    // Decide whether to abort or return.
    //-----------------------------------
#ifndef AVL_ABORT_BYPASS
    avl_abort_bypass = getenv( "AVL_ABORT_BYPASS" );
    if ( avl_abort_bypass == NULL ||
	 ( avl_abort_bypass[0] != 0 &&
	   strtoul( avl_abort_bypass, NULL, 0 ) == 0 ) ) {
	abort();
    }
#endif /* AVL_ABORT_BYPASS */

    //-- Return if abort() was bypassed.
    return 1;
}

