#include <stdio.h>
#include "dbext.h"    /* LONG, etc. */
#include "posn.h"     /* POSHUN(), HUNPOS() */
#include "time_.h"    /* UPCKTIM() */
#include "prtspecl.h" /* int_bit_pattern(), print_dmsh_position(), print_ymdhms_time() */
/*****************************************************************************
*                                                                            *
*      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:  prtspecl.c

              Specialized printing functions and supporting routines.

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

   FUNCTION:  print_ymdhms_time

      It prints a YMDHMS_TIME_TYPE structure in the standard format.

   PARAMETER:

      fp = pointer to file to print to

      t = pointer to YMDHMS_TIME_TYPE structure

   RETURN:  VOID

*/
#if PROTOTYPE_ALLOWED
unsigned int print_ymdhms_time(FILE *fp, YMDHMS_TIME_TYPE *t)
#else
unsigned int print_ymdhms_time(fp, t)
FILE *fp;
YMDHMS_TIME_TYPE *t;
#endif
{
   fprintf(fp, "%04d/%02d/%02d %02d:%02d:", t->year, t->month, t->day,
      t->hour, t->minute);
   if (t->second & 0x8000)
      fprintf(fp, "%05.2f", (USHORT)(t->second ^ 0x8000) / 100.0);
   else
      fprintf(fp, "%02d", t->second);

   return(sizeof(YMDHMS_TIME_TYPE));
}

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

   FUNCTION:  print_dmsh_position

      It prints a DMSH_POSITION_TYPE structure in the standard format.

   PARAMETERS:

      fp = pointer to file to print to

      p = pointer to DMSH_POSITION_TYPE structure

   RETURN:  VOID

*/
#if PROTOTYPE_ALLOWED
unsigned int print_dmsh_position(FILE *fp, DMSH_POSITION_TYPE *p)
#else
unsigned int print_dmsh_position(fp, p)
FILE *fp;
DMSH_POSITION_TYPE *p;
#endif
{
   char c = ' ';
   LONG l;
   DMSH_POSITION_TYPE temp;

   if (p->degree == BADSHORT)
      fprintf(fp,"MAX");
   else
   {
      l = POSHUN(p);
      if (l >= 129600000L) l -= 129600000L; /* l should be < 360 degrees */
      if (l >=  64800000L) l -= 129600000L; /* -180 < l <= +180 */
      HUNPOS(&temp, &l);
      if ((temp.degree < 0) ||
         (temp.minute < 0) ||
         (temp.second < 0) ||
         (temp.hundredth < 0))
         c = '-';
      fprintf(fp, "%c%3d %2d' %2d.%02d\"", c,
         abs_val(temp.degree),
         abs_val(temp.minute),
         abs_val(temp.second),
         abs_val(temp.hundredth));
   }
   return(sizeof(DMSH_POSITION_TYPE));
}

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

   FUNCTION: int_bit_pattern

      Given a ULONG integer k, it generates the binary representation of k
      in a CHAR array.

   PARAMETERS:

      k = the ULONG integer

      b = array to store binary representation in

   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
void int_bit_pattern(ULONG k, char *b)
#else
void int_bit_pattern(k, b)
ULONG k;
char *b;
#endif
{
   int i;
   for (i = 0;  i < 8 * sizeof(ULONG);  i++)
   {
      if (k & ( 1L << i))
         b[i] = '\001';
      else b[i] = '\0';
   }
   return;
}

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

   FUNCTION:  print_bit_pattern

      It prints the bit pattern of a ULONG integer k in the format

            bbbb bbbb  bbbb bbbb  bbbb bbbb  bbbb bbbb

   PARAMETERS:

      fp = pointer to file to print bit pattern to (usually stdout)

      k = pointer to ULONG integer to print as bit pattern

   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
unsigned int print_bit_pattern(FILE *fp, ULONG *k)
#else
unsigned int print_bit_pattern(fp, k)
FILE *fp;
ULONG *k;
#endif
{
   char b[32];
   int ibyte, i1, i;

   int_bit_pattern(*k, b);
   for (ibyte = 3;  ibyte >= 0;  ibyte--)
   {
      i1 = ibyte * 8 + 7;
      for (i = i1;  i > (i1 - 4);  i--)
         fprintf(fp, "%1d", b[i]);
      fprintf(fp, " ");
      for (i = (i1 - 4);  i > (i1 - 8);  i--)
         fprintf(fp, "%1d", b[i]);
      fprintf(fp, "  ");
   }
   return(sizeof(ULONG));
}

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

   FUNCTION:  prpckt

      It unpacks then prints a packed time ULONG in the standard time format.

   PARAMETERS:

      fp = pointer to file to print to

      pckt = pointer to packed time

   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
unsigned int prpckt(FILE *fp, ULONG *pckt)
#else
unsigned int prpckt(fp, pckt)
FILE *fp;
ULONG *pckt;
#endif
{
   YMDHMS_TIME_TYPE t;
   if (*pckt == MINULONG)
      fprintf(fp, "MIN           ");
   else if (*pckt == MAXULONG)
      fprintf(fp, "MAX           ");
   else
   {
      UPCKTIM(&t, pckt);
      print_ymdhms_time(fp, &t);
   }
   return(sizeof(ULONG));
}

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

   FUNCTION:  prpckp

      It unpacks then prints a packed position LONG in the standard format.

   PARAMETERS:

      fp = pointer to file to print to

      pckp = pointer to packed position

   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
unsigned int prpckp(FILE *fp, LONG *pckp)
#else
unsigned int prpckp(fp, pckp)
FILE *fp;
LONG *pckp;
#endif
{
   DMSH_POSITION_TYPE p;
   if (*pckp == MINLONG)
      fprintf(fp, "MIN           ");
   else if (*pckp == MAXLONG)
      fprintf(fp, "MAX           ");
   else
   {
      HUNPOS(&p, pckp);
      print_dmsh_position(fp, &p);
   }
   return(sizeof(LONG));
}

