*****************************************************************
*	This is the save file bit, it'll handle all warnings	*
*	about overwriting existing files etc, so it should be	*
*	fairly user friendly.					*
*	Call like this...SAVEFILE(data.L,length.L)		*
*								*
*	Returns:	0.L	for user decided not to		*
*				overwrite an existing file	*
*				or not correct an error		*
*				either way the file did not	*
*				get written			*
*			1.L	for complete success		*
*			-1.L	for some general error		*
*								*
*	I'll handle most general errors such as disk write	*
*	protected by asking if they wanna try again.		*
*****************************************************************
savefile:
	move.l	4(sp),filelength
	move.l	8(sp),fileaddress
	movem.l	d1-d7/a0-a6,-(sp)
retry:
	move    #1,-(SP)        ;Open file for write
	move.l  #myfp,-(SP) 	;Address of filename
	move    #$3d,-(SP)
	trap    #1              ;Get handle number in D0
	addq.l  #8,SP
	cmpi.w	#-33,d0		;doesn't exist
	beq	create
	tst.w	d0		;other error
	bmi	docn2
	move.w	d0,-(sp)
	lea	existalert,a0
	jsr	doalert
	move.w	(sp)+,d0
	cmpi.w	#1,intout
	beq	conts
	moveq	#0,d0
	bra	saveback
conts:
	move.w	d0,-(sp)
cb:
	move.w	(sp),d0
	move.l  fileaddress,-(SP)
	move.l  filelength,-(SP)	;length to write in
	move    D0,-(SP)	;Handle
	move    #$40,-(SP)	;bios number
	trap    #1		;write file
	add.l   #12,SP
	tst.w	d0
	bmi	failwrite
	move    #$3E,-(SP)      ;Handle already on stack
	trap    #1              ;Close file
	addq.l  #4,SP
	moveq	#1,d0
saveback:
	movem.l	(sp)+,d1-d7/a0-a6
	rts
create:
	move.w	#$0,-(sp)	;its a new file so I gotta create it
	move.l	#myfp,-(sp)	;filename
	move.w	#$3c,-(sp)
	trap	#1
	addq.l	#8,sp
	tst.w	d0
	bmi.s	docn		;still something wrong
	move.w	d0,-(sp)	;put the handle on the stack
	bra	cb
docn:
	lea	save1,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	create
	moveq	#0,d0
	bra	saveback
docn2:
	lea	save1,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	retry
	moveq	#0,d0
	bra	saveback
failwrite:
	lea	save2,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	cb
	
	move    #$3E,-(SP)      ;Handle already on stack
	trap    #1              ;Close file
	addq.l  #4,SP
	moveq	#-1,d0
	bra	saveback

filelength:	dc.l	0
fileaddress:	dc.l	0
save1:
		dc.b	'[1][Cannot create file.|'
		dc.b	'Is the disk write protected ?]'
		dc.b	'[Try again|Cancel]',0,0
save2:
		dc.b	'[1][Cannot write file.|'
		dc.b	'Is the disk write protected ?]'
		dc.b	'[Try again|Cancel]',0,0
existalert:
		dc.b	'[1][File already exists !|'
		dc.b	'Overwrite ?]'
		dc.b	'[Yes|No]',0,0

	even
*****************************************************************
*	Load a file with complete path and name in myfp		*
*	Simplicity itself					*
*	call LOADFILE(address.L)				*
*				just give me the place to put	*
*				it.				*
*	Returns:						*
*				1.L	for complete success	*
*				-1.L	for file did not exist	*
*					and user wants to try	*
*					again.			*
*				-2.L	for file did not exist	*
*					and user doesn't give	*
*					a shit.			*
*				-3.L	for some general error	*
*****************************************************************
loadfile:
	move.l	4(sp),fileaddress
	movem.l	d1-d7/a0-a6,-(sp)

	move    #0,-(SP)        ;Open file
	move.l  #myfp,-(SP) ;Address of filename
	move    #$3D,-(SP)
	trap    #1              ;Get handle number in D0
	addq.l  #8,SP
	tst.w	d0
	bmi	loaderror
	move.w	d0,-(sp)
	move.w	(sp),d0
	move.l  fileaddress,-(SP)	;load it straight onto screen
	move.l  #1000000,-(SP)	;shouldn't be any files bigger than this
	move    D0,-(SP)	;Handle
	move    #$3F,-(SP)	;bios number
	trap    #1		;Read file in
	add.l   #12,SP
	tst.l	d0
	bmi	loaderror
	move.l	d0,loadlength
	move    #$3E,-(SP)      ;Handle already on stack
	trap    #1              ;Close file
	addq.l  #4,SP
	moveq	#1,d0
