//-----------------------------------------------------------------------------
// 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_uca.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ucs_store_share
//
// purpose	Store the given data into the current member in the mapping.
//		The data pointer is stored as given and a duplicate is not
//		made.  This results in data being shared between the mapping
//		and the calling program.  The calling program may modify the
//		data in place, and is responsible for the consequences of
//		doing so since it changes the data (but not the type) stored
//		in the mapping.  Such a change would also affect references
//		of fetched data pointers.
//
// arguments	1 (MAP) mapping handle
//		2 (unsigned char *) data value to store
//
// returns	(int) -2 = error, no map, no member
//		(int)  0 = success, data stored
//-----------------------------------------------------------------------------
#define map_ucs_store_share(m,d) map_uca_store_share((m),(d),~(size_t)0)
__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_uca_store_share
//
// purpose	Store the given data into the current member in the mapping.
//		The data pointer is stored as given and a duplicate is not
//		made.  This results in data being shared between the mapping
//		and the calling program.  The calling program may modify the
//		data in place, and is responsible for the consequences of
//		doing so since it changes the data (but not the type) stored
//		in the mapping.  Such a change would also affect references
//		of fetched data pointers.
//
// arguments	1 (MAP) mapping handle
//		2 (unsigned char *) data value to store
//		3 (size_t) length of data value
//
// returns	(int) -2 = error, no map, no member
//		(int)  0 = success, data stored
//-----------------------------------------------------------------------------
int
map_uca_store_share (
    MAP			arg_map
    ,
    unsigned char *		arg_data_ptr
    ,
    size_t		arg_data_len
   )
__PROTO_END__
{
    struct map_node_uca *	node_p	;

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

    //-- Free any old duplicated data.
    if ( map_type_is_string( node_p->data.type ) && node_p->data.mix.array.dup ) {
	free( node_p->data.mix.array.ptr );
    }

    //-- Store the data type, length, and pointer.
    node_p->data.mix.array.ptr = (void *) arg_data_ptr;
    node_p->data.mix.array.len = arg_data_len;
    node_p->data.mix.array.dup = 0;
    node_p->data.type = MAP_DATA_UCA;

    //-- Return a success status.
    return 0;
}


