/*--------------------------------------------------------------------
 * FICHERO:       Fichero.h
 * OBJETIVO:      Definir el objeto "Fichero"
 * IDENTIFICADOR: Fch
 * AUTOR:         Pedro Reina
 * FECHA:         V.14.7.1995
 * OBJETOS UTILIZADOS: Lista, Cadena, Usuario
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Funciones pblicas
 *
 *   Fch_Manejador()         Fichero.h
 *   Fch_NombreReal()        Fichero.h
 *   Fch_Separador()         Fichero.h
 *   Fch_Abre()              FchAbre.c
 *   Fch_Cierra()            FchAbre.c
 *   Fch_Existe()            FchExist.c
 *   Fch_AbreLeer()          FchAbreL.c
 *   Fch_AbreGrabar()        FchAbreG.c
 *   Fch_Borra()             FchBorra.c
 *   Fch_LeeLinea()          FchLeeL.c
 *   Fch_LeeOcteto()         Fichero.h
 *   Fch_EscribeLinea()      FchEscrL.c
 *   Fch_Nombre()            FchNombr.c
 *   Fch_EscribeOcteto()     Fichero.h
 *   Fch_Coloca()            Fichero.h
 *   Fch_AbreActualizar()    FchAbreA.c
 *   Fch_ColocaFinal()       Fichero.h
 *   Fch_Posicion()          Fichero.h
 *   Fch_ListaNombre()       FchLisNo.c
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Funciones conocidas
 *
 *   Ninguna
 *------------------------------------------------------------------*/

/*--------------------------------------------------------------------
 * Funciones privadas
 *
 *   Fch_PonManejador()      Fichero.h
 *   Fch_PonNombreReal()     Fichero.h
 *
 * Funciones privadas PC
 *
 *   Fch_Inicia()            FchInic.c
 *------------------------------------------------------------------*/

#ifndef _FICHERO_
#define _FICHERO_

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

#include "Lista.h"
#include "Cadena.h"
#include "Usuario.h"

#include <stdio.h>       /* fwrite()  fread()  fseek()  ftell() */

/*--------------------------------------------------------------------
 * Definicin de macros constantes
 *------------------------------------------------------------------*/

#define FCH_TEXTO      (octeto)0
#define FCH_BINARIO    (octeto)1

/*--------------------------------------------------------------------
 * Definicin del tipo "fichero"
 *------------------------------------------------------------------*/
typedef struct {
               FILE  *Manejador;
               cadena NombreReal;
               } Fch_st, *fichero;

/*--------------------------------------------------------------------
 * Variables globales
 *------------------------------------------------------------------*/

   /* Ninguna */

/*--------------------------------------------------------------------
 * Declaracin de funciones
 *------------------------------------------------------------------*/

logico  Fch_Existe();
fichero Fch_Abre();
fichero Fch_AbreLeer();
fichero Fch_AbreGrabar();
fichero Fch_AbreActualizar();
logico  Fch_Cierra();
void    Fch_Borra();
cadena  Fch_LeeLinea();
logico  Fch_EscribeLinea();
cadena  Fch_Nombre();
lista   Fch_ListaNombre();

#ifdef OLIMPO_PC
void Fch_Inicia();
#endif

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

/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_Manejador()
 * OBJETIVO: Decir el puntero a FILE que est usando un fichero
 * ENTRADAS: El fichero
 * SALIDAS:  Un puntero a FILE
 * EJEMPLO:  Fch_Manejador ( Entrada )
 * NOTA:     Esta funcin permite acceder a cualquier funcin C
 *           que sirva para manejar ficheros, pero se recomienda
 *           usar las de Olimpo siempre que sea posible
 *------------------------------------------------------------------*/
#define Fch_Manejador(f)  ((f)->Manejador)

/*--------------------------------------------------------------------
 * MACRO:    Fch_PonManejador()
 * OBJETIVO: Poner el puntero a FILE en un fichero
 * ENTRADAS: El fichero y el puntero
 * SALIDAS:  El puntero a FILE
 * EJEMPLO:  Fch_PonManejador ( Entrada, PunteroFILE )
 *------------------------------------------------------------------*/
