/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : mplan.c                                                       */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <malloc.h>
#include <stdio.h>
#include "mplan.h"

#define AllocIncr 10

static InputAttributes *_attributes = 0;

void createInputAttributes();


void createInputAttributes()
{
  _attributes = (InputAttributes *)malloc(sizeof(InputAttributes));

  _attributes->nInputs = 0;

  _attributes->inputs = 0;

  _attributes->nAllocd = 0;
}


void addInputAttribute(const char *name, const char *value)
{
  int debug = 0;
  InputAttribute *input;
  int nBytes;
  int nAllocd;
  int i;

  dprintf("addInputAttribute() - name=%s, value=%s\n", name, value);

  if (_attributes->nInputs == _attributes->nAllocd) {

    /* Need to expand array */
    nAllocd = _attributes->nAllocd + AllocIncr;

    nBytes = nAllocd * sizeof(InputAttribute);

    dprintf("addInputAttribute() - realloc(), nAllocd=%d, nBytes=%d\n",
	    nAllocd, nBytes);

    _attributes->inputs = 
      (InputAttribute **)realloc((void *)_attributes->inputs, nBytes);

    dprintf("addInputAttribute() - done with realloc()\n");

    for (i = _attributes->nAllocd; i < _attributes->nAllocd + AllocIncr; i++) {

      _attributes->inputs[i] = 
	(InputAttribute *)malloc(sizeof(InputAttribute));
    }

    _attributes->nAllocd = nAllocd;
  }

  input = _attributes->inputs[_attributes->nInputs];
  input->name = (const char *)strdup(name);
  input->value = (const char *)strdup(value);
  _attributes->nInputs++;
}


void initInputAttributes()
{
  int i;

  if (!_attributes)
    createInputAttributes();

  for (i = 0; i < _attributes->nInputs; i++) {
    free((void *)_attributes->inputs[i]->name);
    free((void *)_attributes->inputs[i]->value);
  }

  _attributes->nInputs = 0;
}


const InputAttributes *inputAttributes()
{
  return _attributes;
}

