/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_typefree[] = "@(#)typefree.c	1.4 11/13/92 10:37:45 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Linda Stanberry
			Computer Software and Networking Division

	DATE WRITTEN:	7/8/92

	ROUTINES:	Rofreei()
			Rofree()

PURPOSE:
	These routines are usable to free space allocated by the system
	for components of a given type.

IN:
	typename - character string name of type
	value - pointer to the value of given type to be freed
	interf - interface in which typename is defined

OUT:
	Revised __ROv_current_interface_information, if interf parameter
	is specified.


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <stdio.h>
#include <string.h>

/* THE FOLLOWING IS AN ELROS GLOBAL VARIABLE defined in "STDinit_inst.c" */

extern __RO_Instance_Info *__ROv_current_interface_information;

int Rofree(typename,value)

   char *typename;
   void *value;

{
   if (!__ROv_current_interface_information)
   {
      fprintf(stderr,"No Interface specified; must use Rofreei() before using Rofree().\n");
      return(__ROx_PRINT_NOINTERFACESPECIFIED);
   }

   return(Rofreei(typename,value,NULL));
}

int Rofreei(typename,value,interf)

   char *typename;
   void *value;
   __RO_Instance_Info *interf;

{
   if (interf)
         __ROv_current_interface_information = interf;

   if (__ROv_current_interface_information)
   {
      __RO_Free_table_Entry *p;

	/* Look for 'typename' and print and return if found */
      for (p=__ROv_current_interface_information->free_table_ptr;
           p->type_name; p++)
         if (!strcmp(p->type_name,typename))
         {
            p->type_free(value);
            return(SUCCESS);
         }
   }

   if (strcmp(typename,"PrintableString(*)") == 0   ||
       strcmp(typename,"VisibleString(*)") == 0     ||
       strcmp(typename,"NumericString(*)") == 0     ||
       strcmp(typename,"GraphicString(*)") == 0) {

      char **temp = (char **)value;

      if (*temp != NULL) {
         free(*temp);
         *temp = 0;
      }
   }
         
   else if (strcmp(typename,"IA5String(*)") == 0          ||
            strcmp(typename,"TeletexString(*)") == 0      ||
            strcmp(typename,"VideotexString(*)") == 0     ||
            strcmp(typename,"GeneralString(*)") == 0      ||
            strcmp(typename,"OctetString(*)") == 0        ||
            strcmp(typename,"Bitstring(*)") == 0) {
   
      struct String { unsigned long len; char *val; } *temp;

      temp = (struct String *)value;
      if (temp->val != NULL) {
         free(temp->val);
         temp->val = 0;
         temp->len = 0;
      }
   }

   else if (strncmp(typename,"ObjectIdentifier(*",18) == 0) {
      struct OID { int size; unsigned long *comp; } *temp;
      temp = (struct OID *)value;
      if (temp->comp != NULL) {
         free(temp->comp);
         temp->comp = 0;
         temp->size = 0;
      }
   }
     
   else {
     fprintf(stderr,"No free routine exists for this typename: %s\n",typename);
     return(__ROx_PRINT_UNKNOWNTYPENAME);
   }

   return(SUCCESS);
}
