/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	descriptor
 *
 * DESCRIPTION:	Routines to find various descriptors and handles for an
 *		RCI task, either by name or by 'id' number.
 * 
 * CAVEATS:	This set of interfaces is not the most orthogonal or
 *		consistent ever designed.
 *
 * AUTHOR/DATE:	John Lloyd, Fall 1987
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include "../h/cdefs.h"
#include "../h/rciTask.h"

extern RCI_DESC rciTaskTable[];
extern int rciNumTasks;
extern int rciTaskId;

static char names[MAX_RCI_TASKS][TASK_NAME_SIZE+1];

RCI_DESC *getRciTask (name)
const char *name;
{	
	/* Get descriptor for an RCI task by name */

	register const char *s1;
	register char *s2;
	register int n, i;

	for (i=0; i<rciNumTasks; i++)
	 { 
	   s1 = name;
	   s2 = names[i];
	   n = TASK_NAME_SIZE;

	   while (--n >= 0 && *s1 == *s2++)
	    { if (*s1++ == '\0')
	       { return (&rciTaskTable[i]);
	       }
	    }
	   if (n < 0)
	    { return (&rciTaskTable[i]);
	    }
	 }
	return (NULL);
}

RCI_DESC *rciDescriptor(name)
const char *name;
{
	/* old name for getRciTask() */

	return (getRciTask(name));
}

RCI_DESC *findRciTask(num)
int num;
{
	/* Get descriptor for an RCI task by task ID number */

	return (&rciTaskTable[num]);
}

RCI_DESC *getCurrentTask()
{
	/* Get descriptor for the current RCI task */

	return (&rciTaskTable[rciTaskId]);
}

putRciName (name, id)
{
	/* Used internally. Copy the RCI task name into the names table. */

	STRNCPY (names[id], name, TASK_NAME_SIZE);
}

char *rciTaskName (td)
RCI_DESC *td;
{
	/* Return the name for an RCI task */

	return (names[td->taskID]);
}

RCI_RBT *getTaskRobot (name)
const char *name;
{
	/* Get the descriptor for a robot connected to an
	   RCI task, using the robot's name. 
	 */
	register const char *s1;
	register char *s2;
	register int i, k;
	RCI_RBT *rbt;

	for (i=0; i<rciNumTasks; i++)
	 { for (k=0; k<MAX_TASKROBOTS; k++)
	    { if (rbt = rciTaskTable[i].rbts[k])
	       { s1 = name;
		 s2 = rbt->name;
		 while (*s1 == *s2++)
		  { if (*s1++ == '\0')
		     { return (rbt);
		     }
		  }
	       }
	    }
	 }
	return (NULL);
}

RCI_RBT *findTaskRobot (id)
int id;
{
	/* Get the descriptor for a robot connected to an
	   RCI task, using the robot's ID number.
	 */
	register int i, k;
	RCI_RBT *rbt;

	for (i=0; i<rciNumTasks; i++)
	 { for (k=0; k<MAX_TASKROBOTS; k++)
	    { if (rbt = rciTaskTable[i].rbts[k])
	       { if (rbt->id == id)
		  { return (rbt);
		  }
	       }
	    }
	 }
	return (NULL);
}
	
