/* Application - HPDI32 - Any OS */
/* $Workfile: util_select.c $ */
/* $Revision: 2 $ */
/* $Date: 3/12/08 6:14p $ */
/* $Log: /NEW SOURCESAFE DATABASE/SDK/hpdi32/utils/util_select.c $
 * 
 * 2     3/12/08 6:14p Dnewbold
 * Updated selection code.
 * 
 * 1     8/22/05 4:02p Dnewbold
*/

#include <stdio.h>

#include "util_select.h"

#include "util_label.h"



/*****************************************************************************
*
*	Function:	select_1_board
*
*	Purpose:
*
*		Select the board to utilize when more than one is present.
*
*	Arguments:
*
*		qty		The number of installed boards.
*
*		index	The board to use. This starts as the default, which
*				we adjust here as appropriate.
*
*	Returned:
*
*		>= 0	The number of errors encountered here.
*
*****************************************************************************/

int select_1_board(int qty, int* index)
{
	char	buf[128];
	int		errs	= 0;
	char*	psz;	// Used to supress warnings only.

	if (qty < 2)
	{
		index[0]	= 0;
	}
	else if ((index[0] >= 0) && (index[0] < qty))
	{
	}
	else
	{
		printf("Enter a board index from 0 to %d: ", qty - 1);
		psz	= fgets(buf, sizeof(buf), stdin);
		sscanf(buf, "%d", index);

		if ((index[0] < 0) || (index[0] >= qty))
		{
			errs	= 1;
			printf("%d is an invalid index.\n", index[0]);
		}
	}

	return(errs);
}



/*****************************************************************************
*
*	Function:	select_2_boards
*
*	Purpose:
*
*		Select two boards to utilize when more than two are present.
*
*	Arguments:
*
*		qty		The number of installed boards.
*
*		index_1	The first board to use.
*
*		index_2	The second board to use.
*
*	Returned:
*
*		>= 0	The number of errors encountered here.
*
*****************************************************************************/

int select_2_boards(int qty, int* index_1, int* index_2)
{
	char	buf[128];
	int		errs	= 0;
	char*	psz;	// used to supress warnings only.

	for (;;)	// A convenience loop.
	{
		if (qty == 0)
		{
			errs	= label_fail_text(1, "no boards present, two are required");
			break;
		}

		if (qty == 1)
		{
			errs	= label_fail_text(1, "only one board present, two are required");
			break;
		}

		if ((qty == 2) && (index_1[0] < 0) && (index_2[0] < 0))
		{
			index_1[0]	= 0;
			index_2[0]	= 1;
		}


		if ((index_1[0] < 0) || (index_1[0] >= qty))
		{
			printf("Enter the index for the first board: ");
			psz	= fgets(buf, sizeof(buf), stdin);
			sscanf(buf, "%d", index_1);
		}

		if ((index_2[0] < 0) || (index_2[0] >= qty))
		{
			printf("Enter the index for the second board: ");
			psz	= fgets(buf, sizeof(buf), stdin);
			sscanf(buf, "%d", index_2);
		}

		label_text("HPDI32s to Access");

		if ((index_1[0] < 0) || (index_1[0] >= qty))
		{
			sprintf(buf, "the first index is invalid: %d", index_1[0]);
			errs	= label_fail_text(1, buf);
			break;
		}

		if ((index_2[0] < 0) || (index_2[0] >= qty) || (index_2 == index_1))
		{
			sprintf(buf, "the second index is invalid: %d", index_2[0]);
			errs	= label_fail_text(1, buf);
			break;
		}

		printf("%d and %d\n", index_1[0], index_2[0]);
		break;
	}

	return(errs);
}


