/* ---------------------------------------------------------------------
   (c) ED 2003
   Project      : CHRO
   Function     : chronometer
   Module       : CHRO
   File         : chro.c
   Created      : 19-05-2003
   Modified     : 19-05-2003
   --------------------------------------------------------------------- */

/* ---------------------------------------------------------------------
   Log

   0.0 19-05-2003 Created
   1.0 19-05-2003 Initial version

   --------------------------------------------------------------------- */
#ifdef __cplusplus
#error This is not C++. Please use a C compiler.
#endif

#include "ed/inc/chro.h"
#include "ed/inc/sysalloc.h"
#include "ed/inc/sys.h"

/* private macro definitions =========================================== */

#define ID "CHRO"
#define VER "1.0"

/* private constants =================================================== */

/* private types ======================================================= */

/* private structures ================================================== */

struct chro
{
   chro_out_f *pf;
   void *p_usr;

   clock_t start;
   clock_t stop;
};

/* private functions =================================================== */

/* public internal functions =========================================== */

/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   chro_sid ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *chro_sid (void)
{
   return ID;
}

/* ---------------------------------------------------------------------
   chro_sver ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *chro_sver (void)
{
   return VER;
}

/* ---------------------------------------------------------------------
   chro_create ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_s *chro_create (void)
{
   chro_s *this = malloc (sizeof *this);
   if (this != NULL)
   {
      CLR (this, chro_s);

/*    -> Insert your code here */

   }
   return this;
}

/* ---------------------------------------------------------------------
   chro_delete ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void chro_delete (chro_s * this)
{
   if (this != NULL)
   {

/*   -> Insert your code here */

      free (this);
   }
}

/* ---------------------------------------------------------------------
   chro_init ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_err_e chro_init (chro_s * this)
{
   chro_err_e err = CHRO_OK;

   if (this != NULL)
   {

/*   -> Insert your code here */

   }
   else
   {
      err = CHRO_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   chro_install_out ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_err_e chro_install_out (chro_s * this, chro_out_f * pf, void *p_usr)
{
   chro_err_e err = CHRO_OK;

   if (this != NULL)
   {
      this->pf = pf;
      this->p_usr = p_usr;
   }
   else
   {
      err = CHRO_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   chro_in ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_err_e chro_in (chro_s * this, int data)
{
   chro_err_e err = CHRO_OK;

   if (this != NULL)
   {
      if (this->pf != NULL)
      {
         int ret = (*this->pf) (this->p_usr, data);
         if (ret != 0)

         {
            err = CHRO_ERR_CB_OUT;
         }
      }
   }
   else
   {
      err = CHRO_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   chro_start ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_err_e chro_start (chro_s * this)
{
   chro_err_e err = CHRO_OK;

   if (this != NULL)
   {
      this->start = clock ();
   }
   else
   {
      err = CHRO_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   chro_stop ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_err_e chro_stop (chro_s * this)
{
   chro_err_e err = CHRO_OK;

   if (this != NULL)
   {
      this->stop = clock ();
   }
   else
   {
      err = CHRO_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   chro_elapsed ()
   ---------------------------------------------------------------------
   retourne le temps ecoule en ms
   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
chro_err_e chro_elapsed (chro_s * this, ulong * p_ms)
{
   chro_err_e err = CHRO_OK;

   if (this != NULL)
   {
      double ms = (this->stop - this->start) * 1000 / CLOCKS_PER_SEC;

      if (p_ms)
      {
         *p_ms = ms;
      }
   }
   else
   {
      err = CHRO_ERR_CONTEXT;
   }
   return err;
}

/* public data ========================================================= */

/* ---------------------------------------------------------------------
   Generated by NEW (c) ED 2.1
   Powered by C-code generator (c) ED 2003 1.0
   --------------------------------------------------------------------- */
