/*             Sat  09-21-1996  EF streamlined set_block_dpmask

    FILE:  dbdpmask.c

           Source routines for setting and checking the data processing mask
           in a CODAS database.

*/
#include <stdio.h>
#include "dbext.h"   /* ULONG, BLOCK_PROFILE_SEARCH, DATA_PROC_MASK, ... */
#include "ioserv.h"  /* error_found(), report_msg(), set_long_bit() */
#include "use_db.h"  /* db*_cnf() */

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

   FUNCTION: set_block_dpmask

      Turns on/off indicated bit in data processing mask of current block.

   PARAMETER:

      bit_num = which bit to turn on/off (see INCLUDE\DPMASK.H)

   RETURNS:

      0 if okay
      1 if database error

*/

#if PROTOTYPE_ALLOWED
int set_block_dpmask(int bit_num)
#else
int set_block_dpmask(bit_num)
int bit_num;
#endif
{
   ULONG dpmask;
   unsigned int nmask = sizeof(ULONG);

   if (dbget_cnf(DATA_PROC_MASK, (char *) &dpmask, &nmask, "getting data processing mask"))
      return(1);
   set_long_bit((FILE *) NULL, (char *) &dpmask, bit_num);
   if (dbput_cnf(DATA_PROC_MASK, (char *) &dpmask, &nmask, "storing data processing mask"))
      return(1);
   return(0);
}

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

   FUNCTION: set_db_dpmask

      Turns on/off the appropriate bit in the data processing mask for each
      block in an open database.

   PARAMETER:

      bit_num = which bit to turn on/off (see INCLUDE\DPMASK.H)

   RETURNS:

      0 if okay
      1 if database error

*/

#if PROTOTYPE_ALLOWED
int set_db_dpmask(int bit_num)
#else
int set_db_dpmask(bit_num)
int bit_num;
#endif
{
   int iblkprf[2];
   int ierr = 0;

   iblkprf[0] = iblkprf[1] = 0;
   if (dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) iblkprf)) return(1);
                                         /* reposition to database beginning */
   do
   {
      set_block_dpmask(bit_num);
      iblkprf[0]++;
      ierr = dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) iblkprf);
   } while (!ierr);
   if (error_found((ierr != NO_SUCH_BLOCK_PROFILE),
      "searching by block-profile index")) return(1);
   return(0);
}

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

    FUNCTION:  check_block_mask

      Checks if data processing mask for current database block has the
      given bit already set.

    PARAMETER:

      bit_num = which bit position to check (see INCLUDE\DPMASK.H)

    RETURNS:

      0 if not set
     >0 if set
     -1 if database error

*/

#if PROTOTYPE_ALLOWED
int check_block_dpmask(int bit_num)
#else
int check_block_dpmask(bit_num)
int bit_num;
#endif
{
   ULONG test_mask = 1L, dpmask;
   unsigned int n = sizeof(ULONG);

   test_mask <<= bit_num;

   if (dbget_cnf(DATA_PROC_MASK, (char *) &dpmask, &n, "getting data processing mask"))
      return(-1);
   return(dpmask & test_mask);
}
