/* ---------------------------------------------------------------------
   (c) ED 2003
   Project      : CLIB
   Function     : Counter with saturation and max memory
   Module       : CNT
   File         : cnt.c
   Created      : 11-07-2003
   Modified     : 11-07-2003
   --------------------------------------------------------------------- */

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

   0.0 11-07-2003 Created
   1.0 11-07-2003 Initial version

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

#include "ed/inc/cnt.h"
#include "ed/inc/sys.h"

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

#define ID "CNT"
#define VER "1.0"

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

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

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

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

/* ---------------------------------------------------------------------
   rst ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
static void rst (cnt_s * this)
{
   this->count = 0;
   this->mem_max = 0;
   this->empty = 1;
   this->full = 0;
}

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

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

/* ---------------------------------------------------------------------
   cnt_sid ()
   ---------------------------------------------------------------------

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

/* ---------------------------------------------------------------------
   cnt_sver ()
   ---------------------------------------------------------------------

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

/* ---------------------------------------------------------------------
   cnt_init ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
cnt_err_e cnt_init (cnt_s * this, ulong max)
{
   cnt_err_e err = CNT_OK;

   if (this != NULL)
   {
      this->cnt_max = max;
      rst (this);
   }
   else
   {
      err = CNT_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   cnt_inc ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
cnt_err_e cnt_inc (cnt_s * this)
{
   cnt_err_e err = CNT_OK;

   if (this != NULL)
   {
      if (!this->full)
      {
         this->count++;
         this->full = this->count == this->cnt_max;
         this->empty = 0;

         if (this->mem_max < this->count)
         {
            this->mem_max = this->count;
         }
      }
      else
      {
         err = CNT_ERR_FULL;
      }
   }
   else
   {
      err = CNT_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   cnt_dec ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
cnt_err_e cnt_dec (cnt_s * this)
{
   cnt_err_e err = CNT_OK;

   if (this != NULL)
   {
      if (!this->empty)
      {
         this->count--;
         this->empty = this->count == 0;
         this->full = 0;

      }
      else
      {
         err = CNT_ERR_EMPTY;
      }
   }
   else
   {
      err = CNT_ERR_CONTEXT;
   }
   return err;
}

/* ---------------------------------------------------------------------
   cnt_rrs ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
cnt_err_e cnt_rst (cnt_s * this)
{
   cnt_err_e err = CNT_OK;

   if (this != NULL)
   {
      rst (this);
   }
   else
   {
      err = CNT_ERR_CONTEXT;
   }
   return err;
}

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

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