/*
 * This file is part of libdyn.a, the C Dynamic Object library.  It
 * contains the source code for the functions DynGet() and DynAdd().
 *
 * There are no restrictions on this code; however, if you make any
 * changes, I request that you document them so that I do not get
 * credit or blame for your modifications.
 *
 * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
 * and MIT-Project Athena, 1989.
 */

#include <stdio.h>
#include "DynObject.H"

DynPtr DynObject::Get(int num) const
{
     if (num < 0) {
	  if (debug)
	       fprintf(stderr, "dyn: get: bad index %d.\n", num);
	  return NULL;
     }
     
     if (num >= num_el) {
	  if (debug)
	       fprintf(stderr, "dyn: get: highest elements is %d.\n",
		       num_el);
	  return NULL;
     }
     
     if (debug)
	  fprintf(stderr, "dyn: get: Returning address %d + %d.\n",
		  array, el_size*num);
     
     return (DynPtr) array + el_size*num;
}

int DynObject::Add(DynPtr el)
{
     int	ret;

     ret = Put(el, num_el);
     if (ret != DYN_OK)
	  return ret;

     num_el += 1;
     return ret;
}

/* WARNING!  There should be a warning here, but it is missing. */
int DynObject::Put(DynPtr el, int index)
{
     int ret;
     
     if (debug)
	  fprintf(stderr, "dyn: put: Writing %d bytes from %d to %d + %d\n",
		  el_size, el, array, index*el_size);

     if ((ret = Resize(index)) != DYN_OK)
	  return ret;
     
     bcopy(el, array + index*el_size, el_size);

     if (debug)
	  fprintf(stderr, "dyn: put: done.\n");
     
     return DYN_OK;
}
