/*-=-=-=-=-=-=-=-=-=-=-=- 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.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include "../h/stan_kynvar.h"

#define NCONF	3	/* hack - should be defined specified to robot */

struct confCodeDesc {
	int code;
	char set, clear;
};

static struct confCodeDesc confTable [NCONF] = 
{
	STAN_RIGHT_CONF, 'r', 'l',
	STAN_DOWN_CONF,  'd', 'u',
	STAN_FLIP_CONF,	 'f', 'n'
};

stanConfigToStr (str, conf, kyn)
char *str;
unsigned long conf;
KYN *kyn;
{
	int i;

	for (i=0; i<NCONF; i++)
	 { if (conf & confTable[i].code)
	    { str[i] = confTable[i].set;
	    }
	   else 
	    { str[i] = confTable[i].clear;
	    }
	 }
	str[NCONF] = '\0';
	return (1);
}

stanStrToConfig (confp, str, kyn)
unsigned long *confp;
char *str;
KYN *kyn;
{
	struct confCodeDesc *desc;
	unsigned int set, clear;
	int c, j;

	if (strlen(str) > NCONF)
	 { return (-1);
	 }
	
	set = clear = 0;
	while (c = *str++)
	 { desc = confTable;
	   for (j=0; j<NCONF; j++)
	    { if (c == desc->set)
	       { if (desc->code & (set | clear))
		  { return (-1);	/* already specified */
		  }
		 else
		  { set |= desc->code;
		    break;
		  }
	       }
	      else if (c == desc->clear)
	       { if (desc->code & (set | clear))
		  { return (-1);	/* already specified */
		  }
		 else
		  { clear |= desc->code;
		    break;
		  }
	       }
	      desc++;
	    }
	   if (j == NCONF)		/* invalid character */
	    { return (-1);
	    }
	 }

	*confp = set;
	return (set | clear);
}
