
#include <exec/types.h>
#include <clib/exec_protos.h>
#include <libraries/asl.h>
#include <clib/asl_protos.h>
#include <string.h>

#include "filereq.h"
/*--------------------------------------------------------*/
/* ProjectOmega                 									 */
/* Written by T.Miles												 */
/* ID: 289175									 						 */
/* Module:	FileRequester Class		 					       */
/* A simple Class to make opening an Amiga File Requester */
/* That little bit easier :)                              */
/*--------------------------------------------------------*/

/*--------------------------------------------------------*/
/* Create Requester - Initialises our libraries and memory*/
/*    Also pops up a requester with the title specified   */
/*    and initialised to the required drawer              */
/*    I might add pattern matching if necessary           */
/*  	Can also be made a Save Requester by setting last   */
/*    parameter=true                                      */
/*--------------------------------------------------------*/

//
// 27/1/00 - Class now opens Library, saves the caller from having to do so
//           Caller still has to declare AslBase though
// 13/2/00 - Added support of opening and closing files
 
FILE *FileReq::CreateRequester(char *title, char * name, char *drawer, char *pattern, BOOL save, BOOL autoOpen){
	if (!drawer)
		drawer="PROGDIR:";
	if (!name)
		name="";
	
	int size;
	saveMode=save;
	
	if (AslBase){						
		char *temp=new char[256];
		requester=(FileRequester *)AllocAslRequest(ASL_FileRequest,NULL);
	
		fr_cancelled=!AslRequestTags(requester,
					ASLFR_Screen, pubScreen,
					ASLFR_InitialFile, name,
					ASLFR_TitleText,title,
					ASLFR_InitialDrawer,drawer,
					ASLFR_DoSaveMode, save,
					ASLFR_InitialPattern, pattern,
					ASLFR_DoPatterns, TRUE,					
					NULL);
// Copy the results into a string
		size=strlen(requester->fr_Drawer);
		memcpy(temp,requester->fr_Drawer,size);
		size=strlen(temp)-1;
		if (temp[size]!=':' && temp[size]!='/') 
			temp=strcat(temp,"/");
		temp=strcat(temp,requester->fr_File);
		filename=strcpy(filename,temp);
		FreeAslRequest(requester);
		delete[] temp;
		
		if (autoOpen){
			if (saveMode==FALSE)
				fHandle=fopen(filename,"r");
			else
				fHandle=fopen(filename,"w");
		}
	}
	return fHandle;
}

void FileReq::Initialise(){
	AslBase=OpenLibrary("asl.library",0);
	pubScreen=NULL;
	requester=NULL;
	saveMode=FALSE;
	fr_cancelled=FALSE;
	// Allocate space for the name
	filename=new char[255];
}

FileReq::FileReq(){
	Initialise();
}

FileReq::FileReq(Screen *scr){
	Initialise();
	pubScreen=scr;
}

/*--------------------------------------------------------*/
/* Free our memory again - but only if it's been allocated*/
/*--------------------------------------------------------*/
FileReq::~FileReq(){
	if (filename)
		delete[] filename;
	if (AslBase)
		CloseLibrary(AslBase);
}

char *FileReq::GetName(){
	return filename;
}
			
void FileReq::CloseFile(){
	if (fHandle)
		fclose(fHandle);
}

