//-----------------------------------------------------------------------------
// 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_cmp_feature
//
// purpose	Set the comparison feature to enable or disable the options
//		specified in the 3rd argument.
//
// arguments	1 (MAP) mapping handle
//		2 (int) -1: no change, 0: disable, 1: enable
//		3 (int) which options
//
// returns	(int)  < 0 : error
//		(int) == 0 : OK, all previously disabled
//		(int)  > 0 : OK, one or more previously enabled
//
// WARNING	This function will remove all members from the mapping since
//		the collating order is disrupted by this setting.
//-----------------------------------------------------------------------------
#define map_ucs_cmp_feature map_uca_cmp_feature
__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_uca_cmp_feature
//
// purpose	Set the comparison feature to enable or disable the options
//		specified in the 3rd argument.
//
// arguments	1 (MAP) mapping handle
//		2 (int) -1: no change, 0: disable, 1: enable
//		3 (int) which options
//
// returns	(int)  < 0 : error
//		(int) == 0 : OK, all previously disabled
//		(int)  > 0 : OK, one or more previously enabled
//
// WARNING	This function will remove all members from the mapping since
//		the collating order is disrupted by this setting.
//-----------------------------------------------------------------------------
int
map_uca_cmp_feature (
    MAP		arg_map
    ,
    int		arg_state
    ,
    int		arg_flag
   )
__PROTO_END__
{
    int		previous	;

    //-- Get the previous setting before making changes.
    previous = ( arg_map->options & arg_flag ) ? 1 : 0;

    //-- If changes are to be made, make them.
    if ( arg_state >= 0 ) {

	//-- Make sure the mapping is empty.
	map_empty( arg_map );

	//-- Change appropriate settings.
	arg_map->cmp_func = NULL;
	if ( arg_state == 0 ) arg_map->options &= ~arg_flag;
	if ( arg_state == 1 ) arg_map->options |= arg_flag;

	//-- Set up the appropriate AVL comparison function and re-initialize.
	if ( ! avl_init( & (arg_map->avl_tree),
			 struct map_node_uca, link,
			 ( arg_map->options
			     & (MAP_OPT_LOWER|MAP_OPT_UPPER|MAP_OPT_DIGITS|MAP_OPT_SPACES|MAP_OPT_PATHS) )
			 ? map_uca_compare_str
			 : map_uca_compare
	    ) ) {
	    return -1;
	}
    }

    //-- Return the previous setting of this option.
    return previous;
}


