/*
   FILE: DB_CNF.C      ( DB_Check Non-Fatal )

   This file contains routines that set up calls to CODAS database
   functions and check the resulting error code.  There are also
   routines for checking profile times and block-profile numbers
   against some range.

   Your program must #include "use_db.h", the header file for these routines.

*/

#include "geninc.h"          /* DB*() */
#include "ioserv.h"          /* error_found(), report_msg(), set_long_bit() */
#include "use_db.h"

#if PROTOTYPE_ALLOWED
int dbget_cnf(int type, char *data, unsigned int *n, char *msg)
#else
int dbget_cnf(type, data, n, msg)
int type;
char *data, *msg;
unsigned int *n;
#endif
{
   int ierr;

   DBGET(&type, data, n, &ierr);
   error_found(ierr, msg);
   return(ierr);
}

#if PROTOTYPE_ALLOWED
int dbget_f_cnf(int type, float *data, unsigned int *n, char *msg)
#else
int dbget_f_cnf(type, data, n, msg)
int type;
float *data;
char *msg;
unsigned int *n;
#endif
{
   int ierr;

   DBGET_F(&type, data, n, &ierr);
   error_found(ierr, msg);
   return(ierr);
}


#if PROTOTYPE_ALLOWED
int dbmove_cnf(int nsteps)
#else
int dbmove_cnf(nsteps)
int nsteps;
#endif
{
   int ierr;

   DBMOVE(&nsteps, &ierr);
   switch (ierr)
   {
      case SEARCH_BEFORE_BEGINNING:
         report_msg("\n%% WARNING: Already at database beginning\n");
         break;
      case SEARCH_BEYOND_END:
         report_msg("\n%% WARNING: Reached end of database\n");
         break;
      default:
         error_found(ierr, "DBMOVE");
   }
   return(ierr);
}

#if PROTOTYPE_ALLOWED
int dbopen_cnf(int dbid, char *dbname, int accmode, int memmode)
#else
int dbopen_cnf(dbid, dbname, accmode, memmode)
int dbid, memmode, accmode;
char *dbname;
#endif
{
   int ierr;
   char buff[255];

   DBOPEN(&dbid, dbname, &accmode, &memmode, &ierr);
   if (ierr)
      sprintf(buff, "\n%%%% ERROR: %d opening database %s\n\n", ierr, dbname);
   else
      sprintf(buff, "\n%% DATABASE: %s successfully opened\n", dbname);
   report_msg(buff);
   return(ierr);
}

#if PROTOTYPE_ALLOWED
void dbclose_cnf(void)
#else
void dbclose_cnf()
#endif
{
   int ierr;

   DBCLOSE(&ierr);
   if (error_found(ierr, "closing database")) return;
   report_msg("\n% DATABASE: Closed successfully\n\n");
}

#if PROTOTYPE_ALLOWED
int dbsrch_cnf(int search_type, char *search_param)
#else
int dbsrch_cnf(search_type, search_param)
int search_type;
char *search_param;
#endif
{
   int ierr;

   DBSRCH(&search_type, search_param, &ierr);
   switch (ierr)
   {
      case SEARCH_BEFORE_BEGINNING:
         report_msg("\n%% WARNING: Search before database beginning\n");
         break;
      case SEARCH_BEYOND_END:
         report_msg("\n%% WARNING: Search beyond database end\n");
         break;
      default:
         break;
   }
   return(ierr);
}

#if PROTOTYPE_ALLOWED
int dbset_cnf(int dbid)
#else
int dbset_cnf(dbid)
int dbid;
#endif
{
   int ierr;

   DBSET(&dbid, &ierr);
   return(error_found(ierr, "DBSET"));
}

/*-----------------------------------------------------------------------------

   FUNCTION: check_prftime

      Gets time of current profile and compares it with the given end time.

   PARAMETERS:

      now = pointer to profile time structure
      end = pointer to end time structure

   RETURNS:

      0 if profile time is same as end time
     -1 if profile time is less than end time
      1 if profile time is greater than end time
      2 if database error

*/

#if PROTOTYPE_ALLOWED
int check_prftime(YMDHMS_TIME_TYPE *now, YMDHMS_TIME_TYPE *end)
#else
int check_prftime(now, end)
YMDHMS_TIME_TYPE *now, *end;
#endif
{
   LONG cmp;
   unsigned int n = sizeof(YMDHMS_TIME_TYPE);

   if (dbget_cnf(TIME, (char *) now, &n, "getting profile time")) return(2);
   cmp = TIMCMP(now, end);
   if (cmp == 0) return(0);
   if (cmp == 1) return(-1);
   return(1);
}

/*-----------------------------------------------------------------------------

   FUNCTION: check_blkprf

      Retrieves current block-profile no. and compares with given block-
      profile no.

   PARAMETERS:

      c_blkprf = pointer to block-profile index of current profile
      e_blkprf = pointer to block-profile index to compare to

   RETURNS:

      -1 if c_blkprf < e_blkprf
       1 if c_blkprf > e_blkprf
       0 if c_blkprf = e_blkprf
       2 if database error

*/

#if PROTOTYPE_ALLOWED
int check_blkprf(int c_blkprf[], int e_blkprf[])
#else
int check_blkprf(c_blkprf, e_blkprf)
int c_blkprf[], e_blkprf[];
#endif
{
   unsigned int n = 2 * sizeof(int);

   if (dbget_cnf(BLOCK_PROFILE_INDEX, (char *) c_blkprf, &n,
      "getting block-profile index")) return(2);
   if (c_blkprf[0] < e_blkprf[0]) return(-1);  /* compare block index */
   if (c_blkprf[0] > e_blkprf[0]) return(1);
   if (c_blkprf[1] < e_blkprf[1]) return(-1);  /* compare profile index */
   if (c_blkprf[1] > e_blkprf[1]) return(1);
   return(0);
}

