/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 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:	jointDescStr.c
 *
 * DESCRIPTION:	Contains the routine 'jointDescStr()', which takes
 *		an input bit mask denoting a set of joint angles
 *		and produces a readable string describing those
 *		joint angles.
 *
 * AUTHOR/DATE:	John Lloyd, July 15, 1990
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

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

char *jointDescStr(buf, jntMask)
char *buf;
int jntMask;
{
	int tmpMask;
	int first;
	int second;
	int last;
	int num;
	int j;

	first = UNDEF;
	second = UNDEF;
	num = 0;

	for (j=0, tmpMask=jntMask; tmpMask; tmpMask >>= 1, j++)
	 { if (tmpMask & 0x1)
	    { if (first == UNDEF)
	       { first = j;
	       }
	      else if (second == UNDEF)
	       { second = j;
	       }
	      num++;
	    }
	 }
	last = j-1;
	if (num == 0)
	 { sprintf (buf, "no joints");
	 }
	else if (num == 1)
	 { sprintf (buf, "joint %d", first+1);
	 }
	else if (num == 2)
	 { sprintf (buf, "joints %d and %d", first+1, second+1);
	 }
	else
	 { char *bp;

	   sprintf (buf, "joints ");
	   bp = buf+strlen(buf);
	   for (j=0, tmpMask=jntMask; tmpMask; tmpMask >>= 1, j++)
	    { if (tmpMask & 0x1)
	       { if (j != last)
		  { sprintf (bp, "%d, ", j+1);
		  }
		 else
		  { sprintf (bp, "and %d", j+1);
		  }
		 bp = buf+strlen(buf);		 
	       }
	    }
	 }
	return (buf);
}

#if TEST

main(argc, argv)
int argc;
char **argv;
{
	int mask;
	char buf[32];

	argc--; argv++;
	sscanf (*argv, "%x", &mask);
	printf ("%s\n", jointDescStr(buf, mask));
}

#endif


