
/****************************************************************
*                                                               *
* fortnum.c                                                     *
*                                                               *
* The fortran formatted file is really hard for C to read       *
*   because a record in fortran is 1024 bytes long max, but     *
*   the DEM file can split up a record to an arbitrary number   *
*   of lines (\n in C).  Reading a whole record implies         *
*   meticulously keeping track of the bookkeeping information   *
*   in the beginning of every DEM record. (# of numbers that    *
*   follow.                                                     *
*                                                               *
*****************************************************************/


#include <stdio.h>
#include <stdlib.h> /* log: provides function exit() */
#include <string.h>
#include "proto.h"

extern char *bufptr, *buf_end;
char *index(), *rindex();

void GetInt(int *num)
{
    int tmp; /* log: removed unused variable i */
    char str[7];
    char temp[51];

    /* fprintf(stderr, "   buf_end - bufptr is %d\n",buf_end-bufptr); */
    if ((bufptr + 7) > buf_end) {
        /* fprintf(stderr, "   getting a new buffer!!!\n"); */
        GetBuffer();
        /* fprintf(stderr, "   buf_end - bufptr is %d\n",buf_end-bufptr); */
        if ((bufptr + 7) > buf_end) {
            /* fprintf(stderr, "   getting a new buffer!!!\n"); */
            GetBuffer();
            /* fprintf(stderr, "   buf_end - bufptr is %d\n",buf_end-bufptr); */
        }
    }
    bcopy(bufptr, str, 6);
    str[6] = '\0';
    if (sscanf(str, "%d", &tmp) != 1) {
        fprintf(stderr, "GetInt: error reading number: \n");
        fprintf(stderr, " (%s)\n", str);
        fprintf(stderr, "Next 50 characters: \n");
        bcopy(bufptr,temp,50);
        fprintf(stderr, " (%s)\n", temp);
        *num = 0;
        exit(1);
    }
    /* fprintf(stderr, "   the string read was (%s)\n",str); */
    *num = tmp;
    bufptr += 6;
}



void GetDouble(double *num)
{
    double tmp;
    char str[25], *ptr = NULL;

    if ((bufptr + 24) >= buf_end)
        GetBuffer();
    (void) strncpy(str, bufptr, 24);
    str[24] = '\0';

    ptr = index(str, 'D');
    if (ptr && ((ptr[1] == '+') || (ptr[1] == '-')))
        *ptr = 'E';
    if (sscanf(str, "%lf", &tmp) != 1) {
        fprintf(stderr, "GetDouble: error reading double.\n");
        exit(1);
    }
    *num = tmp;
    bufptr += 24;
}



void GetFloat(float *num)
{
    float tmp;
    char str[13];

    if ((bufptr + 12) >= buf_end)
        GetBuffer();
    bcopy(bufptr, str, 12);
    str[12] = '\0';
    if (sscanf(str, "%f", &tmp) != 1) {
        fprintf(stderr, "GetFloat: error reading number: \n");
        fprintf(stderr, " (%s)\n", str);
        *num = 0.0;
        exit(1);
    }
    *num = tmp;
    bufptr += 12;
}

/*** end fortnum.c ***/