loadback:
	movem.l	(sp)+,d1-d7/a0-a6
	rts

loaderror:
	addq.l	#2,sp
	cmpi.w	#-33,d0
	beq	fnferror
	moveq	#-3,d0
	bra	loadback
fnferror:

	bsr	doalert
	cmpi.w	#1,intout
	bne	fnf2
	moveq	#-1,d0		;say user wants to try again
	bra	loadback
fnf2:
	moveq	#-2,d0
	bra	loadback

fnealert:
	dc.b	'[1][Cannot load file!]'
	dc.b	'[Try again|Cancel]'
	even
*****************************************************************
*	FORMAT, this is a bit clumsy at present as nothing	*
*	is visible when the format is happenning, something	*
*	to look at in the phase II release.			*
*	Call by:						*
*			Format()				*
*								*
*	I do all the alerts n shit and let em choose A or B.	*
*****************************************************************
format:
	movem.l	d0-d7/a0-a6,-(sp)	;save all the regs
	lea	formatexplain,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	formatreturn
	lea	format2,a0
	bsr	doalert
	cmpi.w	#1,intout
	bne	fb
	move.w	#0,drive
	bra	formatter
fb:
	move.w	#1,drive
formatter:
	moveq	#0,d7
fml:
	move.w	d7,-(sp)

	move.w	#$0,-(sp)
	move.l	#$87654321,-(sp)
	move.w	#1,-(sp)
	move.w	#0,-(sp)	;side
	move.w	d7,-(sp)
	move.w	#9,-(sp)
	move.w	drive,-(sp)
	clr.l	-(sp)
	move.l	#fbuffer,-(sp)
	move.w	#10,-(sp)
	trap	#14
	add.l	#26,sp
	tst.w	d0
	bmi	ferror
	move.w	(sp),d7
	move.w	#$0,-(sp)
	move.l	#$87654321,-(sp)
	move.w	#1,-(sp)
	move.w	#1,-(sp)	;side
	move.w	d7,-(sp)
	move.w	#9,-(sp)
	move.w	drive,-(sp)
	clr.l	-(sp)
	move.l	#fbuffer,-(sp)
	move.w	#10,-(sp)
	trap	#14
	add.l	#26,sp
	tst.w	d0
	bmi	ferror
	move.w	(sp)+,d7
	addq.w	#1,d7
	cmpi.w	#79,d7
	ble	fml

	move.w	#0,-(sp)	;not executable
	move.w	#3,-(sp)	;80 trks dbl
	move.l	#$01000001,-(sp)
	move.l	#fbuffer,-(sp)	;produce boot sector
	move.w	#18,-(sp)
	trap	#14
	add.l	#14,sp

	MOVE #1,-(A7)		number of sectors
	MOVE #0,-(A7)		side 0
	MOVE #0,-(A7)		track 0
	MOVE #1,-(A7)		sector 1
	MOVE drive,-(A7)		drive 0
	MOVE.L #0,-(A7)		not used
	MOVE.L #fbuffer,-(A7)		buffer address
	MOVE #9,-(A7)		FLOPWR
	TRAP #14		write boot sector & directory
	ADDA.L #20,A7		tidy stack
	tst.w	d0
	bmi	ferror2
	lea	fsuccess,a0
	bsr	doalert
	bra	formatreturn
ferror:
	move.w	(sp)+,d7
	lea	ferroralert,a0
ferror2:
	bsr	doalert
	bra	formatreturn

formatreturn:
	movem.l	(sp)+,d0-d7/a0-a6
	rts

drive:	dc.w	0
fsuccess:
	dc.b	'[1][Disk formatting complete.|'
	dc.b	'This disk has 726016 bytes|'
	dc.b	'available.][ OK ]',0,0
ferroralert:
	dc.b	'[1][An error has occurred|'
	dc.b	'during formatting. Disk|'
	dc.b	'may be unusable.][ OK ]',0,0
formatexplain:
	dc.b	'[3][Formatting a disk will|'
	dc.b	'ERASE everything on it, click|'
	dc.b	'OK only if you are sure you|'
	dc.b	'wish to continue.]'
	dc.b	'[CANCEL|OK]',0,0
format2:
	dc.b	'[2][Which drive do you wish|'
	dc.b	'to format ?][ A | B ]',0,0
	even
