/****************************************************************
 * Filename  :	Config.c
 * Functions :	
 *		Config_New
 *		Config_Delete
 *		Config_Initialize
 *		Config_OpenFile
 *		Config_ReadHeader
 *		Config_ReadColumn
 *		Config_ReadLine
 *		Config_LoadFile
 * Purpose   : 	Functions for the Config class
 $Revision: 1.1 $
 ***************************************************************/

#include <stdioLib.h>           	/* VxWorks standard I/O library       */
#include "dmvideo.h"                    /* data manager definitions           */
#include "OVtypes.h"                    /* video overlay type definitions     */
#include "Config.h" 			/* definitions for Config class       */

/*---------------------------------------------------------------
 | Function  :	Config_New
 | Purpose   : 	object construction 
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    OV_Config*
Config_New(OV_char *name)
{
    OV_Config 	*p_config;		/* a configuration */
    OV_int	nb_char;		/* number of characters */

    p_config = (OV_Config *) malloc ( sizeof(OV_Config));

    nb_char = My_get_length ( name);
    p_config->name = (char *) malloc ( nb_char+1);
    strncpy ( p_config->name, name, nb_char);
    *(p_config->name+nb_char) = NULL;

    return (p_config);

} /* Config_New */

/*---------------------------------------------------------------
 | Function  :	Config_Delete
 | Purpose   : 	object destruction
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 8/7/96
 |                to free all malloced components of structure
 ---------------------------------------------------------------*/
    void
Config_Delete(OV_Config *p_config)
{
    if (p_config->name != NULL)
	free(p_config->name);

    free ( p_config);

    return;

} /* Config_Delete */

/*---------------------------------------------------------------
 | Function  :	Config_Initialize
 | Purpose   : 	object initialization
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Config_Initialize(OV_Config *p_config)
{
	p_config->header_length = 5;
	p_config->nb_item = 0;
	p_config->nb_alarms = 0;
	p_config->nb_curve = 0;
	p_config->nb_conditions = 0;
	p_config->refresh_period = 0.;
	p_config->fd_file = NULL;

        return;

} /* Config_Initialize */



/*---------------------------------------------------------------
 | Function  :	Config_OpenFile
 | Purpose   : 	open the configuration file
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 10/30/96
 |                commented out all the French routines because
 |                they would bus error if the file did not exist
 ---------------------------------------------------------------*/
	void
Config_OpenFile(OV_Config *p_config)
{

	OV_int	 kerr;		/* error code */
	FILE	 *fd_file;	/* pointer on file descriptor */

	kerr = 0;

	/* check file existence */
/*
	kerr = ctrasc ( p_config->name);

	if (kerr == 0) {
	  printf ("--> opening file %s ...\n", p_config->name) ;
	  kerr = clsasc ( fd_file) ;
	  kerr = opiasc ( &fd_file, p_config->name) ;
	  p_config->fd_file = fd_file;
	} else {
	  printf ("--> %d: non existent file %s ...\n", kerr, p_config->name) ;
	}
*/

        if ((fd_file = fopen(p_config->name, "r")) == NULL)
        {
            kerr = -1;
            printf("--> non existent file %s ...\n", p_config->name) ;
        }
        else
        {
            p_config->fd_file = fd_file;
            printf ("--> opening file %s ...\n", p_config->name);
        }


        return;

} /* Config_OpenFile */


/*---------------------------------------------------------------
 | Function  :	Config_ReadHeader
 | Purpose   : 	read information in file header
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	fill attributes with header information
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Config_ReadHeader(OV_Config *p_config)
{

	/* ... Local variables ... */

	OV_float rentt[LENTM];		/* array to store header */
	OV_int 	 lent;			/* header length */
	OV_int	 ient;			/* current header index */
	OV_int	 kerr;			/* error code */

	lent = p_config->header_length;

	kerr = etlasc ( p_config->fd_file, 
			rentt, 
			&lent) ;
	if ( !kerr) {
/*debug
	   for ( ient = 0 ; ient <  lent; ient++) {
	      printf (" [%d] = %f\n", ient + 1, rentt[ient] ) ;
	   }
enddebug*/
	}
	else {
	   printf ("\n *** erreur n0 %d \n", kerr);
	}

	p_config->nb_item  = (OV_int) rentt[0];
	p_config->nb_alarms  = (OV_int) rentt[1];
	p_config->nb_curve = (OV_int) rentt[2];
	p_config->nb_conditions = (OV_int) rentt[3];
	p_config->refresh_period = (OV_float) rentt[4];

        return;

} /* Config_ReadHeader */


