/* ---------------------------------------------------------------------

   PWS16UTL written by R. Baines

   Misc. Utils to aid some of processing done in various parts of
   PWS16PC.

   --------------------------------------------------------------------- */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "pwstruct.h"
#include "pwsglobl.h"
#include "proto.h"


/* -------------------------------------------------------------------
   TIME2SEC written by R. Baines
   convert time in years, day of year, hour, minute and second to seconds
   since Jan. 1, 1966.
   ------------------------------------------------------------------- */

double time2sec(int year, int doy, int hour, int min, float sec)
{

   register double the_time;

   year -= 65;
   doy--;

   the_time = year / 4 * DAY_SEC;
   the_time += year * YEAR_SEC + doy * DAY_SEC + hour * (double) HOUR_SEC
               + min * (double) MIN_SEC + sec;

   return(the_time);
}



/* -------------------------------------------------------------------
   SEC2TIME written by R. Baines
   convert seconds since Jan. 1, 1966 to time in years, day of year,
   hour, minute and seconds.
   ------------------------------------------------------------------- */

void sec2time(double tyme, int *yr, int *doy, int *hr, int *min, float *sec)
{
   long leap_yr;

   leap_yr = (long) (tyme / YEAR_SEC / 4) * DAY_SEC;
   *yr =  (double) (tyme-leap_yr) / (YEAR_SEC);
   tyme -= (*yr * YEAR_SEC + leap_yr);
   *yr += 65;

   *doy = tyme / DAY_SEC;
   tyme -= (*doy) * DAY_SEC;
   (*doy)++;

   *hr = tyme / (HOUR_SEC);
   tyme -= *hr * (long) HOUR_SEC;
   *min = tyme/ MIN_SEC;
   tyme -= *min * (long) MIN_SEC;
   *sec = tyme;
}



/* -------------------------------------------------------------------
   DOY written by R. Baines
   finds what month it is and the day of the month from the day of
   the year.  (EX:  day 319 is Nov. 15 if not a leap year)
   ------------------------------------------------------------------- */

void doy(doy_date *the_date, int doy, int year)
{
   static int mon_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   int i;

   if (year%4)
      mon_days[1] = 28;
   else
      mon_days[1] = 29;

   for (i=0; i<12; i++)
   {
      if (doy<mon_days[i])
      {
         the_date->month = i+1;
         the_date->mday = doy;
         return;
      }
      doy -= mon_days[i];
   }
}


/* -------------------------------------------------------------------
   PAUSE written by R. Baines
   pauses until any key is hit.
   ------------------------------------------------------------------- */

void pause(void)
{
   if (kbhit()) getch();
   getch();
}

/* -------------------------------------------------------------------
   PAUSE written by R. Baines
   pauses, beeps and waits until any key is hit.
   ------------------------------------------------------------------- */
void pause_bell(void)
{
   putchar('\a');
   if (kbhit()) getch();
   getch();
}



/* -------------------------------------------------------------------
   FILEOUT written by R. Baines
   does a simple output of data files in an ascii format. for test
   purposes only.
   ------------------------------------------------------------------- */

void fileout(int position, int data[], int datmin, int datmax)
{
	int ibin;

	fprintf(fpOut, "%10ld %6d %6d %6d %6d %6d %6d\n", Dat.length, position, Dat.flag,
                Dat.spare0, Dat.spare1, datmin, datmax);

	for (ibin=0; ibin<900; ibin++) {
	 fprintf(fpOut, "%6d", data[ibin]);
		 if ((ibin+1)%13 == 0)
			 fprintf(fpOut, "\n");
	}
	for (ibin=0; ibin < 124; ibin++) {
		 fprintf(fpOut, "%6d",0);
		 if ((ibin+1)%13 == 0)
			 fprintf(fpOut, "\n");
	}
	fprintf(fpOut, "\n");
}



/* -------------------------------------------------------------------
   STRPOS written by R. Baines
   Returns position of character in a string as an indexed position.
   If not found returns ZERO.

   ------------------------------------------------------------------- */

int strpos(char *src, char ch)
{
   char *pos;

   pos = strchr(src, ch);

   if (pos)
      return( (int) (src - pos));

   return (0);
}


/* -------------------------------------------------------------------
   CTCHR written by R. Baines
   Returns number of occurrences of character in a string.

   ------------------------------------------------------------------- */

int ctchr(char *src, char ch)
{
   char *pos;
   int ct=0;

   pos = src;

   while (pos=strchr(pos, ch)) {
      ct++;
      pos++;
   }

   return (ct);
}


/*-------------------------------------------------------------------

  ERROR written by R. Baines
  organize error messages all in one area. Allows you to exit
  program or return to where error message was given.

  ------------------------------------------------------------------- */

void error(int num)
{
  printf("ERROR %d:\t", num);
  switch (num) {

    case 0:
         puts("Request Not Found.");
		exit(0);

    case 1:
         puts("Invalid Length in Request.");
         exit(0);

    case 2:
         puts("Survey Not TRUE.");
         exit(0);

    case 3:
         puts("Invalid Length On Second Check - Should Never Happen");
         exit(0);

    case 4:
         puts("Data Input Corruption");
         exit(0);

    case 5:
         puts("Found Unexpected EOF in Data File");
         return;

    case 6:
	 puts("Data File Not Found");
	 pws16_end();
	 exit(0);

    case 7:
         puts("Not Enough Memory To Allocate Tables");
         exit(0);

    case 8:
	 puts("Unable to use Output Device");
         exit(0);

    case 9:
	 puts("Calibration File Not Found");
	 pws16_end();
         exit(0);

    case 10:
         puts("BGI Object File Not included at Compile Time");
	 pws16_end();
         exit(0);

    case 11:
	 puts("Unable to Open Graphics Mode Indicated");
	 pws16_end();
         exit(0);

    case 12:
	 puts("Unable to Establish Font");
	 pws16_end();
         exit(0);

    case 13:
         puts("Error establishing network connection");
         exit(0);

    case 14:
         puts("Error sending first network message");
	 pws16_end();
         exit(0);

    case 15:
	 puts("Error receiving first network message");
	 pws16_end();
         exit(0);

    case 16:
	 puts("Incorrect status returned by PWS16_SERVER on first network dialog.");
	 pws16_end();
         exit(0);

    case 17:
	 puts("Invalid INPUT type: Valid Types = NET, FILE, and SERVER");
	 return;

    case 18:
         puts("Invalid Filename");
         return;

    case 19:
	 puts("Unable to Run Program");
	 return;

    case 20:
	 puts("Unable to Shell Out");
	 return;

    case 21:
	 puts("");
	 pws16_end();
         exit(0);

    case 22:
	 puts("");
	 pws16_end();
         exit(0);

    case 23:
	 puts("");
	 pws16_end();
         exit(0);

    default:
	 puts("Undefined Error");
	 pws16_end();
	 exit(0);
  }
}


