/*****************************************************************/
/**                                                             **/
/**             (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_getP_vector[] = "@(#)getP_vector.c	1.3 10/20/92 05:51:52 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Linda Stanberry
			Computer Software and Networking Division

	DATE WRITTEN:	04/29/92

	ROUTINE:	__ROr_BERgetP_Array

PURPOSE:
	To receive up to "number" elements of an integer or real array.

IN:
	instance - the instance record
	value - the array to receive the element values
	valuelength - the size of each element of value
	maxlength - max number of bits in the mantissa of a real, or
                    max number of bytes in an integer
	maxlength2 - max number of bits in the exponent
	number - max number of elements to receive
	elemtag - tag from getP_header of an ASN value for an element

OUT:
	instance - updated receive state
	number - updated to reflect actual number of elements received


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/


#include <elros/instance.h>
#include <elros/getP_element.h>
#include <elros/exceptions.h>
#include <elros/typetags.h>


int __ROr_BERgetP_Array(instance, value, valuelength,
                        maxlength, maxlength2, number, elemtag)

  __RO_Instance_Record	* instance;
  void			* value;
  unsigned long		* valuelength;
  unsigned long		* maxlength;
  unsigned long		* maxlength2;
  unsigned long		* number;
  __RO_int32  		elemtag;

{
  int i = 0;
  int nelems = 0;
  unsigned long length;
  __RO_int32 type;
  int dynamic = *number == ~0;
  char *next;
  next = dynamic ? (char *)(*(void **)value): (char *)(value);

  if (instance->getP_header(instance,&type,&length))
    return (EXCEPTION);

  while ((type != 0x00000000) && (i < *number)) { /* Not EOC */

    /* Check if more space needed for dynamic array */
    if (dynamic) {
      if (i+1 > nelems) {
        if (nelems == 0)
          nelems = 1;
        else
          nelems += nelems;
        if (__ROr_GetSpace(instance,value,nelems * *valuelength))
          return(EXCEPTION);
        next = (char *)(*(void **)value);
      }
    }

    if (next) {
      if ( type == elemtag) { 
        if ( type == 0x00000002 ) { /* Integer */
          if (instance->getP_Integer(instance,next+(*valuelength * i),
                                     valuelength,&length,maxlength,maxlength2))
            return (EXCEPTION);
        }
        else { /* type == 0x00000009 - Real */
          if (instance->getP_Real(instance,next+(*valuelength * i),
                                  valuelength,&length,maxlength,maxlength2))
            return (EXCEPTION);
        }
        i++;
      }
      else {
            __RO_RETURN_RUNTIME(instance,__ROx_ASN_BADOFMEMBER);
      }
    }
    else {
      if (__ROr_STDconsume_data(instance,&length))
        return (EXCEPTION);
    }

    if (instance->getP_header(instance,&type,&length))
      return (EXCEPTION);

  } /* While */

  if (type != 0x00000000) {
    if (instance->getP_header(instance,&type,&length))
      return (EXCEPTION);
    if (type != 0x00000000) {
      __RO_RETURN_RUNTIME(instance,__ROx_EL_ARRAYTOOBIG);
    }
  }

  *number = i;
  return (SUCCESS);
}


int __ROr_GetSpace(instance,variable,size)
  __RO_Instance_Record *instance;
  void **variable;
  int size;
{
  void *new;
  if (*variable == 0)
    new = (void *)malloc(size);
  else
    new = (void *)realloc(*variable,size);
  if (new == 0) {
    __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_OUTOFMEM);
  }
  *variable = new;
  return (SUCCESS);
}
