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

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_str_store
//
// purpose	Store the given data into the current member in the mapping.
//		String/array data is stored as a duplicate so the original
//		data can be modified or the space reused or freed after
//		this function returns.
//
// arguments	1 (MAP) mapping handle
//		2 (const char *) data value to store
//
// returns	(int) -2 = error, no map, no member, out of memory
//		(int)  0 = success, data stored
//-----------------------------------------------------------------------------
#define map_str_store(m,d) map_ca_store((m),(d),~(size_t)0)
__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_cs_store
//
// purpose	Store the given data into the current member in the mapping.
//		String/array data is stored as a duplicate so the original
//		data can be modified or the space reused or freed after
//		this function returns.
//
// arguments	1 (MAP) mapping handle
//		2 (const char *) data value to store
//
// returns	(int) -2 = error, no map, no member, out of memory
//		(int)  0 = success, data stored
//-----------------------------------------------------------------------------
#define map_cs_store(m,d) map_ca_store((m),(d),~(size_t)0)
__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_ca_store
//
// purpose	Store the given data into the current member in the mapping.
//		String/array data is stored as a duplicate so the original
//		data can be modified or the space reused or freed after
//		this function returns.
//
// arguments	1 (MAP) mapping handle
//		2 (const char *) data value to store
//		3 (size_t) length of data value
//
// returns	(int) -2 = error, no map, no member, out of memory
//		(int)  0 = success, data stored
//-----------------------------------------------------------------------------
int
map_ca_store (
    MAP			arg_map
    ,
    const char *	arg_data_ptr
    ,
    size_t		arg_data_len
   )
__PROTO_END__
{
    struct map_node_ca *	node_p	;
    char *	data_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;

    //-- If the length is not given, determine it as zero terminated string.
    if ( arg_data_len == ~ (size_t) 0U ) arg_data_len = map_ca_length( arg_data_ptr );

    //-- If the data type and length are the same, reuse the old data space.
    if ( node_p->data.type == MAP_DATA_CA && node_p->data.mix.array.len == arg_data_len ) {
	data_p = node_p->data.mix.array.ptr;
    }

    //-- Else the old data is not suitable for reuse.
    else {

	//-- 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 );
	}

	//-- Allocate space for the data to be duplicated.
	if ( ! ( data_p = malloc( ( arg_data_len + 1 ) * sizeof (char) ) ) ) return -2;
    }

    //-- Copy the data to the duplication space.
    map_ca_copy_array( data_p, arg_data_ptr, arg_data_len );

    //-- Store data in the node.
    node_p->data.mix.array.ptr = (void *) data_p;
    node_p->data.mix.array.len = arg_data_len;
    node_p->data.mix.array.dup = 1;
    node_p->data.type = MAP_DATA_CA;

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


