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

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	put_config_map
//
// purpose	Output a mapping into a config file.
//
// arguments	1 (MAP) mapping to be output
//		2 (FILE *) pointer to open file to output to
//
// returns	(long) == -1 : error
//		(long) >=  0 : number of items output
//-----------------------------------------------------------------------------
long
put_config_map (
    MAP			arg_map
    ,
    FILE *		arg_file
    )
__PROTO_END__
{
    const char *	name_ptr	;
    const char *	data_ptr	;
    size_t		name_len	;
    size_t		data_len	;
    long		count		;
    int			data_type	;

    //-----------------
    // Check arguments.
    //-----------------
    if ( ! arg_map ) return -1;
    if ( ! arg_file ) arg_file = stdin;

    //---------------------------------------------
    // Loop through mapping and output each member.
    //---------------------------------------------
    count = 0;
    map_loop_forward( arg_map ) {
	name_ptr = (char *) map_uca_key_ptr( arg_map );
	name_len = map_uca_key_len( arg_map );
	data_type = map_data_type( arg_map );
	if ( data_type == 0 ) {
	    put_config_item( name_ptr, name_len, NULL, 0, arg_file );
	    ++ count;
	}
	else if ( data_type == MAP_DATA_STR ) {
	    data_ptr = (char *) map_uca_fetch_ptr( arg_map );
	    data_len = map_uca_fetch_len( arg_map );
	    put_config_item( name_ptr, name_len, data_ptr, data_len, arg_file );
	    ++ count;
	}
    }

    //-----------------------------------------
    // Return the count of config items output.
    //-----------------------------------------
    return count;
}