#define Fch_PonManejador(f,p)  ((f)->Manejador=(p))

/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_NombreReal()
 * OBJETIVO: Decir el nombre que se us al abrir un fichero
 * ENTRADAS: El fichero
 * SALIDAS:  Una cadena con el nombre
 * EJEMPLO:  Fch_NombreReal ( Entrada )
 *------------------------------------------------------------------*/
#define Fch_NombreReal(f)  ((f)->NombreReal)

/*--------------------------------------------------------------------
 * MACRO:    Fch_PonNombreReal()
 * OBJETIVO: Poner el nombre que se va a usar al abrir un fichero
 * ENTRADAS: El fichero y una cadena con el nombre
 * SALIDAS:  La cadena
 * EJEMPLO:  Fch_PonNombreReal ( Entrada, "Dato.dat" )
 *------------------------------------------------------------------*/
#define Fch_PonNombreReal(f,n)  ((f)->NombreReal=Cad_Duplica(n))

/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_Separador()
 * OBJETIVO: Decir el carcter separador entre el nombre y la
 *           extensin usado por el sistema operativo
 * ENTRADAS: Ninguna
 * SALIDAS:  Un carcter
 * EJEMPLO:  Fch_Separador()
 *------------------------------------------------------------------*/

#ifdef OLIMPO_QL
#define Fch_Separador()  '_' 
#endif

#ifdef OLIMPO_PC
#define Fch_Separador()  '.' 
#endif
                            
/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_EscribeOcteto()
 * OBJETIVO: Escribir en un fichero cierta cantidad de octetos
 * ENTRADAS: El fichero, la zona de memoria de donde se toman
 *           los octetos y la cantidad de octetos
 * SALIDAS:  Lgica, que indica si todo ha ido bien
 * EJEMPLO:  Fch_EscribeOcteto ( Salida, Item, 1024 )
 *------------------------------------------------------------------*/
#define Fch_EscribeOcteto(F,Z,N)  (fwrite((char *)Z,(size_t)1,N, \
                                   Fch_Manejador(F))==N) 
  
/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_LeeOcteto()
 * OBJETIVO: Leer de un fichero cierta cantidad de octetos
 * ENTRADAS: El fichero, la zona de memoria donde dejar lo 
 *           leido y la cantidad de octetos
 * SALIDAS:  Lgica, que indica si todo ha ido bien
 * EJEMPLO:  Fch_LeeOcteto ( Datos, Info, 1024 )
 *------------------------------------------------------------------*/
#define Fch_LeeOcteto(F,Z,N)  (fread((char *)Z,(size_t)1,N, \
                               Fch_Manejador(F))==N) 
    
/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_Coloca()
 * OBJETIVO: Colocar el puntero interno de un fichero en una
 *           determinada posicin
 * ENTRADAS: El fichero y la nueva posicin, contada desde 
 *           el principio
 * SALIDAS:  Lgica, que indica si todo ha ido bien
 * EJEMPLO:  Fch_Coloca ( Dato, 50 )
 *------------------------------------------------------------------*/
#define Fch_Coloca(f,p)  (fseek(Fch_Manejador(f),(long)p,SEEK_SET)==0)

/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_ColocaFinal()
 * OBJETIVO: Colocar el puntero interno de un fichero al final
 * ENTRADAS: El fichero
 * SALIDAS:  Lgica, que indica si todo ha ido bien
 * EJEMPLO:  Fch_ColocaFinal ( Dato )
 *------------------------------------------------------------------*/
#define Fch_ColocaFinal(f)  (fseek(Fch_Manejador(f),0L,SEEK_END)==0)

/*--------------------------------------------------------------Olimpo
 * MACRO:    Fch_Posicion()
 * OBJETIVO: Decir la posicin del puntero interno de un fichero
 * ENTRADAS: El fichero
 * SALIDAS:  La posicin o -1 si hay error
 * EJEMPLO:  Fch_Posicion ( Dato )
 *------------------------------------------------------------------*/
#define Fch_Posicion(f)    (ftell(Fch_Manejador(f)))
                                            
#endif /* _FICHERO_ */