
/* readriff.c - a slightly hacked file originating with Jim Kent at
   A-Squared Systems.  Basically loads a RIFF file in two steps.  First 
   you call load_riff_head().  This will get the header stuff so you
   can check if you're in the right resolution etc before loading up
   the bulk of the file.  load_riff_body() loads the rest, calling load_vrun,
   which loads a single frame.  */

#include <exec/types.h>
#include <graphics/gfx.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <functions.h>
#include <ctype.h>
#include "jiff.h"
#include "vcomp.h"
#include "playriff.h"


/* load_vrun() -
	loads one frame from the file "infile" a global variable points to.
	Allocates a buffer to hold the frame, loads the frame, and returns a
	pointer to the buffer if all goes well.  Returns NULL if something
	goes wrong. */
Vcomp_iff *
load_vrun()
{
struct iff_chunk chunk;
struct iff_chunk *vrun;	/* a little type-punning here */

if (Read(infile, &chunk, (long)sizeof(chunk)) < sizeof(chunk))
	{
	truncated();
	return(NULL);
	}
if (chunk.iff_type.b4_type != VRUN)
	{
	fatal_load = 1;
	error("not a VRUN!", NULL);
	return(NULL);
	}
if ((vrun = SafeAllocMem(chunk.iff_length+8, 0L)) == NULL)
	{
	outta_memory();
	return(NULL);
	}
*vrun = chunk;	/* fill in the 8 bytes of the vrun */
if (Read(infile, vrun+1, chunk.iff_length) < chunk.iff_length)
	{
	truncated();
	FreeMem(vrun, chunk.iff_length+8);
	return(NULL);
	}
return((Vcomp_iff *)vrun);
}


/* load_riff_head() - opens infile on name, both globals (yuck!).  
    Reads the header into demo_rh (yet another global).  Returns
	1 if ok, 0 if io error etc. */
load_riff_head()
{
fatal_load = 0;
if ((infile = Open(name, (long)MODE_OLDFILE)) == NULL)
	{
	cant_open();
	return(NULL);
	}
if (Read(infile, &demo_rh, (long)sizeof(demo_rh)) < sizeof(demo_rh) )
	{
	truncated();
	goto BAD_EXIT;
	}
if (demo_rh.iff_type != RIFF)
	{
	error("Not a RIFF!", NULL);
	goto BAD_EXIT;
	}
return(1);
BAD_EXIT:
Close(infile);
infile = NULL;
return(NULL);
}

/* load_riff_body() - finishes up the load you started with load_riff_head.
	stuffs the frames one at a time into riff_list and bumps the riff_count
	each time it gets a frame.  Returns 1 on success, 0 on failure, and
	sets fatal_load on really bad failure */

load_riff_body()
{
int success = 0;
WORD frames_read = 0;
WORD i;
WORD start;

/* skip over the frame offset table ... wonder why I bothered with it... */
if (Seek(infile, (long)demo_rh.frame_count*sizeof(long), 
	(long)OFFSET_CURRENT) 
	== -1)
	{
	truncated();
	goto BAD_EXIT;
	}
start = riff_count;
if (demo_rh.frames_written != 0)
	demo_rh.frame_count = demo_rh.frames_written;
if (demo_rh.frame_count > MAX_RIFFS - start)
	demo_rh.frame_count = MAX_RIFFS - start;
if (demo_rh.jiffies_frame == 0)
	demo_rh.jiffies_frame = (demo_rh.height > 200 ? 8 : 4);
for (i=0; i<demo_rh.frame_count; i++)
	{
	if ((riff_list[i+start] = load_vrun(infile, name)) == NULL)
		{
		if (i == 0)
			goto BAD_EXIT;	/* need at least one frame! */
		break;	/* not enough memory for it all, but let's go on anyways*/
		}
	riff_count++;
	}
Close(infile);
return(riff_count);

BAD_EXIT:
if (infile != NULL)
	Close(infile);
return(0);
}

