/***********************************************************************
 *
 * TITLE:	util.c
 *
 * AUTHOR:	Kevin Miller
 *
 * DESCRIPTION:	This module is part of the OPPS editor, xopps.  It contains
 *		utilities for sfos.
 *
 *                              CHANGE HISTORY
 *
 * $Log: util.c,v $
 * Revision 1.7  1994/05/04  23:34:36  clm
 * ported to ANSI C
 *
 * Revision 1.6  1992/07/07  19:46:01  clm
 * changed chart.start_date to objary[0].z.chtptr->start_date
 * same for end_date
 *
 * Revision 1.5  1992/07/06  17:54:37  clm
 * added issue_date function
 *
 * Revision 1.4  1992/06/12  18:18:41  clm
 * added x_to_utc routine
 *
 * Revision 1.2  1992/05/20  19:50:57  clm
 * cleaned up file and help format
 *
 * Revision 1.1  1992/05/11  22:39:37  kevin
 * renamed project include file to xopps.h
 *
 ***********************************************************************
 *
 * WARNINGS:
 *
 * EXTERNAL CALLABLE COMPONENTS (PUBLIC):
 *
 * GLOBALS:
 *
 * WAIVERS:
 *
 * NOTES:
 *
 * MANPAGE:
 *
 ***********************************************************************/

#ifndef lint
static char rcsid[] =
    "$Id: util.c,v 1.7 1994/05/04 23:34:36 clm OEL $";
#endif

#include "xopps.h"

/***********************************************************************
 *
 * FUNCTION:
 *      skip_blanks()
 *
 * INPUTS:
 *      ptr     - (char **)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	checks for white spaces and ignores them
 *
 */

void 
skip_blanks(char **ptr)
{
    while (isspace(**ptr)) ++(*ptr);
}


/***********************************************************************
 *
 * FUNCTION:
 *      get_number()
 *
 * INPUTS:
 *      ptr     - (char **)
 *      num     - (int *)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	gets a number 
 *
 */

int 
get_number(char **ptr, int *num)
{
    int tmpsign, tmpval;        /* temporary sign and value */

    if (**ptr == '+') {
        tmpsign = 1;
        ++(*ptr);
    } else if (**ptr == '-') {
        tmpsign = -1;
        ++(*ptr);
    } else if (isdigit(**ptr)) {
        tmpsign = 1;
    } else {
         return -1;
    }
    tmpval = 0;
    while ((!isspace(**ptr)) && (**ptr != '\0')) {
        if (isdigit(**ptr)) {
            tmpval = 10 * tmpval + (**ptr - '0');
            ++(*ptr);
        } else {
            return -1;
        }
    }
    if ((**ptr) == '\0') {
        (*ptr)--;
    }
    (*num) = tmpsign * tmpval;
    return 0;
}

/***********************************************************************
 *
 * FUNCTION:
 *      utc_to_x()
 *
 * INPUTS:
 *      time    - (utc_val)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *      converts the utc time to a x position.
 *
 */

int 
utc_to_x(utc_val time)
{
    int x;              /* x-coordinate */
    utc_val t1;		/* begin time */ 
    utc_val t2;     	/* end - begin time */

    t1 = time - objary[0].z.chtptr->start_date;
    t2 = objary[0].z.chtptr->end_date - objary[0].z.chtptr->start_date;

    if ( time < objary[0].z.chtptr->start_date) {
        x = XVAL_EARLY;
    } else if (time >= objary[0].z.chtptr->end_date) {
        x = XVAL_LATE;
    } else {
        while (t2 > (1024 * 1024)) {
            t2 /= 1024;
            t1 /= 1024;
        }
        x = (2 * (x_right_margin - x_time_offset) * t1 + t2 - 1) / (2 * t2) +
             x_time_offset;
    }
    return x;
}

/***********************************************************************
 *
 * FUNCTION:
 *      x_to_utc()
 *
 * INPUTS:
 *      x               - (int)
 *      utc_time        - (utc_val *)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *      converts the x position to a utc time.
 *
 */

void 
x_to_utc(int x, utc_val *utc_time)
{
    int dx;
    int time;
    int counter;
    utc_val dt;
    utc_val totaltime;

    counter = 0;
    dx = x - x_time_offset;
    time = x_right_margin - x_time_offset;
    totaltime = objary[0].z.chtptr->end_date - objary[0].z.chtptr->start_date;

    if (totaltime > (1024 * 1024)) {
        while (totaltime > (1024 * 1024)) {
            totaltime /= 1024;
            counter++;
        }
        dt = (2 * totaltime * dx + time - 1)/(2 * time);
        (*utc_time) = objary[0].z.chtptr->start_date + ((counter * 1024) * dt);
    } else {
        counter = 1;
        dt = (2 * totaltime * dx + time - 1)/(2 * time);
        (*utc_time) = objary[0].z.chtptr->start_date + dt;
    }
}

/********************************************************************
 *
 * FUNCTION:
 *	issue_date()
 *
 * INPUTS:
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	sets the issue date display on the screen
 */

void 
issue_date(void)
{
    long s;			/* seconds since 00:00:00 GMT, 01/01/70 */
    struct tm *t;		/* local time structure */

    s = time(NULL);
    t = localtime(&s);
    (void)sprintf(issue_dt, "ISSUE DATE: %02d/%02d/%02d", t->tm_mon + 1, 
	t->tm_mday, t->tm_year % 100);
}
