
	/* these routines check to see if the given value is a valid
	   one - valid time, date, etc. generally, they return 0 if the
	   parameter is valid, or -1 if not. */

#include <stdio.h>
#include "bait.h"
#include "pf.h"

static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	/* return 0 if the given string is a valid 24-hour time, 
	   in format HH:MM:SS. Note that hours range from 0-23, minutes
	   and seconds 0-59. Therefore, hour=24 or minute=60 or second=60
	   is INVALID! if invalid, return -1. */

is_time(s)
char *s;
{
	int h, m, sec;

	if (sscanf(s, "%2d:%2d:%2d", &h, &m, &sec) != 3)
		return(-1);
	if ((h < 0) || (h > 23) || (m < 0) || (m > 59) || (sec < 0) || (sec > 60))
		return(-1);
	return(0);
}

	/* return 0 if the given string is a valid date, in the form
	   DD/MM/YYYY, or -1 otherwise. 

	   valid day values are 1-31, month values 1-12, years 1990-2100. Yes,
	   in 2101, someone is going to have to think about this more carefully. */

is_date(s)
char *s;
{
	int d, m, y;

	if (sscanf(s, "%2d/%2d/%4d", &d, &m, &y) != 3)
		return(-1);
	if ((d < 1) || (d > 31) || (m < 1) || (m > 12) || (y < 1900) || (y > 2100))
		return(-1);
	if ((m == 2) && (y % 4 == 0) && (y % 400 != 0)) {
		if (d > 29)
			return(-1);
	}
	else
		if (d > days[m - 1])
			return(-1);

	return(0);
}


	/* return 0 if the given string is a valid RA, in the format
	   HH:MM:SS.sss, or -1 otherwise. hour=24 is considered invalid,
	   as is minute=60 or second=60. */

is_ra(s)
char *s;
{
	int h, m;
	double sec;

	if (sscanf(s, "%2d:%2d:%lf", &h, &m, &sec) != 3)
		return(-1);
	if ((h < 0) || (h > 23) || (m < 0) || (m > 59) || (sec < 0.0) ||
	    (sec >= 60.0))
		return(-1);
	return(0);
}
	
	/* return 0 if the given string is a valid Dec, in the format
	   (+/-)DD:MM:SS.sss, or -1 otherwise. Doesn't recognize a
	   value of '90:45:43' as invalid, but so what?. returns -1
	   if there's some invalid number. */

is_dec(s)
char *s;
{
	int d, m, sign;
	double sec;

	sign = 1;
	while (*s == ' ')
		s++;
	if (*s == '+') {
		sign = 1;
		s++;
	}
	else if (*s == '-') {
		sign = -1;
		s++;
	}	
	if (sscanf(s, "%2d:%2d:%lf", &d, &m, &sec) != 3)
		return(-1);
	d *= sign;
	if ((d < -90) || (d > 90) || (m < 0) || (m > 59) || (sec < 0.0) ||
	    (sec >= 60.0))
		return(-1);
	return(0);
}

	/* return 0 if the given string describes a valid data section on
	   the CCD chip, or -1 if not. A valid section looks like this:
	                [a:b,c:d]
	   where min <= a <= b <= max   (exact bounds in ait.config file)
	         min <= c <= d <= max   (ditto)
	*/

is_ccdsec(s)
char *s;
{	
	char ch1, ch2;
	int rowmin, rowmax, colmin, colmax, r1, r2, c1, c2;
	char string[PF_LEN + 1], val[PF_LEN + 1];
	pf_handle config;

	if ((config = pf_open(CONFIG_FILE, 1, PF_EXIST)) < 0)
		return(-1);
	if (pf_getval(config, "CCDSEC", val) == NULL) {
		pf_close(config);
		return(-1);
	}
	pf_close(config);

	if (pf_valtostr(val, string) < 0)
		return(-1);
	if (sscanf(string, "%c%d:%d,%d:%d%c", &ch1, &rowmin, &rowmax, &colmin, 
				&colmax, &ch2) != 6)
		return(-1);
	if ((ch1 != '[') || (ch2 != ']'))
		return(-1);
	if (sscanf(s, "%c%d:%d,%d:%d%c", &ch1, &r1, &r2, &c1, &c2, &ch2) != 6)
		return(-1);
	if ((ch1 != '[') || (ch2 != ']'))
		return(-1);
	if ((r1 < rowmin) || (r1 > rowmax) || (r2 < rowmin) || (r2 > rowmax) ||
	    (c1 < colmin) || (c1 > colmax) || (c2 < colmin) || (c2 > colmax) ||
	    (r1 > r2) || (c1 > c2))
		return(-1);

	return(0);
}

	/* return 0 if the filter name is a valid one, -1 otherwise */

is_filter(s)
char *s;
{
	int nfilter, i, filtcount;
	char string[PF_LEN + 1], val[PF_LEN + 1], filtname[PF_LEN + 1];
	pf_handle config;

	if ((config = pf_open(CONFIG_FILE, 1, PF_EXIST)) < 0)
		return(-1);
	if (pf_getval(config, "NFILTERS", val) == NULL) {
		pf_close(config);
		return(-1);
	}
	if (pf_valtoint(val, &nfilter) < 0) {
		pf_close(config);
		return(-1);
	}	
	for (i = 0, filtcount = 0; (i < 99) && (filtcount < nfilter); i++) {
		sprintf(string, "FILTER%02d", i);
		if (pf_getval(config, string, val) != NULL) {
			filtcount++;
			if (pf_valtostr(val, filtname) < 0) {
				pf_close(config);
				return(-1);
			}
			if (strncmp(s, filtname, strlen(s)) == 0) {
				pf_close(config);
				return(0);
			}
		}
	}
	pf_close(config);
	return(-1);
}

		
