/*--------------------------------------------------------------------
 * FICHERO:  IndCrea.c
 * OBJETIVO: Definir la funcin Ind_Crea()
 * AUTOR:    Pedro Reina
 * FECHA:    D.30.7.1995
 *------------------------------------------------------------------*/

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

#include "Indice.h"

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

/*--------------------------------------------------------------Olimpo
 * FUNCION:  Ind_Crea()
 * OBJETIVO: Crear un fichero de base de datos y obtener un
 *           objeto en memoria para manejarlo
 * ENTRADAS: El nombre completo del fichero que hay que crear,
 *           la expresin que usar para comparar claves,
 *           la longitud de las claves y el nmero de decimales
 *           de la clave (si es numrica)
 * SALIDAS:  Un objeto indice o NIL si no se puede crear
 * EJEMPLO:  Ind_Crea ( "Agenda.ntx", "Apellido+Nombre", 40, 0 )
 * NOTAS:    1. La expresin no suele llevar espacios
 *           2. La expresin debe permitir regenerar el ndice
 *              si es necesario.
 *           3. La longitud de una clave no puede ser mayor de 100
 *           4. La longitud de la expresin de una clave no puede
 *              ser mayor de 255
 *------------------------------------------------------------------*/
indice Ind_Crea (NombreFichero, Expresion, Longitud, Decimal)
cadena NombreFichero, Expresion;
octeto Longitud, Decimal;
  {
  indice   Nuevo;             
  fichero  Fichero;
  contador ClavesPagina, i;

  if ( Nuevo = Ind_CreaObjeto() )
    {
    if ( Fichero = Fch_AbreGrabar (NombreFichero,FCH_BINARIO) )
      {
      Mem_EscribeContador (Ind_Pagina(Nuevo),    MEM_INTEL, 6);
      Mem_EscribeContador (Ind_Pagina(Nuevo)+2,  MEM_INTEL, 1);
      Mem_EscribeContador (Ind_Pagina(Nuevo)+4,  MEM_INTEL, IND_PAGINA);
      Mem_EscribeContador (Ind_Pagina(Nuevo)+12, MEM_INTEL, Longitud+8);
      Mem_EscribeContador (Ind_Pagina(Nuevo)+14, MEM_INTEL, Longitud);
      Mem_EscribeContador (Ind_Pagina(Nuevo)+16, MEM_INTEL, Decimal);
           
      ClavesPagina = (IND_PAGINA - 8) / (Longitud + 10);                                         
      Mem_EscribeContador (Ind_Pagina(Nuevo)+18, MEM_INTEL, ClavesPagina);
      Mem_EscribeContador (Ind_Pagina(Nuevo)+20, MEM_INTEL, ClavesPagina/2);

      for ( i=0 ; i<256 && Expresion[i] ; i++ )
        { Ind_Pagina(Nuevo)[22+i] = Expresion[i]; }
              
      Ind_PonFichero (Nuevo, Fichero);                  
      Ind_PonModoApertura (Nuevo, IND_ACTUALIZACION);
      Ind_PonLongitudClave (Nuevo, Longitud);
      Ind_PonClavesPagina (Nuevo, ClavesPagina);
      Ind_PonNivel (Nuevo,0);
      Ind_PonPosicion (Nuevo, IND_PAGINA);
      Ind_PonNivel (Nuevo,IND_FUERA);
                                           
      Fch_EscribeOcteto (Fichero, Ind_Pagina(Nuevo), IND_PAGINA);
      Ind_NuevaPagina (Nuevo);
      }
    else
      {
      Ind_Destruye (Nuevo);
      Nuevo = NIL;
      }
    }

  return ( Nuevo );
  }