/* -*- c-basic-offset: 3; mode:c++ -*-
 *
 * PANDORE (PANTHEON Project)
 *
 * GREYC IMAGE
 * 6 Boulevard Maréchal Juin
 * F-14050 Caen Cedex France
 *
 * This file is free software. You can use it, distribute it
 * and/or modify it. However, the entire risk to the quality
 * and performance of this program is with you.
 *
 * Please email any bugs, comments, and/or additions to this file to:
 *  pandore@greyc.ensicaen.fr
 * http://www.greyc.ensicaen.fr/EquipeImage/Pandore/
 */

/**
 * @author Alexandre Duret-Lutz. 1999-10-08
 * @author Régis Clouard - 2001-04-10 (version 3.00)
 * @author Régis Clouard - 2006-09-05 (fix buf on Visual C++ 6)
 */

/**
 * @file errc.cpp
 *
 * Error class and exit values.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pandore.h>
#ifdef _MSC_VER
#include <io.h>
#endif

/**
 *
 * @param e	
 */
void pandore::Exit (const Errc &e ) { e.Exit(); }

/**
 *
 */
void pandore::Errc::Exit( ) const {
   switch (_ret) {
   case FS_RET : pandore::Exit(_val.fs); 
   case Char_RET : pandore::Exit(_val.v_Char);
   case Uchar_RET : pandore::Exit(_val.v_Uchar);
   case Short_RET : pandore::Exit(_val.v_Short);
   case Ushort_RET : pandore::Exit(_val.v_Ushort);
   case Long_RET : pandore::Exit(_val.v_Long);
   case Ulong_RET : pandore::Exit(_val.v_Ulong);
   case Float_RET : pandore::Exit(_val.v_Float);
   case Double_RET : pandore::Exit(_val.v_Double);
   }
}

/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Errc values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( FS_t statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      if (statut == FAILURE){
	 write(fp,"E",sizeof(Char));
	 close(fp);
	 exit(1);
      }
      else{
	 write(fp,"S",sizeof(Char));
	 close(fp);
	 exit(0);
      }
   }
   exit(1);
}

/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Char values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Char statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"0",sizeof(Char));
      write(fp,&statut,sizeof(Char));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Short values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Short statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"1",sizeof(Char));
      write(fp,&statut,sizeof(Short));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Long values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Long statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"2",sizeof(Char));
      write(fp,&statut,sizeof(Long));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Uchar values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Uchar statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"3",sizeof(Char));
      write(fp,&statut,sizeof(Uchar));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Ushort values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Ushort statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"4",sizeof(Char));
      write(fp,&statut,sizeof(Ushort));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Ulong values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Ulong statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"5",sizeof(Char));
      write(fp,&statut,sizeof(Ulong));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Float values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Float statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"6",sizeof(Char));
      write(fp,&statut,sizeof(Float));
      close(fp);
      exit(0);
   }
   exit(1);
}
/*
 * Writes the result of an operator execution into the $HOME/.pandore file.
 * This allows to handle Double values, while the exit command
 * only allows CHAR values.
 */
void pandore::Exit( Double statut ) {
   int	 fp;
   char	nomf[256];
   char *dir;
   
   if (!(dir=getenv("HOME"))) exit(2);
   strcpy(nomf,dir);
   strcat(nomf,RESFIL);
   if ((fp=open(nomf,O_CREAT|O_RDWR,00666))>0){
      write(fp,"7",sizeof(Char));
      write(fp,&statut,sizeof(Double));
      close(fp);
      exit(0);
   }
   exit(1);
}
