/*--------------------------------------------------------------------
 * FICHERO:  IndCreOb.c
 * OBJETIVO: Definir las funciones Ind_CreaObjeto() e Ind_Destruye()
 * AUTOR:    Pedro Reina
 * FECHA:    D.30.7.1995
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Ficheros de cabecera
 *------------------------------------------------------------------*/

#include "Indice.h"

/*--------------------------------------------------------------------
 * Definicin de funciones
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * FUNCION:  Ind_CreaObjeto()
 * OBJETIVO: Crear un objeto "indice" y dar valores iniciales a sus campos
 * ENTRADAS: Ninguna
 * SALIDAS:  Un ndice
 * EJEMPLO:  Ind_CreaObjeto()
 *------------------------------------------------------------------*/
indice Ind_CreaObjeto ()
  {
  indice  Nuevo;
  logico  Error = NO;
  memoria Item, Posicion;

  Nuevo = (indice) (Mem_Crea (sizeof(Ind_st)));
  if ( Nuevo )
    { 
    Mem_Asigna (Nuevo, 0, sizeof(Ind_st)); /* Puesta a cero de todo */
    
    if ( ( Item = Mem_Crea (IND_MINNIVEL * sizeof(contador)) ) &&
         ( Posicion = Mem_Crea (IND_MINNIVEL * sizeof(entero)) ) )
      {
      Ind_PonDirItem (Nuevo, (contador *) Item);
      Ind_PonDirPosicion (Nuevo, (entero *) Posicion);
      }
    else 
      {
      Ind_Destruye (Nuevo); 
      Error = SI; 
      }
    }
  else { Error = SI; }

  if ( Error )
    {
    Nuevo = NIL;
    Usr_Avisa ("No se puede crear un ndice por falta de memoria");
    }

  return ( Nuevo );
  }
  
/*--------------------------------------------------------------------
 * FUNCION:  Ind_Destruye()
 * OBJETIVO: Destruir un objeto indice, liberando su memoria
 * ENTRADAS: El ndice
 * SALIDAS:  Ninguna
 * EJEMPLO:  Ind_Destruye ( Agenda )
 * ALGORITMO:
 *      Si se ha reservado memoria para DirItem y DirPosicin, liberarla
 *      Liberar la memoria del objeto indice
 *------------------------------------------------------------------*/
void Ind_Destruye ( Indice )
indice Indice;
  {
  if ( Ind_DirItem(Indice) ) { Mem_Destruye (Ind_DirItem(Indice)); }
  if ( Ind_DirPosicion(Indice) ) { Mem_Destruye (Ind_DirPosicion(Indice)); }

  Mem_Destruye (Indice);
  }  