/* Extract.c
 * Extract the relevant information from a Z-Code story-file
 *  Musus Umbra 1997
 */


#include "extract.h"

#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* These are the externally visible variables (deliberately given the same */
/* names as Frotz gives them :-)                                           */

int		h_version;			/* the Z-Code version */
int		h_release;			/* the 'release' byte of the header */
char	h_serial[6];		/* the 6 'serial#' characters NB: no \0 */
int		story_size;			/* bytesize of the story-file */



/* Right, now back to our private stuff */

int extract_header_info( char *filename )
{
	char	z_header[32];
	FILE	*istream = fopen( filename, "rb" );
	int		chk;

	if ( !istream ) { return EXTRACT_OPENERROR; }		/* ie. failed */
	fseek( istream, 0, SEEK_END );
	story_size = (int) ftell( istream );
	fseek( istream, 0, SEEK_SET );
	if ( fread( z_header, 32, 1, istream ) != 1 )
	{
		fclose(istream);
		return EXTRACT_READERROR;
	}
	fclose(istream);
	h_version = z_header[0];
	h_release = (z_header[2]<<8) + z_header[3];
	memcpy( h_serial, z_header+18, 6 );
	for ( chk = 0; chk<6 ; chk++ )
		if ( !isprint(h_serial[chk]) ) { return EXTRACT_BADHEADER; }
	if ( h_version<1 || h_version>8 ) { return EXTRACT_BADHEADER; }
	return EXTRACT_SUCCESS;
}

