/*
** demlib.c -- a couple functions to mess with DEM files
**
** By Brian "Beej" Hall
**
** This code is in the public domain.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "demlib.h"

/*
** read_a_header() -- reads the A record header.  Should be improved to
**                    use fread()s.
*/
void read_a_header(FILE *fp, struct a_header *a)
{
	char t[200], t2[100], *p;
	int i, j;

	fread(t,1,144,fp);    /* quad name */
	t[144] = '\0';
	strcpy(a->qname, t);

	fscanf(fp, " %s", t); /* level code */
	a->lcode = atoi(t);

	fscanf(fp, " %s", t);     /* pattern code */
	a->pcode = atoi(t);

	fscanf(fp, " %s", t);     /* reference system code */
	a->rsyscode = atoi(t);

	fscanf(fp, " %s", t);     /* zone code */
	a->zcode = atoi(t);

	for(i = 0; i < 15; i++) { /* map projection parameters */
		fscanf(fp, " %s", t);
		if ((p = strchr(t, 'D')) != NULL)
			*p = 'E';
		a->projparam[i] = atof(t);
	}

	fscanf(fp, " %s", t);     /* planimetric units code */
	a->punits = atoi(t);

	fscanf(fp, " %s", t);     /* elevation units code */
	a->eunits = atoi(t);

	fscanf(fp, " %s", t);     /* polygon sides */
	a->psides = atoi(t);

	for(i = 0; i < 4; i++)    /* four corners of DEM */
		for(j = 0; j < 2; j++) {
			fscanf(fp, " %s", t);
			if ((p = strchr(t, 'D')) != NULL)
				*p = 'E';
			a->gcoords[i][j] = atof(t);
		}

	fscanf(fp, " %s", t);    /* minimum elevation */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	a->minelev = atof(t);

	fscanf(fp, " %s", t);    /* maximum elevation */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	a->maxelev = atof(t);

	fscanf(fp, " %s", t);    /* offset angle */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	a->angle = atof(t);

	fscanf(fp, " %s", t);    /* accuracy data present, spatial res */
	strncpy(t2,t,1);
	a->crecpresent = atoi(t2);
	strncpy(t2,t+1,12);
	a->sres[0] = atof(t2);
	strncpy(t2,t+13,12);
	a->sres[1] = atof(t2);
	strncpy(t2,t+25,12);
	a->sres[2] = atof(t2);
	
	fscanf(fp, " %s", t);     /* rows of B data */
	a->rows = atoi(t);

	fscanf(fp, " %s", t);     /* cols of B data */
	a->cols = atoi(t);

}

/*
** read_b_header() -- reads the B record header.  Should be improved to
**                    use fread()s.
*/
void read_b_header(FILE *fp, struct b_header *b)
{
	char t[200], *p;

	fscanf(fp, " %s", t);     /* row # */
	b->row = atoi(t);

	fscanf(fp, " %s", t);     /* col # */
	b->col = atoi(t);

	fscanf(fp, " %s", t);     /* rows of data */
	b->rows = atoi(t);

	fscanf(fp, " %s", t);     /* cols of data */
	b->cols = atoi(t);

	fscanf(fp, " %s", t);    /* ground coord x */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	b->gcoord[0] = atof(t);

	fscanf(fp, " %s", t);    /* ground coord y */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	b->gcoord[1] = atof(t);

	fscanf(fp, " %s", t);    /* elevation offset */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	b->elev = atof(t);

	fscanf(fp, " %s", t);    /* minimum elevation */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	b->minelev = atof(t);

	fscanf(fp, " %s", t);    /* maximum elevation */
	if ((p = strchr(t, 'D')) != NULL)
		*p = 'E';
	b->maxelev = atof(t);
}

/*
** seek_a_header() -- seeks to the A record header for this file
*/
void seek_a_header(FILE *fp)
{
	fseek(fp, 0L, SEEK_SET);
}

/*
** seek_b_header() -- seeks to a B record header for this file
*/
void seek_b_header(FILE *fp, int num)
{
	fseek(fp, num * 0x2000L + 0x400L, SEEK_SET);
}
