/*--------------------------------------------------------------------
 * FICHERO:  FchBorra.c
 * OBJETIVO: Definir la funcin Fch_Borra()
 * AUTOR:    Pedro Reina
 * FECHA:    D.23.4.1995
 *------------------------------------------------------------------*/

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

#include "Fichero.h"

#include <stdio.h>       /* remove()                                */

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

/*--------------------------------------------------------------Olimpo
 * FUNCION:  Fch_Borra()
 * OBJETIVO: Borrar un fichero
 * ENTRADAS: El nombre del fichero
 * SALIDAS:  Ninguna
 * EJEMPLO:  Fch_Borra ( "Viejo.txt" )
 * ALGORITMO:
 *      Si el fichero existe
 *        Si el usuario quiere borrar, hay que borrar
 *        Si no quiere, no hay que borrar
 *      Si el fichero no existe
 *        Se informa al usuario y no hay que grabar
 *      Si hay que borrar
 *        Intentar borrar el fichero
 *        Si no se puede, se avisa al usuario
 *        Si se puede, se informa al usuario
 *------------------------------------------------------------------*/
void Fch_Borra (Nombre)
char Nombre[];
  {
  fichero Fichero;
  cadena Mensaje;
  logico HayQueBorrar;
  int    Estado;

  if ( Fch_Existe (Nombre) )
    {
    Mensaje = Cad_Une ("Quieres borrar el fichero \"", Nombre, "\"?", CAD_FIN);
    if ( Usr_Consulta (Mensaje) ) { HayQueBorrar = SI; }
    else                          { HayQueBorrar = NO; }
    Cad_Destruye (Mensaje);
    }
  else
    {
    HayQueBorrar = NO;
    Mensaje = Cad_Une ("El fichero \"",Nombre,"\" no existe. ", CAD_FIN);
    Usr_Avisa (Mensaje);
    Cad_Destruye (Mensaje);
    }

  if ( HayQueBorrar )
    {
    Estado = remove (Nombre);
    if ( Estado == -1 )
      {
      Mensaje = Cad_Une ("El fichero \"",Nombre,"\" no se puede borrar", 
                          CAD_FIN);
      Usr_Avisa (Mensaje);
      Cad_Destruye (Mensaje);
      }
    else
      {
      Cad_Copia (Mensaje,"Se ha borrado el fichero ");
      Mensaje = Cad_Une ("El fichero \"",Nombre, "\" ha sido borrado", CAD_FIN);
      Usr_PulsaUnaTecla (Mensaje);
      Cad_Destruye (Mensaje);
      }
    }
  }
