/***************************************************************

   CHECK_DB.C

      Functions that simplify common CODAS access calls
      from C.

      Header: use_db.h
      Library: use_db_x.lib

****************************************************************/

#include "geninc.h"  /* DB*() */

/***************************************************************

   Note: DBGET and DBGET_F will return ierr as
   INVALID_TYPE_REQUEST (not multiplied by 1000) if a
   request is made for profile data (ID from 0-99) of
   a type that is not in the data list.  To the user,
   this is functionally the same as the situation in which
   the data type is in the data list, but zero bytes are
   stored.  Therefore the two functions
   check_dbget_f and check_db_get trap this error
   and convert it to a return of zero bytes.
   Similarly, check_dbsrch handles the errors
   SEARCH_BEFORE BEGINNING and SEARCH_BEYOND_END
   with just a warning message.  The function "lock_on_time"
   (called by DBSRCH when searching by time) correctly
   sets the db to the beginning and ending profiles,
   respectively, in case of these errors.

   In all of these functions, we are relying on check_error,
   which calls exit(), to handle the serious errors.  This
   is the simplest thing to do, and should have no major
   disadvantages when data are being read.  When data are
   being written, it would probably be better to use the
   milder error_found routine, and handle the errors in
   the calling program by ending profiles and blocks as needed,
   and then closing the database.  For this purpose we
   should write a similar set of routines, with some prefix
   replacing "check".  Return of a negative number instead
   of the number of bytes would signal a serious error.

******************************************************************************/

#if PROTOTYPE_ALLOWED
unsigned int check_dbget(int type, char *x, unsigned int n, char *err_msg)
#else
unsigned int check_dbget(type, x, n, err_msg)
int type;
char *x;
unsigned int n;
char *err_msg;
#endif
{
   int ierr;

   DBGET(&type, x, &n, &ierr);
   if (ierr == INVALID_TYPE_REQUEST)
      return(0);
   if (DBERROR(&ierr, err_msg)) exit(-1);
   return(n);
}

#if PROTOTYPE_ALLOWED
void check_dbset(int dbid, char *err_msg)
#else
void check_dbset(dbid, err_msg)
int dbid;
char *err_msg;
#endif
{
   int ierr;

   DBSET(&dbid, &ierr);
   if (DBERROR(&ierr, err_msg)) exit(-1);
}

#if PROTOTYPE_ALLOWED
void check_dbopen(int dbid, char *dbname, int access, int mem_mode)
#else
void check_dbopen(dbid, dbname, access, mem_mode)
int dbid;
char *dbname;
int access, mem_mode;
#endif
{
   int ierr;

   DBOPEN(&dbid, dbname, &access, &mem_mode, &ierr);
   if (DBERROR(&ierr, "DBOPEN")) exit(-1);
}

#if PROTOTYPE_ALLOWED
void check_dbsrch(int search_type, char *search_parameters)
#else
void check_dbsrch(search_type, search_parameters)
int search_type;
char *search_parameters;
#endif
{
   int ierr;

   DBSRCH(&search_type, search_parameters, &ierr);
   switch (ierr)
   {
      case 0:
         break;
      case SEARCH_BEFORE_BEGINNING:
         printf("\n%%%% WARNING: Search before beginning\n");
         break;
      case SEARCH_BEYOND_END:
         printf("\n%%%% WARNING: Search beyond end\n");
         break;
      default:
         DBERROR(&ierr, "check_dbsrch");
         exit(-1);
   }
}

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

   DBMOVE(&nsteps, &ierr);
   switch (ierr)
   {
      case 0:
         break;
      case SEARCH_BEFORE_BEGINNING: /* non-fatal */
         printf("\n%%%% WARNING: Search before beginning\n");
         break;
      case SEARCH_BEYOND_END:  /* non-fatal */
         printf("\n%%%% WARNING: Search before end\n");
         break;
      default:      /* fatal */
         DBERROR(&ierr, "check_dbsrch");
         exit(-1);
   }
}

#if PROTOTYPE_ALLOWED
void flag_data(FLOAT data[], UBYTE profile_flags[], int n, UBYTE mask)
#else
void flag_data(data, profile_flags, n, mask)
FLOAT data[];
UBYTE profile_flags[];
int n;
UBYTE mask;
#endif
{
   int i;

   for (i = 0; i < n; i++)
      if (profile_flags[i] & mask)
         data[i] = BADFLOAT;
}