fbuffer:	ds.b	$2000
*****************************************************************
*	Createdir, call this sucker to bring up the fileselector*
*	get the name of a directory from the file line.		*
*	make sure they wanna. and do it				*
*	Simply call   createdir()				*
*								*
*	Returns:	Buggerall				*
*****************************************************************
createdir:
	movem.l	d0-d7/a0-a6,-(sp)	;save all the regs
	lea	createexplain,a0
	bsr	doalert
	move.l	#createstring,-(sp)
	move.l	#nosearch,-(sp)
	jsr	fileselector
	addq.l	#8,sp
	cmpi.w	#1,d0		;was it successful
	beq.s	docreate
	tst.w	d0		;was it cancelled
	beq	createreturn
	lea	derroralert,a0	;musta been a general error
	bsr	doalert
createreturn:
	movem.l	(sp)+,d0-d7/a0-a6
	rts
docreate:
	lea	deletealert,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	createreturn
cretry:
	move.l	#myfp,-(sp)
	move.w	#$39,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.w	d0		;was there an error
	bpl	createreturn	;nope so return to Murray
	lea	dfailalert,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	cretry
	bra	createreturn


createstring:
	dc.b	'Create a new Sub-directory',0
createexplain:
	dc.b	'[1][Locate where you wish the new|'
	dc.b	'sub-directory to appear and|'
	dc.b	'type the name you wish on the|'
	dc.b	'selection line.]'
	dc.b	'[ OK ]',0,0
	even
*****************************************************************
*	DELETE, call this sucker to bring up the fileselector	*
*	get the name of a file and remove after asking if the	*
*	wally is certain they wanna wipe out 3 days work.	*
*	Simply call   delete()					*
*								*
*	Returns:	Buggerall				*
*****************************************************************
delete:
	movem.l	d0-d7/a0-a6,-(sp)	;save all the regs
	move.l	#deletestring,-(sp)
	move.l	#nosearch,-(sp)
	jsr	fileselector
	addq.l	#8,sp
	cmpi.w	#1,d0		;was it successful
	beq.s	dodelete
	tst.w	d0		;was it cancelled
	beq	deletereturn
	lea	derroralert,a0	;musta been a general error
	bsr	doalert
deletereturn:
	movem.l	(sp)+,d0-d7/a0-a6
	rts

dodelete:
	lea	deletealert,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	deletereturn
dretry:
	move.l	#myfp,-(sp)
	move.w	#$41,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.w	d0		;was there an error
	bpl	deletereturn	;nope so return to Murray
	lea	dfailalert,a0
	bsr	doalert
	cmpi.w	#1,intout
	beq	dretry
	bra	deletereturn


derroralert:
	dc.b	'[3][An unknown error has occurred.]'
	dc.b	'[Return to SoundscopeII]',0,0
dfailalert:
	dc.b	'[2][An error has occurred|'
	dc.b	'Was the disk write protected ?]'
	dc.b	'[Try again|Forget it]',0,0
deletealert:
	dc.b	'[3][Are you sure]'
	dc.b	'[Not really|Sure am]',0,0

deletestring:
	dc.b	'Delete a file',0
nosearch:
	dc.b	-1
	even
*****************************************************************
*	Call fileselector, call with address of search pattern	*
*	On stack and address of name for fselector on stack	*
*	ie:							*
*		fileselector(name.l,extender.l)			*
*								*
*	If pattern matching is not required ie: we don't give	*
*	a shit what the 3 byte extender is, then set the search	*
*	mask with the first byte negative.			*
*	eg:							*
*		searchmask:	dc.b	-1			*
*	Returns in d0:		0.L for user chose cancel	*
*				1.L for complete success	*
*				    and filename and path	*
*				    are both in myfp		*
*				-1.Lfor file not matching search*
*				    criteria			*
*				-2.Lfor some general error	*
*				-3.Lfor OK hit but no file	*
*				    selected.			*
*								*
*	Note that for errors -2 and -3 myfp is not valid	*
*								*
*	Also:		The complete filename and path in	*
*			myfp.					*
*								*
*****************************************************************
fileselector:
	move.l	8(sp),savetitle	;save the title
	move.l	4(sp),savesearch	;save the ad of .xxx
	movem.l	d1-d7/a0-a6,-(sp)

	tst.w	firstfile
	bpl	firsttime
fback:
	bsr	setsearch		;go and set the path and pattern
	lea	myfile,a0
	move.b	#0,(a0)
	move.l	#mypath,addrin		;must get these adresses
	move.l	#myfile,addrin+4	;from C
	move.l	savetitle,addrin+8	;title for fsel
	move.w	#91,contrl		;new routine with 91 for ext in TT
	move.w	#0,contrl+2		;title ability
	move.w	#2,contrl+4
	move.w	#3,contrl+6		;3 for ext in TT
	move.w	#0,contrl+8
	move.l	#aespb,d1
	move.w	#$c8,d0
	trap	#2
	tst.w	intout
	bne	nogeneralerror
	moveq	#-2,d0			;return general error occurred
	movem.l	(sp)+,d1-d7/a0-a6
	rts
