//-----------------------------------------------------------------------------
// 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/map
// 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.
//-----------------------------------------------------------------------------

#include "map_sll.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_sll_fetch_cast
//
// purpose	Fetch the data from the current node in the mapping and return
//		directly as a value cast or converted to the type expected by
//		caller if the stored data value is a compatible type.  There
//		is no means to indicate an error.  Valid values may be changed
//		depending on the difference in sign and/or scale of the value
//		and the target type.  An error will result in the value 0.
//
// arguments	1 (MAP) mapping handle
//
// return	(signed long long) data value returned
//		(signed long long) 0 cast to the data type if error
//-----------------------------------------------------------------------------
signed long long
map_sll_fetch_cast (
    MAP			arg_map
    )
__PROTO_END__
{
    struct map_node_sll *	node_p	;

    //-- Make sure we have a mapping and a current node.
    if ( ! arg_map ) return (signed long long) 0;
    if ( ! ( node_p = avl_find( & (arg_map->avl_tree), NULL, 0 ) ) ) return (signed long long) 0;

    //-- Cast to correct type and return data value.
    switch ( node_p->data.type ) {
    case MAP_DATA_SI:
        return (signed long long) ( node_p->data.mix.scalar.si );
    case MAP_DATA_UI:
        return (signed long long) ( node_p->data.mix.scalar.ui );
#if ULONG_MAX>UINT_MAX
    case MAP_DATA_SL:
        return (signed long long) ( node_p->data.mix.scalar.sl );
    case MAP_DATA_UL:
        return (signed long long) ( node_p->data.mix.scalar.ul );
#endif
#if (defined(ULLONG_MAX)&&(ULLONG_MAX>ULONG_MAX)) || (defined(ULONG_LONG_MAX)&&(ULONG_LONG_MAX>ULONG_MAX))
    case MAP_DATA_SLL:
        return (signed long long) ( node_p->data.mix.scalar.sll );
    case MAP_DATA_ULL:
        return (signed long long) ( node_p->data.mix.scalar.ull );
#endif
    case MAP_DATA_D:
        return (signed long long) ( node_p->data.mix.scalar.d );
#if LDBL_MANT_DIG!=DBL_MANT_DIG || LDBL_MAX_EXP!=DBL_MAX_EXP
    case MAP_DATA_LD:
        return (signed long long) ( node_p->data.mix.scalar.ld );
#endif
    default:
        return (signed long long) 0;
    }
}

