/*
 * Copyright (c) 2006 Claus Assmann
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the license/LICENSE.3C file which can be found at the
 * top level of this source code distribution.
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: mapclist.c,v 1.3 2006/11/18 15:25:53 ca Exp $")

#include "sm/assert.h"
#include "sm/error.h"
#include "map.h"
#include "sm/map.h"
#include "sm/maps.h"
#include "sm/mapc.h"
#include "sm/mapclasses.h"
#include "sm/io.h"

typedef struct sm_mapc_prt_S sm_mapc_prt_T, *sm_mapc_prt_P;
struct sm_mapc_prt_S
{
	sm_file_T	*sm_mapc_prt_fp;
	int		 sm_mapc_prt_verbosity;
	uint32_t	 sm_mapc_prt_flags;
};

/*
**  SM_MAPC_PRINT -- list one map class
**
**	Parameters:
**		bht_entry -- mapc
**		ctx -- file to print to
**
**	Returns:
**		usual sm_error code
*/

static sm_ret_T
sm_mapc_print(bht_entry_P bht_entry, void *ctx)
{
	sm_mapc_P mapc;
	sm_mapc_prt_P sm_mapc_prt;

	SM_REQUIRE(ctx != NULL);
	if (NULL == bht_entry)
		return SM_SUCCESS;
	mapc = (sm_mapc_P) bht_entry->bhe_value;
	SM_IS_MAPC(mapc);
	sm_mapc_prt = (sm_mapc_prt_P) ctx;

	if (sm_mapc_prt->sm_mapc_prt_flags != 0 &&
	    (sm_mapc_prt->sm_mapc_prt_flags & mapc->sm_mapc_flags) !=
	     sm_mapc_prt->sm_mapc_prt_flags)
		return SM_SUCCESS;

	if (sm_mapc_prt->sm_mapc_prt_verbosity > 0)
	{
		sm_io_fprintf(sm_mapc_prt->sm_mapc_prt_fp,
			"%C:%c:%c:%c:%c\n"
			, mapc->sm_mapc_type
			, (mapc->sm_mapc_addf != NULL) ? 'A' : '-'
			, (mapc->sm_mapc_lookupf != NULL) ? 'L' : '-'
			, (mapc->sm_mapc_rmf != NULL) ? 'R' : '-'
			, SM_IS_FLAG(mapc->sm_mapc_flags, SMMAPC_FL_FILE)
				? 'F' : '-'
			);
	}
	else
	{
		sm_io_fprintf(sm_mapc_prt->sm_mapc_prt_fp, "%C\n"
			, mapc->sm_mapc_type);
	}
	return SM_SUCCESS;
}

/*
**  SM_MAPC_LIST -- list all known map classes
**
**	Parameters:
**		fp -- file to print to
**		maps -- map system context
**		verbosity -- how much information?
**		flags -- flags that must be satisfied
**
**	Returns:
**		usual sm_error code
*/

sm_ret_T
sm_mapc_list(sm_file_T *fp, sm_maps_P maps, int verbosity, uint32_t flags)
{
	sm_mapc_prt_T sm_mapc_prt;

	sm_mapc_prt.sm_mapc_prt_fp = fp;
	sm_mapc_prt.sm_mapc_prt_verbosity = verbosity;
	sm_mapc_prt.sm_mapc_prt_flags = flags;
	SM_IS_MAPS(maps);
	bht_walk(maps->sm_maps_ht_mapc, sm_mapc_print, &sm_mapc_prt);
	return sm_io_flush(fp);
}


syntax highlighted by Code2HTML, v. 0.9.1