//-----------------------------------------------------------------------------
// 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 112 columns wide.
//-----------------------------------------------------------------------------

#include "map_ssa.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_sss_fetch_copy
//
// purpose	Fetch the data from the current member in the mapping.
//
// arguments	1 (MAP) mapping handle
//		2 (signed short *) pointer where to store data
//		3 (size_t) maximum length to store
//
// returns	(size_t) ~0U = error, no map, no member, bad type
//		(size_t) length of full string (copy may be truncated)
//-----------------------------------------------------------------------------
#define map_sss_fetch_copy map_ssa_fetch_copy
__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_ssa_fetch_copy
//
// purpose	Fetch the data from the current member in the mapping.
//
// arguments	1 (MAP) mapping handle
//		2 (signed short *) pointer where to store data
//		3 (size_t) maximum length to store
//
// returns	(size_t) ~0U = error, no map, no member, bad type
//		(size_t) length of full string (copy may be truncated)
//-----------------------------------------------------------------------------
size_t
map_ssa_fetch_copy (
    MAP			arg_map
    ,
    signed short *		arg_data_ptr
    ,
    size_t		arg_data_len
   )
__PROTO_END__
{
    struct map_node_ssa *	node_p	;
    size_t		len	;

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

    //-- Make sure data type is correct.
    if ( node_p->data.type != MAP_DATA_SSA ) return ~ (size_t) 0U;

    //-- Copy the data to the destination, truncated if necessary.
    len = node_p->data.mix.array.len;
    if ( len > arg_data_len ) len = arg_data_len;
    map_ssa_copy_array( arg_data_ptr, (signed short *) ( node_p->data.mix.array.ptr ), len );

    //-- Return original length for convenience.
    return node_p->data.mix.array.len;
}


