#include <stdio.h>
#include <string.h>
#include "dbhost.h"          /* PROTOTYPE_ALLOWED */
#include "dbext.h"           /* DOUBLE, 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:  prtarray.c

              General array printing functions and supporting routines.

*/
#if PROTOTYPE_ALLOWED
void clear_screen(void);
void check_screen_lines(int increm);
#else
void clear_screen();
void check_screen_lines();
#endif

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

    The following external variable declarations are used by the printing
    routines below in order to keep track of the cursor position in the
    case of printing to stdout.  The variable screen_lines is initialized to
    zero each time a call to clear_screen is made, and is incremented
    by the print functions each time a line is printed to the screen.  The
    variable max_screen_lines is used as a cutoff point to pause screen
    output until the user presses the enter key, so screen output does not
    scroll off.  It is set to a default value appropriate for 24/25-line
    monitors.  It may be reset using the function set_max_screen_lines below.

*/
int screen_lines;
int max_screen_lines = 20;

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

    FUNCTION:  clear_screen

       Uses ANSI escape sequence to clear the screen and sets the external
       variable screen_lines back to zero.

*/
#if PROTOTYPE_ALLOWED
void clear_screen(void)
#else
void clear_screen()
#endif
{
   printf("%c[2J%c[H", 0x1B, 0x1B);
   screen_lines = 0;
}

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

   FUNCTION:  check_screen_lines

      It increments the global screen_lines counter by the no. of
      additional lines just printed as indicated by its argument.
      It then checks if more than max_screen_lines have been printed,
      in which case it pauses until the user presses the enter key,
      prior to clearing the screen.

   PARAMETER:  increm = no. by which to increment screen_lines counter

*/
#if PROTOTYPE_ALLOWED
void check_screen_lines(int increm)
#else
void check_screen_lines(increm)
int increm;
#endif
{
   screen_lines += increm;
   if (screen_lines >= max_screen_lines)
   {
      printf("\n--More--");
      getchar();
      clear_screen();
   }
}

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

   FUNCTIONS:  print_array_1u, print_array_1s, print_array_2, print_array_4,
               print_array_float, print_array_double

      These print the array elements to a file.

   PARAMETERS:

      fp = pointer to FILE to print to (usually stdout)

      array = pointer to first element in array

      nv = number of values in the array to be printed

      ncol = no. of columns per print line

      format = print format string, e.g., "%7d"

   RETURN:  VOID

*/
#if PROTOTYPE_ALLOWED
unsigned int print_array_1u(FILE *fp, UBYTE array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_1u(fp, array, nv, ncol, format)
FILE *fp;
UBYTE array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      fprintf(fp, format, array[i]);
   }
   return(nv);
}

#if PROTOTYPE_ALLOWED 
unsigned int print_array_1s(FILE *fp, BYTE array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_1s(fp, array, nv, ncol, format)
FILE *fp;
BYTE array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      fprintf(fp, format, array[i]);
   }
   return(nv);
}

#if PROTOTYPE_ALLOWED 
unsigned int print_array_2u(FILE *fp, USHORT array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_2u(fp, array, nv, ncol, format)
FILE *fp;
USHORT array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      fprintf(fp, format, array[i]);
   }
   return(nv * sizeof(SHORT));
}

#if PROTOTYPE_ALLOWED 
unsigned int print_array_2s(FILE *fp, SHORT array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_2s(fp, array, nv, ncol, format)
FILE *fp;
SHORT array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      fprintf(fp, format, array[i]);
   }
   return(nv * sizeof(SHORT));
}

#if PROTOTYPE_ALLOWED 
unsigned int print_array_4(FILE *fp, LONG array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_4(fp, array, nv, ncol, format)
FILE *fp;
LONG array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      fprintf(fp, format, array[i]);
   }
   return(nv * sizeof(LONG));
}

#define badfloat(x) ((x) < ADJ_BADFLOAT ? 0 : 1)

#if PROTOTYPE_ALLOWED
unsigned int print_array_float(FILE *fp, FLOAT array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_float(fp, array, nv, ncol, format)
FILE *fp;
FLOAT array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;
   char *fptr;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      if (badfloat(array[i]) && (fptr = strchr(format, '%')) != NULL && 
	 (fptr = strchr(fptr, 'f')) != NULL)
      {
	 *fptr = 'g';  /* switch to scientfic notation */
	 fprintf(fp, format, array[i]);
	 *fptr = 'f';  /* restore */
      }
      else
         fprintf(fp, format, array[i]);
   }
   return(nv * sizeof(FLOAT));
}

#if PROTOTYPE_ALLOWED
unsigned int print_array_double(FILE *fp, DOUBLE array[], unsigned int nv, int ncol, char *format)
#else
unsigned int print_array_double(fp, array, nv, ncol, format)
FILE *fp;
DOUBLE array[];
unsigned int nv;
int ncol;
char *format;
#endif
{
   unsigned int i;
   char *fptr;

   ncol = max_val(ncol, 1);
   for (i = 0;  i < nv;  i++)
   {
      if (i)
         if (i % ncol == 0)
         {
            fprintf(fp, "\n");
            if (fp == stdout) check_screen_lines(1);
         }
      if (badfloat(array[i]) && (fptr = strchr(format, '%')) != NULL && 
	 (fptr = strchr(fptr, 'f')) != NULL)
      {
	 *fptr = 'g';  /* switch to scientific notation */
	 fprintf(fp, format, array[i]);
	 *fptr = 'f';  /* restore */
      }
      else
         fprintf(fp, format, array[i]);
   }
   return(nv * sizeof(DOUBLE));
}