nogeneralerror:
	tst.w	intout+2		;was it exited by cancel
	beq	chosecancel

getfileandpath:
	move.l	savesearch,a6
	lea	mypath,a0
	lea	myfile,a2
	lea	myfp,a1			;place to put the final
gpl:					;construction of file and path
	move.b	(a0)+,d0
	beq	getf
	move.b	d0,(a1)+
	bra.s	gpl
getf:
	move.b	-(a1),d0
	cmpi.b	#'\',d0
	bne.s	getf
	addq.l	#1,a1

	tst.b	(a2)		;check if any file was selected
	beq	nofile		;if first char of file name is 0
getf2:
	move.b	(a2)+,d0
	beq	geto
	move.b	d0,(a1)+
	bra.s	getf2
geto:
	move.b	#0,(a1)		;null terminator
	subq.l	#3,a1
	tst.b	(a6)		;if first byte is negative
	bmi.s	nocheckpath	;skip check
	cmp.b	(a6)+,(a1)+	;check for correct file type
	bne	notright
	cmp.b	(a6)+,(a1)+
	bne	notright
	cmp.b	(a6)+,(a1)+
	bne	notright
nocheckpath:
	moveq	#1,d0		;return success
	movem.l	(sp)+,d1-d7/a0-a6
	rts
chosecancel:
	moveq	#0,d0		;return that cancel was chosen
	movem.l	(sp)+,d1-d7/a0-a6
	rts
notright:
	moveq	#-1,d0		;return that file was not correct
	movem.l	(sp)+,d1-d7/a0-a6
	rts			;extender
nofile:
	moveq	#-3,d0		;return that file was not correct
	movem.l	(sp)+,d1-d7/a0-a6
	rts			;extender
savesearch:	dc.l	0
savetitle:	dc.l	0
*****************************************************************
*	Subroutine to get path and set search string		*
*	Pass the address of the 3 byte extender in a0		*
*****************************************************************
setsearch:
	lea	mypath,a0
epl:					;construction of file and path
	move.b	(a0)+,d0
	beq	etf
	bra.s	epl
etf:
	move.b	(a0),d0
	cmp.b	#'\',d0
	beq	etf2
	move.b	#0,(a0)
	subq.l	#1,a0
	bra.s	etf
etf2:
*	move.w	#0,-(sp)
	move.l	#mypath,-(sp)
	move.w	#$3b,-(sp)
	trap	#1
	addq.l	#6,sp
	move.l	savesearch,a1
	lea	mypath,a0
infloop:
	tst.b	(a0)+
	bne.s	infloop		;find the end of the path, null byte
	subq.l	#1,a0
*	move.b	#'\',(a0)+	;stick in the new search pattern
	move.b	#'*',(a0)+
	move.b	#'.',(a0)+
	tst.b	(a1)
	bmi.s	nopat
	move.b	(a1)+,(a0)+	;3 byte extender
	move.b	(a1)+,(a0)+
	move.b	(a1)+,(a0)+
	move.b	#0,(a0)+
	rts
nopat:
	move.b	#'*',(a0)+
	move.b	#0,(a0)+
	rts

firsttime:
	move.w	#-1,firstfile
	move.w	#0,-(sp)
	move.l	#mypath,-(sp)
	move.w	#$47,-(sp)
	trap	#1
	addq.l	#8,sp
	lea	mypath,a0
sple:
	move.b	(a0)+,d0
	beq	sple2
	bra	sple
sple2:
	move.b	#'\',-1(a0)
	move.b	#0,(a0)
	bra	fback

firstfile:	dc.w	1
*********************************************************
*	Pass a0 address of description			*
*********************************************************
doalert:
	move.w	#52,contrl
	move.w	#1,contrl+2
	move.w	#1,contrl+4
	move.w	#1,contrl+6
	move.w	#0,contrl+8
	move.w	#1,intin
	move.l	a0,addrin
	move.l	#aespb,d1
	move.w	#$c8,d0
	trap	#2
	rts

mypath:
	ds.b	64
	even
myfile:	ds.b	13
	even

myfp:	ds.b	100
*addrin:		ds.w	128
*addrout:	ds.w	128
*global:		dc.w	0,0,0,0,0,0,0
*		dc.w	0,0,0,0,0,0,0,0
*aespb:		dc.l	contrl,global,intin,intout,addrin,addrout
**********************************************************