/*---------------------------------------------------------------
 | Function  :	Config_ReadColumn
 | Purpose   : 	read a column of values
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Config_ReadColumn(OV_Config *p_config, OV_int column)
{

	/* ... Local variables ... */

	OV_int	 kerr;			/* error code */
	OV_float rvalt[NECHM];		/* array to store column */
	OV_int 	 kser;			/* matrix index */
	OV_int 	 kvec;			/* vector index */
	OV_int	 iechd;			/* index of the first value */
	OV_int	 nech;			/* number of values */
	OV_int	 nechlu;		/* number of values read */
	OV_int	 ival;			/* index */

	/* ... Initializations ... */

	kser  = 1;
	kvec  = column;
	iechd = 1;
	nech  = p_config->nb_item;

	/* ... Begin ... */

	printf ("--> reading vector %d from matrix %d \n", kvec, kser); 
	kerr = vecasc ( p_config->fd_file, 
			&kser,  
			&kvec,  
			&iechd,  
			&nech,  
			rvalt,  
			&nechlu);
	if ( kerr >= 0) {
/*debug
	   for( ival = 0; ival < nechlu; ival ++) {
		   printf ("\n [%d] = %f", ival + 1, rvalt[ival]) ;
	   }
enddebug*/
	}
	else {
	   printf ("\n *** error n0 %d in reading column ***\n", kerr);
	}

	/* ... End ... */

        return;

} /* Config_ReadColumn */

/*---------------------------------------------------------------
 | Function  :	Config_ReadLine
 | Purpose   : 	read a line of information (text and values)
 | Inputs    :	Pointer to configuration structure, number of the matrix to be
 |           :  read, number of the line to be read, array to store values and
 |           :  array to store strings.
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Config_ReadLine(OV_Config *p_config, OV_int matrix, OV_int line,
		OV_float rvalt[], OV_char *zstrt[])
{
	OV_int	 kerr;			/* error code */
	OV_int 	 kser;			/* matrix index */
	OV_int	 iechd;			/* index of the first value */
	OV_int	 ival;			/* current value index */
	OV_int	 istr;			/* current string index */
	OV_int	 ivec;			/* current vector index */
	OV_int	 nser;			/* number of matrix */
	OV_int	 nvec;			/* max number of vectors */
	OV_int	 nvallu;		/* number of values read */
	OV_int	 nstrlu;		/* number of string read */

	/* ... Initializations ... */

	kser  = matrix;
	iechd = line+1;
	nvec  = NVECM;

	/* ... Begin ... */

	kerr = ligasc ( p_config->fd_file, 
			&kser,  
			&iechd,  
			&nvec,
			rvalt,  
			zstrt,  
			&nvallu,
			&nstrlu);

	if ( kerr >= 0) {
/*debug
	   printf ( " matrix = %d \n", matrix);
	   for( ival = 0; ival < nvallu; ival ++) {
		   printf ("[%d] = %f \n", ival + 1, rvalt[ival]) ;
	   }
	   for( istr = 0; istr < nstrlu; istr ++) {
		   printf ("[%d] = %s \n", istr + 1, zstrt[istr]) ;
	   }
enddebug*/

	}
	else {
	   printf ("\n *** error n0 %d in reading line ***\n", kerr);
	}

	/* ... End ... */

        return;

} /* Config_ReadLine */


/*---------------------------------------------------------------
 | Function  :	Config_LoadFile
 | Purpose   : 	load values from configuration file
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Config_LoadFile(OV_Config *p_config)
{
	OV_int	 kerr;			/* error code */

	Config_OpenFile(p_config);
	Config_ReadHeader(p_config);

        return;

} /* Config_LoadFile */

