/*****************************************************************************
;  :Module.		dumpfile.c
;  :Author.		Bert Jahn
;  :EMail.		jah@fh-zwickau.de
;  :Address.	Franz-Liszt-Straße 16, Rudolstadt, 07404, Germany
;  :Version.	$Id: dumpfile.c 0.2 1998/12/13 23:35:08 jah Exp jah $
;  :History.	18.07.98 started
;				13.12.98 dumpfilename from whdload.prefs
;  :Copyright.	All Rights Reserved
;  :Language.	C
;  :Tabsize.	4
;  :Translator.	SAS 6.58
*****************************************************************************/

#include <libraries/iffparse.h>
#include "whddump.h"

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/muimaster_protos.h>

#include <pragmas/muimaster_pragmas.h>

#include <string.h>

extern struct Library *MUIMasterBase;
extern APTR app,win;

extern struct whddump_header	* header;
extern struct whddump_cpu		* cpu;
extern struct whddump_custom	* custom;
extern struct whddump_cia		* ciaa;
extern struct whddump_cia		* ciab;
extern APTR						* slave;
extern APTR						* mem;

/****************************************************************************/

APTR	* dumpfile = NULL;

/****************************************************************************/

void freedump(void) {
	if (dumpfile) {
		FreeVec(dumpfile);
		dumpfile = NULL;
	}
}

BOOL loaddump(STRPTR name) {
	BOOL ret = FALSE;
	BPTR fh;
	ULONG size;
	ULONG *tmp;
	char filename[256]="";
	char s[256];
	char *t;

	/*
	 * if there is no filename for the dump overgiven try to load
	 * whdload config and get the path from there
	 */
	if (name) {
		strcpy(filename,name);
	} else {
		fh = Open("S:whdload.prefs",MODE_OLDFILE);
		if (fh) {
			while (FGets(fh,s,256)) {
				if (strnicmp("coredumppath=",s,13) == 0) {
					t = strpbrk(&s[13]," \t\n\r");
					if (t) strncpy(filename,&s[13],t-s-13);
					else strcpy(filename,&s[13]);
					break;
				}
			}
			Close(fh);
		}
		strcat(filename,".whdl_dump");
	}
		
	/*
	 * free any loaded dump
	 */
	freedump();

	/*
	 * load dump
	 */
	if (NULL == (fh = Open(filename,MODE_OLDFILE))) {
			MUI_Request(app,win,0,NULL,"Ok","Could not open \"%s\".",filename);
		} else {
		Seek(fh,0,OFFSET_END);
		size = Seek(fh,0,OFFSET_BEGINNING);
		if (dumpfile = AllocVec(size,0)) {
			if (size == Read(fh,dumpfile,size)) {
				tmp = (ULONG*)dumpfile;
				if ((*tmp == ID_FORM) &&(*(tmp+2) == ID_WHDD)) {
					tmp += 3;
					size -= 12;
					while (size>0) {
						tmp += 2;
						size -= 8;
						switch (*(tmp-2)) {
						case ID_HEAD:
							header = (struct whddump_header*)tmp;
							break;
						case ID_CPU:
							cpu = (struct whddump_cpu*)tmp;
							break;
						case ID_CUST:
							custom = (struct whddump_custom*)tmp;
							break;
						case ID_CIAA:
							ciaa = (struct whddump_cia*)tmp;
							break;
						case ID_CIAB:
							ciab = (struct whddump_cia*)tmp;
							break;
						case ID_SLAV:
							slave = (APTR)tmp;
							break;
						case ID_MEM:
							mem = (APTR)tmp;
							break;
						}
						size -= *(tmp-1);
						tmp += (*(tmp-1))>>2;
					}
					ret = TRUE;
				}
			}
			if (!ret) {
				FreeVec(dumpfile);
				dumpfile = NULL;
			}
		}
		Close(fh);
	}
	return ret;
}

/****************************************************************************/

