#include "dbext.h"         /* LONG, etc. */
/*****************************************************************************
*                                                                            *
*      COMMON OCEANOGRAPHIC DATA ACCESS SYSTEM (CODAS)                       *
*                                                                            *
*      WRITTEN BY:  RAMON CABRERA, ERIC FIRING, and JULIE RANADA             *
*                   JOINT INSTITUTE FOR MARINE AND ATMOSPHERIC RESEARCH      *
*                   1000 POPE ROAD  MSB 404                                  *
*                   HONOLULU, HI 96822                                       *
*                                                                            *
*      VERSION:     3.00                                                     *
*                                                                            *
*      DATE:        APRIL 1989                                               *
*                                                                            *
*****************************************************************************/
/*

       FILE:  mem_.c

              Memory access functions.

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

   FUNCTIONS:  set_long, set_short, set_byte

      Given a buffer address and a LONG/SHORT/BYTE mask, the functions
      set n consecutive LONG/SHORT/BYTE variables to that mask.

   PARAMETERS:
      
      l/s/b buff = pointer to first LONG/SHORT/BYTE to set
                
      l/s/b_ask = value to which buffer will be set
                 
      nv = number of values to set

   RETURNS:  VOID

   CALLED FROM:  C

*/
#if PROTOTYPE_ALLOWED
void set_long(LONG *lbuff, LONG l_mask, unsigned int nv)
#else
void set_long(lbuff, l_mask, nv)
LONG *lbuff, l_mask;
unsigned int nv;
#endif
{
   unsigned int i;

   for (i = 0; i < nv; i++)
      *(lbuff++) = l_mask;
}

void set_short(sbuff, s_mask, nv)
USHORT *sbuff, s_mask;
unsigned int nv;
{
   unsigned int i;

   for (i = 0; i < nv; i++)
      *(sbuff++) = s_mask;
}

void set_byte(bbuff, b_mask, nb)
UBYTE *bbuff, b_mask;
unsigned int nb;
{
   unsigned int i;

   for (i = 0; i < nb; i++)
      *(bbuff++) = b_mask;
}

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

   FUNCTIONS:  move_byte, move_short, move_long

      These copy n BYTEs/SHORTs/LONGs from one memory location to another.

   PARAMETERS:
   
      buff1 = pointer to first element of source
                
      buff2 = pointer to first element of destination
                
      nv = number of values to copy

   RETURNS:  VOID

   CALLED FROM:  C

*/
#if PROTOTYPE_ALLOWED
void move_byte(CHAR *bbuff1, CHAR *bbuff2, unsigned int nv)
#else
void move_byte(bbuff1, bbuff2, nv)
CHAR *bbuff1, *bbuff2;
unsigned int nv;
#endif
{
   if (nv == 0)
      return;
   while (nv--)
      *(bbuff2++) = *(bbuff1++);
}

void move_short(sbuff1, sbuff2, nv)
SHORT *sbuff1, *sbuff2;
unsigned int nv;
{
   if (nv == 0)
      return;
   while (nv--)
      *(sbuff2++) = *(sbuff1++);
}

void move_long(lbuff1, lbuff2, nv)
LONG *lbuff1, *lbuff2;
unsigned int nv;
{
   if (nv <= 0)
      return;
   while (nv-- > 0)
      *(lbuff2++) = *(lbuff1++);
}

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

   FUNCTION:  cmp_byte

      It compares <nb> bytes located at <buf1> and <buf2>.

   PARAMETERS:

      buf1 = pointer to first element of first location to compare

      buf2 = pointer to first element of second location to compare

      nb = number of bytes to compare

   RETURNS:  0 if *buf1 = *buf2 in the first nb bytes
             1 if *buf1 < *buf2 in the first nb bytes
             2 if *buf1 > *buf2 in the first nb bytes

*/
#if PROTOTYPE_ALLOWED
int cmp_byte(char *buf1, char *buf2, unsigned int nb)
#else
int cmp_byte(buf1, buf2, nb)
char *buf1, *buf2;
unsigned int nb;
#endif
{
   if (nb <= 0)
      return(0);
   while (nb--)
   {
      if (*buf1 < *buf2) 
         return(1);
      else if (*buf1 > *buf2) 
         return(2);
      buf1++;
      buf2++;
   }
   return(0);
}

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

   FUNCTION:  break_short

      It breaks up an array of unsigned two-BYTE integers into two arrays:
      the first consisting of unsigned CHARS corresponding to the upper BYTE 
      of the original integers and the second consisting of unsigned CHARs 
      corresponding to the lower BYTE of the original integers.

   PARAMETERS:
   
      twobyte = pointer to array of two-BYTE integers
      
      upbyte  = pointer to UBYTE array of upper BYTEs
      
      lobyte  = pointer to UBYTE array of lower BYTEs

      nv = number of shorts in the source array

   RETURNS:  VOID

   CALLED FROM:  C

*/
#if PROTOTYPE_ALLOWED
void break_short(USHORT *twobyte, UBYTE *upbyte, UBYTE *lobyte, unsigned int nv)
#else
void break_short(twobyte, upbyte, lobyte, nv)
USHORT *twobyte;
UBYTE *upbyte, *lobyte;
unsigned int nv;
#endif
{
   unsigned int i, k;

   for (i = 0; i < nv; i++)
   {
      k = twobyte[i];
      upbyte[i] = k / 256;
      lobyte[i] = k % 256;
   }
   return;
}
