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

/*=~=~=~=~ McGill-Computer-Vision-and-Robotics-Laboratory--C-Module ~=~=~=~=
 *		
 * MODULE NAME:	gtype
 *
 * DESCRIPTION:	Functions which copy and free information associated with
 *		GTYPE. The value stcak is also implemented here.
 * 
 * AUTHOR/DATE:	
 *
 *~=~=~=~=~=~= McGill-Computer-Vision-and-Robotics-Laboratory ~=~=~=~=~=~=*/

#include "cdefs.h"
#include "types.h"

GTYPE *tcopy_gtype (gr, g1)
GTYPE *gr, *g1;
{
	*gr = *g1;
	switch (g1->type)
	 { case MATRIX:
	      if ((gr->val.m = (matrix *)tcopy_matrix (g1->val.m)) == NULL)
	       { return (NULL);
	       }
	      break;
	   case STRING:
	      if ((gr->val.s = (char *)talloc (strlen(g1->val.s)+1)) == NULL)
	       { return (NULL);
	       }
	      strcpy (gr->val.s, g1->val.s);
	      break;
	 }
	return (gr);
}

GTYPE *mcopy_gtype (gr, g1)
GTYPE *gr, *g1;
{
	*gr = *g1;
	switch (g1->type)
	 { case MATRIX:
	      if ((gr->val.m = (matrix *)mcopy_matrix (g1->val.m)) == NULL)
	       { return (NULL);
	       }
	      break;
	   case STRING:
	      if ((gr->val.s = (char *)malloc (strlen(g1->val.s)+1)) == NULL)
	       { return (NULL);
	       }
	      strcpy (gr->val.s, g1->val.s);
	      break;
	 }
	return (gr);
}

free_gtype (g)
GTYPE *g;
{
	switch (g->type)
	 { case MATRIX:
	      free (g->val.m);
	      break;
	 }
}

/* ---- functions to implement the value stack ---- */

#define VALUE_STACK_SIZE	0x2000
extern GTYPE valueStack[];
GTYPE valueStack [VALUE_STACK_SIZE];
static int vsp = 0;

push_value_stack (g)
GTYPE g;
{
	if (vsp == VALUE_STACK_SIZE)
	 { error (0, "value stack overflow; more than %d elements\n",
		     VALUE_STACK_SIZE);
	   return (-1);
	 }
	else
	 { valueStack[vsp++] = g;
	 }
 	return (1);
}

pop_value_stack (count)
int count;
{
	if (count > vsp)
	 { error (0, "internal error; value stack reference out of range\n");
	   return (-1);	   
	 }
 	else
	 { vsp -= count;
	 }
 	return (1);
}

clearValueStack ()
{
	vsp = 0;
}

valueStackIndex ()
{
	return (vsp);
}

